Class: Pathway::State

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/pathway.rb

Instance Method Summary collapse

Constructor Details

#initialize(operation, values = {}) ⇒ State

Returns a new instance of State.



61
62
63
64
# File 'lib/pathway.rb', line 61

def initialize(operation, values = {})
  @hash = operation.context.merge(values)
  @result_key = operation.result_key
end

Instance Method Details

#resultObject



71
# File 'lib/pathway.rb', line 71

def result = @hash[@result_key]

#to_hashObject Also known as: to_h



72
# File 'lib/pathway.rb', line 72

def to_hash = @hash

#update(kargs) ⇒ Object



66
67
68
69
# File 'lib/pathway.rb', line 66

def update(kargs)
  @hash.update(kargs)
  self
end

#use(&bl) ⇒ Object Also known as: u, unwrap

Raises:

  • (ArgumentError)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/pathway.rb', line 74

def use(&bl)
  raise ArgumentError, 'a block must be provided' if !block_given?
  if bl.parameters in [*, [:rest|:keyrest,], *]
    raise ArgumentError, 'rest arguments are not supported'
  end

  keys = bl.parameters.select { _1 in :key|:keyreq, }.map(&:last)
  names = bl.parameters.select { _1 in :req|:opt, }.map(&:last)

  if keys.any? && names.any?
    raise ArgumentError, 'cannot mix positional and keyword arguments'
  elsif keys.any?
    bl.call(**to_hash.slice(*keys))
  else
    bl.call(*to_hash.values_at(*names))
  end
end