class Grumlin::Bytecode

Incapsulates logic of converting step chains and step arguments to queries that can be sent to the server and to human readable strings.

Constants

NONE_STEP

Public Class Methods

new(step, no_return: false) click to toggle source
# File lib/grumlin/bytecode.rb, line 15
def initialize(step, no_return: false)
  @step = step
  @no_return = no_return
end

Public Instance Methods

inspect() click to toggle source
# File lib/grumlin/bytecode.rb, line 20
def inspect
  to_readable_bytecode.to_s
end
Also aliased as: to_s
to_bytecode() click to toggle source
# File lib/grumlin/bytecode.rb, line 41
def to_bytecode
  @to_bytecode ||= {
    "@type": "g:Bytecode",
    "@value": { step: (steps + (@no_return ? [NONE_STEP] : [])).map { |s| serialize_arg(s) } }
  }
end
to_query() click to toggle source
# File lib/grumlin/bytecode.rb, line 25
def to_query
  {
    requestId: SecureRandom.uuid,
    op: "bytecode",
    processor: "traversal",
    args: {
      gremlin: to_bytecode,
      aliases: { g: :g }
    }
  }
end
to_readable_bytecode() click to toggle source
# File lib/grumlin/bytecode.rb, line 37
def to_readable_bytecode
  @to_readable_bytecode ||= steps.map { |s| serialize_arg(s, serialization_method: :to_readable_bytecode) }
end
to_s()
Alias for: inspect

Private Instance Methods

serialize_arg(arg, serialization_method: :to_bytecode) click to toggle source

Serializes step or a step argument to either an executable query or a human readable string representation depending on the `serialization_method` parameter. I should be either `:to_readable_bytecode` for human readable representation or `:to_bytecode` for query.

# File lib/grumlin/bytecode.rb, line 53
def serialize_arg(arg, serialization_method: :to_bytecode)
  return arg.send(serialization_method) if arg.respond_to?(:to_bytecode)
  return arg unless arg.is_a?(AnonymousStep)

  arg.args.flatten.each.with_object([arg.name]) do |a, res|
    res << if a.instance_of?(AnonymousStep)
             a.bytecode.send(serialization_method)
           else
             serialize_arg(a, serialization_method: serialization_method)
           end
  end
end
steps() click to toggle source
# File lib/grumlin/bytecode.rb, line 66
def steps
  @steps ||= [].tap do |result|
    step = @step
    until step.nil?
      result.unshift(step)
      step = step.previous_step
    end
  end
end