Class: Attribs
- Inherits:
-
Module
- Object
- Module
- Attribs
- Defined in:
- lib/attribs.rb,
lib/attribs/instance_methods.rb
Defined Under Namespace
Modules: InstanceMethods
Instance Attribute Summary collapse
-
#defaults ⇒ Object
readonly
Returns the value of attribute defaults.
-
#names ⇒ Object
readonly
Returns the value of attribute names.
Instance Method Summary collapse
- #add(*attrs) ⇒ Object
- #included(descendant) ⇒ Object
-
#initialize(*attrs) ⇒ Attribs
constructor
A new instance of Attribs.
- #remove(*attrs) ⇒ Object
Constructor Details
#initialize(*attrs) ⇒ Attribs
Returns a new instance of Attribs.
6 7 8 9 |
# File 'lib/attribs.rb', line 6 def initialize(*attrs) @defaults = attrs.last.instance_of?(Hash) ? attrs.pop : {} @names = (attrs + @defaults.keys).uniq end |
Instance Attribute Details
#defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
4 5 6 |
# File 'lib/attribs.rb', line 4 def defaults @defaults end |
#names ⇒ Object (readonly)
Returns the value of attribute names.
4 5 6 |
# File 'lib/attribs.rb', line 4 def names @names end |
Instance Method Details
#add(*attrs) ⇒ Object
11 12 13 14 |
# File 'lib/attribs.rb', line 11 def add(*attrs) defaults = attrs.last.instance_of?(Hash) ? attrs.pop : {} self.class.new(*[*(names+attrs), @defaults.merge(defaults)]) end |
#included(descendant) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/attribs.rb', line 20 def included(descendant) descendant.module_exec(self) do |this| include InstanceMethods, Anima.new(*this.names) define_singleton_method(:attributes) { this } end end |
#remove(*attrs) ⇒ Object
16 17 18 |
# File 'lib/attribs.rb', line 16 def remove(*attrs) self.class.new(*[*(names-attrs), @defaults.reject {|k| attrs.include?(k) }]) end |