Class: Importable::Mapper

Inherits:
Object
  • Object
show all
Defined in:
lib/importable/mapper.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, params = {}) ⇒ Mapper

Returns a new instance of Mapper.



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

def initialize(data, params={})
  @params = params
  @raw_data = data
  @invalid_items = []

  before_mapping
  map_to_objects
  after_mapping
  validate_items
  save_items
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



120
121
122
123
# File 'lib/importable/mapper.rb', line 120

def method_missing(sym, *args, &block)
  return @params[sym] if !!@params[sym]
  super(sym, *args, &block)
end

Class Attribute Details

.from_mappingsObject

Returns the value of attribute from_mappings.



21
22
23
# File 'lib/importable/mapper.rb', line 21

def from_mappings
  @from_mappings
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



4
5
6
# File 'lib/importable/mapper.rb', line 4

def data
  @data
end

#invalid_itemsObject

Returns the value of attribute invalid_items.



5
6
7
# File 'lib/importable/mapper.rb', line 5

def invalid_items
  @invalid_items
end

#raw_dataObject

Returns the value of attribute raw_data.



3
4
5
# File 'lib/importable/mapper.rb', line 3

def raw_data
  @raw_data
end

Class Method Details

.mapper_rootObject



35
36
37
# File 'lib/importable/mapper.rb', line 35

def mapper_root
  "#{Rails.root}/app/imports"
end

.mapper_type_exists?(type) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
# File 'lib/importable/mapper.rb', line 46

def mapper_type_exists?(type)
  type = type.try(:sub, '-', '/')

  mapper_types.flat_map do |t|
    [ t.pluralize, t.singularize ]
  end.include?(type)
end

.mapper_typesObject



39
40
41
42
43
44
# File 'lib/importable/mapper.rb', line 39

def mapper_types
  Dir["#{mapper_root}/**/*.rb"].map do |file|
    offset = mapper_root.length + 1
    file.slice(offset..-11)
  end.sort
end

.method_missing(sym, *args, &block) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/importable/mapper.rb', line 59

def method_missing(sym, *args, &block)
  if sym.to_s.ends_with('_maps_from')
    from = sym.to_s.slice(0...-10).to_sym
    options = args.second || {}

    register_from_mapping(from, args.first)
    return
  end

  super(sym, *args, &block)
end

.register_from_mapping(from, to) ⇒ Object



54
55
56
57
# File 'lib/importable/mapper.rb', line 54

def register_from_mapping(from, to)
  @from_mappings ||= {}
  @from_mappings[from] = to
end

.require_param(name, message) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/importable/mapper.rb', line 23

def require_param(name, message)
  @required_params ||= []
  @required_params << {
    name: name,
    message: message
  }
end

.required_paramsObject



31
32
33
# File 'lib/importable/mapper.rb', line 31

def required_params
  @required_params || []
end

Instance Method Details

#after_mappingObject



75
76
# File 'lib/importable/mapper.rb', line 75

def after_mapping
end

#before_mappingObject



72
73
# File 'lib/importable/mapper.rb', line 72

def before_mapping
end

#map_rowObject

Raises:

  • (NotImplementedError)


78
79
80
# File 'lib/importable/mapper.rb', line 78

def map_row
  raise NotImplementedError.new('map_row method must be overriden by mapper')
end

#map_to_objectsObject



86
87
88
89
90
91
92
93
94
95
# File 'lib/importable/mapper.rb', line 86

def map_to_objects
  @data = @raw_data.flat_map do |raw_row|
    row = if raw_row.instance_of?(Hash)
      Importable::Row.from_hash(raw_row)
    else
      Importable::Row.from_resource(raw_row)
    end
    map_row(row)
  end
end

#original_value_for(line_number, field) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/importable/mapper.rb', line 110

def original_value_for(line_number, field)
  if self.class.from_mappings
    original_key = self.class.from_mappings[field]
    if original_key
      line = @raw_data[line_number - 2]
      line[original_key.to_s]
    end
  end
end

#save_itemsObject



97
98
99
100
101
# File 'lib/importable/mapper.rb', line 97

def save_items
  if valid?
    @data.each { |object| object.save! if object.new_record? }
  end
end

#valid?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/importable/mapper.rb', line 82

def valid?
  @invalid_items.empty?
end

#validate_itemsObject



103
104
105
106
107
108
# File 'lib/importable/mapper.rb', line 103

def validate_items
  @data.each_with_index do |object, index|
    line_number = index + 2
    @invalid_items << [object, line_number] unless object.valid?
  end
end