Class: Neo4j::Rails::Relationships::NodesDSL
- Inherits:
-
Object
- Object
- Neo4j::Rails::Relationships::NodesDSL
- Includes:
- Enumerable
- Defined in:
- lib/neo4j/rails/relationships/node_dsl.rb
Overview
Instances of this class is returned from the #outgoing, #incoming and generated accessor methods: has_n and has_one. Notice that this class includes the Ruby Enumerable mixin. If you want to full traversal api use the core version of these methods (some_node._outgoing(…)).
Constant Summary
- CORE_TRAVERSAL_METHODS =
These methods are using the Neo4j::Core::Traversal::Traverser which means that only persisted relationship will be seen but more advanced traversal can be performed.
[:depth, :outgoing, :incoming, :both, :expand, :depth_first, :breadth_first, :eval_paths, :unique, :expander, :prune, :filter, :include_start_node, :rels, :eval_paths, :query]
Instance Method Summary (collapse)
-
- (Object) <<(other)
Adds a new node to the relationship, no transaction is needed.
-
- (Object) [](index)
Returns the n:th item in the relationship.
-
- (Object) all(*args)
Same as #find except that it returns an Enumerator of all nodes found.
- - (Boolean) blank?
-
- (Neo4j::Rails::Model) build(attrs = {})
Creates a new node given the specified attributes and connect it with a relationship.
-
- (Object) count
(also: #length)
Counts all relationships.
-
- (Neo4j::Rails::Model) create(attrs = {})
Same as #build except that the relationship and node are saved.
-
- (Neo4j::Rails::Model) create!(attrs)
Same as #create but will raise an exception if an error (like validation) occurs.
-
- (Object) delete(*nodes)
Delete relationships to the given nodes.
-
- (Object) delete_all
Deletes all nodes and relationships, similar to #destroy_all.
-
- (Object) destroy_all
Destroys all nodes (!) and relationship.
- - (Object) each
- - (Boolean) empty?
-
- (Neo4j::Rails::Model) find(*args, &block)
Find one node in the relationship.
-
- (Object) first(*args)
Returns first node in the relationship specified by the arguments or returns nil.
-
- (NodesDSL) initialize(storage, dir)
constructor
A new instance of NodesDSL.
- - (Boolean) is_a?(type)
- - (Boolean) persisted?
- - (Boolean) rel_changed?
- - (Object) to_ary
- - (Object) to_s
Constructor Details
- (NodesDSL) initialize(storage, dir)
A new instance of NodesDSL
13 14 15 16 |
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 13 def initialize(storage, dir) @storage = storage @dir = dir end |
Instance Method Details
- (Object) <<(other)
Adds a new node to the relationship, no transaction is needed.
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 65 def <<(other) if other.is_a?(String) # this is typically called in an assignment operator, person.friends = ['42', '32'] node = Neo4j::Node.load(other) @storage.create_relationship_to(node, @dir) unless all.include?(node) else # allow multiple relationships to the same node @storage.create_relationship_to(other, @dir) end self end |
- (Object) [](index)
Returns the n:th item in the relationship. This method simply traverse all relationship and returns the n:th one.
87 88 89 90 91 |
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 87 def [](index) i = 0 each { |x| return x if i == index; i += 1 } nil # out of index end |
- (Object) all(*args)
Same as #find except that it returns an Enumerator of all nodes found.
143 144 145 146 147 148 149 150 |
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 143 def all(*args) unless args.empty? raise "Illegal argument, expected a node" unless args.first.kind_of?(Neo4j::NodeMixin) enum = Enumerator.new(@storage, :each_node, @dir).find { |n| n == args.first } else enum = Enumerator.new(@storage, :each_node, @dir) end end |
- (Boolean) blank?
197 198 199 |
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 197 def blank? false unless empty? end |
- (Neo4j::Rails::Model) build(attrs = {})
Creates a new node given the specified attributes and connect it with a relationship. The new node and relationship will not be saved. Both the relationship class and the node class can be specified with the has_n and has_one.
32 33 34 35 |
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 32 def build(attrs = {}) self << (node = @storage.build(attrs)) node end |
- (Object) count Also known as: length
Counts all relationships
177 178 179 |
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 177 def count @storage.count(@dir) end |
- (Neo4j::Rails::Model) create(attrs = {})
Same as #build except that the relationship and node are saved.
40 41 42 43 44 |
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 40 def create(attrs = {}) self << (node = @storage.create(attrs)) node.save node end |
- (Neo4j::Rails::Model) create!(attrs)
Same as #create but will raise an exception if an error (like validation) occurs.
49 50 51 52 53 |
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 49 def create!(attrs) self << (node = @storage.create(attrs)) node.save! node end |
- (Object) delete(*nodes)
Delete relationships to the given nodes
189 190 191 |
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 189 def delete(*nodes) @storage.destroy_rels(@dir, *nodes) end |
- (Object) delete_all
Deletes all nodes and relationships, similar to #destroy_all
172 173 174 |
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 172 def delete_all each { |n| n.delete } end |
- (Object) destroy_all
Destroys all nodes (!) and relationship. Notice, if you only want to destroy the relationship use the #rels(:friends).destroy_all method instead.
167 168 169 |
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 167 def destroy_all each { |n| n.destroy } end |
- (Object) each
183 184 185 |
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 183 def each @storage.each_node(@dir) { |n| yield n } # Why passing the &block through doesn't work on JRuby 1.9? end |
- (Boolean) empty?
193 194 195 |
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 193 def empty? !@storage.relationships?(@dir) end |
- (Neo4j::Rails::Model) find(*args, &block)
Find one node in the relationship.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 123 def find(*args, &block) return super(*args, &block) if block case args.first when :all, :first kind = args.shift send(kind, *args) when "0", 0 nil else if ((args.first.is_a?(Integer) || args.first.is_a?(String)) && args.first.to_i > 0) find_by_id(*args) else first(*args) end end end |
- (Object) first(*args)
Returns first node in the relationship specified by the arguments or returns nil.
153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 153 def first(*args) if result = all(*args) if result.respond_to?(:collect) #if it's enumerable, get the first result result.first else result end else nil end end |
- (Boolean) is_a?(type)
93 94 95 96 97 |
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 93 def is_a?(type) # ActionView requires this for nested attributes to work return true if Array == type super end |
- (Boolean) persisted?
77 78 79 |
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 77 def persisted? @storage.persisted? end |
- (Boolean) rel_changed?
205 206 207 |
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 205 def rel_changed? @storage.persisted? end |
- (Object) to_ary
81 82 83 |
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 81 def to_ary all.to_a end |
- (Object) to_s
201 202 203 |
# File 'lib/neo4j/rails/relationships/node_dsl.rb', line 201 def to_s "Node dir: #{@dir}, #{@storage}" end |