neo4j-wrapper 0.0.5-java → 0.0.6-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -53,14 +53,21 @@ module Neo4j
53
53
  # has_one(:folder).from(FolderNode, :files)
54
54
  # end
55
55
  #
56
+ # @example Using Cypher
57
+ # # from FolderNode example above
58
+ # folder.files.query{ cypher query DSL, see neo4j-core}
59
+ # folder.files{ } # same as above
60
+ # folder.files.query(:name => 'file.txt') # a cypher query with WHERE and statements
61
+ # folder.files(:name => 'file.txt') # same as above
62
+ # folder.files.query.to_s # the cypher query explained as a String
56
63
  #
57
- # @return [Neo4j::Wrapper::HasN::DeclRel] a DSL object where the has_n relationship can be futher specified
64
+ # @return [Neo4j::Wrapper::HasN::DeclRel] a DSL object where the has_n relationship can be further specified
58
65
  def has_n(rel_type)
59
66
  clazz = self
60
67
  module_eval(%Q{
61
- def #{rel_type}
68
+ def #{rel_type}(cypher_hash_query = nil, &cypher_block)
62
69
  dsl = _decl_rels_for('#{rel_type}'.to_sym)
63
- Neo4j::Wrapper::HasN::Nodes.new(self, dsl)
70
+ Neo4j::Wrapper::HasN::Nodes.new(self, dsl, cypher_hash_query, &cypher_block)
64
71
  end}, __FILE__, __LINE__)
65
72
 
66
73
 
@@ -107,7 +114,7 @@ module Neo4j
107
114
  end}, __FILE__, __LINE__)
108
115
 
