Module: SM
- Defined in:
- lib/entities/record.rb,
lib/super_migration.rb,
lib/entities/fields_map.rb
Defined Under Namespace
Classes: FieldsMap, Record, SuperMigration
Instance Method Summary
collapse
Instance Method Details
#define_init_classes(from, to) ⇒ Object
#field(options = {}, &block) ⇒ Object
122
123
124
125
126
127
128
129
130
|
# File 'lib/super_migration.rb', line 122
def field(options = {}, &block)
return if options.size < 2
if block_given?
raise "Arity for block is not good." if block.arity != 1
end
fields_map = FieldsMap.new(options[:from], options[:to], block)
SuperMigration.current_record.fields_maps << fields_map
end
|
#table(table = {}, &block) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/super_migration.rb', line 86
def table(table = {}, &block)
return unless block_given?
define_init_classes(table[:from], table[:to])
puts "Migrating data from table #{table[:from]} to table #{table[:to]}"
puts "-----------------------------------------------------------------"
yield
from_table = SuperMigration.current_table_from
to_table = SuperMigration.current_table_to
Kernel.const_get(from_table).all.collect do |record|
rc = SuperMigration.current_record.dup
new_record_hash = Hash.new
rc.fields_maps.each do |field_map|
unless record.respond_to?(field_map.from)
raise "Undefined attribute name \"#{field_map.from}\" for Model #{from_table}."
end
if field_map.block
new_record_hash[field_map.to] = field_map.block.call(record.send(field_map.from))
else
new_record_hash[field_map.to] = record.send(field_map.from)
end
end
new_record = Kernel.const_get(to_table).new(new_record_hash)
new_record.save!
end
SuperMigration.current_record = Hash.new
end
|