Class: ActiveImporter::Base
- Inherits:
-
Object
- Object
- ActiveImporter::Base
- Defined in:
- lib/active_importer/base.rb
Constant Summary collapse
- EVENTS =
Callbacks
[ :row_success, :row_error, :row_processing, :row_skipped, :row_processed, :import_started, :import_finished, :import_failed, :import_aborted, ]
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#header ⇒ Object
readonly
Implementation.
-
#model ⇒ Object
readonly
Implementation.
-
#row ⇒ Object
readonly
Implementation.
-
#row_count ⇒ Object
readonly
Returns the value of attribute row_count.
-
#row_errors ⇒ Object
readonly
Returns the value of attribute row_errors.
-
#row_index ⇒ Object
readonly
Returns the value of attribute row_index.
Class Method Summary collapse
- .column(title, field = nil, &block) ⇒ Object
- .columns ⇒ Object
- .event_handlers ⇒ Object
- .fetch_model(&block) ⇒ Object
- .fetch_model_block ⇒ Object
- .fire_event(instance, event, param = nil) ⇒ Object
- .import(file, options = {}) ⇒ Object
- .imports(klass) ⇒ Object
- .model_class ⇒ Object
- .on(event, &block) ⇒ Object
- .sheet(index) ⇒ Object
- .skip_rows_block ⇒ Object
- .skip_rows_if(&block) ⇒ Object
Instance Method Summary collapse
- #abort! ⇒ Object
- #fetch_model ⇒ Object
- #fetch_model_block ⇒ Object
- #import ⇒ Object
-
#initialize(file, options = {}) ⇒ Base
constructor
A new instance of Base.
- #model_class ⇒ Object
- #row_error_count ⇒ Object
- #row_processed_count ⇒ Object
- #row_success_count ⇒ Object
Constructor Details
#initialize(file, options = {}) ⇒ Base
Returns a new instance of Base.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/active_importer/base.rb', line 121 def initialize(file, = {}) @row_errors = [] @context = .delete(:context) @book = Roo::Spreadsheet.open(file, ) load_sheet load_header @data_row_indices = ((@header_index+1)..@book.last_row) @row_count = @data_row_indices.count rescue => e @book = @header = nil @row_count = 0 @row_index = 1 fire_event :import_failed, e end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
119 120 121 |
# File 'lib/active_importer/base.rb', line 119 def context @context end |
#header ⇒ Object (readonly)
Implementation
116 117 118 |
# File 'lib/active_importer/base.rb', line 116 def header @header end |
#model ⇒ Object (readonly)
Implementation
116 117 118 |
# File 'lib/active_importer/base.rb', line 116 def model @model end |
#row ⇒ Object (readonly)
Implementation
116 117 118 |
# File 'lib/active_importer/base.rb', line 116 def row @row end |
#row_count ⇒ Object (readonly)
Returns the value of attribute row_count.
117 118 119 |
# File 'lib/active_importer/base.rb', line 117 def row_count @row_count end |
#row_errors ⇒ Object (readonly)
Returns the value of attribute row_errors.
118 119 120 |
# File 'lib/active_importer/base.rb', line 118 def row_errors @row_errors end |
#row_index ⇒ Object (readonly)
Returns the value of attribute row_index.
117 118 119 |
# File 'lib/active_importer/base.rb', line 117 def row_index @row_index end |
Class Method Details
.column(title, field = nil, &block) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/active_importer/base.rb', line 55 def self.column(title, field = nil, &block) title = title.strip if columns[title] raise "Duplicate importer column '#{title}'" end columns[title] = { field_name: field, transform: block } end |
.columns ⇒ Object
23 24 25 |
# File 'lib/active_importer/base.rb', line 23 def self.columns @columns ||= {} end |
.event_handlers ⇒ Object
83 84 85 |
# File 'lib/active_importer/base.rb', line 83 def self.event_handlers @event_handlers ||= EVENTS.inject({}) { |hash, event| hash.merge({event => []}) } end |
.fetch_model(&block) ⇒ Object
39 40 41 |
# File 'lib/active_importer/base.rb', line 39 def self.fetch_model(&block) @fetch_model_block = block end |
.fetch_model_block ⇒ Object
43 44 45 |
# File 'lib/active_importer/base.rb', line 43 def self.fetch_model_block @fetch_model_block end |
.fire_event(instance, event, param = nil) ⇒ Object
99 100 101 102 103 |
# File 'lib/active_importer/base.rb', line 99 def self.fire_event(instance, event, param = nil) event_handlers[event].each do |block| instance.instance_exec(param, &block) end end |
.import(file, options = {}) ⇒ Object
63 64 65 |
# File 'lib/active_importer/base.rb', line 63 def self.import(file, = {}) new(file, ).import end |
.imports(klass) ⇒ Object
19 20 21 |
# File 'lib/active_importer/base.rb', line 19 def self.imports(klass) @model_class = klass end |
.model_class ⇒ Object
27 28 29 |
# File 'lib/active_importer/base.rb', line 27 def self.model_class @model_class end |
.on(event, &block) ⇒ Object
87 88 89 90 |
# File 'lib/active_importer/base.rb', line 87 def self.on(event, &block) raise "Unknown ActiveImporter event '#{event}'" unless EVENTS.include?(event) event_handlers[event] << block end |
.sheet(index) ⇒ Object
35 36 37 |
# File 'lib/active_importer/base.rb', line 35 def self.sheet(index) @sheet_index = index end |
.skip_rows_block ⇒ Object
51 52 53 |
# File 'lib/active_importer/base.rb', line 51 def self.skip_rows_block @skip_rows_block end |
.skip_rows_if(&block) ⇒ Object
47 48 49 |
# File 'lib/active_importer/base.rb', line 47 def self.skip_rows_if(&block) @skip_rows_block = block end |
Instance Method Details
#abort! ⇒ Object
12 13 14 |
# File 'lib/active_importer/base.rb', line 12 def abort! @aborted = true end |
#fetch_model ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/active_importer/base.rb', line 142 def fetch_model if fetch_model_block self.instance_exec(&fetch_model_block) else model_class.new end end |
#fetch_model_block ⇒ Object
138 139 140 |
# File 'lib/active_importer/base.rb', line 138 def fetch_model_block self.class.send(:fetch_model_block) end |
#import ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/active_importer/base.rb', line 150 def import return if @book.nil? fire_event :import_started @data_row_indices.each do |index| @row_index = index @row = row_to_hash @book.row(index) if skip_row? fire_event :row_skipped next end import_row if @aborted fire_event :import_aborted break end end fire_event :import_finished end |
#model_class ⇒ Object
31 32 33 |
# File 'lib/active_importer/base.rb', line 31 def model_class self.class.model_class end |
#row_error_count ⇒ Object
179 180 181 |
# File 'lib/active_importer/base.rb', line 179 def row_error_count row_errors.count end |
#row_processed_count ⇒ Object
169 170 171 172 173 |
# File 'lib/active_importer/base.rb', line 169 def row_processed_count row_index - @header_index rescue 0 end |
#row_success_count ⇒ Object
175 176 177 |
# File 'lib/active_importer/base.rb', line 175 def row_success_count row_processed_count - row_errors.count end |