Class: DMV::Form
- Inherits:
-
Object
- Object
- DMV::Form
- Extended by:
- Uber::InheritableAttr
- Defined in:
- lib/dmv/form.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Class Method Summary collapse
-
.attribute(*names) ⇒ Object
Adds an attribute to the forms attributes set and creates accessors.
-
.attributes ⇒ Object
Returns a cloned frozen hash containing the details of the attributes on this form.
Instance Method Summary collapse
-
#get(attribute) ⇒ Object
Retrieve the value for an attribute.
-
#initialize(attributes = {}) ⇒ Form
constructor
Initialize a new form instance from values.
-
#set(attribute, value) ⇒ Object
Sets an attribute value and performs any coercions needed.
Constructor Details
#initialize(attributes = {}) ⇒ Form
Initialize a new form instance from values
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
#attributes ⇒ Object (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
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 = 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] = .freeze class_eval <<-ATTR, __FILE__, __LINE__ + 1 def #{name} get(:#{name}) end def #{name}=(value) set(:#{name}, value) end ATTR end end |
.attributes ⇒ Object
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
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
64 65 66 |
# File 'lib/dmv/form.rb', line 64 def set attribute, value attributes[attribute] = value end |