Sha256: 0d95b518a8eba3a952afa9ede68dd5269e74be9952e65c288e0520dd200ce7aa

Contents?: true

Size: 1.23 KB

Versions: 22

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

module Grumlin
  module StepsSerializers
    class String < 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] ? 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?(Steps)

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

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
grumlin-0.23.0 lib/grumlin/steps_serializers/string.rb
grumlin-0.22.5 lib/grumlin/steps_serializers/string.rb
grumlin-0.22.4 lib/grumlin/steps_serializers/string.rb
grumlin-0.22.3 lib/grumlin/steps_serializers/string.rb
grumlin-0.22.2 lib/grumlin/steps_serializers/string.rb
grumlin-0.22.1 lib/grumlin/steps_serializers/string.rb
grumlin-0.22.0 lib/grumlin/steps_serializers/string.rb
grumlin-0.21.1 lib/grumlin/steps_serializers/string.rb
grumlin-0.21.0 lib/grumlin/steps_serializers/string.rb
grumlin-0.20.2 lib/grumlin/steps_serializers/string.rb
grumlin-0.20.1 lib/grumlin/steps_serializers/string.rb
grumlin-0.20.0 lib/grumlin/steps_serializers/string.rb
grumlin-0.19.7 lib/grumlin/steps_serializers/string.rb
grumlin-0.19.6 lib/grumlin/steps_serializers/string.rb
grumlin-0.19.5 lib/grumlin/steps_serializers/string.rb
grumlin-0.19.4 lib/grumlin/steps_serializers/string.rb
grumlin-0.19.3 lib/grumlin/steps_serializers/string.rb
grumlin-0.19.2 lib/grumlin/steps_serializers/string.rb
grumlin-0.19.1 lib/grumlin/steps_serializers/string.rb
grumlin-0.19.0 lib/grumlin/steps_serializers/string.rb