Class: SimpleEtl::Source::BaseContext
- Inherits:
-
Object
- Object
- SimpleEtl::Source::BaseContext
- Defined in:
- lib/simple_etl/source/base_context.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#generators ⇒ Object
readonly
Returns the value of attribute generators.
-
#row_count_to_skip ⇒ Object
readonly
Returns the value of attribute row_count_to_skip.
-
#transformations ⇒ Object
readonly
Returns the value of attribute transformations.
Instance Method Summary collapse
- #field(name, args = {}) ⇒ Object
- #generate(name, args = {}, &block) ⇒ Object
-
#initialize ⇒ BaseContext
constructor
A new instance of BaseContext.
- #method_missing(name, *params, &block) ⇒ Object
- #skip_rows(row_count) ⇒ Object
- #transform(field, &block) ⇒ Object
Constructor Details
#initialize ⇒ BaseContext
9 10 11 12 13 14 |
# File 'lib/simple_etl/source/base_context.rb', line 9 def initialize @fields = [] @transformations = {} @generators = [] @row_count_to_skip = 0 end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *params, &block) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/simple_etl/source/base_context.rb', line 39 def method_missing name, *params, &block md = name.to_s.match /^(required_)?(\w+)$/ type = md && md[2].to_sym if type && FieldCaster.respond_to?("parse_#{type}") params << {} unless params.last.is_a? Hash params.last[:type] = type params.last[:required] = true if md[1] field *params else super end end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
4 5 6 |
# File 'lib/simple_etl/source/base_context.rb', line 4 def fields @fields end |
#generators ⇒ Object (readonly)
Returns the value of attribute generators.
6 7 8 |
# File 'lib/simple_etl/source/base_context.rb', line 6 def generators @generators end |
#row_count_to_skip ⇒ Object (readonly)
Returns the value of attribute row_count_to_skip.
7 8 9 |
# File 'lib/simple_etl/source/base_context.rb', line 7 def row_count_to_skip @row_count_to_skip end |
#transformations ⇒ Object (readonly)
Returns the value of attribute transformations.
5 6 7 |
# File 'lib/simple_etl/source/base_context.rb', line 5 def transformations @transformations end |
Instance Method Details
#field(name, args = {}) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/simple_etl/source/base_context.rb', line 16 def field name, args = {} args = {:required => false, :type => :object}.merge args unless FieldCaster.respond_to? "parse_#{args[:type]}" raise FieldArgumentError.new "#{name}:type (#{args[:type]}) is unknown" end fields << { :name => name }.merge(args) end |
#generate(name, args = {}, &block) ⇒ Object
31 32 33 |
# File 'lib/simple_etl/source/base_context.rb', line 31 def generate name, args = {}, &block generators << args.merge(:name => name, :block => block) end |
#skip_rows(row_count) ⇒ Object
35 36 37 |
# File 'lib/simple_etl/source/base_context.rb', line 35 def skip_rows row_count @row_count_to_skip = row_count end |
#transform(field, &block) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/simple_etl/source/base_context.rb', line 24 def transform field, &block field = field.to_sym raise FieldNotFoundError.new(field) unless fields.detect { |d| d[:name] == field } transformations[field.to_sym] = block end |