Module: Filemaker::Model::Builder

Defined in:
lib/filemaker/model/builder.rb

Class Method Summary collapse

Class Method Details

.build(record, object) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/filemaker/model/builder.rb', line 18

def build(record, object)
  object.instance_variable_set('@new_record', false)
  object.instance_variable_set('@record_id', record.record_id)
  object.instance_variable_set('@mod_id', record.mod_id)
  object.instance_variable_set('@portals', record.portals)

  record.each_key do |fm_field_name|
    # record.keys are all lowercase
    field = object.class.find_field_by_name(fm_field_name)

    # Do not bother with undefined field, we don't necessarily need all
    # FM's fields
    next unless field

    setter = :"#{field.name}="
    value = field.cast(record[fm_field_name])
    object.public_send(setter, value)

    if record[fm_field_name].is_a?(Array) && field.max_repeat > 1
      field.max_repeat.times do |idx|
        index = idx + 1
        repeated_setter = "#{field.name}__#{index}="
        repeated_value = field.cast(record[fm_field_name][idx])
        object.public_send(repeated_setter, repeated_value)
      end
    end

    # So after hydrating, we do not say it was dirty
    object.clear_changes_information
  end

  object
end

.collection(resultset, klass) ⇒ Object

Given an array of resultset, build out the exact same number of model objects.



8
9
10
11
12
13
14
15
16
# File 'lib/filemaker/model/builder.rb', line 8

def collection(resultset, klass)
  models = []

  resultset.each do |record|
    models << build(record, klass.new)
  end

  models
end