I don't have a blog to post this on, and I just tried something which 
turned out to be insanely useful. I thought other Sequel users might really 
enjoy if they hadn't realized it already, so I hope it's ok to post this 
here :)

Let's say you have a plain old ruby object that is associated with one of 
your models, perhaps a mortgage interest calculator or an address or 
something like that. You might use `composition` in this case to generate a 
ruby object from value columns in the database. However, if you've got a 
composition object that has a lot of inputs, or potentially variable 
inputs, this can bloat your table with a ton columns or even a bunch of 
optional and often null columns.

Well, if the object can be converted to/from a hash, and you're using 
Postgres, you could make a beautiful serialization just using the hstore 
extension.

Assuming you have a model `PriceQuote`, and a complicated business logic 
object `PriceCalculator`, you could add `column :calculator, :hstore` to 
your `price_quotes` table, and then do this:

class PriceQuote < Sequel::Model
  def calculator=(price_calculator)
    super(price_calculator.to_hash)
  end


  def calculator
    PriceCalculator.new(super)
  end
end

Now when you want to use these...
price_quote = PriceQuote.first
price_quote.calculator.class #=> PriceCalculator
other_quote = PriceQuote.new(...)
other_quote.calculator = PriceCalculator.new(...)
other_quote.save
other_quote.reload
other_quote.calculator #=> #<PriceCalculator:0x00512314 ... >
other_quote.calculator.do_some_calculation #=> yes this works :)

Jeremy, I just have to say that Sequel is super awesome. Thanks for giving 
the world this fantastic library!

Andrew

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to