Module: Schema::Arrays::ClassMethods

Defined in:
lib/schema/arrays.rb

Overview

adds from_array method to the class

Instance Method Summary collapse

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_arrayObject



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 |_, field_options|
    next if field_options[:alias_of]

    data <<
      case field_options[:type]
      when :has_one
        const_get(field_options[:class_name]).to_empty_array
      when :has_many
        field_options[:size].times.map { const_get(field_options[: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 |_, field_options|
    next if field_options[:alias_of]

    headers <<
      case field_options[:type]
      when :has_one
        const_get(field_options[:class_name]).to_headers(prefix.to_s + field_options[:key] + '.')
      when :has_many
        field_options[:size].times.map do |i|
          const_get(field_options[:class_name]).to_headers(prefix.to_s + field_options[:key] + "[#{i +1}].")
        end
      else
        prefix.to_s + field_options[:key]
      end
  end
  headers.flatten
end