class CsvRowModel::Import::Presenter

Attributes

row_model[R]

Public Class Methods

new(row_model) click to toggle source
# File lib/csv_row_model/import/presenter.rb, line 14
def initialize(row_model)
  @row_model = row_model
end

Protected Class Methods

_dependencies() click to toggle source

@return [Hash{Symbol => Array}] map of ‘dependency => [array of presenter attributes dependent on dependency]`

# File lib/csv_row_model/import/presenter.rb, line 109
def _dependencies
  dependencies = {}
  attribute_names.each do |attribute_name|
    options(attribute_name)[:dependencies].each do |dependency|
      dependencies[dependency] ||= []
      dependencies[dependency] << attribute_name
    end
  end
  dependencies
end
attribute(attribute_name, options={}, &block) click to toggle source

Adds column to the row model

@param [Symbol] attribute_name name of attribute to add @param [Proc] block to calculate the attribute @param options [Hash] @option options [Hash] :memoize whether to memoize the attribute (default: true) @option options [Hash] :dependencies the dependcies it has with the underlying row_model (default: [])

# File lib/csv_row_model/import/presenter.rb, line 131
def attribute(attribute_name, options={}, &block)
  options = check_and_merge_options(options, memoize: true, dependencies: [])

  merge_attributes(attribute_name.to_sym => [options, block])
  define_attribute_method(attribute_name)
end
attribute_names() click to toggle source

@return [Array<Symbol>] attribute names for the Presenter

# File lib/csv_row_model/import/presenter.rb, line 91
def attribute_names
  attributes.keys
end
block(attribute_name) click to toggle source

@param [Symbol] attribute_name name of attribute to find block @return [Proc, Lambda] block called for attribute

# File lib/csv_row_model/import/presenter.rb, line 103
def block(attribute_name)
  attributes[attribute_name].last
end
define_attribute_method(attribute_name) click to toggle source

Define the attribute_method @param [Symbol] attribute_name name of attribute to add

# File lib/csv_row_model/import/presenter.rb, line 140
def define_attribute_method(attribute_name)
  define_method("__#{attribute_name}", &block(attribute_name))

  define_method(attribute_name) do
    return unless valid_dependencies?(attribute_name)
    self.class.options(attribute_name)[:memoize] ?
      memoize(attribute_name) { public_send("__#{attribute_name}") } :
      public_send("__#{attribute_name}")
  end
end
inspect_methods() click to toggle source
# File lib/csv_row_model/import/presenter.rb, line 120
def inspect_methods
  @inspect_methods ||= %i[row_model].freeze
end
options(attribute_name) click to toggle source

@param [Symbol] attribute_name name of attribute to find option @return [Hash] options for the attribute_name

# File lib/csv_row_model/import/presenter.rb, line 97
def options(attribute_name)
  attributes[attribute_name].first
end

Public Instance Methods

abort?() click to toggle source

Safe to override.

@return [Boolean] returns true, if the entire csv file should stop reading

# File lib/csv_row_model/import/presenter.rb, line 28
def abort?
  false
end
attributes() click to toggle source
# File lib/csv_row_model/import/presenter.rb, line 43
def attributes
  self.class.attribute_names
    .zip(self.class.attribute_names.map { |attribute_name| public_send(attribute_name) })
    .to_h
end
previous() click to toggle source

@return [Presenter] returns the presenter of the previous row_model

# File lib/csv_row_model/import/presenter.rb, line 39
def previous
  row_model.previous.try(:presenter)
end
skip?() click to toggle source

Safe to override.

@return [Boolean] returns true, if this instance should be skipped

# File lib/csv_row_model/import/presenter.rb, line 21
def skip?
  !valid?
end
valid?(*args) click to toggle source
Calls superclass method
# File lib/csv_row_model/import/presenter.rb, line 32
def valid?(*args)
  super
  filter_errors
  errors.empty?
end

Protected Instance Methods

_filter_errors() click to toggle source
# File lib/csv_row_model/import/presenter.rb, line 57
def _filter_errors
  row_model.valid?
  self.class.attribute_names.each do |attribute_name|
    next unless errors.messages[attribute_name] &&
      row_model.errors.messages.slice(*self.class.options(attribute_name)[:dependencies]).present?
    errors.delete attribute_name
  end

  errors.messages.reverse_merge!(row_model.errors.messages)
end
filter_errors() click to toggle source

add errors from row_model and remove each dependent attribute from errors if it’s row_model_dependencies are in the errors

# File lib/csv_row_model/import/presenter.rb, line 53
def filter_errors
  using_warnings? ? row_model.using_warnings { _filter_errors } : _filter_errors
end
memoize(method_name) { || ... } click to toggle source

equal to: @method_name ||= yield @param [Symbol] method_name method_name in description @return [Object] the memoized result

# File lib/csv_row_model/import/presenter.rb, line 84
def memoize(method_name)
  variable_name = "@#{method_name}"
  instance_variable_get(variable_name) || instance_variable_set(variable_name, yield)
end
row_model_present?(*column_names) click to toggle source

@param [Array] Array of column_names to check @return [Boolean] if column_names are present

# File lib/csv_row_model/import/presenter.rb, line 70
def row_model_present?(*column_names)
  column_names.each { |column_name| return false if row_model.public_send(column_name).blank? }
  true
end
valid_dependencies?(attribute_name) click to toggle source

@param [Symbol] attribute_name the attribute to check @return [Boolean] if the dependencies are valid

# File lib/csv_row_model/import/presenter.rb, line 77
def valid_dependencies?(attribute_name)
  row_model_present?(*self.class.options(attribute_name)[:dependencies])
end