Sha256: 9ab21ca52fc58f24b3d4932a640c15f5ced35f2a6dca8985c926d9d0aadec5c5

Contents?: true

Size: 1.18 KB

Versions: 15

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

class Grumlin::StepsSerializers::String < Grumlin::StepsSerializers::Serializer
  # constructor params: apply_shortcuts: true|false, default: false
  # constructor params: anonymous: true|false, default: false
  # TODO: add pretty

  def serialize
    steps = @params[:apply_shortcuts] ? Grumlin::ShortcutsApplyer.call(@steps) : @steps

    steps = [steps.configuration_steps, steps.steps].map do |stps|
      stps.map { |step| serialize_step(step) }
    end

    "#{prefix}.#{(steps[0] + steps[1]).join(".")}"
  end

  private

  def serialize_step(step)
    "#{step.name}(#{(step.args + [step.params.any? ? step.params : nil].compact).map do |a|
                      serialize_arg(a)
                    end.join(", ")})"
  end

  def prefix
    @prefix ||= @params[:anonymous] ? "__" : "g"
  end

  def serialize_arg(arg)
    return "\"#{arg}\"" if arg.is_a?(::String) || arg.is_a?(Symbol)
    return "#{arg.type}.#{arg.value}" if arg.is_a?(Grumlin::TypedValue)
    return arg.to_s if arg.is_a?(Grumlin::Expressions::WithOptions)

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

    Grumlin::StepsSerializers::String.new(arg, anonymous: true, **@params).serialize
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

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