it's Valuable -- like attr_accessor on steriods
Take a look at the ruby gem named valuable
I recently worked on a Rails project where most of the domain-related data came from a HyperCube database, rather than a relational one. I have to admint, albeit begrudgingly, that it provided a lot of value. Conversely, the interface was crazily complicated, and incorporating non-relational data with Rails was at best incrediby painful.
One "feature" of the hypercube was that we didn't ever get a complete schema. Data was provided when it was available, and when it wasn't available, it just didn't exist. For instance, a category might have a collection of items. If there were no items, there was no empty collection.
We needed a dry way to model the parts of the data we were using. We also wanted to be able to transparently ignore any other data that might unexpectedly appear. Finally, for my own sanity, I wanted it to feel as much like a rails model as possible.
Goals:
* dry class decorators
* generate getters and setters for each attribute
* generate a class-level list of the attributes we handled
* provide an instance-level attribute hash like Rails
* optionally provide light-weight type casting ( '3' + '3' is not the same as 3+3)
* allow default values
From the Readme, Here's an example of its usage.
This has also come in handy with the Flix4r code I've been writing... more about that soon.
I recently worked on a Rails project where most of the domain-related data came from a HyperCube database, rather than a relational one. I have to admint, albeit begrudgingly, that it provided a lot of value. Conversely, the interface was crazily complicated, and incorporating non-relational data with Rails was at best incrediby painful.
One "feature" of the hypercube was that we didn't ever get a complete schema. Data was provided when it was available, and when it wasn't available, it just didn't exist. For instance, a category might have a collection of items. If there were no items, there was no empty collection.
We needed a dry way to model the parts of the data we were using. We also wanted to be able to transparently ignore any other data that might unexpectedly appear. Finally, for my own sanity, I wanted it to feel as much like a rails model as possible.
Goals:
* dry class decorators
* generate getters and setters for each attribute
* generate a class-level list of the attributes we handled
* provide an instance-level attribute hash like Rails
* optionally provide light-weight type casting ( '3' + '3' is not the same as 3+3)
* allow default values
From the Readme, Here's an example of its usage.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
class BaseballPlayer < Valuable::Base has_value :at_bats, :klass => Integer has_value :hits, :klass => Integer has_value :jersey, :klass => Jersey, :default => 'unknown' has_value :league, :default => 'unknown' has_value :name has_collection :teammates def roi hits/at_bats.to_f if hits&&at_bats end end class Jersey < String def initialize(object) super "Jersey Number #{object}" end end >> joe = BaseballPlayer.new(:name => 'Joe', :hits => 5, :at_bats => 20) >> joe.at_bats => 20 >> joe.league => 'unknown' >> joe.roi=> 0.25 >> joe.at_bats = nil >> joe.roi=> nil >> joe.teammates=> [] >> joe.jersey => 'unknown' >> joe.jersey = 20 >> 'Jersey Number 20' |
This has also come in handy with the Flix4r code I've been writing... more about that soon.