Sha256: ef5c4fb32140e0a3e47e87dad44d064b5dec18e4e51068853918fcb903ff2083

Contents?: true

Size: 1.83 KB

Versions: 15

Compression:

Stored size: 1.83 KB

Contents

# frozen_string_literal: true

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

  NONE_STEP = Grumlin::StepData.new("none")

  def serialize
    steps = Grumlin::ShortcutsApplyer.call(@steps)
    no_return = @params.fetch(:no_return, false)
    {}.tap do |result|
      result[:step] = serialize_steps(steps.steps + (no_return ? [NONE_STEP] : [])) if steps.steps.any?
      result[:source] = serialize_steps(steps.configuration_steps) if steps.configuration_steps.any?
    end
  end

  private

  def serialize_steps(steps)
    steps.map { |s| serialize_step(s) }
  end

  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?(Grumlin::TypedValue)
    return serialize_predicate(arg) if arg.is_a?(Grumlin::Expressions::P::Predicate)
    return arg.value if arg.is_a?(Grumlin::Expressions::WithOptions)

    return arg unless arg.is_a?(Grumlin::Steps)

    { :@type => "g:Bytecode",
      :@value => Grumlin::StepsSerializers::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

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
grumlin-1.2.0 lib/grumlin/steps_serializers/bytecode.rb
grumlin-1.1.0 lib/grumlin/steps_serializers/bytecode.rb
grumlin-1.0.4 lib/grumlin/steps_serializers/bytecode.rb
grumlin-1.0.3 lib/grumlin/steps_serializers/bytecode.rb
grumlin-1.0.3.beta1 lib/grumlin/steps_serializers/bytecode.rb
grumlin-1.0.2 lib/grumlin/steps_serializers/bytecode.rb
grumlin-1.0.1 lib/grumlin/steps_serializers/bytecode.rb
grumlin-1.0.0 lib/grumlin/steps_serializers/bytecode.rb
grumlin-1.0.0.rc7 lib/grumlin/steps_serializers/bytecode.rb
grumlin-1.0.0.rc6 lib/grumlin/steps_serializers/bytecode.rb
grumlin-1.0.0.rc5 lib/grumlin/steps_serializers/bytecode.rb
grumlin-1.0.0.rc4 lib/grumlin/steps_serializers/bytecode.rb
grumlin-1.0.0.rc3 lib/grumlin/steps_serializers/bytecode.rb
grumlin-1.0.0.rc2 lib/grumlin/steps_serializers/bytecode.rb
grumlin-1.0.0.rc1 lib/grumlin/steps_serializers/bytecode.rb