module Schema::Arrays::ClassMethods

adds from_array method to the class

Public Instance Methods

from_array(array, mapped_headers) click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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