Sha256: b1800f2fb8535a397374563d06e14fc430ee758d8c88ffd8872e3c59458604af

Contents?: true

Size: 999 Bytes

Versions: 3

Compression:

Stored size: 999 Bytes

Contents

# frozen_string_literal: true

module Grumlin
  module Translator
    class << self
      def to_string_query(steps) # rubocop:disable Metrics/MethodLength
        counter = 0
        string_steps, bindings = steps.each_with_object([[], {}]) do |step, (acc_g, acc_b)|
          args = step.args.map do |arg|
            binding_name(counter).tap do |b|
              acc_b[b] = arg
              counter += 1
            end
          end.join(", ")

          acc_g << "#{step.name}(#{args})"
        end

        ["g.#{string_steps.join(".")}", bindings]
      end

      def to_bytecode_query(steps)
        steps.map do |step|
          [step.name, *step.args.flatten]
        end
      end

      def to_string(steps)
        "g." + steps.map do |step| # rubocop:disable Style/StringConcatenation
          "#{step.name}(#{step.args.map(&:inspect).join(", ")})"
        end.join(".")
      end

      private

      def binding_name(num)
        "b_#{num.to_s(16)}"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
grumlin-0.1.3 lib/grumlin/translator.rb
grumlin-0.1.1 lib/grumlin/translator.rb
grumlin-0.1.0 lib/grumlin/translator.rb