Class: ActiveNode::Graph

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

Constant Summary collapse

MULTI_VALUE_METHODS =
[:includes, :eager_load, :preload, :select, :group,
:order, :joins, :where, :having, :bind, :references,
:extending, :unscope]
SINGLE_VALUE_METHODS =
[:limit, :offset, :lock, :readonly, :from, :reordering,
:reverse_order, :distinct, :create_with, :uniq]
VALUE_METHODS =
MULTI_VALUE_METHODS + SINGLE_VALUE_METHODS

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, #limit!, #offset, #offset!, #order, #order!, #reorder, #reorder!, #reverse_order, #reverse_order!

Constructor Details

#initialize(klass, *includes) ⇒ Graph

Returns a new instance of Graph.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/active_node/graph.rb', line 20

def initialize klass, *includes
  @klass = klass if klass < ActiveNode::Base
  @matches = []
  @reflections =[]
  @object_cache = {}
  @relationship_cache = {}
  @loaded_assoc_cache = {}
  @where = {}
  @includes = includes
  @values = {}
  @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.



15
16
17
# File 'lib/active_node/graph.rb', line 15

def klass
  @klass
end

#loadedObject (readonly) Also known as: loaded?

Returns the value of attribute loaded.



15
16
17
# File 'lib/active_node/graph.rb', line 15

def loaded
  @loaded
end

#matchesObject (readonly)

Returns the value of attribute matches.



15
16
17
# File 'lib/active_node/graph.rb', line 15

def matches
  @matches
end

#reflectionsObject (readonly)

Returns the value of attribute reflections.



15
16
17
# File 'lib/active_node/graph.rb', line 15

def reflections
  @reflections
end

Instance Method Details

#==(other) ⇒ Object

Compares two relations for equality.



102
103
104
105
106
107
108
109
# File 'lib/active_node/graph.rb', line 102

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

#allObject



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

def all
  self
end

#any?Boolean

Returns true if there are any records.

Returns:

  • (Boolean)


84
85
86
87
88
89
90
# File 'lib/active_node/graph.rb', line 84

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

#as_json(options = nil) ⇒ Object

:nodoc:



61
62
63
# File 'lib/active_node/graph.rb', line 61

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

#build(*objects) ⇒ Object



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

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

#delete_allObject

def first

limit 1
to_a.first

end



120
121
122
# File 'lib/active_node/graph.rb', line 120

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)


71
72
73
74
75
76
77
78
79
80
81
# File 'lib/active_node/graph.rb', line 71

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



37
38
39
40
# File 'lib/active_node/graph.rb', line 37

def includes *includes
  @includes += includes
  self
end

#loadObject



51
52
53
54
# File 'lib/active_node/graph.rb', line 51

def load
  parse_results execute['data'] unless loaded?
  self
end

#many?Boolean

Returns true if there is more than one record.

Returns:

  • (Boolean)


93
94
95
96
97
98
99
# File 'lib/active_node/graph.rb', line 93

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



111
112
113
# File 'lib/active_node/graph.rb', line 111

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

#sizeObject

Returns size of the records.



66
67
68
# File 'lib/active_node/graph.rb', line 66

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

#to_aObject



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

def to_a
  load
  @records
end

#where(hash) ⇒ Object



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

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