Class: ActiveMigration::Transformer::GroupedFieldFixedSpelling
Instance Attribute Summary collapse
Instance Method Summary
collapse
#full_title, #pluralize_without_count, #puts_on_file, #title
#name_or_empty, #name_or_nothing
#after_row_saved, #begin_transaction, #end_transaction, #transform_ignore_fields
Constructor Details
Returns a new instance of GroupedFieldFixedSpelling.
11
12
13
14
15
16
17
18
19
20
|
# File 'lib/active_migration/transformer/grouped_field_fixed_spelling.rb', line 11
def initialize(schema)
@schema = schema
@field_group = :name
@field_group_fixed = :name_correct
@group = {}
@rows = []
@domain_name = "domain"
end
|
Instance Attribute Details
#domain_dest ⇒ Object
Returns the value of attribute domain_dest.
9
10
11
|
# File 'lib/active_migration/transformer/grouped_field_fixed_spelling.rb', line 9
def domain_dest
@domain_dest
end
|
#domain_name ⇒ Object
Returns the value of attribute domain_name.
9
10
11
|
# File 'lib/active_migration/transformer/grouped_field_fixed_spelling.rb', line 9
def domain_name
@domain_name
end
|
Instance Method Details
#begin(schema_from, schema_to) ⇒ Object
22
23
24
25
26
27
28
|
# File 'lib/active_migration/transformer/grouped_field_fixed_spelling.rb', line 22
def begin(schema_from, schema_to)
@schema_from = schema_from
@schema_to = schema_to
@group = {}
@domain_dictionary = {}
@rows = []
end
|
#end(schema_from, schema_to) ⇒ Object
61
62
63
64
|
# File 'lib/active_migration/transformer/grouped_field_fixed_spelling.rb', line 61
def end(schema_from, schema_to)
save_on_cache
save_on_db
end
|
#fix_field(row) ⇒ Object
52
53
54
55
56
57
58
59
|
# File 'lib/active_migration/transformer/grouped_field_fixed_spelling.rb', line 52
def fix_field(row)
if (!row[@field_group_fixed].nil? && !row[@field_group_fixed].to_s.empty?)
row[@field_group_fixed] = row[@field_group_fixed].to_s.strip
@domain_dictionary[row[@field_group]] = row[@field_group_fixed]
row[@field_group] = row[@field_group_fixed]
end
end
|
#save_on_cache ⇒ Object
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/active_migration/transformer/grouped_field_fixed_spelling.rb', line 83
def save_on_cache
@cache_path = "db/external_migrate/cache/"
Dir.mkdir path if not Dir.exists? @cache_path
puts_on_file @cache_path + "#{@domain_name}_dictionary.yml", @domain_dictionary.to_yaml
puts_on_file @cache_path + "#{@domain_name}_grouped.csv" do
lines = []
@group.each do |key, data|
line = []
data.each do |key, value|
if value.is_a? String
line << value
elsif value.respond_to? :name
line << value.name
end
end
lines << line.join(",")
end
lines.join("\n")
end
end
|
#save_on_db ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/active_migration/transformer/grouped_field_fixed_spelling.rb', line 70
def save_on_db
class_to = schema_to_class
@group.each do |key, row|
if not class_to.send(("find_by_%s" % @field_group).to_sym, row[@field_group])
record = class_to.new(row)
puts "SaveOnDB: "+row.to_yaml
record.save!
end
end
end
|
#schema_to_class ⇒ Object
66
67
68
|
# File 'lib/active_migration/transformer/grouped_field_fixed_spelling.rb', line 66
def schema_to_class
eval @schema_to[:url]
end
|
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/active_migration/transformer/grouped_field_fixed_spelling.rb', line 30
def transform(row)
begin
@rows << row
fix_field row
return :ignore if row[@field_group].nil?
row[@field_group].gsub! /\s{2,}/, ' '
row.delete @field_group_fixed
@group[row[@field_group]] = row
return :ignore
rescue Exception => e
Rails.logger.debug "Error on transform. "+e.message+"\n"+e.backtrace.join("\n ")
false
end
end
|