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.



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

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

Instance Method Details

#resultObject



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

def result
  @hash[@result_key]
end

#to_hashObject Also known as: to_h



82
83
84
# File 'lib/pathway.rb', line 82

def to_hash
  @hash
end

#update(kargs) ⇒ Object



73
74
75
76
# File 'lib/pathway.rb', line 73

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

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

Raises:

  • (ArgumentError)


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/pathway.rb', line 86

def use(&bl)
  raise ArgumentError, 'a block must be provided' if !block_given?
  if bl.parameters.any? {|(type,_)| type == :keyrest || type == :rest }
    raise ArgumentError, 'rest arguments are not supported'
  end

  keys = bl.parameters.select {|(type,_)| type == :key || type == :keyreq }.map(&:last)
  names = bl.parameters.select {|(type,_)| type == :req || type == :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