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.



63
64
65
66
# File 'lib/pathway.rb', line 63

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

Instance Method Details

#resultObject



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

def result = @hash[@result_key]

#to_hashObject Also known as: to_h



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

def to_hash = @hash

#update(kargs) ⇒ Object



68
69
70
71
# File 'lib/pathway.rb', line 68

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

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

Raises:

  • (ArgumentError)


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

def use(&bl)
  raise ArgumentError, "a block must be provided" unless 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