109
116
  module_eval(%Q{def #{rel_type}
110
- dsl = _decl_rels_for(:#{rel_type})
117
+ dsl = _decl_rels_for('#{rel_type}'.to_sym)
111
118
  dsl.single_node(self)
112
119
  end}, __FILE__, __LINE__)
113
120
 
@@ -9,13 +9,45 @@ module Neo4j
9
9
  include Enumerable
10
10
  include Neo4j::Core::ToJava
11
11
 
12
- def initialize(node, decl_rel) # :nodoc:
12
+ def initialize(node, decl_rel, cypher_query_hash = nil, &cypher_block) # :nodoc:
13
13
  @node = node
14
14
  @decl_rel = decl_rel
15
+ @cypher_block = cypher_block
16
+ @cypher_query_hash = cypher_query_hash
17
+ self.class.define_rule_methods_on(self, decl_rel)
18
+ end
19
+
20
+ def self.define_rule_methods_on(instance, decl_rel )
21
+ rule_node = Neo4j::Wrapper::Rule::Rule.rule_node_for(decl_rel.target_class)
22
+
23
+ singelton = class << instance;
24
+ self;
25
+ end
26
+
27
+ rule_node && rule_node.rules.each do |rule|
28
+ next if rule.rule_name == :all
29
+ singelton.send(:define_method, rule.rule_name) do |*cypher_query_hash, &cypher_block|
30
+ proc = Proc.new do |m|
31
+ m.incoming(rule.rule_name)
32
+ if cypher_block
33
+ self.instance_exec(m, &cypher_block)
34
+ end
35
+ end
36
+ query(cypher_query_hash.first, &proc)
37
+ end
38
+ end
15
39
  end
16
40
 
17
41
  def to_s
18
- "HasN::Nodes [#{@decl_rel.dir}, id: #{@node.neo_id} type: #{@decl_rel && @decl_rel.rel_type} decl_rel:#{@decl_rel}]"
42
+ if @cypher_block || @cypher_query_hash
43
+ query(@cypher_query_hash, &@cypher_block).to_s
44
+ else
45
+ "HasN::Nodes [#{@decl_rel.dir}, id: #{@node.neo_id} type: #{@decl_rel && @decl_rel.rel_type} decl_rel:#{@decl_rel}]"
46
+ end
47
+ end
48
+
49
+ def query(cypher_query_hash = nil, &block)
50
+ Neo4j::Core::Traversal::CypherQuery.new(@node.neo_id, @decl_rel.dir, [@decl_rel.rel_type], cypher_query_hash, &block)
19
51
  end
20
52
 
21
53
  # Traverse the relationship till the index position
@@ -35,7 +67,11 @@ module Neo4j
35
67
 
36
68
  # Required by the Enumerable mixin.
37
69
  def each
38
- @decl_rel.each_node(@node) { |n| yield n } # Should use yield here as passing &block through doesn't always work (why?)
70
+ if @cypher_block || @cypher_query_hash
71
+ query(@cypher_query_hash, &@cypher_block).each { |i| yield i }
72
+ else
73
+ @decl_rel.each_node(@node) { |n| yield n } # Should use yield here as passing &block through doesn't always work (why?)
74
+ end
39
75
  end
40
76
 
41
77
  # returns none wrapped nodes, you may get better performance using this method
@@ -134,6 +134,9 @@ module Neo4j
134
134
 
135
135
  # @macro node.delegate
136
136
  delegate :getId
137
+
138
+ # @macro node.delegate
139
+ delegate :getRelationships
137
140
  end
138
141
  end
139
142
  end
@@ -102,9 +102,9 @@ module Neo4j
102
102
  end
103
103
 
104
104
  # define class methods
105
- singleton.send(:define_method, rule_name) do
105
+ singleton.send(:define_method, rule_name) do |*args, &cypher_block|
106
106
  rule_node = Rule.rule_node_for(self)
107
- rule_node.traversal(rule_name)
107
+ rule_node.traversal(rule_name, args.first, &cypher_block)
108
108
  end unless respond_to?(rule_name)
109
109
 
110
110
  # define instance methods
@@ -129,7 +129,7 @@ module Neo4j
129
129
  end
130
130
 
131
131
  # Assigns the reference node for a class via a supplied block.
132
- # Example of usage:
132
+ # @example usage:
133
133
  # class Person
134
134
  # include Neo4j::NodeMixin
135
135
  # ref_node { Neo4j.default_ref_node }
@@ -12,6 +12,7 @@ module Neo4j
12
12
  self
13
13
  end
14
14
 
15
+
15
16
  def functions_method(func, rule_node, rule_name)
16
17
  singelton = class << self;
17
18
  self;
@@ -122,6 +122,7 @@ module Neo4j
122
122
 
123
123
  def bulk_trigger_rules(classname, class_change, map)
124
124
  rule_node = rule_node_for(classname)
125
+ return if rule_node.rules.size > 1 # this optimization is not allowed when using more then one rule
125
126
  rule_node.classes_changed(class_change)
126
127
  if (clazz = rule_node.model_class.superclass) && clazz.include?(Neo4j::NodeMixin)
127
128
  bulk_trigger_rules(clazz.name, class_change, map) if clazz.to_s != "Neo4j::Rails::Model"
@@ -97,17 +97,21 @@ module Neo4j
97
97
 
98
98
  # Return a traversal object with methods for each rule and function.
99
99
  # E.g. Person.all.old or Person.all.sum(:age)
100
- def traversal(rule_name)
100
+ def traversal(rule_name, cypher_query_hash = nil, &cypher_block)
101
101
  traversal = rule_node.outgoing(rule_name)
102
- @rules.each do |rule|
103
- traversal.filter_method(rule.rule_name) do |path|
104
- path.end_node.rel?(:incoming, rule.rule_name)
105
- end
106
- rule.functions && rule.functions.each do |func|
107
- traversal.functions_method(func, self, rule_name)
102
+ if cypher_query_hash || cypher_block
103
+ traversal.query(cypher_query_hash, &cypher_block)
104
+ else
105
+ @rules.each do |rule|
106
+ traversal.filter_method(rule.rule_name) do |path|
107
+ path.end_node.rel?(:incoming, rule.rule_name)
108
+ end
109
+ rule.functions && rule.functions.each do |func|
110
+ traversal.functions_method(func, self, rule_name)
111
+ end
108
112
  end
113
+ traversal
109
114
  end
110
- traversal
111
115
  end
112
116
 
113
117
  def find_function(rule_name, function_name, function_id)
@@ -1,5 +1,5 @@
1
1
  module Neo4j
2
2
  module Wrapper
3
- VERSION = '0.0.5'
3
+ VERSION = '0.0.6'
4
4
  end
5
5
  end
@@ -27,5 +27,5 @@ It comes included with the Apache Lucene document database.
27
27
  s.extra_rdoc_files = %w( README.rdoc )
28
28
  s.rdoc_options = ["--quiet", "--title", "Neo4j.rb", "--line-numbers", "--main", "README.rdoc", "--inline-source"]
29
29
 
30
- s.add_dependency("neo4j-core", "0.0.9")
30
+ s.add_dependency("neo4j-core", "0.0.10")
31
31
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: neo4j-wrapper
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.5
5
+ version: 0.0.6
6
6
  platform: java
7
7
  authors:
8
8
  - Andreas Ronge
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-04-15 00:00:00 Z
13
+ date: 2012-04-18 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: neo4j-core
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - "="
22
22
  - !ruby/object:Gem::Version
23
- version: 0.0.9
23
+ version: 0.0.10
24
24
  type: :runtime
25
25
  version_requirements: *id001
26
26
  description: |