Class: Nfcollector::Mapping::Transpiler
- Inherits:
-
Object
- Object
- Nfcollector::Mapping::Transpiler
- Defined in:
- lib/nfcollector/mapping/transpiler.rb
Instance Attribute Summary collapse
-
#outputs ⇒ Object
readonly
Returns the value of attribute outputs.
Instance Method Summary collapse
- #add_column(output) ⇒ Object
- #after_row(processor, options = {}) ⇒ Object
- #headers ⇒ Object
-
#index_of(column) ⇒ Object
The index of the column name in the output.
-
#initialize(input_definition, column_options = {}) ⇒ Transpiler
constructor
A new instance of Transpiler.
- #primary_key(output_row) ⇒ Object
- #process_after_row_hooks(mapped_row) ⇒ Object
- #process_categories(row) ⇒ Object
- #process_categories_with(categories_processor) ⇒ Object
- #transpile(row) ⇒ Object
Constructor Details
#initialize(input_definition, column_options = {}) ⇒ Transpiler
Returns a new instance of Transpiler.
21 22 23 24 25 26 |
# File 'lib/nfcollector/mapping/transpiler.rb', line 21 def initialize(input_definition, = {}) @input_definition = input_definition @outputs = [] @after_row_hooks = [] @column_options = end |
Instance Attribute Details
#outputs ⇒ Object (readonly)
Returns the value of attribute outputs.
19 20 21 |
# File 'lib/nfcollector/mapping/transpiler.rb', line 19 def outputs @outputs end |
Instance Method Details
#add_column(output) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/nfcollector/mapping/transpiler.rb', line 32 def add_column(output) # TODO: Make this output.should_transpile?(@input_definition) if output.inputs.blank? || @input_definition.has_index_for?(output.inputs) || output.required? column_transpiler = ColumnTranspiler.new(output, @column_options) column_transpiler.build!(@input_definition) @outputs << column_transpiler if output.primary_key? @primary_key_index = @outputs.size - 1 end end end |
#after_row(processor, options = {}) ⇒ Object
51 52 53 54 55 |
# File 'lib/nfcollector/mapping/transpiler.rb', line 51 def after_row(processor, = {}) required_columns = [:using] indicies = Array(required_columns).map { |column| index_of(column) } @after_row_hooks << AfterRowHook.new(processor, indicies) end |
#headers ⇒ Object
28 29 30 |
# File 'lib/nfcollector/mapping/transpiler.rb', line 28 def headers @outputs.map(&:name) end |
#index_of(column) ⇒ Object
Returns the index of the column name in the output.
59 60 61 |
# File 'lib/nfcollector/mapping/transpiler.rb', line 59 def index_of(column) @outputs.index { |output| output.name == column } end |
#primary_key(output_row) ⇒ Object
86 87 88 89 |
# File 'lib/nfcollector/mapping/transpiler.rb', line 86 def primary_key(output_row) return nil if @primary_key_index.blank? output_row[@primary_key_index] end |
#process_after_row_hooks(mapped_row) ⇒ Object
74 75 76 77 78 |
# File 'lib/nfcollector/mapping/transpiler.rb', line 74 def process_after_row_hooks(mapped_row) @after_row_hooks.each do |hook| hook.perform(mapped_row) end end |
#process_categories(row) ⇒ Object
80 81 82 83 84 |
# File 'lib/nfcollector/mapping/transpiler.rb', line 80 def process_categories(row) if @categories_processor.present? && @category_index.present? @categories_processor.perform(row[@category_index]) end end |
#process_categories_with(categories_processor) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/nfcollector/mapping/transpiler.rb', line 44 def process_categories_with(categories_processor) @categories_processor = categories_processor if @input_definition.has_index_for?(:category) || @categories_processor.required? @category_index = @input_definition.column_index(:category) end end |
#transpile(row) ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/nfcollector/mapping/transpiler.rb', line 63 def transpile(row) MappedRow.new.tap do |out| @outputs.each do |ct| out << ct.go(row) end out.category_ids = process_categories(row) out.id = primary_key(out) process_after_row_hooks(out) end end |