On Tuesday, October 22, 2013 11:13:22 AM UTC-7, GregD wrote:

> All,
>
> I have a legacy DB that use mnemonics to define state, status and flags of 
> several DB objects.  I'd like to cache them some way and then have each 
> object have methods to return appropriate messages.  Most of them are 
> defined by a pattern, but not all follow the pattern. Almost all, if not 
> all, use the same attributes of status, state and flags.  I'd like to use a 
> module to do this and then just include the module for the Models that have 
> those attributes.  However, I don't want to join the table at all and just 
> get the cached values.
>
> My thoughts for that module would be to use method_missing and respond_to? 
> to create a method that would return the mnemonic message(s) like define a 
> method status_message if the model respond_to? :status.  This would take 
> the instance status integer value into the class's cached status mnemonic 
> messages returning the message.  My problem is setting up the cache.
>
> Most of the mnemonics follow a specific pattern, like I said, and there is 
> a view that returns them all.  The pattern is the object name and the 
> attribute. Eg. Order would have status, flags, and state attributes and 
> have mnemonic type/code/values of "ORDER_STATUSES", "ORDER_FLAGS", and 
> "ORDER_STATES"  I was thinking of using a class and class level variable to 
> store them in a hash.  But, I don't like using class level variables for 
> numerous reasons.  But, I know I would only hit the DB once or limit the 
> number of times the DB is called for get those by maybe providing a class 
> level method to refresh the hash.  The mnemonics are pretty much static 
> data, but can be changed (very, very rarely).
>
> Someone has to have done this type of thing before, right?
>

I admit I have a hard time understanding exactly what you want to do.  It 
would probably be better if you posted examples of your schema and what you 
want your model methods to return.

>From what you posted, I'm guessing you want something like:

CACHE = {}
DB[:mnemonics_view].select_hash_groups(:type, [:code, :value]).each do 
|key, values|
  h = CACHE[key.downcase.to_sym] = {}
  values.each do |code, value|
    h[code] = value
  end
end

Your mnemonic methods would index into this cache:

class Order
  def status
    CACHE[:order_status][status_column]
  end
end

Hopefully that makes sense.  If not, you probably need to post an example 
of what you want to do.

Thanks,
Jeremy

-- 
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/groups/opt_out.

Reply via email to