Class: CsvRowModel::Import::Presenter

Inherits:
Object
  • Object
show all
Includes:
ActiveWarnings, Concerns::InheritedClassVar, Concerns::Inspect
Defined in:
lib/csv_row_model/import/presenter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::Inspect

#inspect

Methods included from Concerns::InheritedClassVar

class_cache, clear_class_cache, deep_clear_class_cache, hidden_variable_name, inherited_ancestors, inherited_class_hash, inherited_class_var, inherited_class_variable_name, inherited_custom_class

Methods included from Concerns::InvalidOptions

check_and_merge_options

Constructor Details

#initialize(row_model) ⇒ Presenter

Returns a new instance of Presenter.



14
15
16
# File 'lib/csv_row_model/import/presenter.rb', line 14

def initialize(row_model)
  @row_model = row_model
end

Instance Attribute Details

#row_modelObject (readonly)

Returns the value of attribute row_model.



10
11
12
# File 'lib/csv_row_model/import/presenter.rb', line 10

def row_model
  @row_model
end

Class Method Details

._dependenciesHash{Symbol => Array} (protected)

Returns map of dependency => [array of presenter attributes dependent on dependency].

Returns:

  • (Hash{Symbol => Array})

    map of dependency => [array of presenter attributes dependent on dependency]



109
110
111
112
113
114
115
116
117
118
# 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) ⇒ Object (protected)

Adds column to the row model

Parameters:

  • attribute_name (Symbol)

    name of attribute to add

  • block (Proc)

    to calculate the attribute

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :memoize (Hash)

    whether to memoize the attribute (default: true)

  • :dependencies (Hash)

    the dependcies it has with the underlying row_model (default: [])



131
132
133
134
135
136
# 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_namesArray<Symbol>

Returns attribute names for the Presenter.

Returns:

  • (Array<Symbol>)

    attribute names for the Presenter



91
92
93
# File 'lib/csv_row_model/import/presenter.rb', line 91

def attribute_names
  attributes.keys
end

.block(attribute_name) ⇒ Proc, Lambda

Returns block called for attribute.

Parameters:

  • attribute_name (Symbol)

    name of attribute to find block

Returns:

  • (Proc, Lambda)

    block called for attribute



103
104
105
# File 'lib/csv_row_model/import/presenter.rb', line 103

def block(attribute_name)
  attributes[attribute_name].last
end

.define_attribute_method(attribute_name) ⇒ Object (protected)

Define the attribute_method

Parameters:

  • attribute_name (Symbol)

    name of attribute to add



140
141
142
143
144
145
146
147
148
149
# 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_methodsObject (protected)



120
121
122
# File 'lib/csv_row_model/import/presenter.rb', line 120

def inspect_methods
  @inspect_methods ||= i[row_model].freeze
end

.options(attribute_name) ⇒ Hash

Returns options for the attribute_name.

Parameters:

  • attribute_name (Symbol)

    name of attribute to find option

Returns:

  • (Hash)

    options for the attribute_name



97
98
99
# File 'lib/csv_row_model/import/presenter.rb', line 97

def options(attribute_name)
  attributes[attribute_name].first
end

Instance Method Details

#_filter_errorsObject (protected)



57
58
59
60
61
62
63
64
65
66
# 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

#abort?Boolean

Safe to override.

Returns:

  • (Boolean)

    returns true, if the entire csv file should stop reading



28
29
30
# File 'lib/csv_row_model/import/presenter.rb', line 28

def abort?
  false
end

#attributesObject



43
44
45
46
47
# 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

#filter_errorsObject (protected)

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



53
54
55
# 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) ⇒ Object (protected)

equal to: @method_name ||= yield

Parameters:

  • method_name (Symbol)

    method_name in description

Returns:

  • (Object)

    the memoized result



84
85
86
87
# 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

#previousPresenter

Returns the presenter of the previous row_model

Returns:

  • (Presenter)

    returns the presenter of the previous row_model



39
40
41
# File 'lib/csv_row_model/import/presenter.rb', line 39

def previous
  row_model.previous.try(:presenter)
end

#row_model_present?(*column_names) ⇒ Boolean (protected)

Returns if column_names are present.

Parameters:

  • Array (Array)

    of column_names to check

Returns:

  • (Boolean)

    if column_names are present



70
71
72
73
# 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

#skip?Boolean

Safe to override.

Returns:

  • (Boolean)

    returns true, if this instance should be skipped



21
22
23
# File 'lib/csv_row_model/import/presenter.rb', line 21

def skip?
  !valid?
end

#valid?(*args) ⇒ Boolean

Returns:



32
33
34
35
36
# File 'lib/csv_row_model/import/presenter.rb', line 32

def valid?(*args)
  super
  filter_errors
  errors.empty?
end

#valid_dependencies?(attribute_name) ⇒ Boolean (protected)

Returns if the dependencies are valid.

Parameters:

  • attribute_name (Symbol)

    the attribute to check

Returns:

  • (Boolean)

    if the dependencies are valid



77
78
79
# 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