Class: DatabaseTransform::SchemaTable
- Inherits:
-
Object
- Object
- DatabaseTransform::SchemaTable
- Defined in:
- lib/database_transform/schema_table.rb
Overview
Represents a transformation from a source table to a destination table.
Instance Method Summary collapse
-
#column(*args, &block) ⇒ Object
Declare the mapping for the source table to the new table.
-
#initialize(source, destination, default_scope) ⇒ SchemaTable
constructor
Initialises the table definition for a particular schema.
-
#primary_key(id) ⇒ Object
Declare the primary key of the source table.
- #run_transform ⇒ Object
-
#save(options = {}) ⇒ Object
Specifies a save clause.
Constructor Details
#initialize(source, destination, default_scope) ⇒ SchemaTable
Initialises the table definition for a particular schema
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/database_transform/schema_table.rb', line 8 def initialize(source, destination, default_scope) @source = source @source.extend(DatabaseTransform::SchemaTableRecordMapping) @destination = destination @destination.extend(DatabaseTransform::SchemaTableRecordMapping) if @destination @default_scope = default_scope @primary_key = nil @save = nil @columns = [] end |
Instance Method Details
#column(*args, &block) ⇒ Object
Declare the mapping for the source table to the new table.
This function takes source columns to provide to the mapping proc as arguments.
The destination column is specified as the to hash parameter. If no mapping block is specified, the source column is copied to the destination column without modification. This is not possible if more than one source column is specified. If the mapping block is specified, if the destination column is specified, the result of the block is used as the destination column’s value. Otherwise, the result is unused. This can be used to execute the block purely for side-effects
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/database_transform/schema_table.rb', line 43 def column(*args, &block) raise ArgumentError if args.length < 1 # Get the columns = args. source_columns = args to_column = .delete(:to) to_column ||= source_columns.first unless block (source_columns, to_column, , block) # Store the mapping @columns << { from: source_columns, to: to_column, null: [:null].nil? ? true : [:null], block: block } end |
#primary_key(id) ⇒ Object
Declare the primary key of the source table.
23 24 25 26 27 |
# File 'lib/database_transform/schema_table.rb', line 23 def primary_key(id) raise DatabaseTransform::DuplicateError.new(id) if @primary_key @primary_key = id @source.primary_key = id end |
#run_transform ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/database_transform/schema_table.rb', line 83 def run_transform = if @destination format("-- transforming '%s' to '%s'\n", @source.table_name, @destination.table_name) else format("-- transforming '%s'\n", @source.table_name) end time_block(, " -> %fs\n", &method(:transform!)) end |
#save(options = {}) ⇒ Object
Specifies a save clause.
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/database_transform/schema_table.rb', line 69 def save( = {}) raise ArgumentError.new('unless and if cannot be both specified') if [:unless] && [:if] raise ArgumentError.new('Cannot specify a save clause twice for the same table') if @save if [:unless] clause = .delete(:unless) [:if] = ->(*callback_args) { !self.instance_exec(*callback_args, &clause) } end @save = end |