Class: FormObj::Form

Inherits:
Struct show all
Extended by:
ActiveModel::Naming, ActiveModel::Translation
Includes:
ActiveModel::Conversion, ActiveModel::Validations
Defined in:
lib/form_obj/form.rb,
lib/form_obj/form/array.rb,
lib/form_obj/form/attribute.rb,
lib/form_obj/form/attributes.rb

Defined Under Namespace

Classes: Array, Attribute, Attributes

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Struct

attribute, attributes, inspect, #inspect, #to_hash

Constructor Details

#initializeForm

Returns a new instance of Form.



42
43
44
45
# File 'lib/form_obj/form.rb', line 42

def initialize()
  @errors = ActiveModel::Errors.new(self)
  @persisted = false
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



40
41
42
# File 'lib/form_obj/form.rb', line 40

def errors
  @errors
end

#persistedObject

Returns the value of attribute persisted.



39
40
41
# File 'lib/form_obj/form.rb', line 39

def persisted
  @persisted
end

Class Method Details

.array_classObject



22
23
24
# File 'lib/form_obj/form.rb', line 22

def array_class
  Array
end

.attribute_classObject



30
31
32
# File 'lib/form_obj/form.rb', line 30

def attribute_class
  Attribute
end

.model_nameObject



34
35
36
# File 'lib/form_obj/form.rb', line 34

def model_name
  @model_name || super
end

.nested_classObject



26
27
28
# File 'lib/form_obj/form.rb', line 26

def nested_class
  ::FormObj::Form
end

Instance Method Details

#_set_attribute_value(attribute, value) ⇒ Object



51
52
53
54
# File 'lib/form_obj/form.rb', line 51

def _set_attribute_value(attribute, value)
  @persisted = false
  super
end

#persisted?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/form_obj/form.rb', line 47

def persisted?
  @persisted
end

#primary_keyObject



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

def primary_key
  send(self.class.primary_key)
end

#primary_key=(val) ⇒ Object



60
61
62
# File 'lib/form_obj/form.rb', line 60

def primary_key=(val)
  send("#{self.class.primary_key}=", val)
end

#savedObject



81
82
83
84
# File 'lib/form_obj/form.rb', line 81

def saved
  @persisted = true
  self
end

#update_attributes(new_attrs, raise_if_not_found: true) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/form_obj/form.rb', line 64

def update_attributes(new_attrs, raise_if_not_found: true)
  new_attrs.each_pair do |new_attr, new_val|
    attr = self.class._attributes.find(new_attr)
    if attr.nil?
      raise UnknownAttributeError.new(new_attr) if raise_if_not_found
    else
      if attr.subform?
        self.send(new_attr).update_attributes(new_val)
      else
        self.send("#{new_attr}=", new_val)
      end
    end
  end

  self
end