Sha256: 776e6ff89eacbcc1e3f28e1db77ec15221d429c1fa4ce9222d9a585654c6a19c

Contents?: true

Size: 1.87 KB

Versions: 13

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true

module Grumlin
  module StepsSerializers
    class Bytecode < Serializer
      # constructor params: no_return: true|false, default false
      # TODO: add pretty

      NONE_STEP = StepData.new("none")

      def serialize
        steps = ShortcutsApplyer.call(@steps)
        no_return = @params[:no_return] || false

        {
          step: (steps.steps + (no_return ? [NONE_STEP] : [])).map { |s| serialize_step(s) }
        }.tap do |v|
          v.merge!(source: steps.configuration_steps.map { |s| serialize_step(s) }) if steps.configuration_steps.any?
        end
      end

      private

      def serialize_step(step)
        [step.name].tap do |result|
          step.args.each do |arg|
            result << serialize_arg(arg)
          end
          result << step.params if step.params.any?
        end
      end

      def serialize_arg(arg)
        return serialize_typed_value(arg) if arg.is_a?(TypedValue)
        return serialize_predicate(arg) if arg.is_a?(Expressions::P::Predicate)
        return arg.value if arg.is_a?(Expressions::WithOptions)

        return arg unless arg.is_a?(Steps)

        { :@type => "g:Bytecode", :@value => Bytecode.new(arg, **@params.merge(no_return: false)).serialize }
      end

      def serialize_typed_value(value)
        return value.value if value.type.nil?

        {
          "@type": "g:#{value.type}",
          "@value": value.value
        }
      end

      def serialize_predicate(value)
        {
          "@type": "g:#{value.namespace}",
          "@value": {
            predicate: value.name,
            value: if value.type.nil?
                     value.value
                   else
                     {
                       "@type": "g:#{value.type}",
                       "@value": value.value
                     }
                   end
          }
        }
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
grumlin-0.21.1 lib/grumlin/steps_serializers/bytecode.rb
grumlin-0.21.0 lib/grumlin/steps_serializers/bytecode.rb
grumlin-0.20.2 lib/grumlin/steps_serializers/bytecode.rb
grumlin-0.20.1 lib/grumlin/steps_serializers/bytecode.rb
grumlin-0.20.0 lib/grumlin/steps_serializers/bytecode.rb
grumlin-0.19.7 lib/grumlin/steps_serializers/bytecode.rb
grumlin-0.19.6 lib/grumlin/steps_serializers/bytecode.rb
grumlin-0.19.5 lib/grumlin/steps_serializers/bytecode.rb
grumlin-0.19.4 lib/grumlin/steps_serializers/bytecode.rb
grumlin-0.19.3 lib/grumlin/steps_serializers/bytecode.rb
grumlin-0.19.2 lib/grumlin/steps_serializers/bytecode.rb
grumlin-0.19.1 lib/grumlin/steps_serializers/bytecode.rb
grumlin-0.19.0 lib/grumlin/steps_serializers/bytecode.rb