Class: Neo4j::Wrapper::HasN::DeclRel
- Inherits:
-
Object
- Object
- Neo4j::Wrapper::HasN::DeclRel
- Defined in:
- lib/neo4j-wrapper/has_n/decl_rel.rb
Overview
A DSL for declared relationships has_n and has_one This DSL will be used to create accessor methods for relationships. Instead of using the ‘raw’ Neo4j::NodeMixin#rels method where one needs to know the name of relationship and direction one can use the generated accessor methods.
The DSL can also be used to specify a mapping to a Ruby class for a relationship, see Neo4j::HasN::DeclRelationshipDsl#relationship
The following methods will be generated:
- Folder#files
-
returns an Enumerable of outgoing nodes for relationship ‘files’
- Folder#files_rels
-
returns an Enumerable of outgoing relationships for relationship ‘files’
- File#folder
-
for adding one node for the relationship ‘files’ from the outgoing Folder node
- File#folder_rel
-
for accessing relationship ‘files’ from the outgoing Folder node
- File#folder
-
for accessing nodes from relationship ‘files’ from the outgoing Folder node
Instance Attribute Summary collapse
-
#dir ⇒ Object
readonly
Returns the value of attribute dir.
-
#rel_type ⇒ Object
readonly
Returns the value of attribute rel_type.
-
#source_class ⇒ Object
readonly
Returns the value of attribute source_class.
Instance Method Summary collapse
- #_all_relationships(node) ⇒ Object
- #_each_node(node, &block) ⇒ Object
- #all_relationships(node) ⇒ Object
- #create_relationship_to(node, other) ⇒ Object
- #each_node(node, &block) ⇒ Object
-
#from(*args) ⇒ Object
Specifies an incoming relationship.
- #has_n? ⇒ true, false
- #has_one? ⇒ true, false
- #incoming? ⇒ true, false
- #inherit_new ⇒ Object
-
#initialize(method_id, has_one, source_class) ⇒ DeclRel
constructor
A new instance of DeclRel.
-
#relationship(rel_class) ⇒ Object
Specifies which relationship ruby class to use for the relationship.
- #relationship_class ⇒ Object
- #single_node(node) ⇒ Object
- #single_relationship(node) ⇒ Object
- #target_class ⇒ Object
- #target_name ⇒ Object
-
#to(target, rel_type = @method_id) ⇒ Object
Declares an outgoing relationship type.
- #to_s ⇒ Object
Constructor Details
#initialize(method_id, has_one, source_class) ⇒ DeclRel
Returns a new instance of DeclRel.
38 39 40 41 42 43 44 |
# File 'lib/neo4j-wrapper/has_n/decl_rel.rb', line 38 def initialize(method_id, has_one, source_class) @method_id = method_id @has_one = has_one @dir = :outgoing @rel_type = method_id.to_sym @source_class = source_class end |
Instance Attribute Details
#dir ⇒ Object (readonly)
Returns the value of attribute dir.
36 37 38 |
# File 'lib/neo4j-wrapper/has_n/decl_rel.rb', line 36 def dir @dir end |
#rel_type ⇒ Object (readonly)
Returns the value of attribute rel_type.
36 37 38 |
# File 'lib/neo4j-wrapper/has_n/decl_rel.rb', line 36 def rel_type @rel_type end |
#source_class ⇒ Object (readonly)
Returns the value of attribute source_class.
36 37 38 |
# File 'lib/neo4j-wrapper/has_n/decl_rel.rb', line 36 def source_class @source_class end |
Instance Method Details
#_all_relationships(node) ⇒ Object
252 253 254 |
# File 'lib/neo4j-wrapper/has_n/decl_rel.rb', line 252 def _all_relationships(node) #:nodoc: node._rels(dir, rel_type) end |
#_each_node(node, &block) ⇒ Object
234 235 236 237 238 |
# File 'lib/neo4j-wrapper/has_n/decl_rel.rb', line 234 def _each_node(node, &block) #:nodoc: node._rels(dir, rel_type).each do |rel| block.call rel._other_node(node) end end |
#all_relationships(node) ⇒ Object
257 258 259 |
# File 'lib/neo4j-wrapper/has_n/decl_rel.rb', line 257 def all_relationships(node) node.rels(dir, rel_type) end |
#create_relationship_to(node, other) ⇒ Object
262 263 264 265 266 267 268 269 270 |
# File 'lib/neo4j-wrapper/has_n/decl_rel.rb', line 262 def create_relationship_to(node, other) # :nodoc: from, to = incoming? ? [other, node] : [node, other] if relationship_class relationship_class.new(@rel_type, from._java_node, to._java_node) else Neo4j::Relationship.new(@rel_type, from, to) end end |
#each_node(node, &block) ⇒ Object
227 228 229 230 231 |
# File 'lib/neo4j-wrapper/has_n/decl_rel.rb', line 227 def each_node(node, &block) node.rels(dir, rel_type).each do |rel| block.call(rel.other_node(node)) end end |
#from(*args) ⇒ Object
Specifies an incoming relationship. Will use the outgoing relationship given by the from class.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/neo4j-wrapper/has_n/decl_rel.rb', line 161 def from(*args) @dir = :incoming if args.size > 1 # handle specified (prefixed) relationship, e.g. has_n(:known_by).from(clazz, :type) @target_name = args[0] @relationship_name = args[1].to_sym @rel_type = "#{@target_name}##{args[1]}".to_sym elsif Symbol === args[0] # handle unspecified (unprefixed) relationship, e.g. has_n(:known_by).from(:type) name = args[0].to_s if name.include?("#") @target_name, @relationship_name = name.split("#").map(&:to_sym) end @rel_type = args[0] else raise "Expected a symbol for, got #{args[0].inspect}" end self end |
#has_n? ⇒ true, false
68 69 70 |
# File 'lib/neo4j-wrapper/has_n/decl_rel.rb', line 68 def has_n? !@has_one end |
#has_one? ⇒ true, false
63 64 65 |
# File 'lib/neo4j-wrapper/has_n/decl_rel.rb', line 63 def has_one? @has_one end |
#incoming? ⇒ true, false
73 74 75 |
# File 'lib/neo4j-wrapper/has_n/decl_rel.rb', line 73 def incoming? #:nodoc: @dir == :incoming end |
#inherit_new ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/neo4j-wrapper/has_n/decl_rel.rb', line 46 def inherit_new base = self dr = DeclRel.new(@method_id, @has_one, @source_class) dr.instance_eval do @dir = base.dir @rel_type = base.rel_type @target_name = base.target_name if base.target_name @source_class = base.source_class end dr end |
#relationship(rel_class) ⇒ Object
Specifies which relationship ruby class to use for the relationship
order = Order.new
order.products << Product.new
order.products_rels.first # => OrderLine
202 203 204 205 |
# File 'lib/neo4j-wrapper/has_n/decl_rel.rb', line 202 def relationship(rel_class) @relationship = rel_class self end |
#relationship_class ⇒ Object
213 214 215 216 217 218 219 |
# File 'lib/neo4j-wrapper/has_n/decl_rel.rb', line 213 def relationship_class # :nodoc: if @dir == :incoming other_class_dsl = target_class && target_class._decl_rels[@relationship_name] @relationship = other_class_dsl.relationship_class if other_class_dsl end @relationship end |
#single_node(node) ⇒ Object
241 242 243 244 |
# File 'lib/neo4j-wrapper/has_n/decl_rel.rb', line 241 def single_node(node) rel = single_relationship(node) rel && rel.other_node(node) end |
#single_relationship(node) ⇒ Object
247 248 249 |
# File 'lib/neo4j-wrapper/has_n/decl_rel.rb', line 247 def single_relationship(node) #:nodoc: node.rel(dir, rel_type) end |
#target_class ⇒ Object
221 222 223 |
# File 'lib/neo4j-wrapper/has_n/decl_rel.rb', line 221 def target_class @target_name && (@target_name.is_a?(Class) ? @target_name : Neo4j::Wrapper.to_class(@target_name.to_s)) end |
#target_name ⇒ Object
208 209 210 |
# File 'lib/neo4j-wrapper/has_n/decl_rel.rb', line 208 def target_name @target_name end |
#to(target, rel_type = @method_id) ⇒ Object
Declares an outgoing relationship type. It is possible to prefix relationship types so that it’s possible to distinguish different incoming relationships. There is no validation that the added node is of the specified class.
folder = FolderNode.new
# generate a relationship between folder and file of type 'FileNode#files'
folder.files << FileNode.new
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/neo4j-wrapper/has_n/decl_rel.rb', line 107 def to(target, rel_type = @method_id) @dir = :outgoing if Class === target || String === target # handle e.g. has_n(:friends).to(class) @target_name = target @rel_type = "#{@source_class}##{rel_type}".to_sym elsif Symbol === target # handle e.g. has_n(:friends).to(:knows) or to("Person#friends") @target_name = target.to_s.split("#")[0] if target.to_s.include?("#") @rel_type = target.to_sym else raise "Expected a class or a symbol for, got #{target}/#{target.class}" end self end |
#to_s ⇒ Object
58 59 60 |
# File 'lib/neo4j-wrapper/has_n/decl_rel.rb', line 58 def to_s "DeclRel #{object_id} dir: #{@dir} rel_id: #{@method_id}, rel_type: #{@rel_type}, target_class:#{@target_name} rel_class:#{@relationship}" end |