Class: CsvRowModel::Import::Presenter
- Inherits:
-
Object
- Object
- CsvRowModel::Import::Presenter
- Includes:
- ActiveWarnings, Concerns::InheritedClassVar, Concerns::Inspect
- Defined in:
- lib/csv_row_model/import/presenter.rb
Instance Attribute Summary collapse
-
#row_model ⇒ Object
readonly
Returns the value of attribute row_model.
Class Method Summary collapse
-
._dependencies ⇒ Hash{Symbol => Array}
protected
Map of
dependency => [array of presenter attributes dependent on dependency]. -
.attribute(attribute_name, options = {}, &block) ⇒ Object
protected
Adds column to the row model.
-
.attribute_names ⇒ Array<Symbol>
Attribute names for the Presenter.
-
.block(attribute_name) ⇒ Proc, Lambda
Block called for attribute.
-
.define_attribute_method(attribute_name) ⇒ Object
protected
Define the attribute_method.
- .inspect_methods ⇒ Object protected
-
.options(attribute_name) ⇒ Hash
Options for the attribute_name.
Instance Method Summary collapse
- #_filter_errors ⇒ Object protected
-
#abort? ⇒ Boolean
Safe to override.
- #attributes ⇒ Object
-
#filter_errors ⇒ Object
protected
add errors from row_model and remove each dependent attribute from errors if it's row_model_dependencies are in the errors.
-
#initialize(row_model) ⇒ Presenter
constructor
A new instance of Presenter.
-
#memoize(method_name) ⇒ Object
protected
equal to: @method_name ||= yield.
-
#previous ⇒ Presenter
Returns the presenter of the previous row_model.
-
#row_model_present?(*column_names) ⇒ Boolean
protected
If column_names are present.
-
#skip? ⇒ Boolean
Safe to override.
- #valid?(*args) ⇒ Boolean
-
#valid_dependencies?(attribute_name) ⇒ Boolean
protected
If the dependencies are valid.
Methods included from Concerns::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
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_model ⇒ Object (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
._dependencies ⇒ Hash{Symbol => Array} (protected)
Returns 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| (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
131 132 133 134 135 136 |
# File 'lib/csv_row_model/import/presenter.rb', line 131 def attribute(attribute_name, ={}, &block) = (, memoize: true, dependencies: []) merge_attributes(attribute_name.to_sym => [, block]) define_attribute_method(attribute_name) end |
.attribute_names ⇒ Array<Symbol>
Returns 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.
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
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.(attribute_name)[:memoize] ? memoize(attribute_name) { public_send("__#{attribute_name}") } : public_send("__#{attribute_name}") end end |
.inspect_methods ⇒ Object (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.
97 98 99 |
# File 'lib/csv_row_model/import/presenter.rb', line 97 def (attribute_name) attributes[attribute_name].first end |
Instance Method Details
#_filter_errors ⇒ Object (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.[attribute_name] && row_model.errors..slice(*self.class.(attribute_name)[:dependencies]).present? errors.delete attribute_name end errors..reverse_merge!(row_model.errors.) end |
#abort? ⇒ Boolean
Safe to override.
28 29 30 |
# File 'lib/csv_row_model/import/presenter.rb', line 28 def abort? false end |
#attributes ⇒ Object
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_errors ⇒ Object (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
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 |
#previous ⇒ 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.
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.
21 22 23 |
# File 'lib/csv_row_model/import/presenter.rb', line 21 def skip? !valid? end |
#valid?(*args) ⇒ Boolean
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.
77 78 79 |
# File 'lib/csv_row_model/import/presenter.rb', line 77 def valid_dependencies?(attribute_name) row_model_present?(*self.class.(attribute_name)[:dependencies]) end |