Sha256: eb04dde00b34ef5f4cbc1e83b24ae05cd36bbdbf3c2ee8ce87d90ce68d28c266

Contents?: true

Size: 1.5 KB

Versions: 6

Compression:

Stored size: 1.5 KB

Contents

class N4j::Cypher
  PARAMETERS = [:start, :match, :where, :return, :instantiate_with]
  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
      @hint_klass = entity.class
      "start a = #{entity.entity_type}(#{entity.to_key.first})"
    end
  end
  
  def return
    @return || (match && "return #{match.split(/\W+/).last}")
  end
  
  def go
    result = self.class.query(to_query)
    result['data'].collect do |from_neo4j|
      (instantiate_with || GenericNode).new(from_neo4j.first)
      # GenericNode.new(from_neo4j.first)
    end
  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

6 entries across 6 versions & 1 rubygems

Version Path
n4j-0.0.1.7 lib/n4j/cypher.rb
n4j-0.0.1.6.3 lib/n4j/cypher.rb
n4j-0.0.1.6.1 lib/n4j/cypher.rb
n4j-0.0.1.6 lib/n4j/cypher.rb
n4j-0.0.1.5 lib/n4j/cypher.rb
n4j-0.0.1.4 lib/n4j/cypher.rb