Class: ActiveImporter::Base

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, options = {}) ⇒ Base

Returns a new instance of Base.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/active_importer/base.rb', line 122

def initialize(file, options = {})
  @row_errors = []
  @context = options.delete(:context)

  @book = Roo::Spreadsheet.open(file, options)
  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

#contextObject (readonly)

Returns the value of attribute context.



120
121
122
# File 'lib/active_importer/base.rb', line 120

def context
  @context
end

#headerObject (readonly)

Implementation



117
118
119
# File 'lib/active_importer/base.rb', line 117

def header
  @header
end

#modelObject (readonly)

Implementation



117
118
119
# File 'lib/active_importer/base.rb', line 117

def model
  @model
end

#rowObject (readonly)

Implementation



117
118
119
# File 'lib/active_importer/base.rb', line 117

def row
  @row
end

#row_countObject (readonly)

Returns the value of attribute row_count.



118
119
120
# File 'lib/active_importer/base.rb', line 118

def row_count
  @row_count
end

#row_errorsObject (readonly)

Returns the value of attribute row_errors.



119
120
121
# File 'lib/active_importer/base.rb', line 119

def row_errors
  @row_errors
end

#row_indexObject (readonly)

Returns the value of attribute row_index.



118
119
120
# File 'lib/active_importer/base.rb', line 118

def row_index
  @row_index
end

Class Method Details

.column(title, field = nil, &block) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/active_importer/base.rb', line 56

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

.columnsObject



24
25
26
# File 'lib/active_importer/base.rb', line 24

def self.columns
  @columns ||= {}
end

.event_handlersObject



84
85
86
# File 'lib/active_importer/base.rb', line 84

def self.event_handlers
  @event_handlers ||= EVENTS.inject({}) { |hash, event| hash.merge({event => []}) }
end

.fetch_model(&block) ⇒ Object



40
41
42
# File 'lib/active_importer/base.rb', line 40

def self.fetch_model(&block)
  @fetch_model_block = block
end

.fetch_model_blockObject



44
45
46
# File 'lib/active_importer/base.rb', line 44

def self.fetch_model_block
  @fetch_model_block
end

.fire_event(instance, event, param = nil) ⇒ Object



100
101
102
103
104
# File 'lib/active_importer/base.rb', line 100

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



64
65
66
# File 'lib/active_importer/base.rb', line 64

def self.import(file, options = {})
  new(file, options).import
end

.imports(klass) ⇒ Object



20
21
22
# File 'lib/active_importer/base.rb', line 20

def self.imports(klass)
  @model_class = klass
end

.model_classObject



28
29
30
# File 'lib/active_importer/base.rb', line 28

def self.model_class
  @model_class
end

.on(event, &block) ⇒ Object



88
89
90
91
# File 'lib/active_importer/base.rb', line 88

def self.on(event, &block)
  raise "Unknown ActiveImporter event '#{event}'" unless EVENTS.include?(event)
  event_handlers[event] << block
end

.sheet(index) ⇒ Object



36
37
38
# File 'lib/active_importer/base.rb', line 36

def self.sheet(index)
  @sheet_index = index
end

.skip_rows_blockObject



52
53
54
# File 'lib/active_importer/base.rb', line 52

def self.skip_rows_block
  @skip_rows_block
end

.skip_rows_if(&block) ⇒ Object



48
49
50
# File 'lib/active_importer/base.rb', line 48

def self.skip_rows_if(&block)
  @skip_rows_block = block
end

Instance Method Details

#abort!(message) ⇒ Object



12
13
14
# File 'lib/active_importer/base.rb', line 12

def abort!(message)
  @abort_message = message
end

#aborted?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/active_importer/base.rb', line 16

def aborted?
  !!@abort_message
end

#fetch_modelObject



143
144
145
146
147
148
149
# File 'lib/active_importer/base.rb', line 143

def fetch_model
  if fetch_model_block
    self.instance_exec(&fetch_model_block)
  else
    model_class.new
  end
end

#fetch_model_blockObject



139
140
141
# File 'lib/active_importer/base.rb', line 139

def fetch_model_block
  self.class.send(:fetch_model_block)
end

#importObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/active_importer/base.rb', line 151

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, @abort_message
      break
    end
  end
  fire_event :import_finished
end

#model_classObject



32
33
34
# File 'lib/active_importer/base.rb', line 32

def model_class
  self.class.model_class
end

#row_error_countObject



180
181
182
# File 'lib/active_importer/base.rb', line 180

def row_error_count
  row_errors.count
end

#row_processed_countObject



170
171
172
173
174
# File 'lib/active_importer/base.rb', line 170

def row_processed_count
  row_index - @header_index
rescue
  0
end

#row_success_countObject



176
177
178
# File 'lib/active_importer/base.rb', line 176

def row_success_count
  row_processed_count - row_errors.count
end