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, inherited_ancestors, inherited_class_var, inherited_class_variable_name, inherited_custom_class

Constructor Details

#initialize(row_model) ⇒ Presenter

Returns a new instance of Presenter.



12
13
14
# File 'lib/csv_row_model/import/presenter.rb', line 12

def initialize(row_model)
  @row_model = row_model
end

Instance Attribute Details

#row_modelObject (readonly)

Returns the value of attribute row_model.



8
9
10
# File 'lib/csv_row_model/import/presenter.rb', line 8

def row_model
  @row_model
end

Class Method Details

.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: [])

Raises:

  • (ArgumentError)


146
147
148
149
150
151
152
153
154
155
# File 'lib/csv_row_model/import/presenter.rb', line 146

def attribute(attribute_name, options={}, &block)
  default_options = { memoize: true, dependencies: [] }
  invalid_options = options.keys - default_options.keys
  raise ArgumentError.new("Invalid option(s): #{invalid_options}") if invalid_options.present?

  options = options.reverse_merge(default_options)

  merge_attribute(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



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

def attribute_names
  attributes.keys
end

.attributesHash{Symbol => Array}

Returns map of attribute_name => [options, block].

Returns:

  • (Hash{Symbol => Array})

    map of attribute_name => [options, block]



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

def attributes
  inherited_class_var :@_presenter_attributes, {}, :merge
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



109
110
111
# File 'lib/csv_row_model/import/presenter.rb', line 109

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



159
160
161
162
163
164
165
166
167
168
# File 'lib/csv_row_model/import/presenter.rb', line 159

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

.dependenciesHash{Symbol => Array}

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]



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/csv_row_model/import/presenter.rb', line 114

def dependencies
  class_cache(:@_presenter_dependencies) do
    dependencies = {}
    attribute_names.each do |attribute_name|
      options(attribute_name)[:dependencies].each do |dependency|
        dependencies[dependency] ||= []
        dependencies[dependency] << attribute_name
      end
    end
    dependencies
  end
end

.inherited_class_moduleObject



87
88
89
# File 'lib/csv_row_model/import/presenter.rb', line 87

def inherited_class_module
  Presenter
end

.inspect_methodsObject (protected)



128
129
130
# File 'lib/csv_row_model/import/presenter.rb', line 128

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

.merge_attribute(attribute_hash) ⇒ Object (protected)



132
133
134
135
136
137
# File 'lib/csv_row_model/import/presenter.rb', line 132

def merge_attribute(attribute_hash)
  @_presenter_attributes ||= {}
  deep_clear_class_cache(:@_presenter_attributes)
  deep_clear_class_cache(:@_presenter_dependencies)
  @_presenter_attributes.merge! attribute_hash
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



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

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

Instance Method Details

#_filter_errorsObject (protected)



55
56
57
58
59
60
61
62
63
64
# File 'lib/csv_row_model/import/presenter.rb', line 55

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



26
27
28
# File 'lib/csv_row_model/import/presenter.rb', line 26

def abort?
  false
end

#attributesObject



41
42
43
44
45
# File 'lib/csv_row_model/import/presenter.rb', line 41

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



51
52
53
# File 'lib/csv_row_model/import/presenter.rb', line 51

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



81
82
83
84
# File 'lib/csv_row_model/import/presenter.rb', line 81

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



37
38
39
# File 'lib/csv_row_model/import/presenter.rb', line 37

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

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

Returns if column_names are valid.

Parameters:

  • Array (Array)

    of column_names to check

Returns:

  • (Boolean)

    if column_names are valid



68
69
70
# File 'lib/csv_row_model/import/presenter.rb', line 68

def row_model_valid?(*column_names)
  row_model.valid? || (row_model.errors.keys & column_names).empty?
end

#skip?Boolean

Safe to override.

Returns:

  • (Boolean)

    returns true, if this instance should be skipped



19
20
21
# File 'lib/csv_row_model/import/presenter.rb', line 19

def skip?
  !valid?
end

#valid?(*args) ⇒ Boolean

Returns:



30
31
32
33
34
# File 'lib/csv_row_model/import/presenter.rb', line 30

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



74
75
76
# File 'lib/csv_row_model/import/presenter.rb', line 74

def valid_dependencies?(attribute_name)
  row_model_valid?(*self.class.options(attribute_name)[:dependencies])
end