Class: Transformator::Transformation

Inherits:
Object
  • Object
show all
Defined in:
lib/transformator/transformation.rb

Defined Under Namespace

Classes: Step

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTransformation



31
32
33
34
35
36
# File 'lib/transformator/transformation.rb', line 31

def initialize
  # steps are instanced once, which means that instance variables retain
  @steps = self.class.steps.flatten.map do |_step|
    _step.is_a?(Class) ? _step.new(self) : _step
  end
end

Instance Attribute Details

#sourceObject

Returns the value of attribute source.



6
7
8
# File 'lib/transformator/transformation.rb', line 6

def source
  @source
end

#targetObject

Returns the value of attribute target.



7
8
9
# File 'lib/transformator/transformation.rb', line 7

def target
  @target
end

Class Method Details

.call(*args) ⇒ Object



20
21
22
# File 'lib/transformator/transformation.rb', line 20

def self.call(*args)
  new.call(*args)
end

.require_directory(directory) ⇒ Object

since a transformation can have many steps, writing a “require” for each is tedious



25
26
27
28
29
# File 'lib/transformator/transformation.rb', line 25

def self.require_directory(directory)
  Dir.glob("#{File.expand_path(directory)}/*.rb").each do |_filename|
    require _filename
  end
end

.steps(value = nil) ⇒ Object Also known as: sequence



10
11
12
13
14
15
16
# File 'lib/transformator/transformation.rb', line 10

def steps(value = nil)
  unless value
    @steps
  else
    @steps = value
  end
end

Instance Method Details

#call(source, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/transformator/transformation.rb', line 38

def call(source, options = {})
  @source = source
  @target = (options || {})[:target]

  @steps.each do |_step|
    _step.is_a?(Proc) ? instance_exec(&_step) : _step.call
  end

  return @target
end