Module: Lean::Attributes::ClassMethods

Defined in:
lib/lean-attributes/attributes/class_methods.rb

Overview

Methods that extend classes that include Lean::Attributes

Since:

  • 0.0.1

Instance Method Summary collapse

Instance Method Details

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

Defines a new attribute. Adds getter and setter methods to the class.

Parameters:

  • name (Symbol)

    describe name

  • type (Class, String, Symbol)

    describe type

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

Options Hash (options):

  • :default (Object)

    default value or method name to call as a Symbol

Returns:

Since:

  • 0.0.1



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lean-attributes/attributes/class_methods.rb', line 18

def attribute(name, type, options = {})
  attribute = Attribute.new(
    default:  options[:default],
    name:     name,
    type:     type
  )

  defined_attributes << attribute.name

  class_eval(attribute.coercion_method, __FILE__, __LINE__ + 1)
  class_eval(attribute.getter_method,   __FILE__, __LINE__ + 1)
  class_eval(attribute.setter_method,   __FILE__, __LINE__ + 1)

  attribute
end

#defined_attributesArray<Symbol>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns names of defined attributes.

Returns:

  • (Array<Symbol>)

    names of defined attributes

Since:

  • 0.2.0



38
39
40
41
42
43
44
45
46
# File 'lib/lean-attributes/attributes/class_methods.rb', line 38

def defined_attributes
  return @defined_attributes if @defined_attributes

  @defined_attributes = if superclass.respond_to?(:defined_attributes)
                          superclass.defined_attributes.clone
                        else
                          []
                        end
end