Class: ActiveNode::Graph

Inherits:
Object
  • Object
show all
Includes:
Delegation, FinderMethods, QueryMethods, Neography::Rest::Helpers
Defined in:
lib/active_node/graph.rb

Constant Summary

Constants included from Delegation

Delegation::BLACKLISTED_ARRAY_METHODS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FinderMethods

#find, #find_by, #find_by!, #first, #first!, #last, #last!, #raise_record_not_found_exception!, #take, #take!

Methods included from QueryMethods

#limit_value, #offset, #offset_value, #order, #order_values, #reverse_order

Constructor Details

#initialize(klass, *includes) ⇒ Graph

Returns a new instance of Graph.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/active_node/graph.rb', line 9

def initialize klass, *includes
  @klass = klass if klass < ActiveNode::Base
  @matches = []
  @reflections =[]
  @object_cache = {}
  @relationship_cache = {}
  @loaded_assoc_cache = {}
  @where = {}
  @includes = includes
  @offsets = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActiveNode::Delegation

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



6
7
8
# File 'lib/active_node/graph.rb', line 6

def klass
  @klass
end

#loadedObject (readonly) Also known as: loaded?

Returns the value of attribute loaded.



6
7
8
# File 'lib/active_node/graph.rb', line 6

def loaded
  @loaded
end

#matchesObject (readonly)

Returns the value of attribute matches.



6
7
8
# File 'lib/active_node/graph.rb', line 6

def matches
  @matches
end

#reflectionsObject (readonly)

Returns the value of attribute reflections.



6
7
8
# File 'lib/active_node/graph.rb', line 6

def reflections
  @reflections
end

Instance Method Details

#==(other) ⇒ Object

Compares two relations for equality.



99
100
101
102
103
104
105
106
# File 'lib/active_node/graph.rb', line 99

def ==(other)
  case other
    when Graph
      other.to_cypher == to_cypher
    when Array
      to_a == other
  end
end

#allObject



21
22
23
# File 'lib/active_node/graph.rb', line 21

def all
  self
end

#any?Boolean

Returns true if there are any records.

Returns:

  • (Boolean)


81
82
83
84
85
86
87
# File 'lib/active_node/graph.rb', line 81

def any?
  if block_given?
    to_a.any? { |*block_args| yield(*block_args) }
  else
    !empty?
  end
end

#as_json(options = nil) ⇒ Object

:nodoc:



58
59
60
# File 'lib/active_node/graph.rb', line 58

def as_json(options = nil) #:nodoc:
  to_a.as_json(options)
end

#build(*objects) ⇒ Object



44
45
46
# File 'lib/active_node/graph.rb', line 44

def build *objects
  find objects.map { |o| o.is_a?(ActiveNode::Base) ? o.id.tap { |id| @object_cache[id]=o } : extract_id(o) }
end

#countObject



25
26
27
# File 'lib/active_node/graph.rb', line 25

def count
  to_a.count
end

#delete_allObject

def first

limit 1
to_a.first

end



117
118
119
# File 'lib/active_node/graph.rb', line 117

def delete_all
  Neo.db.execute_query("#{initial_match} OPTIONAL MATCH (n0)-[r]-() DELETE n0,r")
end

#empty?Boolean

Returns true if there are no records.

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
77
78
# File 'lib/active_node/graph.rb', line 68

def empty?
  return @records.empty? if loaded?

  if limit_value == 0
    true
  else
    # FIXME: This count is not compatible with #select('authors.*') or other select narrows
    c = count
    c.respond_to?(:zero?) ? c.zero? : c.empty?
  end
end

#includes(*includes) ⇒ Object



29
30
31
32
# File 'lib/active_node/graph.rb', line 29

def includes *includes
  @includes += includes
  self
end

#limit(count) ⇒ Object



39
40
41
42
# File 'lib/active_node/graph.rb', line 39

def limit count
  @limit = count
  self
end

#loadObject



48
49
50
51
# File 'lib/active_node/graph.rb', line 48

def load
  parse_results execute unless loaded?
  self
end

#many?Boolean

Returns true if there is more than one record.

Returns:

  • (Boolean)


90
91
92
93
94
95
96
# File 'lib/active_node/graph.rb', line 90

def many?
  if block_given?
    to_a.many? { |*block_args| yield(*block_args) }
  else
    limit_value ? to_a.many? : size > 1
  end
end

#pretty_print(q) ⇒ Object



108
109
110
# File 'lib/active_node/graph.rb', line 108

def pretty_print(q)
  q.pp(self.to_a)
end

#sizeObject

Returns size of the records.



63
64
65
# File 'lib/active_node/graph.rb', line 63

def size
  loaded? ? @records.length : count
end

#to_aObject



53
54
55
56
# File 'lib/active_node/graph.rb', line 53

def to_a
  load
  @records
end

#where(hash) ⇒ Object



34
35
36
37
# File 'lib/active_node/graph.rb', line 34

def where hash
  @where.merge! hash if hash
  self
end