Class: SimpleEtl::Source::Base

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

Direct Known Subclasses

FixedWidth::Parser

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, &block) ⇒ Base



8
9
10
11
12
# File 'lib/simple_etl/source/base.rb', line 8

def initialize context, &block
  @errors = []
  @context = context
  context.send :instance_eval, &block if block
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/simple_etl/source/base.rb', line 6

def context
  @context
end

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

Instance Method Details

#fetch_field_from_row(row, field) ⇒ Object



14
15
16
# File 'lib/simple_etl/source/base.rb', line 14

def fetch_field_from_row row, field
  raise 'Abstract Method'
end

#generate_field(field, row_obj) ⇒ Object



69
70
71
# File 'lib/simple_etl/source/base.rb', line 69

def generate_field field, row_obj
  row_obj.instance_exec &field[:block]
end

#parse(src, args = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/simple_etl/source/base.rb', line 48

def parse src, args = {}
  result = args[:result] || ParseResult.new
  lines = read_rows src, args
  lines.each_with_index do |row, index|
    if index >= context.row_count_to_skip
      parse_row row, :row_index => index, :result => result
    end
  end
  result
end

#parse_field(row, field, row_obj) ⇒ Object

Raises:



59
60
61
62
63
64
65
66
67
# File 'lib/simple_etl/source/base.rb', line 59

def parse_field row, field, row_obj
  value = FieldCaster.send "parse_#{field[:type]}", fetch_field_from_row(row, field)
  raise FieldRequiredError if field[:required] &&
    (value.nil? || value == '')
  if transformer = context.transformations[field[:name]]
    value = row_obj.instance_exec value, &transformer
  end
  value
end

#parse_row(row, args = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/simple_etl/source/base.rb', line 18

def parse_row row, args = {}
  row_index = args[:row_index]
  result = args[:result] || ParseResult.new
  row_obj = Row.new
  context.fields.each do |field|
    begin
      row_obj.attributes[field[:name]] = parse_field row, field, row_obj
    rescue SimpleEtl::Source::ParseError
      row_info = row_index && "row #{row_index}" || ''
      result.append_error row_index, "Error parsing #{row_info}, column #{field[:name]}: #{$!.message}", $!
    end
  end
  if result.valid?
    context.generators.each do |field|
      begin
        row_obj.attributes[field[:name]] = generate_field field, row_obj
      rescue SimpleEtl::Source::ParseError
        row_info = row_index && "for row #{row_index}" || ''
        result.append_error row_index, "Error generating #{field[:name]} #{row_info}: #{$!.message}", $!
      end
    end
  end
  result.rows << row_obj
  result
end

#read_rows(src, args) ⇒ Object



44
45
46
# File 'lib/simple_etl/source/base.rb', line 44

def read_rows src, args
  raise 'Abstract Method'
end