Class: ManageIQ::Floe::Workflow::ReferencePath

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Path

value, #value

Constructor Details

#initializeReferencePath

Returns a new instance of ReferencePath.



13
14
15
16
17
18
19
20
# File 'lib/manageiq/floe/workflow/reference_path.rb', line 13

def initialize(*)
  require "more_core_extensions/core_ext/hash/nested"
  require "more_core_extensions/core_ext/array/nested"

  super

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

Class Method Details

.set(payload, context, value) ⇒ Object



8
9
10
# File 'lib/manageiq/floe/workflow/reference_path.rb', line 8

def set (payload, context, value)
  new(payload).set(context, value)
end

Instance Method Details

#set(context, value) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/manageiq/floe/workflow/reference_path.rb', line 22

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

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

  # If the payload is '$' then merge the value into the context
  # otherwise use store path to set the value to a sub-key
  #
  # TODO: how to handle non-hash values, raise error if path=$ and value not a hash?
  if path.empty?
    result.merge!(value)
  else
    result.store_path(path, value)
  end

  result
end