-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.rb
More file actions
68 lines (55 loc) · 1.52 KB
/
Copy pathexport.rb
File metadata and controls
68 lines (55 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Export
ALL_EXPORTS = Dir['app/lib/export/*_export.rb'].map { |f| "Export/#{File.basename(f, '.rb')}".classify.constantize }
class ProceedableError < StandardError
# Raising this error does not interrupt the TransformationPipeline
end
class MissingAttributeError < StandardError; end
def initialize(collection, params)
@collection = collection
# Parse Input params
setup_inputs!(params)
# Specific initialization
init
end
def run!
begin
result = transform!
rescue StandardError => e
case e
when ProceedableError
Rails.logger.error(e)
else
raise e
end
end
end
def self.to_sym
self.name.underscore.gsub(/\_?transformation\/?/, '').to_sym
end
def export!
raise 'Missing implementation!'
end
def init
# Do nothing, should be overriden
end
def check_attributes
@inputs.each do |obj_method, trans_method|
raise MissingAttributeError, "Attribute #{i} is not available on #{@obj}" unless @obj.respond_to?(trans_method)
end
end
def setup_inputs!(params)
self.class::INPUTS.each do |input, type|
input = input.to_s
case type
when :string
instance_variable_set "@#{input}", params[input]
when :attributes
instance_variable_set "@#{input}", params[input]
when :attribute
instance_variable_set "@#{input}", params[input]
when :bool
instance_variable_set "@#{input}", (params[input] == "true" ? true : false)
end
end
end
end