Module: CsvRowModel::Import
- Extended by:
- ActiveSupport::Concern
- Includes:
- Concerns::Inspect, Attributes, DynamicColumns
- Defined in:
- lib/csv_row_model/import.rb,
lib/csv_row_model/import/csv.rb,
lib/csv_row_model/import/file.rb,
lib/csv_row_model/import/presenter.rb,
lib/csv_row_model/import/attributes.rb,
lib/csv_row_model/import/file_model.rb,
lib/csv_row_model/import/file/callbacks.rb,
lib/csv_row_model/import/dynamic_columns.rb,
lib/csv_row_model/import/file/validations.rb
Overview
Include this to with Model to have a RowModel for importing csvs that represents just one model. It needs CsvRowModel::Import
Defined Under Namespace
Modules: Attributes, DynamicColumns, FileModel Classes: Csv, File, Presenter
Constant Summary
Constants included from Attributes
Attributes::CLASS_TO_PARSE_LAMBDA, Attributes::PARSE_VALIDATION_CLASSES
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#previous ⇒ Object
readonly
Returns the value of attribute previous.
-
#source_header ⇒ Object
readonly
Returns the value of attribute source_header.
-
#source_row ⇒ Object
readonly
Returns the value of attribute source_row.
Class Method Summary collapse
- .column(column_name, options = {}) ⇒ Object
- .inspect_methods ⇒ Object protected
-
.next(csv, source_header, context = {}, previous = nil) ⇒ Import
The next model instance from the csv.
-
.presenter(&block) ⇒ Object
protected
Call to define the presenter.
-
.presenter_class ⇒ Class
The Class of the Presenter.
Instance Method Summary collapse
-
#abort? ⇒ Boolean
Safe to override.
-
#csv_string_model ⇒ Model::CsvStringModel
A model with validations related to Model::csv_string_model (values are from format_cell).
-
#free_previous ⇒ Object
Free
previousfrom memory to avoid making a linked list. - #initialize(source_row, options = {}) ⇒ Object
-
#mapped_row ⇒ Hash
A map of
column_name => source_row[index_of_column_name]. -
#presenter ⇒ Presenter
The presenter of self.
-
#skip? ⇒ Boolean
Safe to override.
- #valid?(*args) ⇒ Boolean
Methods included from DynamicColumns
define_dynamic_attribute_method, dynamic_column, dynamic_source_headers, #dynamic_source_headers, #dynamic_source_row, format_dynamic_column_cells, #original_attribute, #original_attributes
Methods included from Attributes
add_type_validation, #default_changes, default_lambda, define_attribute_method, format_cell, #original_attribute, #original_attribute_memoized?, #original_attributes, parse_lambda
Methods included from Model::Comparison
Methods included from Concerns::Inspect
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
15 16 17 |
# File 'lib/csv_row_model/import.rb', line 15 def context @context end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
15 16 17 |
# File 'lib/csv_row_model/import.rb', line 15 def index @index end |
#previous ⇒ Object (readonly)
Returns the value of attribute previous.
15 16 17 |
# File 'lib/csv_row_model/import.rb', line 15 def previous @previous end |
#source_header ⇒ Object (readonly)
Returns the value of attribute source_header.
15 16 17 |
# File 'lib/csv_row_model/import.rb', line 15 def source_header @source_header end |
#source_row ⇒ Object (readonly)
Returns the value of attribute source_row.
15 16 17 |
# File 'lib/csv_row_model/import.rb', line 15 def source_row @source_row end |
Class Method Details
.column(column_name, options = {}) ⇒ Object
104 105 106 107 |
# File 'lib/csv_row_model/import.rb', line 104 def column(column_name, ={}) super define_attribute_method(column_name) end |
.inspect_methods ⇒ Object (protected)
138 139 140 |
# File 'lib/csv_row_model/import.rb', line 138 def inspect_methods @inspect_methods ||= %i[mapped_row initialized_at parent context previous].freeze end |
.next(csv, source_header, context = {}, previous = nil) ⇒ Import
Returns the next model instance from the csv.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/csv_row_model/import.rb', line 113 def next(csv, source_header, context={}, previous=nil) csv.skip_header row_model = nil loop do # loop until the next parent or end_of_file? (need to read children rows) csv.read_row row_model ||= new(csv.current_row, index: csv.index, source_header: source_header, context: context, previous: previous) return row_model if csv.end_of_file? next_row_is_parent = !row_model.append_child(csv.next_row) return row_model if next_row_is_parent end end |
.presenter(&block) ⇒ Object (protected)
Call to define the presenter
143 144 145 |
# File 'lib/csv_row_model/import.rb', line 143 def presenter(&block) presenter_class.class_eval(&block) end |
.presenter_class ⇒ Class
Returns the Class of the Presenter.
133 134 135 |
# File 'lib/csv_row_model/import.rb', line 133 def presenter_class @presenter_class ||= inherited_custom_class(:presenter_class, Presenter) end |
Instance Method Details
#abort? ⇒ Boolean
Safe to override.
82 83 84 |
# File 'lib/csv_row_model/import.rb', line 82 def abort? presenter.abort? end |
#csv_string_model ⇒ Model::CsvStringModel
Returns a model with validations related to Model::csv_string_model (values are from format_cell).
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/csv_row_model/import.rb', line 55 def csv_string_model @csv_string_model ||= begin if source_row column_names = self.class.column_names hash = column_names.zip( column_names.map.with_index do |column_name, index| self.class.format_cell(source_row[index], column_name, index, context) end ).to_h else hash = {} end self.class.csv_string_model_class.new(hash) end end |
#free_previous ⇒ Object
Free previous from memory to avoid making a linked list
45 46 47 |
# File 'lib/csv_row_model/import.rb', line 45 def free_previous @previous = nil end |
#initialize(source_row, options = {}) ⇒ Object
29 30 31 32 33 34 35 36 |
# File 'lib/csv_row_model/import.rb', line 29 def initialize(source_row, ={}) = .symbolize_keys.reverse_merge(context: {}) @source_row, @context = source_row, OpenStruct.new([:context]) @index, @source_header, @previous = [:index], [:source_header], [:previous].try(:dup) previous.try(:free_previous) super(source_row, ) end |
#mapped_row ⇒ Hash
Returns a map of column_name => source_row[index_of_column_name].
39 40 41 42 |
# File 'lib/csv_row_model/import.rb', line 39 def mapped_row return {} unless source_row @mapped_row ||= self.class.column_names.zip(source_row).to_h end |
#presenter ⇒ Presenter
Returns the presenter of self.
50 51 52 |
# File 'lib/csv_row_model/import.rb', line 50 def presenter @presenter ||= self.class.presenter_class.new(self) end |
#skip? ⇒ Boolean
Safe to override.
75 76 77 |
# File 'lib/csv_row_model/import.rb', line 75 def skip? !valid? || presenter.skip? end |
#valid?(*args) ⇒ Boolean
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/csv_row_model/import.rb', line 86 def valid?(*args) super proc = -> do csv_string_model.valid?(*args) errors..merge!(csv_string_model.errors..reject {|k, v| v.empty? }) errors.empty? end if using_warnings? csv_string_model.using_warnings(&proc) else proc.call end end |