Class: Neo4j::Wrapper::HasN::Nodes
- Inherits:
-
Object
- Object
- Neo4j::Wrapper::HasN::Nodes
- Includes:
- Enumerable, Core::ToJava
- Defined in:
- lib/neo4j-wrapper/has_n/nodes.rb
Overview
The object created by a has_n or has_one Neo4j::NodeMixin class method which enables creating and traversal of nodes.
Class Method Summary collapse
Instance Method Summary collapse
-
#<<(other) ⇒ Object
Creates a relationship between this and the other node.
-
#[](index) ⇒ Neo4j::NodeMixin, ...
Traverse the relationship till the index position.
-
#_each ⇒ Object
returns none wrapped nodes, you may get better performance using this method.
-
#each ⇒ Object
Required by the Enumerable mixin.
-
#empty? ⇒ Boolean
Returns true if there are no node in this type of relationship.
-
#initialize(node, decl_rel, cypher_query_hash = nil, &cypher_block) ⇒ Nodes
constructor
:nodoc:.
-
#is_a?(type) ⇒ Boolean
Pretend we are an array - this is necessarily for Rails actionpack/actionview/formhelper to work with this.
-
#new(other) ⇒ Object
Creates a relationship instance between this and the other node.
- #query(cypher_query_hash = nil, &block) ⇒ Object
-
#to_ary ⇒ Object
Returns an real ruby array.
- #to_s ⇒ Object
Constructor Details
#initialize(node, decl_rel, cypher_query_hash = nil, &cypher_block) ⇒ Nodes
:nodoc:
12 13 14 15 16 17 18 |
# File 'lib/neo4j-wrapper/has_n/nodes.rb', line 12 def initialize(node, decl_rel, cypher_query_hash = nil, &cypher_block) # :nodoc: @node = node @decl_rel = decl_rel @cypher_block = cypher_block @cypher_query_hash = cypher_query_hash self.class.define_rule_methods_on(self, decl_rel) end |
Class Method Details
.define_rule_methods_on(instance, decl_rel) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/neo4j-wrapper/has_n/nodes.rb', line 20 def self.define_rule_methods_on(instance, decl_rel ) rule_node = Neo4j::Wrapper::Rule::Rule.rule_node_for(decl_rel.target_class) singelton = class << instance; self; end rule_node && rule_node.rules.each do |rule| next if rule.rule_name == :all singelton.send(:define_method, rule.rule_name) do |*cypher_query_hash, &cypher_block| raise "Not a hash, can't create a cypher where clause'" if cypher_query_hash.first && !cypher_query_hash.first.is_a?(Hash) proc = Proc.new do |m| m.incoming(rule.rule_name) if cypher_block self.instance_exec(m, &cypher_block) end end query(cypher_query_hash.first, &proc) end end end |
Instance Method Details
#<<(other) ⇒ Object
Creates a relationship between this and the other node.
113 114 115 116 |
# File 'lib/neo4j-wrapper/has_n/nodes.rb', line 113 def <<(other) @decl_rel.create_relationship_to(@node, other) self end |
#[](index) ⇒ Neo4j::NodeMixin, ...
Traverse the relationship till the index position
57 58 59 60 61 |
# File 'lib/neo4j-wrapper/has_n/nodes.rb', line 57 def [](index) i = 0 each { |x| return x if i == index; i += 1 } nil # out of index end |
#_each ⇒ Object
returns none wrapped nodes, you may get better performance using this method
80 81 82 |
# File 'lib/neo4j-wrapper/has_n/nodes.rb', line 80 def _each @decl_rel._each_node(@node) { |n| yield n } end |
#each ⇒ Object
Required by the Enumerable mixin.
71 72 73 74 75 76 77 |
# File 'lib/neo4j-wrapper/has_n/nodes.rb', line 71 def each if @cypher_block || @cypher_query_hash query(@cypher_query_hash, &@cypher_block).each { |i| yield i } else @decl_rel.each_node(@node) { |n| yield n } # Should use yield here as passing &block through doesn't always work (why?) end end |
#empty? ⇒ Boolean
Returns true if there are no node in this type of relationship
90 91 92 |
# File 'lib/neo4j-wrapper/has_n/nodes.rb', line 90 def empty? first == nil end |
#is_a?(type) ⇒ Boolean
Pretend we are an array - this is necessarily for Rails actionpack/actionview/formhelper to work with this
64 65 66 67 68 |
# File 'lib/neo4j-wrapper/has_n/nodes.rb', line 64 def is_a?(type) # ActionView requires this for nested attributes to work return true if Array == type super end |
#new(other) ⇒ Object
Creates a relationship instance between this and the other node. Returns the relationship object
97 98 99 |
# File 'lib/neo4j-wrapper/has_n/nodes.rb', line 97 def new(other) @decl_rel.create_relationship_to(@node, other) end |
#query(cypher_query_hash = nil, &block) ⇒ Object
51 52 53 |
# File 'lib/neo4j-wrapper/has_n/nodes.rb', line 51 def query(cypher_query_hash = nil, &block) Neo4j::Core::Traversal::CypherQuery.new(@node.neo_id, @decl_rel.dir, [@decl_rel.rel_type], cypher_query_hash, &block) end |
#to_ary ⇒ Object
Returns an real ruby array.
85 86 87 |
# File 'lib/neo4j-wrapper/has_n/nodes.rb', line 85 def to_ary self.to_a end |
#to_s ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/neo4j-wrapper/has_n/nodes.rb', line 43 def to_s if @cypher_block || @cypher_query_hash query(@cypher_query_hash, &@cypher_block).to_s else "HasN::Nodes [#{@decl_rel.dir}, id: #{@node.neo_id} type: #{@decl_rel && @decl_rel.rel_type} decl_rel:#{@decl_rel}]" end end |