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
- #batch_preprocess(&block) ⇒ Object
- #column(name, *args, refs: [], from: nil, **extra, &block) ⇒ Object
- #incremental(scope = nil, **kwargs) ⇒ Object
-
#initialize(table) ⇒ TableSchemaBuilder
constructor
A new instance of TableSchemaBuilder.
- #row_preprocess(&block) ⇒ Object
- #source(source = nil, override_model = nil, **kwargs, &block) ⇒ Object
- #version(version) ⇒ Object
Constructor Details
#initialize(table) ⇒ TableSchemaBuilder
64 65 66 67 |
# File 'lib/inst_data_shipper/schema_builder.rb', line 64 def initialize(table) = 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 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 |
#batch_preprocess(&block) ⇒ Object
108 109 110 111 |
# File 'lib/inst_data_shipper/schema_builder.rb', line 108 def batch_preprocess(&block) [:batch_preprocess] ||= [] [:batch_preprocess] << block end |
#column(name, *args, refs: [], from: nil, **extra, &block) ⇒ Object
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 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/inst_data_shipper/schema_builder.rb', line 118 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.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, "Unsupported options: #{extras.inspect}" end [:incremental] = { on: Array(kwargs[:on]), scope: scope, if: kwargs[:if], } end |
#row_preprocess(&block) ⇒ Object
113 114 115 116 |
# File 'lib/inst_data_shipper/schema_builder.rb', line 113 def row_preprocess(&block) [:row_preprocess] ||= [] [:row_preprocess] << block 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 |