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
18
19
20
# File 'lib/floe/workflow/reference_path.rb', line 8

def initialize(*)
  super

  raise Floe::InvalidWorkflowError, "Invalid Reference Path"              if payload.match?(/@|,|:|\?/)
  raise Floe::InvalidWorkflowError, "Reference Path cannot start with $$" if payload.start_with?("$$") && !payload.start_with?("$$.Credentials")

  path_shift = payload.start_with?("$$.Credentials") ? 3 : 1

  @path = JsonPath.new(payload)
                  .path[path_shift..]
                  .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



22
23
24
25
26
# File 'lib/floe/workflow/reference_path.rb', line 22

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

  context.dig(*path)
end

#set(context, value) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/floe/workflow/reference_path.rb', line 28

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