Class: Datacaster::AroundNode

Inherits:
Base
  • Object
show all
Defined in:
lib/datacaster/around_node.rb

Direct Known Subclasses

Datacaster::AroundNodes::Caster

Instance Method Summary collapse

Methods included from Mixin

#&, #*, #call, #call_with_runtime, #cast_errors, #i18n_key, #i18n_map_keys, #i18n_scope, #i18n_vars, #then, #with_context, #with_object_context, #with_runtime, #|

Constructor Details

#initialize(around = nil, &block) ⇒ AroundNode

Returns a new instance of AroundNode.



3
4
5
6
7
8
# File 'lib/datacaster/around_node.rb', line 3

def initialize(around = nil, &block)
  raise "Expected block" unless block_given?

  @around = around
  @run = block
end

Instance Method Details

#around(*casters) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/datacaster/around_node.rb', line 10

def around(*casters)
  if @around
    raise ArgumentError, "only one call to .around(...) is expect, tried to call second time", caller
  end

  unless casters.all? { |x| Datacaster.instance?(x) }
    raise ArgumentError, "provide datacaster instance to .around(...)", caller
  end

  caster = casters.length == 1 ? casters[0] : Datacaster::Predefined.steps(*casters)

  self.class.new(caster, &@run)
end

#cast(object, runtime:) ⇒ Object



24
25
26
27
28
# File 'lib/datacaster/around_node.rb', line 24

def cast(object, runtime:)
  unless @around
    raise ArgumentError, "call .around(caster) beforehand", caller
  end
end

#inspectObject



30
31
32
# File 'lib/datacaster/around_node.rb', line 30

def inspect
  "#<#{self.class.name}>"
end