Module: Neo4j::Wrapper::HasN::ClassMethods
- Defined in:
- lib/neo4j-wrapper/has_n/class_methods.rb
Instance Method Summary collapse
-
#_decl_rels ⇒ Hash
A hash of all relationship and its configuration defined by has_n and has_one.
-
#has_n(rel_type) ⇒ Neo4j::Wrapper::HasN::DeclRel
Specifies a relationship between two node classes.
-
#has_one(rel_type) ⇒ Neo4j::Wrapper::HasN::DeclRel
Specifies a relationship between two node classes.
-
#inherited(klass) ⇒ Object
make sure the inherited classes inherit the
_decl_rels
hash.
Instance Method Details
#_decl_rels ⇒ Hash
Returns a hash of all relationship and its configuration defined by has_n and has_one.
7 8 9 |
# File 'lib/neo4j-wrapper/has_n/class_methods.rb', line 7 def _decl_rels @_decl_rels ||= {} end |
#has_n(rel_type) ⇒ Neo4j::Wrapper::HasN::DeclRel
Specifies a relationship between two node classes. Generates assignment and accessor methods for the given relationship. Both incoming and outgoing relationships can be declared, see DeclRel
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/neo4j-wrapper/has_n/class_methods.rb', line 68 def has_n(rel_type) clazz = self module_eval(%Q{ def #{rel_type}(cypher_hash_query = nil, &cypher_block) dsl = _decl_rels_for('#{rel_type}'.to_sym) Neo4j::Wrapper::HasN::Nodes.new(self, dsl, cypher_hash_query, &cypher_block) end}, __FILE__, __LINE__) module_eval(%Q{ def #{rel_type}_rels dsl = _decl_rels_for('#{rel_type}'.to_sym) dsl.all_relationships(self) end}, __FILE__, __LINE__) instance_eval(%Q{ def #{rel_type} _decl_rels[:#{rel_type}].rel_type end}, __FILE__, __LINE__) _decl_rels[rel_type.to_sym] = DeclRel.new(rel_type, false, clazz) end |
#has_one(rel_type) ⇒ Neo4j::Wrapper::HasN::DeclRel
Specifies a relationship between two node classes. Generates assignment and accessor methods for the given relationship Old relationship is deleted when a new relationship is assigned. Both incoming and outgoing relationships can be declared, see DeclRel
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/neo4j-wrapper/has_n/class_methods.rb', line 110 def has_one(rel_type) clazz = self module_eval(%Q{def #{rel_type}=(value) dsl = _decl_rels_for(:#{rel_type}) rel = dsl.single_relationship(self) rel.del unless rel.nil? dsl.create_relationship_to(self, value) if value end}, __FILE__, __LINE__) module_eval(%Q{def #{rel_type} dsl = _decl_rels_for('#{rel_type}'.to_sym) dsl.single_node(self) end}, __FILE__, __LINE__) module_eval(%Q{def #{rel_type}_rel dsl = _decl_rels_for(:#{rel_type}) dsl.single_relationship(self) end}, __FILE__, __LINE__) instance_eval(%Q{ def #{rel_type} _decl_rels[:#{rel_type}].rel_type end}, __FILE__, __LINE__) _decl_rels[rel_type.to_sym] = DeclRel.new(rel_type, true, clazz) end |
#inherited(klass) ⇒ Object
make sure the inherited classes inherit the _decl_rels
hash
12 13 14 15 16 17 |
# File 'lib/neo4j-wrapper/has_n/class_methods.rb', line 12 def inherited(klass) copy = _decl_rels.clone copy.each_pair{|k,v| copy[k] = v.inherit_new} klass.instance_variable_set(:@_decl_rels, copy) super end |