Class: ActiveNode::Graph
- Inherits:
-
Object
- Object
- ActiveNode::Graph
- 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
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#loaded ⇒ Object
(also: #loaded?)
readonly
Returns the value of attribute loaded.
-
#matches ⇒ Object
readonly
Returns the value of attribute matches.
-
#reflections ⇒ Object
readonly
Returns the value of attribute reflections.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Compares two relations for equality.
- #all ⇒ Object
-
#any? ⇒ Boolean
Returns true if there are any records.
-
#as_json(options = nil) ⇒ Object
:nodoc:.
- #build(*objects) ⇒ Object
-
#delete_all ⇒ Object
def first limit 1 to_a.first end.
-
#empty? ⇒ Boolean
Returns true if there are no records.
- #includes(*includes) ⇒ Object
-
#initialize(klass, *includes) ⇒ Graph
constructor
A new instance of Graph.
- #load ⇒ Object
-
#many? ⇒ Boolean
Returns true if there is more than one record.
- #pretty_print(q) ⇒ Object
-
#size ⇒ Object
Returns size of the records.
- #to_a ⇒ Object
- #where(hash) ⇒ Object
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
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
15 16 17 |
# File 'lib/active_node/graph.rb', line 15 def klass @klass end |
#loaded ⇒ Object (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 |
#matches ⇒ Object (readonly)
Returns the value of attribute matches.
15 16 17 |
# File 'lib/active_node/graph.rb', line 15 def matches @matches end |
#reflections ⇒ Object (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 |
#all ⇒ Object
33 34 35 |
# File 'lib/active_node/graph.rb', line 33 def all self end |
#any? ⇒ Boolean
Returns true if there are any records.
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( = nil) #:nodoc: to_a.as_json() 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_all ⇒ Object
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.
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 |
#load ⇒ Object
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.
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 |
#size ⇒ Object
Returns size of the records.
66 67 68 |
# File 'lib/active_node/graph.rb', line 66 def size loaded? ? @records.length : count end |
#to_a ⇒ Object
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 |