Sha256: c5b118d1dfc38aeaebe974f67b65b361ff2aba16e32dcda590d9f5355d738b6e

Contents?: true

Size: 1.28 KB

Versions: 4

Compression:

Stored size: 1.28 KB

Contents

class N4j::Cypher
  PARAMETERS = [:start, :match, :where, :return]
  attr_accessor *PARAMETERS
  
  def initialize(opts ={})
    opts.each_pair do |k,v|
      send "#{k}=", v if respond_to?("#{k}=")
    end
  end
  
  def next(type = nil) # hops?
    start_var = start.sub(/start\s+/,'').sub(/\s*=.+/,'')
    self.match = "match (#{start_var})-->(b)"
    self
  end
  
  def start=(entity)
    @start = if entity.kind_of?(String)
      entity
    else
      "start a = #{entity.entity_type}(#{entity.to_key.first})"
    end
  end
  
  def return
    @return || (match && "return #{match.split(/\W+/).last}")
  end
  
  def go
    self.class.query(to_query)
  end
  
  def to_query
    raise "start must contain 'start' (currently: '#{start}')" if start && !start.index('start')
    raise "match must contain 'match' (currently: '#{match}')" if match && !match.index('match')
    raise "where must contain 'where' (currently: '#{where}')" if where && !where.index('where')
    raise "return must contain 'return' (currently: '#{self.return}')" if self.return && !self.return.index('return')
    
    "#{start} #{match} #{where} #{self.return}"
  end
  
  def self.query(query)
    N4j.batch([{:to => '/ext/CypherPlugin/graphdb/execute_query', :method => 'POST', :body => {'query' => query}}]).first['body']
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
n4j-0.0.1.3 lib/n4j/cypher.rb
n4j-0.0.1.2 lib/n4j/cypher.rb
n4j-0.0.1.1 lib/n4j/cypher.rb
n4j-0.0.1 lib/n4j/cypher.rb