Sha256: d1e2f2cba25f9ff6d53bf55ce32a1cbfb069cb485a5b9b2828baf9ff47783ecf

Contents?: true

Size: 1.62 KB

Versions: 129

Compression:

Stored size: 1.62 KB

Contents

module Actions
  module Helpers
    module ArgsSerialization
      class Builder
        def initialize(*objects)
          @hash = {}.with_indifferent_access
          objects.each do |object|
            add_object(object)
          end
        end

        def to_h
          @hash
        end

        private

        def add_object(object)
          case object
          when ForemanTasks::Concerns::ActionSubject
            add(object.action_input_key, object_to_value(object))
          when Hash
            add_hash(object_to_value(object))
          else
            raise "don't know how to serialize #{object.inspect}"
          end
        end

        def object_to_value(object)
          case object
          when Array
            object.map { |item| object_to_value(item) }
          when Hash
            object.reduce({}) do |new_hash, (key, value)|
              new_hash.update(key => object_to_value(value))
            end
          when ForemanTasks::Concerns::ActionSubject
            object.to_action_input
          when String, Numeric, true, false, nil, Dynflow::ExecutionPlan::OutputReference
            object
          else
            object.to_s
          end
        end

        def add_hash(hash)
          hash.each { |key, value| add(key, value) }
        end

        def add(key, value)
          if @hash.key?(key)
            raise KeyError, "Conflict while serializing action args in key #{key}"
          end
          @hash.update(key => value)
        end
      end

      def serialize_args(*objects)
        phase! Dynflow::Action::Plan
        Builder.new(*objects).to_h
      end
    end
  end
end

Version data entries

129 entries across 129 versions & 1 rubygems

Version Path
foreman-tasks-10.0.2 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-10.0.1 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-10.0.0 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-9.2.3 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-9.2.2 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-9.2.1 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-9.2.0 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-9.1.1 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-9.0.4 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-9.1.0 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-9.0.2 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-9.0.1 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-8.3.3 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-9.0.0 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-8.2.1 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-8.1.4 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-8.3.2 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-8.3.1 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-8.1.3 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-8.3.0 app/lib/actions/helpers/args_serialization.rb