Class: InstDataShipper::SchemaBuilder::TableSchemaBuilder

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table) ⇒ TableSchemaBuilder

Returns a new instance of TableSchemaBuilder.



64
65
66
67
# File 'lib/inst_data_shipper/schema_builder.rb', line 64

def initialize(table)
  @options = table
  @columns = table[:columns]
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



62
63
64
# File 'lib/inst_data_shipper/schema_builder.rb', line 62

def columns
  @columns
end

#optionsObject (readonly)

Returns the value of attribute options.



62
63
64
# File 'lib/inst_data_shipper/schema_builder.rb', line 62

def options
  @options
end

Class Method Details

.build(tdef, &block) ⇒ Object



69
70
71
72
73
# File 'lib/inst_data_shipper/schema_builder.rb', line 69

def self.build(tdef, &block)
  builder = new(tdef)
  builder.instance_exec(&block)
  builder.options
end

Instance Method Details

#annotate(key, value) ⇒ Object



75
76
77
# File 'lib/inst_data_shipper/schema_builder.rb', line 75

def annotate(key, value)
  options[key] = value
end

#column(name, *args, refs: [], from: nil, **extra, &block) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/inst_data_shipper/schema_builder.rb', line 108

def column(name, *args, refs: [], from: nil, **extra, &block)
  from ||= name.to_s

  cdef = {
    warehouse_name: name.to_s,
    from: from,
    **extra,
  }

  if args[0].is_a?(Symbol)
    cdef[:type] = args.shift()
  end

  if args[0].is_a?(String)
    cdef[:description] = args.shift()
  end

  if args.present?
    raise ArgumentError, "Received unexpected arguments: #{args.inspect}"
  end

  cdef[:references] = Array(refs)

  if options[:model].is_a?(Class) && cdef[:local_name].to_s.ends_with?('_id')
    rel_name = cdef[:local_name].to_s[0...-3]
    refl = options[:model].reflections[rel_name]
    cdef[:references] << "#{refl.klass}##{refl.options[:primary_key] || 'id'}" if refl.present? && !refl.polymorphic?
  end

  compiled_from = compile_transformer(from)

  cdef[:block] = ->(row) {
    value = instance_exec(row, &compiled_from)
    value = instance_exec(value, row, &block) if block.present?
    value
  }

  @columns << cdef

  cdef
end

#incremental(scope = nil, **kwargs) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/inst_data_shipper/schema_builder.rb', line 83

def incremental(scope=nil, **kwargs)
  if (extras = kwargs.keys - %i[on if]).present?
    raise ArgumentError, "Unsuppored options: #{extras.inspect}"
  end

  options[:incremental] = {
    on: Array(kwargs[:on]),
    scope: scope,
    if: kwargs[:if],
  }
end

#source(source = nil, override_model = nil, **kwargs, &block) ⇒ Object

Raises:

  • (ArgumentError)


95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/inst_data_shipper/schema_builder.rb', line 95

def source(source=nil, override_model=nil, **kwargs, &block)
  raise "Source already set" if options[:sourcer].present?
  raise ArgumentError, "Cannot provide source and block" if source.present? && block.present?

  if source.is_a?(Symbol)
    mthd = :"import_#{source}"
    options = self.options
    source = ->(table_def) { send(mthd, override_model || options[:model] || options[:warehouse_name], schema_name: options[:warehouse_name], **kwargs) }
  end

  self.options[:sourcer] = source
end

#version(version) ⇒ Object



79
80
81
# File 'lib/inst_data_shipper/schema_builder.rb', line 79

def version(version)
  options[:version] = version
end