Implement class variables that inherit from their ancestors. Such as a Hash
:
require 'inherited_class_var'
class Bird
include InheritedClassVar
inherited_class_hash :attributes #, shallow: false, reverse: false (default aoptions)
def self.attribute(attribute_name, options={})
attributes_object.merge(attribute_name.to_sym => options)
end
attribute :name, upcase: true
end
class Duck < Bird
attribute :flying, default: false
end
Bird.attributes # => { name: upcase: true }
Duck.attributes # => { name: upcase: true, flying: false }
Installation
Add this line to your application's Gemfile:
gem 'inherited_class_var'
And then execute:
$ bundle
Or install it yourself as:
$ gem install inherited_class_var
Usage
You can also define your own variable types. This is the source for Hash
:
module InheritedClassVar
class Hash < Variable
alias_method :merge, :change
def default_value
{}
end
def _change(hash1, hash2)
method = options[:shallow] ? :merge! : :deep_merge!
block = options[:reverse] ? Proc.new {|key,left,right| left } : Proc.new {|key,left,right| right }
hash1.public_send(method, hash2, &block)
end
end
end
module InheritedClassVar
def inherited_class_hash(variable_name, options={})
inherited_class_var variable_name, InheritedClassVar::Hash, options
end
end
License
The gem is available as open source under the terms of the MIT License.