Class: DMV::Form

Inherits:
Object
  • Object
show all
Extended by:
Uber::InheritableAttr
Defined in:
lib/dmv/form.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Form

Initialize a new form instance from values

Parameters:

  • attributes (defaults to: {})

    A hash of attributes to be set



55
56
57
58
# File 'lib/dmv/form.rb', line 55

def initialize attributes = {}
  @attributes = {}
  attributes.each { |attribute, value| set attribute, value }
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



15
16
17
# File 'lib/dmv/form.rb', line 15

def attributes
  @attributes
end

Class Method Details

.attribute(*names) ⇒ Object

Adds an attribute to the forms attributes set and creates accessors

Parameters:

  • name

    The name of the attribute

  • options (Hash)

    The options for this attribute (type for example)



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dmv/form.rb', line 21

def self.attribute *names
  options = names.last.kind_of?(Hash) ? names.pop : {}

  names.each do |name|
    name = name.to_sym

    if _attributes.keys.include?(name)
      raise AttributeAlreadyDefined, "the attribute #{name} has already been defined"
    end

    _attributes[name] = options.freeze
    class_eval <<-ATTR, __FILE__, __LINE__ + 1
      def #{name}
        get(:#{name})
      end

      def #{name}=(value)
        set(:#{name}, value)
      end
    ATTR
  end
end

.attributesObject

Returns a cloned frozen hash containing the details of the attributes on this form



48
49
50
# File 'lib/dmv/form.rb', line 48

def self.attributes
  _attributes.clone.freeze
end

Instance Method Details

#get(attribute) ⇒ Object

Retrieve the value for an attribute

Parameters:

  • attribute (Symbol)

    Name of an attribute

Returns:

  • attribute value



72
73
74
# File 'lib/dmv/form.rb', line 72

def get attribute
  attributes[attribute]
end

#set(attribute, value) ⇒ Object

Sets an attribute value and performs any coercions needed

Parameters:

  • attribute

    The name of an attribute

  • value

    The value you would like to set the attribute to



64
65
66
# File 'lib/dmv/form.rb', line 64

def set attribute, value
  attributes[attribute] = value
end