Class: Floe::Workflow::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/floe/workflow/path.rb

Direct Known Subclasses

ReferencePath

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload) ⇒ Path

Returns a new instance of Path.



18
19
20
21
22
23
# File 'lib/floe/workflow/path.rb', line 18

def initialize(payload)
  @payload = payload

  raise Floe::InvalidWorkflowError, "Path [#{payload}] must be a string" if payload.nil? || !payload.kind_of?(String)
  raise Floe::InvalidWorkflowError, "Path [#{payload}] must start with \"$\"" if payload[0] != "$"
end

Instance Attribute Details

#payloadObject (readonly)

Returns the value of attribute payload.



16
17
18
# File 'lib/floe/workflow/path.rb', line 16

def payload
  @payload
end

Class Method Details

.path?(payload) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/floe/workflow/path.rb', line 7

def path?(payload)
  payload.start_with?("$")
end

.value(payload, context, input = {}) ⇒ Object



11
12
13
# File 'lib/floe/workflow/path.rb', line 11

def value(payload, context, input = {})
  new(payload).value(context, input)
end

Instance Method Details

#to_sObject



47
48
49
# File 'lib/floe/workflow/path.rb', line 47

def to_s
  payload
end

#value(context, input = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/floe/workflow/path.rb', line 25

def value(context, input = {})
  obj, path =
    if payload.start_with?("$$")
      [context, payload[1..]]
    else
      [input, payload]
    end

  # If path is $ then just return the entire input
  return obj if path == "$"

  results = JsonPath.on(obj, path)
  case results.count
  when 0
    raise Floe::PathError, "Path [#{payload}] references an invalid value"
  when 1
    results.first
  else
    results
  end
end