class CsvRowModel::Import::Presenter
Attributes
Public Class Methods
# File lib/csv_row_model/import/presenter.rb, line 14 def initialize(row_model) @row_model = row_model end
Protected Class Methods
@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
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
@return [Array<Symbol>] attribute names for the Presenter
# File lib/csv_row_model/import/presenter.rb, line 91 def attribute_names attributes.keys end
@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 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
# File lib/csv_row_model/import/presenter.rb, line 120 def inspect_methods @inspect_methods ||= %i[row_model].freeze end
@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
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
# 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
@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
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
# File lib/csv_row_model/import/presenter.rb, line 32 def valid?(*args) super filter_errors errors.empty? end
Protected Instance Methods
# 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
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
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
@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
@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