Module: Mixture::Extensions::Attributable::ClassMethods

Defined in:
lib/mixture/extensions/attributable.rb

Overview

The class methods for attribution.

Instance Method Summary collapse

Instance Method Details

#attribute(name, options = {}) ⇒ Attribute

Defines an attribute. Defines the getter and the setter for for the attribute, the getter and setter alias to the #attribute.

Parameters:

  • name (Symbol)

    The name of the attribute.

  • options (Hash) (defaults to: {})

    The options for the attribute.

Returns:



17
18
19
20
21
22
23
24
25
# File 'lib/mixture/extensions/attributable.rb', line 17

def attribute(name, options = {})
  name = name.to_s.intern
  attr = attributes.create(name, options)
  define_method(attr.getter) {     attribute(name)    } unless
    options[:wo] || options[:write_only]
  define_method(attr.setter) { |v| attribute(name, v) } unless
    options[:ro] || options[:read_only]
  attr
end

#attributesAttributeList

The attribute list. Acts as a hash for the attributes.

Returns:

See Also:



31
32
33
34
35
36
37
38
# File 'lib/mixture/extensions/attributable.rb', line 31

def attributes
  return @_attributes if @_attributes
  available = ancestors[1..-1]
              .select { |c| c.respond_to?(:attributes) }
              .first
  parent = available ? available.attributes : nil
  @_attributes = AttributeList.new(parent)
end