Class: InstDataShipper::SchemaBuilder::TableSchemaBuilder
- Inherits:
-
Object
- Object
- InstDataShipper::SchemaBuilder::TableSchemaBuilder
- Defined in:
- lib/inst_data_shipper/schema_builder.rb
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #annotate(key, value) ⇒ Object
- #column(name, *args, refs: [], from: nil, **extra, &block) ⇒ Object
- #incremental(scope = nil, **kwargs) ⇒ Object
-
#initialize(table) ⇒ TableSchemaBuilder
constructor
A new instance of TableSchemaBuilder.
- #source(source = nil, override_model = nil, **kwargs, &block) ⇒ Object
- #version(version) ⇒ Object
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
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
62 63 64 |
# File 'lib/inst_data_shipper/schema_builder.rb', line 62 def columns @columns end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
62 63 64 |
# File 'lib/inst_data_shipper/schema_builder.rb', line 62 def @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. 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) [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 [:model].is_a?(Class) && cdef[:local_name].to_s.ends_with?('_id') rel_name = cdef[:local_name].to_s[0...-3] refl = [:model].reflections[rel_name] cdef[:references] << "#{refl.klass}##{refl.[: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 [:incremental] = { on: Array(kwargs[:on]), scope: scope, if: kwargs[:if], } end |
#source(source = nil, override_model = nil, **kwargs, &block) ⇒ Object
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 [:sourcer].present? raise ArgumentError, "Cannot provide source and block" if source.present? && block.present? if source.is_a?(Symbol) mthd = :"import_#{source}" = self. source = ->(table_def) { send(mthd, override_model || [:model] || [:warehouse_name], schema_name: [:warehouse_name], **kwargs) } end self.[:sourcer] = source end |
#version(version) ⇒ Object
79 80 81 |
# File 'lib/inst_data_shipper/schema_builder.rb', line 79 def version(version) [:version] = version end |