MadProps is a simple gem to explore the Property Pattern as described by Steve Yegge in his (massive) blog post:
The Property Pattern is a deceptively simple pattern. MadProp objects are essentially a property list, but with a twist. They contain a link to a "prototype" or "base" object. This enables the objects to inherit properties from it's parent up the inheritance tree.
That's it. Of course, being geeks we will overly comlicate it with caching, recursion and YML files. And maybe even some NoSQL to make it especially buzzword compliant.
Properties can be the following types:
- Float
- Int
- Boolean
- String
- Nil
- Reference (to another MadProp object)
MadProps objects can be initialized with a hash and support simple lookups.
randy = MadProps::Props.new {
key: 'geek:dad',
name: 'Randy',
town: 'Austin',
is: [ 'charming', 'good looking']
}
wyatt = MadProps.new {
key: 'geek:son',
proto, 'geek:dad',
name: 'Wyatt'
}
# wyatt inherits all properties from his proto property except the ones he overrides
puts "#{wyatt.name} lives in #{wyatt.town}"
# => Wyatt lives in Austin
puts "#{wyatt.name} is #{wyatt.is.join(',')}"
# => Wyatt is charming, good looking
The key property identifies a MadProp object. If and when caching is introduced, it will use this unique identifier.
The proto property identifies the parent to inherit from. When getting a property, we look at the object to see if it has that property. If not, we use the proto pointer to see if the parent has it. We do this recursively up the inheritance tree.
We setting a property, we simply set the property on that object. Now, lookups will use this property.
Obviously, the runtime lookup for properties can be speedup with some caching. Of course, once you add caching to anything, it gets more complicated.
Add this line to your application's Gemfile:
gem 'mad_props'
And then execute:
$ bundle
Or install it yourself as:
$ gem install mad_props
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request