Heredity adds on_inherit hooks to Ruby which providing a clean way execute code on inherited classes without the need to override inherited
. It also adds the ability to specify class instance variables that should be copied to subclasses.
Add this line to your application's Gemfile:
gem 'heredity'
And then execute:
$ bundle
Or install it yourself as:
$ gem install heredity
To use Heredity's inheritance hooks, simply call on_herit
with a block:
class Foo
on_inherit do
puts 'Child of Foo!'
end
end
This is very useful for injecting behavior into subclasses that is dependent on class state (e.g. behavior that relies on the columns of an active record model).
Because class instance variables in Ruby are not inherited (and rightfully so), Heredity provides the ability to define specific class instance variables that should be inherited. To define inheritable attributes:
class Foo
include Heredity
# Define inheritable attributes
inheritable_attributes :bar
# Initialize inheritable attributes (so there's something to copy).
self.bar = {}
end
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)