Module: Schema::Arrays::ClassMethods
- Defined in:
- lib/schema/arrays.rb
Overview
adds from_array method to the class
Instance Method Summary collapse
- #from_array(array, mapped_headers) ⇒ Object
- #to_empty_array ⇒ Object
- #to_headers(prefix = nil) ⇒ Object
Instance Method Details
#from_array(array, mapped_headers) ⇒ Object
12 13 14 |
# File 'lib/schema/arrays.rb', line 12 def from_array(array, mapped_headers) new.update_attributes_with_array(array, mapped_headers) end |
#to_empty_array ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/schema/arrays.rb', line 16 def to_empty_array data = [] schema.each do |_, | next if [:alias_of] data << case [:type] when :has_one const_get([:class_name]).to_empty_array when :has_many [:size].times.map { const_get([:class_name]).to_empty_array } else nil end end data end |
#to_headers(prefix = nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/schema/arrays.rb', line 34 def to_headers(prefix = nil) headers = [] schema.each do |_, | next if [:alias_of] headers << case [:type] when :has_one const_get([:class_name]).to_headers(prefix.to_s + [:key] + '.') when :has_many [:size].times.map do |i| const_get([:class_name]).to_headers(prefix.to_s + [:key] + "[#{i +1}].") end else prefix.to_s + [:key] end end headers.flatten end |