Class: Floe::Workflow::ReferencePath

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

Instance Attribute Summary collapse

Attributes inherited from Path

#payload

Instance Method Summary collapse

Methods inherited from Path

path?, #to_s, value, #value

Constructor Details

#initializeReferencePath

Returns a new instance of ReferencePath.



8
9
10
11
12
13
14
15
16
17
# File 'lib/floe/workflow/reference_path.rb', line 8

def initialize(*)
  super

  raise Floe::InvalidWorkflowError, "Invalid Reference Path" if payload.match?(/@|,|:|\?/)

  @path = JsonPath.new(payload)
                  .path[1..]
                  .map { |v| v.match(/\[(?<name>.+)\]/)["name"] }
                  .filter_map { |v| v[0] == "'" ? v.delete("'") : v.to_i }
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/floe/workflow/reference_path.rb', line 6

def path
  @path
end

Instance Method Details

#get(context) ⇒ Object



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

def get(context)
  return context if path.empty?

  context.dig(*path)
end

#set(context, value) ⇒ 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/reference_path.rb', line 25

def set(context, value)
  result = context.dup

  # If the payload is '$' then replace the output with the value
  if path.empty?
    result = value.dup
  else
    child    = result
    keys     = path.dup
    last_key = keys.pop

    keys.each do |key|
      child[key] = {} if child[key].nil?
      child = child[key]
    end

    child[last_key] = value
  end

  result
end