Module: LegacyMigrations::Transformations
- Defined in:
- lib/legacy_migrations/transformations.rb
Instance Method Summary collapse
-
#from(from_attribute, *args) ⇒ Object
Move data from the fromattribute_ in the ‘from’ table to a column :to in the ‘to’ table.
-
#match_same_name_attributes(*options) ⇒ Object
Shortcut for transferring data between similar tables.
- #stored(stored_attribute, *args) ⇒ Object
Instance Method Details
#from(from_attribute, *args) ⇒ Object
Move data from the fromattribute_ in the ‘from’ table to a column :to in the ‘to’ table
Options
-
:to
- (Required) Column in destination table to fill in with value in ‘from’ -
:if
- specify a function that takes one parameter (the source table’s RECORD) and returns true or false. The assignment is only made if the function returns a non-false value. -
&block
- if passed a block, the block takes one parameter, which is the value of the attribute in the from parameter, then inserts the result of the block into the destination attribute. parameter and inserts the result of the block in the provided ‘to’ column. -
:from_record
- If you set the “from” column to “from_record”, the variable passed into the block will be the entire from record, instead of just one of its columns
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 |
# File 'lib/legacy_migrations/transformations.rb', line 20 def from(from_attribute, *args) = args. if [:if] if_method = Proc.new {|record| send([:if], record)} else if_method = Proc.new {|record| true } end #anyone want to give this a try in another language? ;-) custom_method = Proc.new {|record| if if_method.call(record) case when block_given? && from_attribute == :from_record yield(record) when block_given? yield(record.send(:[], from_attribute.to_s)) else record.send(:[], from_attribute.to_s) end else nil end } @columns.merge!({[:to] => custom_method}) end |
#match_same_name_attributes(*options) ⇒ Object
Shortcut for transferring data between similar tables. For example, if two tables have a column named ‘name’, then using this function will transfer data from source table’s ‘name’ column to the destination table’s ‘name’ column.
Options
-
:only
- Array of columns that you want to transfer, and that have the same name on both tables. -
:except
- Array of columns that you DON’T want to transfer, but that have the same name on both tables.
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/legacy_migrations/transformations.rb', line 59 def match_same_name_attributes(*) = . same_name_attributes = @from_table.columns.map(&:name) & @to_table.columns.map(&:name) if same_name_attributes same_name_attributes = (same_name_attributes, ) same_name_attributes.each do |same_name_attribute| from same_name_attribute, :to => same_name_attribute end end end |
#stored(stored_attribute, *args) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/legacy_migrations/transformations.rb', line 72 def stored(stored_attribute, *args) = args. if [:if] if_method = Proc.new {|record| send([:if], record)} else if_method = Proc.new {|record| true } end custom_method = Proc.new {|record| if if_method.call(record) FutureStorage.instance[@from_table.to_s][record.id][stored_attribute] else nil end } @columns.merge!({[:to] => custom_method}) end |