Sha256: 274ac292d7234cc2f77c785d06c361313fda5ae11a0eaef958b3d2ce57f9f338

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

module CypherBuilder
  def Rel(prefix, labels: [])
    Rel.new(prefix, labels: labels)
  end

  class Rel < BasicObject
    def initialize(prefix, labels: [], from: nil, to: nil)
      @prefix, @labels = prefix, ::Kernel.Array(labels)
      @from, @to = from, to
    end

    def as_cypher(payload:, context:)
      ::Kernel.sprintf('%s-[%s]->%s',
                       (@from ? @from.as_cypher(payload: payload, context: context.add(self)) : '()'),
                       [@prefix, *@labels].compact.join(':'),
                       (@to ? @to.as_cypher(payload: payload, context: context.add(self)) : '()'))
    end

    def from(node = nil)
      return Field.new(@prefix, 'from') if node == nil
      Rel.new(@prefix, labels: @labels, from: node, to: @to)
    end

    def to(node = nil)
      return Field.new(@prefix, 'to') if node == nil
      Rel.new(@prefix, labels: @labels, from: @from, to: node)
    end

    def respond_to_missing?(name, include_private = false)
      true
    end

    def method_missing(name, *_)
      Field.new(@prefix, name)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cypher_builder-0.0.5 lib/cypher_builder/rel.rb
cypher_builder-0.0.4 lib/cypher_builder/rel.rb