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
- #create_schema_with_array(field_options, array, mapped_model, offset) ⇒ Object
- #largest_number_of_indexes_from_map(mapped_model) ⇒ Object
- #update_attributes_with_array(array, mapped_headers, offset = nil) ⇒ Object
- #update_nested_has_many_associations_from_array(array, mapped_headers) ⇒ Object
- #update_nested_has_one_associations_from_array(array, mapped_headers, current_offset = nil) ⇒ Object
- #update_nested_schemas_from_array(array, mapped_headers, current_offset = nil) ⇒ Object
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
72 73 74 |
# File 'lib/schema/arrays.rb', line 72 def create_schema_with_array(, array, mapped_model, offset) self.class.const_get([:class_name]).new.update_attributes_with_array(array, mapped_model, offset) end |
#largest_number_of_indexes_from_map(mapped_model) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/schema/arrays.rb', line 76 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 |
#update_attributes_with_array(array, mapped_headers, offset = nil) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/schema/arrays.rb', line 17 def update_attributes_with_array(array, mapped_headers, offset = nil) self.class.schema.each do |_, | next unless (mapped_field = mapped_headers[[: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( [: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
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/schema/arrays.rb', line 56 def update_nested_has_many_associations_from_array(array, mapped_headers) self.class.schema.each do |_, | next unless [:type] == :has_many next unless (mapped_model = mapped_headers[[:name]]) size = largest_number_of_indexes_from_map(mapped_model) instance_variable_set( [:instance_variable], size.times.map do |offset| create_schema_with_array(, array, mapped_model, offset) end ) end end |
#update_nested_has_one_associations_from_array(array, mapped_headers, current_offset = nil) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/schema/arrays.rb', line 44 def update_nested_has_one_associations_from_array(array, mapped_headers, current_offset = nil) self.class.schema.each do |_, | next unless [:type] == :has_one next unless (mapped_model = mapped_headers[[:name]]) instance_variable_set( [:instance_variable], create_schema_with_array(, array, mapped_model, current_offset) ) end end |
#update_nested_schemas_from_array(array, mapped_headers, current_offset = nil) ⇒ Object
39 40 41 42 |
# File 'lib/schema/arrays.rb', line 39 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 |