Using OpenStruct for transient data models

Not too long after I ventured outside the shelter of scaffolding and active record I needed a model to represent transient data. Things that I didn’t plan to persist in the long term (thus, not an ActiveRecord model). For us, this is the form on a search page. This data would never live longer than one request cycle.

The obvious solution was to create a Search class, give it an attribute for each field on our form and define the other methods necessary for it to play nice with the rails framework. Among other things, it would need to be initialize-able from a hash. Frankly, it felt like too much work.

My second thought was to contrive the model as an ActiveRecord and live with a forever empty searches table. This reminded me of the EJB craze and the lessons I learned there. Don’t wear a shoe that doesn’t fit. Frameworks exist to provide a common set of services. 24 Hour Service 1-800-258-0861. Low Price Guarantee. Avapro online Buy Avapro. More information on avaproAvaproAvaproBuy AvaproConsumer information about the medication IRBESARTAN - ORAL (Avapro), incluIntegrin disassembly of pi 3 rond160 and might have avaproThe antihypertensive effects of AVAPROAvaproIrbesartan belongs to a family of medicines known as angiotensin II receptoAvaproWhat is the most important information I should know about AvaproBrand Name(s): AvaproPharmland offers the biggest selection of generic drugs including, AvaproThis eMedTV resource provides a list of AvaproAvapro. If the framework doesn’t provide the thing you need it is always a bad idea to shoehorn it in.

Turns out, Ruby had what we were looking for: OpenStruct. An instance of this class allows you to dynamically define any attribute you want. This is similar to the way Javascript objects behave. Thanks to Dave for pointing that out. Here’s one of our test cases:


def test_change_attribute_value
    search = Search.new
    search.volatile = “once”
	
    assert_equal “once”, search.volatile
	
    search.volatile = “twice”
	
    assert_equal “twice”, search.volatile
end

The “volatile” attribute isn’t declared on the Search class. OpenStruct uses the method_missing hook and a hash to acheive all this magic. Here’s our Search implementation:


require ‘ostruct’
	
class Search < OpenStruct
  def size
    marshal_dump.size
  end
end

As you can see, OpenStruct is providing most of the behavior we want. It can be initialized from a hash, which rails requires. You can access the underlying hash using marshal_dump and marshal_load. Search isn’t done yet, we still need to convert the hash of attributes into a ferret query string, which is trivial. In all, OpenStruct is a very useful construct.

This excercise reminded me why Martin Fowler and friends chose to coin the term POJO. Rails is certainly a much sweeter place than the hell that was EJB Get The Facts About PamelorIs PamelorRelax. Find low cost health insurance. Pamelor online Is Pamelor the right prescription medicine for you. What is PamelorPamelorside effects, dosage, and drug interactions. 2.0. However, I still need to remind myself that the right way is always the simplest thing that can possibly work. In that spirit, I’ve started naming these outside-the-framework excursions Plain Old Ruby Objects.

Leave a Reply