Class: Attribs

Inherits:
Module
  • Object
show all
Defined in:
lib/attribs.rb,
lib/attribs/instance_methods.rb

Defined Under Namespace

Modules: InstanceMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#defaultsObject (readonly)

Returns the value of attribute defaults.



4
5
6
# File 'lib/attribs.rb', line 4

def defaults
  @defaults
end

#namesObject (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