Class: N4j::Cypher

Inherits:
Object
  • Object
show all
Defined in:
lib/n4j/cypher.rb

Constant Summary collapse

PARAMETERS =
[:start, :match, :where, :return, :instantiate_with]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Cypher

Returns a new instance of Cypher.



5
6
7
8
9
# File 'lib/n4j/cypher.rb', line 5

def initialize(opts ={})
  opts.each_pair do |k,v|
    send "#{k}=", v if respond_to?("#{k}=")
  end
end

Class Method Details

.query(query) ⇒ Object



47
48
49
# File 'lib/n4j/cypher.rb', line 47

def self.query(query)
  N4j.batch([{:to => '/ext/CypherPlugin/graphdb/execute_query', :method => 'POST', :body => {'query' => query}}]).first['body']
end

Instance Method Details

#goObject



30
31
32
33
34
35
36
# File 'lib/n4j/cypher.rb', line 30

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

#next(type = nil) ⇒ Object

hops?



11
12
13
14
15
# File 'lib/n4j/cypher.rb', line 11

def next(type = nil) # hops?
  start_var = start.sub(/start\s+/,'').sub(/\s*=.+/,'')
  self.match = "match (#{start_var})-->(b)"
  self
end

#returnObject



26
27
28
# File 'lib/n4j/cypher.rb', line 26

def return
  @return || (match && "return #{match.split(/\W+/).last}")
end

#start=(entity) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/n4j/cypher.rb', line 17

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

#to_queryObject



38
39
40
41
42
43
44
45
# File 'lib/n4j/cypher.rb', line 38

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