Class: DatabaseTransform::SchemaTable

Inherits:
Object
  • Object
show all
Defined in:
lib/database_transform/schema_table.rb

Overview

Represents a transformation from a source table to a destination table.

Instance Method Summary collapse

Constructor Details

#initialize(source, destination, default_scope) ⇒ SchemaTable

Initialises the table definition for a particular schema

Parameters:

  • source (Class)

    The model class to map source records from

  • destination (Class)

    The model class to map destination records to

  • default_scope (nil, Proc)

    The default scope for querying the source table.



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

Parameters:

  • options (Hash)

    a customizable set of options

Raises:

  • (ArgumentError)


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
  options = args.extract_options!
  source_columns = args
  to_column = options.delete(:to)
  to_column ||= source_columns.first unless block

  validate_column_options!(source_columns, to_column, options, block)

  # Store the mapping
  @columns << {
      from: source_columns,
      to: to_column,
      null: options[:null].nil? ? true : options[:null],
      block: block
  }
end

#primary_key(id) ⇒ Object

Declare the primary key of the source table.

Parameters:

  • id (Symbol)

    The name of the column in the source table which is the primary key.

Raises:



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(schema = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/database_transform/schema_table.rb', line 86

def run_transform(schema = nil)
  before_message =
    if @destination
      format("-- transforming '%s' to '%s'", @source.table_name, @destination.table_name)
    else
      format("-- transforming '%s'", @source.table_name)
    end

  time_block(before_message, "   -> %fs") do
    transform!(schema)
  end
end

#save(options = {}) ⇒ Object

Specifies a save clause.

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • unless (Proc)

    The record will be saved if the proc returns a false value. This cannot be used together with if.

  • if (Proc)

    A proc to call. The record will be saved if the proc returns a true value. This cannot be used together with unless.

  • validate (Boolean)

    Whether to run the model validations when saving the transformed record. Defaults to true.

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/database_transform/schema_table.rb', line 71

def save(options = {})
  raise ArgumentError.new('unless and if cannot be both specified') if options[:unless] && options[:if]
  raise ArgumentError.new('Cannot specify a save clause twice for the same table') if @save

  if options[:unless]
    clause = options.delete(:unless)
    options[:if] = ->(*callback_args) { !self.instance_exec(*callback_args, &clause) }
  end
  options[:validate] = true unless options.has_key?(:validate)

  @save = options
end