Sha256: 8aa2301cbdbb033c80b40ee4f4b47576c52c0d8acaea1949ee0ae0b7b7dfd263

Contents?: true

Size: 1.62 KB

Versions: 60

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

60 entries across 60 versions & 1 rubygems

Version Path
foreman-tasks-0.8.6 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-0.8.5 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-0.8.4 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-0.8.3 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-0.8.2 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-0.8.1 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-0.8.0 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-0.7.20 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-0.7.19 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-0.7.18 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-0.7.17 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-0.7.16 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-0.7.15 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-0.7.14 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-0.7.13 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-0.7.12 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-0.7.11 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-0.7.10 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-0.7.9 app/lib/actions/helpers/args_serialization.rb
foreman-tasks-0.7.8 app/lib/actions/helpers/args_serialization.rb