Module: OnForm::Attributes

Included in:
Form
Defined in:
lib/on_form/attributes.rb

Instance Method Summary collapse

Instance Method Details

#[](attribute_name) ⇒ Object

the individual attribute methods are introduced by the expose_attribute class method. here we introduce some methods used for the attribute set as a whole.



6
7
8
# File 'lib/on_form/attributes.rb', line 6

def [](attribute_name)
  send(attribute_name)
end

#[]=(attribute_name, attribute_value) ⇒ Object



10
11
12
# File 'lib/on_form/attributes.rb', line 10

def []=(attribute_name, attribute_value)
  send("#{attribute_name}=", attribute_value)
end

#attribute_namesObject



22
23
24
25
# File 'lib/on_form/attributes.rb', line 22

def attribute_names
  self.class.exposed_attributes.values.flat_map(&:keys).collect(&:to_s) +
    self.class.introduced_attribute_types.keys.collect(&:to_s)
end

#attributesObject



27
28
29
30
31
# File 'lib/on_form/attributes.rb', line 27

def attributes
  attribute_names.each_with_object({}) do |attribute_name, results|
    results[attribute_name] = self[attribute_name]
  end
end

#attributes=(attributes) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/on_form/attributes.rb', line 33

def attributes=(attributes)
  # match ActiveRecord #attributes= behavior on nil, scalars, etc.
  if !attributes.respond_to?(:stringify_keys)
    raise ArgumentError, "When assigning attributes, you must pass a hash as an argument."
  end

  multiparameter_attributes = {}
  attributes.each do |attribute_name, attribute_value|
    attribute_name = attribute_name.to_s
    if attribute_name.include?('(')
      multiparameter_attributes[attribute_name] = attribute_value
    else
      write_attribute(attribute_name, attribute_value)
    end
  end
  assign_multiparameter_attributes(multiparameter_attributes)
end

#read_attribute_for_validation(attribute_name) ⇒ Object



14
15
16
# File 'lib/on_form/attributes.rb', line 14

def read_attribute_for_validation(attribute_name)
  send(attribute_name)
end

#write_attribute(attribute_name, attribute_value) ⇒ Object



18
19
20
# File 'lib/on_form/attributes.rb', line 18

def write_attribute(attribute_name, attribute_value)
  send("#{attribute_name}=", attribute_value)
end