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_post_process_screen(type, &block) ⇒ Object

Define an after post-proces screen block. The type argument must be one of :fatal, :error or :warn



168
169
170
# File 'lib/etl/control/control.rb', line 168

def after_post_process_screen(type, &block)
  after_post_process_screens[type] << block
end

#after_post_process_screensObject

Get the after post-process screen blocks



173
174
175
# File 'lib/etl/control/control.rb', line 173

def after_post_process_screens
  control.after_post_process_screens
end

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

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



214
215
216
# File 'lib/etl/control/control.rb', line 214

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

#after_read_processorsObject

Get the defined “after read” processors



219
220
221
# File 'lib/etl/control/control.rb', line 219

def after_read_processors
  control.after_read_processors
end

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

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



224
225
226
# File 'lib/etl/control/control.rb', line 224

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

#before_write_processorsObject

Get the defined “before write” processors



229
230
231
# File 'lib/etl/control/control.rb', line 229

def before_write_processors
  control.before_write_processors
end

#copy(source, destination) ⇒ Object

Copy the source field to the destination field



183
184
185
# File 'lib/etl/control/control.rb', line 183

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).flatten!
end

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

Define a destination



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/etl/control/control.rb', line 85

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
    end
    raise ControlError, "A destination was specified but no matching destination type was found" if destinations.empty?      
  end
end

#destinationsObject

Get the defined destinations



114
115
116
# File 'lib/etl/control/control.rb', line 114

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



254
255
256
# File 'lib/etl/control/control.rb', line 254

def get_binding
  binding
end

#on_error(&block) ⇒ Object

Register an error handler



46
47
48
# File 'lib/etl/control/control.rb', line 46

def on_error(&block)
  control.error_handlers << block
end

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

Define a post-processor



244
245
246
# File 'lib/etl/control/control.rb', line 244

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

#post_processorsObject

Get the defined post-processors



249
250
251
# File 'lib/etl/control/control.rb', line 249

def post_processors
  control.post_processors
end

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

Define a pre-processor



234
235
236
# File 'lib/etl/control/control.rb', line 234

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

#pre_processorsObject

Get the defined pre-processors



239
240
241
# File 'lib/etl/control/control.rb', line 239

def pre_processors
  control.pre_processors
end

#rename(source, destination) ⇒ Object

Rename the source field to the destination field



178
179
180
# File 'lib/etl/control/control.rb', line 178

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

#screen(type, &block) ⇒ Object

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



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

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

#screensObject

Get the before post-process screen blocks



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

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.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/etl/control/control.rb', line 51

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
    end
    raise ControlError, "A source was specified but no matching type was found" if sources.empty?
  end
end

#sourcesObject

Get the defined source



80
81
82
# File 'lib/etl/control/control.rb', line 80

def sources
  control.sources
end

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

Define a transform



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/etl/control/control.rb', line 119

def transform(name, transformer=nil, configuration={}, &block)
  if transformer
    case transformer
    when String, Symbol
      class_name = "#{transformer.to_s.camelize}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
    when Class
      transforms << transformer.new(self, transformer.name, configuration)
    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, Class 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



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

def transforms
  control.transforms
end