Class: ActiveMigration::Transformer::GroupedFieldFixedSpelling

Inherits:
Object
  • Object
show all
Includes:
ActiveMigration::Transformer, ApplicationHelper
Defined in:
lib/active_migration/transformer/grouped_field_fixed_spelling.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ApplicationHelper

#full_title, #pluralize_without_count, #puts_on_file, #title

Methods included from StringHelper

#name_or_empty, #name_or_nothing

Methods included from ActiveMigration::Transformer

#after_row_saved, #begin_transaction, #end_transaction, #transform_ignore_fields

Constructor Details

#initialize(schema) ⇒ GroupedFieldFixedSpelling

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_destObject

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_nameObject

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)
  #has name_correct?
  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_cacheObject



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
    
  #cached path
  @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}_raw.csv", @rows.map { |i| i.values.join(",")}.join("\n")
  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
  #puts_on_file path + "#{@domain_name}.xml", @rows.to_xml
end

#save_on_dbObject



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_classObject



66
67
68
# File 'lib/active_migration/transformer/grouped_field_fixed_spelling.rb', line 66

def schema_to_class
  eval @schema_to[:url]
end

#transform(row) ⇒ Object



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
    #cache
    @rows << row
    
    fix_field row
    
    return :ignore if row[@field_group].nil?
    
    #validating and cleanup
    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