Sha256: b685ef45fa51b0b3140ab449893eb723bec645e147aa06d2feae1c4b47f6e857

Contents?: true

Size: 1.06 KB

Versions: 7

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module Floe
  class Workflow
    class ReferencePath < Path
      attr_reader :path

      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

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

        context.dig(*path)
      end

      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
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
floe-0.15.1 lib/floe/workflow/reference_path.rb
floe-0.15.0 lib/floe/workflow/reference_path.rb
floe-0.14.0 lib/floe/workflow/reference_path.rb
floe-0.13.1 lib/floe/workflow/reference_path.rb
floe-0.13.0 lib/floe/workflow/reference_path.rb
floe-0.12.0 lib/floe/workflow/reference_path.rb
floe-0.11.3 lib/floe/workflow/reference_path.rb