Using OpenStruct as mock for ActiveRecord
As you may have noticed OpenStruct#id always returns the object id of the OpenStruct instance, even when you set id.
>> o = OpenStruct.new :id => 2
=> #<OpenStruct id=2>
>> o.id
(irb):4: warning: Object#id will be deprecated; use Object#object_id
=> 9850940
Fortunately there is a simple way around this.
OpenStruct.__send__(:define_method, :id) { @table[:id] }
Now OpenStruct behaves like we want.
>> o.id
=> 2
Note that hash and object_id still work fine. You probably want to keep in mind that we’ve redefined the default OpenStruct behaviour and it might cause problems elsewhere.
At Fingertips we design and develop Web, iPhone, iPad and Mac OS X apps.
Find out how to hire us…




Coming soon
Subscribe to our mailing list — Follow us on Twitter — Grab our feed — Fork our code on GitHub