Class: Importable::Mapper
- Inherits:
-
Object
- Object
- Importable::Mapper
show all
- Defined in:
- lib/importable/mapper.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(data, params = {}) ⇒ Mapper
Returns a new instance of Mapper.
6
7
8
9
10
11
12
13
14
15
16
|
# File 'lib/importable/mapper.rb', line 6
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
89
90
91
92
|
# File 'lib/importable/mapper.rb', line 89
def method_missing(sym, *args, &block)
return @params[sym] if !!@params[sym]
super(sym, *args, &block)
end
|
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
3
4
5
|
# File 'lib/importable/mapper.rb', line 3
def data
@data
end
|
#invalid_items ⇒ Object
Returns the value of attribute invalid_items.
4
5
6
|
# File 'lib/importable/mapper.rb', line 4
def invalid_items
@invalid_items
end
|
Class Method Details
.mapper_root ⇒ Object
31
32
33
|
# File 'lib/importable/mapper.rb', line 31
def mapper_root
"#{Rails.root}/app/imports"
end
|
.mapper_type_exists?(type) ⇒ Boolean
42
43
44
45
46
47
48
|
# File 'lib/importable/mapper.rb', line 42
def mapper_type_exists?(type)
type = type.try(:sub, '-', '/')
mapper_types.flat_map do |t|
[ t.pluralize, t.singularize ]
end.include?(type)
end
|
.mapper_types ⇒ Object
35
36
37
38
39
40
|
# File 'lib/importable/mapper.rb', line 35
def mapper_types
Dir["#{mapper_root}/**/*.rb"].map do |file|
offset = mapper_root.length + 1
file.slice(offset..-11)
end.sort
end
|
.require_param(name, message) ⇒ Object
19
20
21
22
23
24
25
|
# File 'lib/importable/mapper.rb', line 19
def require_param(name, message)
@required_params ||= []
@required_params << {
name: name,
message: message
}
end
|
.required_params ⇒ Object
27
28
29
|
# File 'lib/importable/mapper.rb', line 27
def required_params
@required_params || []
end
|
Instance Method Details
#after_mapping ⇒ Object
54
55
|
# File 'lib/importable/mapper.rb', line 54
def after_mapping
end
|
#before_mapping ⇒ Object
51
52
|
# File 'lib/importable/mapper.rb', line 51
def before_mapping
end
|
#map_row ⇒ Object
57
58
59
|
# File 'lib/importable/mapper.rb', line 57
def map_row
raise NotImplementedError.new('map_row method must be overriden by mapper')
end
|
#map_to_objects ⇒ Object
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/importable/mapper.rb', line 65
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
|
#save_items ⇒ Object
76
77
78
79
80
|
# File 'lib/importable/mapper.rb', line 76
def save_items
if valid?
@data.each { |object| object.save! if object.new_record? }
end
end
|
#valid? ⇒ Boolean
61
62
63
|
# File 'lib/importable/mapper.rb', line 61
def valid?
@invalid_items.empty?
end
|
#validate_items ⇒ Object
82
83
84
85
86
87
|
# File 'lib/importable/mapper.rb', line 82
def validate_items
@data.each_with_index do |object, index|
line_number = index + 2
@invalid_items << [object, line_number] unless object.valid?
end
end
|