Module: Schema::Arrays

Defined in:
lib/schema/arrays.rb

Overview

Schema::Arrays maps the array to a schema model

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
# File 'lib/schema/arrays.rb', line 6

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#create_schema_with_array(field_options, array, mapped_model, offset) ⇒ Object



133
134
135
# File 'lib/schema/arrays.rb', line 133

def create_schema_with_array(field_options, array, mapped_model, offset)
  self.class.const_get(field_options[:class_name]).new.update_attributes_with_array(array, mapped_model, offset)
end

#largest_number_of_indexes_from_map(mapped_model) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/schema/arrays.rb', line 137

def largest_number_of_indexes_from_map(mapped_model)
  size = 0
  mapped_model.each do |_, info|
    if info[:indexes]
      size = info[:indexes].size if info[:indexes] && info[:indexes].size > size
    else
      new_size = largest_number_of_indexes_from_map(info)
      size = new_size if new_size > size
    end
  end
  size
end

#to_aObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/schema/arrays.rb', line 55

def to_a
  data = []
  self.class.schema.each do |_, field_options|
    next if field_options[:alias_of]

    value = public_send(field_options[:getter])
    data <<
      case field_options[:type]
      when :has_one
        value ? value.to_a : self.class.const_get(field_options[:class_name]).to_empty_array
      when :has_many
        values = value || []
        field_options[:size].times.map do |idx|
          value = values[idx]
          value ? value.to_a : self.class.const_get(field_options[:class_name]).to_empty_array
        end
      else
        value
      end
  end
  data
end

#update_attributes_with_array(array, mapped_headers, offset = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/schema/arrays.rb', line 78

def update_attributes_with_array(array, mapped_headers, offset = nil)
  self.class.schema.each do |_, field_options|
    next unless (mapped_field = mapped_headers[field_options[:name]])

    if offset
      next unless mapped_field[:indexes]
      next unless (index = mapped_field[:indexes][offset])
    else
      next unless (index = mapped_field[:index])
    end

    public_send(
      field_options[:setter],
      array[index]
    )
  end

  update_nested_schemas_from_array(array, mapped_headers, offset)

  self
end

#update_nested_has_many_associations_from_array(array, mapped_headers) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/schema/arrays.rb', line 117

def update_nested_has_many_associations_from_array(array, mapped_headers)
  self.class.schema.each do |_, field_options|
    next unless field_options[:type] == :has_many
    next unless (mapped_model = mapped_headers[field_options[:name]])

    size = largest_number_of_indexes_from_map(mapped_model)

    instance_variable_set(
      field_options[:instance_variable],
      size.times.map do |offset|
        create_schema_with_array(field_options, array, mapped_model, offset)
      end
    )
  end
end

#update_nested_has_one_associations_from_array(array, mapped_headers, current_offset = nil) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/schema/arrays.rb', line 105

def update_nested_has_one_associations_from_array(array, mapped_headers, current_offset = nil)
  self.class.schema.each do |_, field_options|
    next unless field_options[:type] == :has_one
    next unless (mapped_model = mapped_headers[field_options[:name]])

    instance_variable_set(
      field_options[:instance_variable],
      create_schema_with_array(field_options, array, mapped_model, current_offset)
    )
  end
end

#update_nested_schemas_from_array(array, mapped_headers, current_offset = nil) ⇒ Object



100
101
102
103
# File 'lib/schema/arrays.rb', line 100

def update_nested_schemas_from_array(array, mapped_headers, current_offset = nil)
  update_nested_has_one_associations_from_array(array, mapped_headers, current_offset)
  update_nested_has_many_associations_from_array(array, mapped_headers)
end