Class: DataImp::Porter

Inherits:
Object
  • Object
show all
Defined in:
lib/data_imp/porter.rb

Direct Known Subclasses

Sample

Defined Under Namespace

Classes: Sample

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, index = nil) ⇒ Porter

Returns a new instance of Porter.



19
20
21
22
# File 'lib/data_imp/porter.rb', line 19

def initialize(hash, index = nil)
  @hash = HashWithIndifferentAccess.new(hash)
  @index = index
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *_args) ⇒ Object



28
29
30
31
32
# File 'lib/data_imp/porter.rb', line 28

def method_missing(method, *_args)
  return nil unless respond_to_missing?(method)
  value = hash[method] 
  parse(method,value)
end

Instance Attribute Details

#hashObject (readonly)

Returns the value of attribute hash.



4
5
6
# File 'lib/data_imp/porter.rb', line 4

def hash
  @hash
end

#indexObject (readonly)

Returns the value of attribute index.



4
5
6
# File 'lib/data_imp/porter.rb', line 4

def index
  @index
end

Class Method Details

.after_all_importsObject



55
56
57
# File 'lib/data_imp/porter.rb', line 55

def self.after_all_imports
  # called after all data is processed
end

.before_all_importsObject



38
39
40
# File 'lib/data_imp/porter.rb', line 38

def self.before_all_imports
  # called before any data is processed
end

.find_importer(type) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/data_imp/porter.rb', line 6

def self.find_importer type
  return self if type.blank?
  begin
    const_get type.classify
  rescue NameError => e
    if require_relative "porter/#{type.underscore}"
      retry 
    end
  end
rescue LoadError => e
  raise DataImp::NoImporter.new(type)
end

Instance Method Details

#after_importObject



51
52
53
# File 'lib/data_imp/porter.rb', line 51

def after_import
  # called after a record was imported
end

#before_importObject



42
43
44
# File 'lib/data_imp/porter.rb', line 42

def before_import
  # called for a record before import
end

#importObject



46
47
48
49
# File 'lib/data_imp/porter.rb', line 46

def import
  # called to import a record
  warn "Import: #{self.class.name}:#{index}:#{hash.inspect}"
end

#on_error(e) ⇒ Object



59
60
61
62
# File 'lib/data_imp/porter.rb', line 59

def on_error e
  warn "Error: #{self.class.name}:#{index}:#{hash.inspect}"
  warn e
end

#parse(method, value) ⇒ Object



34
35
36
# File 'lib/data_imp/porter.rb', line 34

def parse method, value
  value # override for special handling
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/data_imp/porter.rb', line 24

def respond_to_missing?(method, include_private = false)
  hash.has_key?(method) || false # super
end