Module: CsvRowModel::Import::Attributes

Extended by:
ActiveSupport::Concern
Includes:
AttributesBase, ParsedModel
Included in:
CsvRowModel::Import
Defined in:
lib/csv_row_model/concerns/import/attributes.rb

Constant Summary collapse

CLASS_TO_PARSE_LAMBDA =

Mapping of column type classes to a parsing lambda. These are applied after Import.format_cell. Can pass custom Proc with :parse option.

{
  nil      => ->(s) { s }, # no type given
  Boolean  => ->(s) { s =~ BooleanFormatValidator.false_boolean_regex ? false : true },
  String   => ->(s) { s },
  Integer  => ->(s) { s.to_i },
  Float    => ->(s) { s.to_f },
  DateTime => ->(s) { s.present? ? DateTime.parse(s) : s },
  Date     => ->(s) { s.present? ? Date.parse(s) : s }
}.freeze

Constants included from AttributesBase

AttributesBase::ATTRIBUTE_METHODS

Instance Method Summary collapse

Methods included from ParsedModel

#_original_attribute, #parsed_model, #valid?

Methods included from AttributesBase

#array_to_block_hash, #attributes, #attributes_from_method_names, #column_names_to_attribute_value, #eql?, #hash, #original_attribute, #to_json

Methods included from Model::Attributes

#headers

Instance Method Details

#_attribute_objects(parsed_model_errors = {}) ⇒ Object (protected)

to prevent circular dependency with parsed_model


42
43
44
45
46
47
# File 'lib/csv_row_model/concerns/import/attributes.rb', line 42

def _attribute_objects(parsed_model_errors={})
  index = -1
  array_to_block_hash(self.class.column_names) do |column_name|
    Attribute.new(column_name, source_row[index += 1], parsed_model_errors[column_name], self)
  end
end

#attribute_objectsObject


28
29
30
31
32
33
# File 'lib/csv_row_model/concerns/import/attributes.rb', line 28

def attribute_objects
  @attribute_objects ||= begin
    parsed_model.valid?
    _attribute_objects(parsed_model.errors)
  end
end

#default_changesObject

return [Hash] a map changes from column's default option': column_name -> [value_before_default, default_set]


36
37
38
# File 'lib/csv_row_model/concerns/import/attributes.rb', line 36

def default_changes
  column_names_to_attribute_value(self.class.column_names, :default_change).delete_if {|k, v| v.blank? }
end