Class: ETL::Control::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/etl/control/control.rb

Overview

The Context is passed to eval.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(control) ⇒ Context

Initialize the context



15
16
17
# File 'lib/etl/control/control.rb', line 15

def initialize(control)
  @control = control
end

Instance Attribute Details

#controlObject (readonly)

Returns the value of attribute control.



5
6
7
# File 'lib/etl/control/control.rb', line 5

def control
  @control
end

Class Method Details

.create(control) ⇒ Object

Create a Context instance



9
10
11
# File 'lib/etl/control/control.rb', line 9

def create(control)
  Context.new(control).get_binding
end

Instance Method Details

#after_read(name, configuration = {}) ⇒ Object

Define an “after read” processor. This must be a row-level processor.



156
157
158
# File 'lib/etl/control/control.rb', line 156

def after_read(name, configuration={})
  define_processor(name, after_read_processors, configuration)
end

#after_read_processorsObject

Get the defined “after read” processors



161
162
163
# File 'lib/etl/control/control.rb', line 161

def after_read_processors
  control.after_read_processors
end

#before_write(name, configuration = {}) ⇒ Object

Define a “before write” processor. This must be a row-level processor.



166
167
168
# File 'lib/etl/control/control.rb', line 166

def before_write(name, configuration={})
  define_processor(name, before_write_processors, configuration)
end

#before_write_processorsObject

Get the defined “before write” processors



171
172
173
# File 'lib/etl/control/control.rb', line 171

def before_write_processors
  control.before_write_processors
end

#copy(source, destination) ⇒ Object

Copy the source field to the destination field



130
131
132
# File 'lib/etl/control/control.rb', line 130

def copy(source, destination)
  after_read :copy_field, :source => source, :dest => destination
end

#dependenciesObject

Get the defined dependencies



38
39
40
# File 'lib/etl/control/control.rb', line 38

def dependencies
  control.dependencies
end

#depends_on(*args) ⇒ Object

Define a list of control files that this file depends on. Those control files will be executed prior to this control file. The list may contain symbols that will be converted to file names by calling to_s + ‘.ctl’, or they may be strings in which case they will be used as is



33
34
35
# File 'lib/etl/control/control.rb', line 33

def depends_on(*args)
  dependencies << args
end

#destination(name, configuration = {}, mapping = {}) ⇒ Object

Define a destination



75
76
77
78
79
80
81
82
# File 'lib/etl/control/control.rb', line 75

def destination(name, configuration={}, mapping={})
  destination_types.each do |dest_type|
    if configuration[dest_type]
      dest_class = ETL::Control::Destination.class_for_name(dest_type)
      destinations << dest_class.new(self, configuration, mapping)
    end
  end
end

#destinationsObject

Get the defined destinations



85
86
87
# File 'lib/etl/control/control.rb', line 85

def destinations
  control.destinations
end

#fileObject

Get the control file



20
21
22
# File 'lib/etl/control/control.rb', line 20

def file
  control.file
end

#get_bindingObject

Get the binding object



196
197
198
# File 'lib/etl/control/control.rb', line 196

def get_binding
  binding
end

#post_process(name, configuration = {}) ⇒ Object

Define a post-processor



186
187
188
# File 'lib/etl/control/control.rb', line 186

def post_process(name, configuration={})
  define_processor(name, post_processors, configuration)
end

#post_processorsObject

Get the defined post-processors



191
192
193
# File 'lib/etl/control/control.rb', line 191

def post_processors
  control.post_processors
end

#pre_process(name, configuration = {}) ⇒ Object

Define a pre-processor



176
177
178
# File 'lib/etl/control/control.rb', line 176

def pre_process(name, configuration={})
  define_processor(name, pre_processors, configuration)
end

#pre_processorsObject

Get the defined pre-processors



181
182
183
# File 'lib/etl/control/control.rb', line 181

def pre_processors
  control.pre_processors
end

#rename(source, destination) ⇒ Object

Rename the source field to the destination field



125
126
127
# File 'lib/etl/control/control.rb', line 125

def rename(source, destination)
  after_read :rename, :source => source, :dest => destination
end

#set_error_threshold(error_threshold) ⇒ Object

Set the allowed error threshold



25
26
27
# File 'lib/etl/control/control.rb', line 25

def set_error_threshold(error_threshold)
  control.error_threshold = error_threshold
end

#source(name, configuration = {}, definition = {}) ⇒ Object

Define a source.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/etl/control/control.rb', line 43

def source(name, configuration={}, definition={})
  if configuration[:type]
    case configuration[:type]
    when Class
      source_class = configuration[:type]
      sources << source_class.new(self, configuration, definition)
    when String, Symbol
      source_class = ETL::Control::Source.class_for_name(configuration[:type])
      sources << source_class.new(self, configuration, definition)
    else
      if configuration[:type].is_a?(ETL::Control::Source)
        sources << configuration[:type]
      else
        raise "Configuration must extend ETL::Control::Source"
      end
    end
  else
    source_types.each do |source_type|
      if configuration[source_type]
        source_class = ETL::Control::Source.class_for_name(source_type)
        sources << source_class.new(self, configuration, definition)
      end
    end
  end
end

#sourcesObject

Get the defined source



70
71
72
# File 'lib/etl/control/control.rb', line 70

def sources
  control.sources
end

#transform(name, transformer = nil, configuration = {}, &block) ⇒ Object

Define a transform



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/etl/control/control.rb', line 90

def transform(name, transformer=nil, configuration={}, &block)
  if transformer
    case transformer
    when String, Symbol
      class_name = "#{transformer.to_s.classify}Transform"
      begin
        transform_class = ETL::Transform.const_get(class_name)
        transforms << transform_class.new(self, name, configuration)
      rescue NameError => e
        raise ControlError, "Unable to find transformer #{class_name}: #{e}"
      end
    else
      #transformer.class.inspect
      if transformer.is_a?(ETL::Transform::Transform)
        Engine.logger.debug "Adding transformer #{transformer.inspect} for field #{name}"
        t = transformer.dup
        t.name = name
        transforms << t 
      else
        raise ControlError, "Transformer must be a String, Symbol or Transform instance"
      end
    end
  elsif block_given?
    transforms << ETL::Transform::BlockTransform.new(self, name, :block => block)
  else
    raise ControlError, "Either a transformer or a block must be specified"
  end
end

#transformsObject

Get the defined transforms



120
121
122
# File 'lib/etl/control/control.rb', line 120

def transforms
  control.transforms
end