Module: CsvRowModel::AttributesBase

Extended by:
ActiveSupport::Concern
Includes:
HiddenModule, Model::Attributes
Included in:
Export::Attributes, Import::Attributes
Defined in:
lib/csv_row_model/concerns/attributes_base.rb

Constant Summary collapse

ATTRIBUTE_METHODS =

Export: source_value - row_model.column1 --> source_model.column1 formatted_value - format_cell(source_value) vale - formatted_value

Import: source_value - form source_row formatted_value - format_cell(source_value) value - calculated_value from a bunch of stuff

{
  original_attributes: :value, # a map of `column_name => original_attribute(column_name)`
  formatted_attributes: :formatted_value, # a map of `column_name => format_cell(column_name, ...)`
  source_attributes: :source_value # a map of `column_name => source (source_row[index_of_column_name] or row_model.public_send(column_name)) `
}.freeze

Instance Method Summary collapse

Methods included from Model::Attributes

#headers

Instance Method Details

#array_to_block_hash(array, &block) ⇒ Object (protected)


62
63
64
# File 'lib/csv_row_model/concerns/attributes_base.rb', line 62

def array_to_block_hash(array, &block)
  array.zip(array.map(&block)).to_h
end

#attributesHash

Returns a map of column_name => public_send(column_name)

Returns:

  • (Hash)

    a map of column_name => public_send(column_name)


12
13
14
# File 'lib/csv_row_model/concerns/attributes_base.rb', line 12

def attributes
  attributes_from_method_names self.class.column_names
end

#attributes_from_method_names(column_names) ⇒ Object (protected)


54
55
56
# File 'lib/csv_row_model/concerns/attributes_base.rb', line 54

def attributes_from_method_names(column_names)
  array_to_block_hash(column_names) { |column_name| try(column_name) }
end

#column_names_to_attribute_value(column_names, attribute_method) ⇒ Object (protected)


58
59
60
# File 'lib/csv_row_model/concerns/attributes_base.rb', line 58

def column_names_to_attribute_value(column_names, attribute_method)
  array_to_block_hash(column_names) { |column_name| attribute_objects[column_name].public_send(attribute_method)}
end

#eql?(other) ⇒ Boolean

Returns:


45
46
47
# File 'lib/csv_row_model/concerns/attributes_base.rb', line 45

def eql?(other)
  other.try(:attributes) == attributes
end

#hashObject


49
50
51
# File 'lib/csv_row_model/concerns/attributes_base.rb', line 49

def hash
  attributes.hash
end

#original_attribute(column_name) ⇒ Object

Returns the column's attribute (the csv_row_model default value to be used for import/export)

Returns:

  • (Object)

    the column's attribute (the csv_row_model default value to be used for import/export)


37
38
39
# File 'lib/csv_row_model/concerns/attributes_base.rb', line 37

def original_attribute(column_name)
  attribute_objects[column_name].try(:value)
end

#to_jsonObject


41
42
43
# File 'lib/csv_row_model/concerns/attributes_base.rb', line 41

def to_json
  attributes.to_json
end