Class: InstDataShipper::SchemaBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/inst_data_shipper/schema_builder.rb

Overview

This class ends up filling two roles - Schema and Mapping. It makes for a clean API, but it’s a little less canonical since, (eg) the S3 destination doesn’t need column type annotations.

Defined Under Namespace

Classes: TableSchemaBuilder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSchemaBuilder

Returns a new instance of SchemaBuilder.



7
8
9
10
11
# File 'lib/inst_data_shipper/schema_builder.rb', line 7

def initialize
  @schema = {
    tables: [],
  }
end

Instance Attribute Details

#schemaObject (readonly)

Returns the value of attribute schema.



5
6
7
# File 'lib/inst_data_shipper/schema_builder.rb', line 5

def schema
  @schema
end

Class Method Details

.build(&block) ⇒ Object



13
14
15
16
17
# File 'lib/inst_data_shipper/schema_builder.rb', line 13

def self.build(&block)
  builder = new
  builder.instance_exec(&block)
  builder.schema
end

Instance Method Details

#extend_table_builder(modul = nil, &block) ⇒ Object



23
24
25
26
27
# File 'lib/inst_data_shipper/schema_builder.rb', line 23

def extend_table_builder(modul = nil, &block)
  @table_builder_class ||= Class.new(TableSchemaBuilder)
  @table_builder_class.class_eval(&block) if block.present?
  @table_builder_class.extend(modul) if modul.present?
end

#table(model_or_name, description = nil, model: nil, query: nil, **extra, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/inst_data_shipper/schema_builder.rb', line 29

def table(model_or_name, description = nil, model: nil, query: nil, **extra, &block)
  tdef = {
    warehouse_name: nil,
    description: description,
    columns: [],

    model: model,
    query: query,
    **extra,
  }

  if model_or_name.is_a?(ActiveRecord::Relation)
    raise "model specified twice" if model.present?
    raise "query specified twice" if query.present?

    tdef[:query] = model_or_name
    tdef[:model] = model_or_name.model
    tdef[:warehouse_name] = model_or_name.model.table_name
  elsif model_or_name.is_a?(Class) && model_or_name < ActiveRecord::Base
    tdef[:warehouse_name] = model_or_name.table_name
    tdef[:model] = model_or_name
  else
    tdef[:warehouse_name] = model_or_name
  end

  (@table_builder_class || TableSchemaBuilder).build(tdef, &block)

  @schema[:tables] << tdef

  tdef
end

#version(version) ⇒ Object



19
20
21
# File 'lib/inst_data_shipper/schema_builder.rb', line 19

def version(version)
  @schema[:version] = version
end