Class: SimpleEtl::Source::BaseContext

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_etl/source/base_context.rb

Direct Known Subclasses

FixedWidth::Context

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBaseContext



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

#fieldsObject (readonly)

Returns the value of attribute fields.



4
5
6
# File 'lib/simple_etl/source/base_context.rb', line 4

def fields
  @fields
end

#generatorsObject (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_skipObject (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

#transformationsObject (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

Raises:



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