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.



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

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.



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

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

#allObject



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

def all
  self
end

#any?Boolean

Returns true if there are any records.

Returns:

  • (Boolean)


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

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

#as_json(options = nil) ⇒ Object

:nodoc:



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

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

#build(*objects) ⇒ Object



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

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



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

def count
  to_a.count
end

#delete_allObject

def first

limit 1
to_a.first

end



122
123
124
# File 'lib/active_node/graph.rb', line 122

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)


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

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



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

def includes *includes
  @includes += includes
  self
end

#loadObject



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

def load
  parse_results execute unless loaded?
  self
end

#many?Boolean

Returns true if there is more than one record.

Returns:

  • (Boolean)


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

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



113
114
115
# File 'lib/active_node/graph.rb', line 113

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

#sizeObject

Returns size of the records.



68
69
70
# File 'lib/active_node/graph.rb', line 68

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

#to_aObject



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

def to_a
  load
  @records
end

#where(hash) ⇒ Object



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

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