Module: Datacaster::Transaction

Defined in:
lib/datacaster/transaction.rb

Defined Under Namespace

Modules: ClassMethods Classes: StepErrorResult

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



69
70
71
72
73
# File 'lib/datacaster/transaction.rb', line 69

def self.included(base)
  base.extend Datacaster::Predefined
  base.extend ClassMethods
  base.include Datacaster::Mixin
end

Instance Method Details

#cast(object, runtime:) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/datacaster/transaction.rb', line 75

def cast(object, runtime:)
  if respond_to?(:perform)
    @runtime = runtime
    begin
      result = perform(object)
    rescue StepErrorResult => e
      return e.result
    end
    @runtime = nil
    return result
  end

  caster = self.class._caster
  unless caster
    raise RuntimeError, "define #perform (#perform_partial, #perform_choosy) method " \
      "or call .perform(caster) or .perform { caster } beforehand", caller
  end
  caster.
    with_object_context(self).
    with_runtime(runtime).
    (object)
end

#step(arg = nil, &block) ⇒ Object



98
99
100
101
102
103
# File 'lib/datacaster/transaction.rb', line 98

def step(arg = nil, &block)
  result = Datacaster::Predefined.cast(&block).
    with_object_context(self).
    with_runtime(@runtime).
    (arg)
end

#step!(arg = nil, &block) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/datacaster/transaction.rb', line 105

def step!(arg = nil, &block)
  result = step(arg, &block)

  if result.valid?
    result.value
  else
    raise StepErrorResult.new(result)
  end
end