Class: Datacaster::SwitchNode

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

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(base = nil, on_casters: [], else_caster: nil, pick_key: nil) ⇒ SwitchNode

Returns a new instance of SwitchNode.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/datacaster/switch_node.rb', line 3

def initialize(base = nil, on_casters: [], else_caster: nil, pick_key: nil)
  @base = base
  @pick_key = pick_key

  if Datacaster::Utils.pickable?(@base)
    unless @pick_key.nil?
      raise RuntimeError, "pick_key expected to be nil because #{@base.inspect} is pickable"
    end
    @pick_key = base
    @base = Datacaster::Predefined.pick(base)
  end

  if !@base.nil? && !Datacaster.instance?(@base)
    raise RuntimeError, "provide a Datacaster::Base instance, a hash key, or an array of keys to switch(...) caster", caller
  end

  @ons = on_casters
  @else = else_caster
end

Instance Method Details

#cast(object, runtime:) ⇒ Object



49
50
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
# File 'lib/datacaster/switch_node.rb', line 49

def cast(object, runtime:)
  if @ons.empty?
    raise RuntimeError, "switch caster requires at least one 'on' statement: switch(...).on(condition, cast)", caller
  end

  if @base.nil?
    switch_result = object
  else
    switch_result = @base.with_runtime(runtime).(object)
    return switch_result unless switch_result.valid?
    switch_result = switch_result.value
  end

  @ons.each do |check, clause|
    result = check.with_runtime(runtime).(switch_result)
    next unless result.valid?

    runtime.checked_key!(@pick_key) if !@pick_key.nil? && !@pick_key.is_a?(Array)
    return clause.with_runtime(runtime).(object)
  end

  # all 'on'-s have failed
  return @else.with_runtime(runtime).(object) if @else

  Datacaster.ErrorResult(
    I18nValues::Key.new(['.switch', 'datacaster.errors.switch'], value: object)
  )
end

#else(else_caster) ⇒ Object

Raises:

  • (ArgumentError)


44
45
46
47
# File 'lib/datacaster/switch_node.rb', line 44

def else(else_caster)
  raise ArgumentError, "Datacaster: double else clause is not permitted", caller if @else
  self.class.new(@base, on_casters: @ons, else_caster: else_caster, pick_key: @pick_key)
end

#inspectObject



78
79
80
# File 'lib/datacaster/switch_node.rb', line 78

def inspect
  "#<Datacaster::SwitchNode base: #{@base.inspect} on: #{@ons.inspect} else: #{@else.inspect} pick_key: #{@pick_key.inspect}>"
end

#on(caster_or_value, clause, strict: false) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/datacaster/switch_node.rb', line 23

def on(caster_or_value, clause, strict: false)
  caster =
    case caster_or_value
    when Datacaster::Base
      caster_or_value
    when String, Symbol
      if strict
        Datacaster::Predefined.compare(caster_or_value)
      else
        Datacaster::Predefined.compare(caster_or_value.to_s) |
          Datacaster::Predefined.compare(caster_or_value.to_sym)
      end
    else
      Datacaster::Predefined.compare(caster_or_value)
    end

  clause = DefinitionDSL.expand(clause)

  self.class.new(@base, on_casters: @ons + [[caster, clause]], else_caster: @else, pick_key: @pick_key)
end