Class: ETL::Control::Context

Inherits:
Object
  • Object
show all
Includes:
Test::Unit::Assertions
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



17
18
19
# File 'lib/etl/control/control.rb', line 17

def initialize(control)
  @control = control
end

Instance Attribute Details

#controlObject (readonly)

Returns the value of attribute control.



7
8
9
# File 'lib/etl/control/control.rb', line 7

def control
  @control
end

Class Method Details

.create(control) ⇒ Object

Create a Context instance



11
12
13
# File 'lib/etl/control/control.rb', line 11

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.



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

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

#after_read_processorsObject

Get the defined “after read” processors



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

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.



201
202
203
# File 'lib/etl/control/control.rb', line 201

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

#before_write_processorsObject

Get the defined “before write” processors



206
207
208
# File 'lib/etl/control/control.rb', line 206

def before_write_processors
  control.before_write_processors
end

#copy(source, destination) ⇒ Object

Copy the source field to the destination field



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

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

#dependenciesObject

Get the defined dependencies



41
42
43
# File 'lib/etl/control/control.rb', line 41

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



36
37
38
# File 'lib/etl/control/control.rb', line 36

def depends_on(*args)
  dependencies << args
end

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

Define a destination



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/etl/control/control.rb', line 80

def destination(name, configuration={}, mapping={})
  if configuration[:type]
    case configuration[:type]
    when Class
      dest_class = configuration[:type]
      destinations << dest_class.new(self, configuration, mapping)
    when String, Symbol
      dest_class = ETL::Control::Destination.class_for_name(configuration[:type])
      destinations << dest_class.new(self, configuration, mapping)
    else
      if configuration[:type].is_a?(ETL::Control::Destination)
        destinations << configuration[:type]
      else
        raise ControlError, "Type must be a Class, String, Symbol or object extending ETL::Control::Destination"
      end
    end
  else
    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)
        break
      end
      raise ControlError, "A destination was specified but no matching destination type was found"
    end
  end
end

#destinationsObject

Get the defined destinations



109
110
111
# File 'lib/etl/control/control.rb', line 109

def destinations
  control.destinations
end

#fileObject

Get the control file



22
23
24
# File 'lib/etl/control/control.rb', line 22

def file
  control.file
end

#get_bindingObject

Get the binding object



231
232
233
# File 'lib/etl/control/control.rb', line 231

def get_binding
  binding
end

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

Define a post-processor



221
222
223
# File 'lib/etl/control/control.rb', line 221

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

#post_processorsObject

Get the defined post-processors



226
227
228
# File 'lib/etl/control/control.rb', line 226

def post_processors
  control.post_processors
end

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

Define a pre-processor



211
212
213
# File 'lib/etl/control/control.rb', line 211

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

#pre_processorsObject

Get the defined pre-processors



216
217
218
# File 'lib/etl/control/control.rb', line 216

def pre_processors
  control.pre_processors
end

#rename(source, destination) ⇒ Object

Rename the source field to the destination field



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

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

#screen(type, &block) ⇒ Object

Define a screen block. The type argument must be one of :fatal, :error or :warn



150
151
152
# File 'lib/etl/control/control.rb', line 150

def screen(type, &block)
  screens[type] << block
end

#screensObject

Get the screen blocks



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

def screens
  control.screens
end

#set_error_threshold(error_threshold) ⇒ Object

Set the allowed error threshold



27
28
29
# File 'lib/etl/control/control.rb', line 27

def set_error_threshold(error_threshold)
  control.error_threshold = error_threshold
end

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

Define a source.



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

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 ControlError, "Type must be a Class, String, Symbol or object extending 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)
        break
      end
      raise ControlError, "A source was specified but no matching type was found"
    end
  end
end

#sourcesObject

Get the defined source



75
76
77
# File 'lib/etl/control/control.rb', line 75

def sources
  control.sources
end

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

Define a transform



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/etl/control/control.rb', line 114

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



144
145
146
# File 'lib/etl/control/control.rb', line 144

def transforms
  control.transforms
end