Class: FormObj::ModelMapper::ModelAttribute

Inherits:
Object
  • Object
show all
Defined in:
lib/form_obj/model_mapper/model_attribute.rb,
lib/form_obj/model_mapper/model_attribute/item.rb

Defined Under Namespace

Classes: Item

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(array:, classes:, default_name:, hash:, model:, names:, nesting:, read_from_model:, subform:, write_to_model:) ⇒ ModelAttribute

Returns a new instance of ModelAttribute.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/form_obj/model_mapper/model_attribute.rb', line 9

def initialize(array:, classes:, default_name:, hash:, model:, names:, nesting:, read_from_model:, subform:, write_to_model:)
  @read_from_model = read_from_model && !(names === false)
  @write_to_model = write_to_model && !(names === false)

  @nesting = nesting
  @model = model
  @array = array

  names = (names || default_name).to_s.split('.') unless names.is_a? ::Enumerable
  classes = classes.nil? ? [] : [classes] unless classes.is_a? ::Enumerable

  if classes.size > 0
    if (subform && (names.size != classes.size)) || (!subform && (names.size != classes.size + 1))
      raise "Since the :model_attribute size is #{names.size} the :model_class size should be #{names.size - (subform ? 0 : 1)} in terms of nested items but it was #{classes.size}" unless names.size == classes.size
    end
  end

  @items = names.zip(classes, [hash], names[0..-2].map{nil} + [array]).map { |item| Item.new(name: item[0], klass: item[1], hash: item[2], array: item[3]) }

  @items.inject do |prev, item|
    prev.hash = true if item.hash_item
    item
  end
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



7
8
9
# File 'lib/form_obj/model_mapper/model_attribute.rb', line 7

def model
  @model
end

Instance Method Details

#create_modelObject



46
47
48
49
# File 'lib/form_obj/model_mapper/model_attribute.rb', line 46

def create_model
  raise 'Creation available only for array attributes' unless @array
  @items.last.create_model
end

#hash_item=(value) ⇒ Object



42
43
44
# File 'lib/form_obj/model_mapper/model_attribute.rb', line 42

def hash_item=(value)
  @items[0].hash_item = value
end

#last_nameObject



38
39
40
# File 'lib/form_obj/model_mapper/model_attribute.rb', line 38

def last_name
  @items.last.name
end

#namesObject



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

def names
  @items.map(&:name)
end

#nesting?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/form_obj/model_mapper/model_attribute.rb', line 51

def nesting?
  @nesting
end

#read_errors_from_model(model) ⇒ Object



92
93
94
95
96
# File 'lib/form_obj/model_mapper/model_attribute.rb', line 92

def read_errors_from_model(model)
  @items.last.try(:read_errors_from_model,
                  @items[0..-2].reduce(model) { |last_model, item| item.read_from_model(last_model, create_nested_model_if_nil: false) }
  )
end

#read_errors_from_models(models) ⇒ Object



98
99
100
# File 'lib/form_obj/model_mapper/model_attribute.rb', line 98

def read_errors_from_models(models)
  read_errors_from_model(models[@model])
end

#read_from_model(model, create_nested_model_if_nil: false) ⇒ Object



59
60
61
# File 'lib/form_obj/model_mapper/model_attribute.rb', line 59

def read_from_model(model, create_nested_model_if_nil: false)
  @items.reduce(model) { |last_model, item| item.read_from_model(last_model, create_nested_model_if_nil: create_nested_model_if_nil) }
end

#read_from_model?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/form_obj/model_mapper/model_attribute.rb', line 55

def read_from_model?
  @read_from_model
end

#read_from_models(models, create_nested_model_if_nil: false) ⇒ Object



63
64
65
# File 'lib/form_obj/model_mapper/model_attribute.rb', line 63

def read_from_models(models, create_nested_model_if_nil: false)
  read_from_model(models[@model], create_nested_model_if_nil: create_nested_model_if_nil)
end

#to_model_hash(value) ⇒ Object



88
89
90
# File 'lib/form_obj/model_mapper/model_attribute.rb', line 88

def to_model_hash(value)
  @items.reverse.reduce(value) { |value, item| item.to_hash(value) }
end

#validate_primary_key!Object



80
81
82
83
84
85
86
# File 'lib/form_obj/model_mapper/model_attribute.rb', line 80

def validate_primary_key!
  if @items.size > 1
    raise PrimaryKeyMappingError.new('Primary key should not be mapped to nested model')
  elsif @items.size == 0
    raise PrimaryKeyMappingError.new('Primary key should not be mapped to non-mapped attribute')
  end
end

#write_to_model(model, value) ⇒ Object



71
72
73
74
# File 'lib/form_obj/model_mapper/model_attribute.rb', line 71

def write_to_model(model, value)
  model = @items[0..-2].reduce(model) { |last_model, item| item.read_from_model(last_model, create_nested_model_if_nil: true) } if @items.size > 1
  @items.last.write_to_model(model, value)
end

#write_to_model?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/form_obj/model_mapper/model_attribute.rb', line 67

def write_to_model?
  @write_to_model
end

#write_to_models(models, value) ⇒ Object



76
77
78
# File 'lib/form_obj/model_mapper/model_attribute.rb', line 76

def write_to_models(models, value)
  write_to_model(models[@model], value)
end