Class: Yaoc::TransformationCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/yaoc/transformation_command.rb

Direct Known Subclasses

TransformationDeferredCommand

Defined Under Namespace

Modules: WithProc, WithoutProc

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(to: nil, from: nil, fetch_method: nil, conversion_proc: nil, fetcher_proc: nil) ⇒ TransformationCommand

Returns a new instance of TransformationCommand.



42
43
44
45
46
47
48
# File 'lib/yaoc/transformation_command.rb', line 42

def initialize(to: nil, from: nil, fetch_method: nil, conversion_proc: nil, fetcher_proc: nil)
  self.to = to
  self.from = from
  self.proc = conversion_proc
  self.fetcher = fetch_method
  self.value_fetcher_proc = fetcher_proc || ->(to_convert, fetcher, from) { to_convert.public_send(fetcher, from) }
end

Instance Attribute Details

#proc=(new_proc) ⇒ Object

Sets the attribute proc

Parameters:

  • value

    the value to set the attribute proc to.



17
18
19
# File 'lib/yaoc/transformation_command.rb', line 17

def proc=(value)
  @proc = value
end

Class Method Details

.create(to: nil, from: nil, deferred: false, conversion_proc: nil, fetcher_proc: nil) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/yaoc/transformation_command.rb', line 29

def self.create(to: nil, from: nil, deferred: false, conversion_proc: nil, fetcher_proc: nil)
  # will be executed in mapper object instance context later through :define_method
  tc_source = deferred ? deferred_source : default_source

  -> (to_convert, result)do
    tc_source.new(to: to, from: from, fetch_method: fetcher, conversion_proc: conversion_proc, fetcher_proc: fetcher_proc).call(to_convert, result)
  end
end

.default_sourceObject



25
26
27
# File 'lib/yaoc/transformation_command.rb', line 25

def self.default_source
  TransformationCommand
end

.deferred_sourceObject



21
22
23
# File 'lib/yaoc/transformation_command.rb', line 21

def self.deferred_source
  TransformationDeferredCommand
end

.fill_result_with_value(result, key, value) ⇒ Object



38
39
40
# File 'lib/yaoc/transformation_command.rb', line 38

def self.fill_result_with_value(result, key, value)
  result.tap { |taped_result| taped_result[key] = value }
end

Instance Method Details

#call(to_convert, result) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/yaoc/transformation_command.rb', line 60

def call(to_convert, result)
  unless proc.nil?
    instance_exec(to_convert, result, &proc)
  else
    TransformationCommand.fill_result_with_value(result, to, value(to_convert))
  end
end

#value(to_convert) ⇒ Object



68
69
70
# File 'lib/yaoc/transformation_command.rb', line 68

def value(to_convert)
  value_fetcher_proc.call(to_convert, fetcher, from)
end