class DMV::Form

Attributes

attributes[R]

Public Class Methods

attribute(*names) click to toggle source

Adds an attribute to the forms attributes set and creates accessors

@param name The name of the attribute @param options [Hash] The options for this attribute (type for example)

# 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
attributes() click to toggle source

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

@return Hash (frozen)

# File lib/dmv/form.rb, line 48
def self.attributes
  _attributes.clone.freeze
end
new(attributes = {}) click to toggle source

Initialize a new form instance from values

@param attributes A hash of attributes to be set

# File lib/dmv/form.rb, line 55
def initialize attributes = {}
  @attributes = {}
  attributes.each { |attribute, value| set attribute, value }
end

Public Instance Methods

get(attribute) click to toggle source

Retrieve the value for an attribute

@param attribute [Symbol] Name of an attribute @return attribute value

# File lib/dmv/form.rb, line 72
def get attribute
  attributes[attribute]
end
set(attribute, value) click to toggle source

Sets an attribute value and performs any coercions needed

@param attribute The name of an attribute @param value The value you would like to set the attribute to

# File lib/dmv/form.rb, line 64
def set attribute, value
  attributes[attribute] = value
end