neo4j-core 2.0.0.alpha.1-java → 2.0.0-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. data/Gemfile +2 -2
  2. data/README.rdoc +161 -13
  3. data/config/neo4j/config.yml +1 -1
  4. data/lib/db/active_tx_log +1 -0
  5. data/lib/db/index/lucene-store.db +0 -0
  6. data/lib/db/index/lucene.log.active +0 -0
  7. data/lib/db/messages.log +2299 -0
  8. data/lib/db/neostore +0 -0
  9. data/lib/db/neostore.id +0 -0
  10. data/lib/db/neostore.nodestore.db +0 -0
  11. data/lib/db/neostore.nodestore.db.id +0 -0
  12. data/lib/db/neostore.propertystore.db +0 -0
  13. data/lib/db/neostore.propertystore.db.arrays +0 -0
  14. data/lib/db/neostore.propertystore.db.arrays.id +0 -0
  15. data/lib/db/neostore.propertystore.db.id +0 -0
  16. data/lib/db/neostore.propertystore.db.index +0 -0
  17. data/lib/db/neostore.propertystore.db.index.id +0 -0
  18. data/lib/db/neostore.propertystore.db.index.keys +0 -0
  19. data/lib/db/neostore.propertystore.db.index.keys.id +0 -0
  20. data/lib/db/neostore.propertystore.db.strings +0 -0
  21. data/lib/db/neostore.propertystore.db.strings.id +0 -0
  22. data/lib/db/neostore.relationshipstore.db +0 -0
  23. data/lib/db/neostore.relationshipstore.db.id +0 -0
  24. data/lib/db/neostore.relationshiptypestore.db +0 -0
  25. data/lib/db/neostore.relationshiptypestore.db.id +0 -0
  26. data/lib/db/neostore.relationshiptypestore.db.names +0 -0
  27. data/lib/db/neostore.relationshiptypestore.db.names.id +0 -0
  28. data/lib/db/nioneo_logical.log.active +0 -0
  29. data/lib/db/tm_tx_log.1 +0 -0
  30. data/lib/neo4j-core.rb +20 -3
  31. data/lib/neo4j-core/cypher/cypher.rb +1033 -0
  32. data/lib/neo4j-core/cypher/result_wrapper.rb +48 -0
  33. data/lib/neo4j-core/database.rb +4 -5
  34. data/lib/neo4j-core/event_handler.rb +1 -1
  35. data/lib/neo4j-core/hash_with_indifferent_access.rb +165 -0
  36. data/lib/neo4j-core/index/class_methods.rb +27 -41
  37. data/lib/neo4j-core/index/index.rb +3 -4
  38. data/lib/neo4j-core/index/index_config.rb +30 -23
  39. data/lib/neo4j-core/index/indexer.rb +65 -53
  40. data/lib/neo4j-core/index/indexer_registry.rb +2 -2
  41. data/lib/neo4j-core/index/lucene_query.rb +53 -42
  42. data/lib/neo4j-core/index/unique_factory.rb +54 -0
  43. data/lib/neo4j-core/node/class_methods.rb +4 -4
  44. data/lib/neo4j-core/node/node.rb +1 -8
  45. data/lib/neo4j-core/property/java.rb +59 -0
  46. data/lib/neo4j-core/property/property.rb +1 -3
  47. data/lib/neo4j-core/relationship/relationship.rb +8 -10
  48. data/lib/neo4j-core/rels/rels.rb +9 -4
  49. data/lib/neo4j-core/rels/traverser.rb +13 -27
  50. data/lib/neo4j-core/traversal/prune_evaluator.rb +2 -2
  51. data/lib/neo4j-core/traversal/traverser.rb +122 -27
  52. data/lib/neo4j-core/version.rb +1 -1
  53. data/lib/neo4j-core/wrapper/class_methods.rb +22 -0
  54. data/lib/neo4j-core/wrapper/wrapper.rb +20 -0
  55. data/lib/neo4j/algo.rb +300 -0
  56. data/lib/neo4j/config.rb +3 -6
  57. data/lib/neo4j/cypher.rb +180 -0
  58. data/lib/neo4j/neo4j.rb +51 -23
  59. data/lib/neo4j/node.rb +27 -0
  60. data/lib/neo4j/relationship.rb +25 -0
  61. data/lib/test.rb~ +27 -0
  62. data/neo4j-core.gemspec +2 -2
  63. metadata +44 -11
  64. data/lib/neo4j-core/type_converters/type_converters.rb +0 -287
  65. data/lib/neo4j-core/version.rb~ +0 -3
  66. data/lib/test.rb +0 -27
data/lib/neo4j/neo4j.rb CHANGED
@@ -5,10 +5,17 @@
5
5
  # The Neo4j modules is used to interact with an Neo4j Database instance.
6
6
  # You can for example start and stop an instance and list all the nodes that exist in the database.
7
7
  #
8
- # === Starting and Stopping Neo4j
8
+ # @example
9
+ # require 'neo4j'
10
+ # Neo4j::Transaction.run { Neo4j.ref_node.outgoing(:friends) << Neo4j::Node.new(:name => 'andreas')}
11
+ # Neo4j.ref_node.node(:outgoing, :friends)[:name] #=> 'andreas'
12
+ # Neo4j.query(Neo4j.ref_node){|ref| ref > ':friends' > :x; :x}
13
+ #
14
+ # ==== Notice - Starting and Stopping Neo4j
9
15
  # You don't normally need to start the Neo4j database since it will be automatically started when needed.
10
16
  # Before the database is started you should configure where the database is stored, see {Neo4j::Config]}.
11
17
  #
18
+ #
12
19
  module Neo4j
13
20
 
14
21
  # @return [String] The version of the Neo4j jar files
@@ -51,9 +58,10 @@ module Neo4j
51
58
  end
52
59
 
53
60
  # Checks read only mode of the database. Only one process can have write access to the database.
61
+ # It will always return false if the database is not started yet.
54
62
  # @return [true, false] if the database has started up in read only mode
55
63
  def read_only?
56
- (@db && @db.graph && @db.read_only?)
64
+ !!(@db && @db.graph && @db.read_only?)
57
65
  end
58
66
 
59
67
  # Returns a started db instance. Starts it's not running.
@@ -64,14 +72,10 @@ module Neo4j
64
72
  db
65
73
  end
66
74
 
67
- # Runs all user defined migrations.
68
- def migrate!
69
-
70
- end
71
-
72
- # @return [String, nil] the current storage path of a running neo4j database. If the database is not running it returns nil.
75
+ # If the database is not running it returns Neo4j::Config.storage_path otherwise it asks the database
76
+ # @return [String] the current storage path of the database
73
77
  def storage_path
74
- return nil unless db.running?
78
+ return Neo4j::Config.storage_path unless db.running?
75
79
  db.storage_path
76
80
  end
77
81
 
@@ -85,18 +89,42 @@ module Neo4j
85
89
  # Check the neo4j
86
90
  # Returns an enumerable of hash values.
87
91
  #
88
- # @example
92
+ # @example Using the Cypher DSL
93
+ # q = Neo4j.query{ match node(3) <=> node(:x); ret :x}
94
+ # q.first[:n] #=> the @node
95
+ # q.columns.first => :n
96
+ #
97
+ # @example Using the Cypher DSL and one parameter (n=Neo4j.ref_node)
98
+ # q = Neo4j.query(Neo4j.ref_node){|n| n <=> node(:x); :x}
99
+ # q.first[:n] #=> the @node
100
+ # q.columns.first => :n
101
+ #
102
+ # @example Using an array of nodes
103
+ # # same as - two_nodes=node(Neo4j.ref_node.neo_id, node_b.neo_id), b = node(b.neo_id)
104
+ # q = Neo4j.query([Neo4j.ref_node, node_b], node_c){|two_nodes, b| two_nodes <=> b; b}
89
105
  #
90
- # q = Neo4j.query("START n=node({node}) RETURN n", 'node' => @node.neo_id)
91
- # q.first['n'] #=> the @node
92
- # q.columns.first => 'n'
106
+ # @example With an string
107
+ # q = Neo4j._query("START n=node(42) RETURN n")
108
+ # q.first(:n) #=> the @node
109
+ # q.columns.first => :n
93
110
  #
94
- # @see {http://docs.neo4j.org/chunked/milestone/cypher-query-lang.html The Cypher Query Language Documentation}
111
+ # @see Cypher
112
+ # @see http://docs.neo4j.org/chunked/milestone/cypher-query-lang.html The Cypher Query Language Documentation
95
113
  # @note Returns a read-once only forward iterable.
96
- # @return [Enumerable] a forward read once only Enumerable, containing hash values.
97
- def query(query, params = {})
98
- engine = org.neo4j.cypher.javacompat.ExecutionEngine.new(db)
99
- engine.execute(query, params)
114
+ # @param params parameter for the query_dsl block
115
+ # @return [Neo4j::Core::Cypher::ResultWrapper] a forward read once only Enumerable, containing hash values.
116
+ def query(*params, &query_dsl)
117
+ q = Cypher.new(*params, &query_dsl).to_s
118
+ _query(q)
119
+ end
120
+
121
+ # Performs a cypher query with given string
122
+ # @param [String] q the cypher query as a String
123
+ # @return (see #query)
124
+ def _query(q)
125
+ engine = Java::OrgNeo4jCypherJavacompat::ExecutionEngine.new(db)
126
+ result = engine.execute(q)
127
+ Neo4j::Core::Cypher::ResultWrapper.new(result)
100
128
  end
101
129
 
102
130
 
@@ -165,9 +193,8 @@ module Neo4j
165
193
 
166
194
  # Usually, a client attaches relationships to this node that leads into various parts of the node space.
167
195
  # ®return the reference node, which is a "starting point" in the node space.
168
- # @note In case the ref_node has been assigned via the threadlocal_ref_node method,
196
+ # @note In case the ref_node has been assigned via the threadlocal_ref_node method,
169
197
  # then that node will be returned instead.
170
- # @see the design guide at http://wiki.neo4j.org/content/Design_Guide
171
198
  def ref_node(this_db = self.started_db)
172
199
  return Thread.current[:local_ref_node] if Thread.current[:local_ref_node]
173
200
  default_ref_node(this_db)
@@ -199,15 +226,16 @@ module Neo4j
199
226
  # Neo4j.management(org.neo4j.management.HighAvailability).isMaster
200
227
  #
201
228
  # @param jmx_clazz the JMX class http://api.neo4j.org/current/org/neo4j/management/package-summary.html
202
- # @param this_db default currently runnig instance or a newly started neo4j db instance
229
+ # @param this_db default currently running instance or a newly started neo4j db instance
203
230
  # @see for the jmx_clazz p
204
- def management(jmx_clazz = org.neo4j.jmx.Primitives, this_db = self.started_db)
231
+ # @node this
232
+ def management(jmx_clazz = Java::OrgNeo4jJmx::Primitives, this_db = self.started_db)
205
233
  this_db.management(jmx_clazz)
206
234
  end
207
235
 
208
236
  # @return [Enumerable] all nodes in the database
209
237
  def all_nodes(this_db = self.started_db)
210
- Enumerable::Enumerator.new(this_db, :each_node)
238
+ Enumerator.new(this_db, :each_node)
211
239
  end
212
240
 
213
241
  # @return [Enumerable] all nodes in the database but not wrapped in ruby classes.
data/lib/neo4j/node.rb CHANGED
@@ -9,14 +9,39 @@ module Neo4j
9
9
  # *org.neo4j.kernel.impl.core.NodeProxy* object. This java object includes the same mixin as this class. The #class method on the java object
10
10
  # returns Neo4j::Node in order to make it feel like an ordinary Ruby object.
11
11
  #
12
+ # @example Create a node with one property (see {Neo4j::Core::Node::ClassMethods})
13
+ # Neo4j::Node.new(:name => 'andreas')
14
+ #
15
+ # @example Create a relationship (see {Neo4j::Core::Traversal})
16
+ # Neo4j::Node.new.outgoing(:friends) << Neo4j::Node.new
17
+ #
18
+ # @example Finding relationships (see {Neo4j::Core::Rels})
19
+ # node.rels(:outgoing, :friends)
20
+ #
21
+ # @example Lucene index (see {Neo4j::Core::Index})
22
+ # Neo4j::Node.trigger_on(:typex => 'MyTypeX')
23
+ # Neo4j::Node.index(:name)
24
+ # a = Neo4j::Node.new(:name => 'andreas', :typex => 'MyTypeX')
25
+ # # finish_tx
26
+ # Neo4j::Node.find(:name => 'andreas').first.should == a
27
+ #
12
28
  class Node
13
29
  extend Neo4j::Core::Node::ClassMethods
30
+ extend Neo4j::Core::Wrapper::ClassMethods
31
+ extend Neo4j::Core::Index::ClassMethods
14
32
 
15
33
  include Neo4j::Core::Property
16
34
  include Neo4j::Core::Rels
17
35
  include Neo4j::Core::Traversal
18
36
  include Neo4j::Core::Equal
19
37
  include Neo4j::Core::Node
38
+ include Neo4j::Core::Wrapper
39
+ include Neo4j::Core::Property::Java # for documentation purpose only
40
+ include Neo4j::Core::Index
41
+
42
+ node_indexer do
43
+ index_names :exact => 'default_node_index_exact', :fulltext => 'default_node_index_fulltext'
44
+ end
20
45
 
21
46
  class << self
22
47
 
@@ -29,6 +54,8 @@ module Neo4j
29
54
  include Neo4j::Core::Traversal
30
55
  include Neo4j::Core::Equal
31
56
  include Neo4j::Core::Node
57
+ include Neo4j::Core::Wrapper
58
+ include Neo4j::Core::Index
32
59
  end
33
60
  end
34
61
  end
@@ -29,13 +29,36 @@ module Neo4j
29
29
  #
30
30
  # node.outgoing(:friends) << other_node << yet_another_node
31
31
  #
32
+ # @example lucene index
33
+ # Neo4j::Relationship.trigger_on(:typey => 123)
34
+ # Neo4j::Relationship.index(:name)
35
+ # a = Neo4j::Relationship.new(:friends, Neo4j::Node.new, Neo4j::Node.new, :typey => 123, :name => 'kalle')
36
+ # # Finish tx
37
+ # Neo4j::Relationship.find(:name => 'kalle').first.should be_nil
38
+ #
32
39
  # @see http://api.neo4j.org/current/org/neo4j/graphdb/Relationship.html
33
40
  #
34
41
  class Relationship
35
42
  extend Neo4j::Core::Relationship::ClassMethods
43
+ extend Neo4j::Core::Wrapper::ClassMethods
44
+ extend Neo4j::Core::Index::ClassMethods
45
+
36
46
  include Neo4j::Core::Property
37
47
  include Neo4j::Core::Equal
38
48
  include Neo4j::Core::Relationship
49
+ include Neo4j::Core::Wrapper
50
+ include Neo4j::Core::Property::Java # for documentation purpose only
51
+ include Neo4j::Core::Index
52
+
53
+
54
+ # (see Neo4j::Core::Relationship::ClassMethods#new)
55
+ def initialize(rel_type, start_node, end_node, props={})
56
+ end
57
+
58
+
59
+ rel_indexer do
60
+ index_names :exact => 'default_rel_index_exact', :fulltext => 'default_rel_index_fulltext'
61
+ end
39
62
 
40
63
  class << self
41
64
  def extend_java_class(java_clazz) #:nodoc:
@@ -43,6 +66,8 @@ module Neo4j
43
66
  include Neo4j::Core::Property
44
67
  include Neo4j::Core::Equal
45
68
  include Neo4j::Core::Relationship
69
+ include Neo4j::Core::Wrapper
70
+ include Neo4j::Core::Index
46
71
  end
47
72
  end
48
73
 
data/lib/test.rb~ ADDED
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+ require 'neo4j-core'
3
+
4
+
5
+ def connect(d)
6
+ d.each{|i| Neo4j::Relationship.new(:flies_to, i[:from], i[:to])}
7
+ end
8
+
9
+ puts "Using #{Neo4j::Core::VERSION}"
10
+
11
+ tx = Neo4j::Transaction.new
12
+ @a, @b, @c, @z = (1..4).map{Neo4j::Node.new}
13
+ connect([ {:from=>@a, :to=>@b}, {:from=>@a, :to=>@c}, {:from=>@b, :to=>@c},
14
+ {:from=>@b, :to=>@z}, {:from=>@c, :to=>@b}, {:from=>@c, :to=>@z}])
15
+ tx.success
16
+ tx.finish
17
+
18
+
19
+ traversal = @a.outgoing(:flies_to).depth(:all).eval_paths { |path| :include_and_continue }
20
+
21
+ # This prints out
22
+ #Path (1)--[flies_to,0]-->(2)
23
+ #Path (1)--[flies_to,1]-->(3)
24
+ #Path (1)--[flies_to,0]-->(2)--[flies_to,3]-->(4)
25
+ traversal.paths.each do |path|
26
+ puts "Path #{path.to_s}"
27
+ end
data/neo4j-core.gemspec CHANGED
@@ -25,7 +25,7 @@ It comes included with the Apache Lucene document database.
25
25
  s.files = Dir.glob("{bin,lib,config}/**/*") + %w(README.rdoc Gemfile neo4j-core.gemspec)
26
26
  s.has_rdoc = true
27
27
  s.extra_rdoc_files = %w( README.rdoc )
28
- s.rdoc_options = ["--quiet", "--title", "Neo4j.rb", "--line-numbers", "--main", "README.rdoc", "--inline-source"]
28
+ s.rdoc_options = ["--quiet", "--title", "Neo4j::Core", "--line-numbers", "--main", "README.rdoc", "--inline-source"]
29
29
 
30
- s.add_dependency("neo4j-community", "1.6.1.alpha.1")
30
+ s.add_dependency("neo4j-community", ">= 1.7.0")
31
31
  end
metadata CHANGED
@@ -1,8 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: neo4j-core
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: 6
5
- version: 2.0.0.alpha.1
4
+ prerelease:
5
+ version: 2.0.0
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-03-11 00:00:00 Z
13
+ date: 2012-05-07 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: neo4j-community
@@ -18,9 +18,9 @@ dependencies:
18
18
  requirement: &id001 !ruby/object:Gem::Requirement
19
19
  none: false
20
20
  requirements:
21
- - - "="
21
+ - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 1.6.1.alpha.1
23
+ version: 1.7.0
24
24
  type: :runtime
25
25
  version_requirements: *id001
26
26
  description: |
@@ -37,9 +37,11 @@ extensions: []
37
37
  extra_rdoc_files:
38
38
  - README.rdoc
39
39
  files:
40
- - lib/test.rb
41
40
  - lib/neo4j-core.rb
41
+ - lib/test.rb~
42
42
  - lib/neo4j/config.rb
43
+ - lib/neo4j/algo.rb
44
+ - lib/neo4j/cypher.rb
43
45
  - lib/neo4j/transaction.rb
44
46
  - lib/neo4j/neo4j.rb
45
47
  - lib/neo4j/neo4j.rb~
@@ -47,13 +49,14 @@ files:
47
49
  - lib/neo4j/relationship.rb
48
50
  - lib/neo4j-core/event_handler.rb
49
51
  - lib/neo4j-core/lazy_map.rb
50
- - lib/neo4j-core/version.rb~
52
+ - lib/neo4j-core/hash_with_indifferent_access.rb
51
53
  - lib/neo4j-core/database.rb
52
54
  - lib/neo4j-core/relationship_set.rb
53
55
  - lib/neo4j-core/version.rb
54
56
  - lib/neo4j-core/to_java.rb
55
57
  - lib/neo4j-core/node/node.rb
56
58
  - lib/neo4j-core/node/class_methods.rb
59
+ - lib/neo4j-core/property/java.rb
57
60
  - lib/neo4j-core/property/property.rb
58
61
  - lib/neo4j-core/equal/equal.rb
59
62
  - lib/neo4j-core/traversal/rel_expander.rb
@@ -62,17 +65,47 @@ files:
62
65
  - lib/neo4j-core/traversal/traversal.rb
63
66
  - lib/neo4j-core/traversal/traverser.rb
64
67
  - lib/neo4j-core/traversal/evaluator.rb
65
- - lib/neo4j-core/type_converters/type_converters.rb
66
68
  - lib/neo4j-core/rels/traverser.rb
67
69
  - lib/neo4j-core/rels/rels.rb
70
+ - lib/neo4j-core/wrapper/wrapper.rb
71
+ - lib/neo4j-core/wrapper/class_methods.rb
68
72
  - lib/neo4j-core/index/index_config.rb
69
73
  - lib/neo4j-core/index/indexer.rb
70
74
  - lib/neo4j-core/index/lucene_query.rb
71
75
  - lib/neo4j-core/index/indexer_registry.rb
76
+ - lib/neo4j-core/index/unique_factory.rb
72
77
  - lib/neo4j-core/index/class_methods.rb
73
78
  - lib/neo4j-core/index/index.rb
74
79
  - lib/neo4j-core/relationship/class_methods.rb
75
80
  - lib/neo4j-core/relationship/relationship.rb
81
+ - lib/neo4j-core/cypher/cypher.rb
82
+ - lib/neo4j-core/cypher/result_wrapper.rb
83
+ - lib/db/neostore.relationshiptypestore.db.names
84
+ - lib/db/neostore.id
85
+ - lib/db/neostore.relationshiptypestore.db.names.id
86
+ - lib/db/neostore.nodestore.db.id
87
+ - lib/db/neostore.propertystore.db.index
88
+ - lib/db/neostore.propertystore.db.strings.id
89
+ - lib/db/nioneo_logical.log.active
90
+ - lib/db/neostore.propertystore.db.strings
91
+ - lib/db/neostore.relationshiptypestore.db.id
92
+ - lib/db/neostore
93
+ - lib/db/neostore.nodestore.db
94
+ - lib/db/neostore.propertystore.db.index.keys.id
95
+ - lib/db/neostore.propertystore.db.arrays.id
96
+ - lib/db/messages.log
97
+ - lib/db/neostore.relationshipstore.db.id
98
+ - lib/db/neostore.propertystore.db.arrays
99
+ - lib/db/neostore.relationshipstore.db
100
+ - lib/db/neostore.propertystore.db.index.id
101
+ - lib/db/neostore.propertystore.db.id
102
+ - lib/db/neostore.propertystore.db
103
+ - lib/db/tm_tx_log.1
104
+ - lib/db/neostore.relationshiptypestore.db
105
+ - lib/db/active_tx_log
106
+ - lib/db/neostore.propertystore.db.index.keys
107
+ - lib/db/index/lucene-store.db
108
+ - lib/db/index/lucene.log.active
76
109
  - config/neo4j/config.yml
77
110
  - README.rdoc
78
111
  - Gemfile
@@ -84,7 +117,7 @@ post_install_message:
84
117
  rdoc_options:
85
118
  - --quiet
86
119
  - --title
87
- - Neo4j.rb
120
+ - Neo4j::Core
88
121
  - --line-numbers
89
122
  - --main
90
123
  - README.rdoc
@@ -100,9 +133,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
100
133
  required_rubygems_version: !ruby/object:Gem::Requirement
101
134
  none: false
102
135
  requirements:
103
- - - ">"
136
+ - - ">="
104
137
  - !ruby/object:Gem::Version
105
- version: 1.3.1
138
+ version: "0"
106
139
  requirements: []
107
140
 
108
141
  rubyforge_project: neo4j-core
@@ -1,287 +0,0 @@
1
- module Neo4j
2
-
3
- # Responsible for converting values from and to Java Neo4j and Lucene.
4
- # You can implement your own converter by implementing the method <tt>convert?</tt>
5
- # <tt>to_java</tt> and <tt>to_ruby</tt> in this module.
6
- #
7
- # There are currently three default converters that are triggered when a Time, Date or a DateTime is read or written
8
- # if there is a type declared for the property.
9
- #
10
- # ==== Example
11
- #
12
- # Example of writing your own marshalling converter:
13
- #
14
- # class Foo
15
- # include Neo4j::NodeMixin
16
- # property :thing, :type => MyType
17
- # end
18
- #
19
- # module Neo4j::TypeConverters
20
- # class MyTypeConverter
21
- # class << self
22
- # def convert?(type)
23
- # type == MyType
24
- # end
25
- #
26
- # def to_java(val)
27
- # "silly:#{val}"
28
- # end
29
- #
30
- # def to_ruby(val)
31
- # val.sub(/silly:/, '')
32
- # end
33
- # end
34
- # end
35
- # end
36
- #
37
- module TypeConverters
38
-
39
- # The default converter to use if there isn't a specific converter for the type
40
- class DefaultConverter
41
- class << self
42
-
43
- def to_java(value)
44
- value
45
- end
46
-
47
- def to_ruby(value)
48
- value
49
- end
50
- end
51
- end
52
-
53
-
54
- class BooleanConverter
55
- class << self
56
-
57
- def convert?(class_or_symbol)
58
- :boolean == class_or_symbol
59
- end
60
-
61
- def to_java(value)
62
- return nil if value.nil?
63
- !!value && value != '0'
64
- end
65
-
66
- def to_ruby(value)
67
- return nil if value.nil?
68
- !!value && value != '0'
69
- end
70
- end
71
- end
72
-
73
- class SymbolConverter
74
- class << self
75
-
76
- def convert?(class_or_symbol)
77
- :symbol == class_or_symbol || Symbol == class_or_symbol
78
- end
79
-
80
- def to_java(value)
81
- return nil if value.nil?
82
- value.to_s
83
- end
84
-
85
- def to_ruby(value)
86
- return nil if value.nil?
87
- value.to_sym
88
- end
89
- end
90
- end
91
-
92
-
93
- class StringConverter
94
- class << self
95
-
96
- def convert?(class_or_symbol)
97
- [String, :string, :text].include? class_or_symbol
98
- end
99
-
100
- def to_java(value)
101
- return nil if value.nil?
102
- value.to_s
103
- end
104
-
105
- def to_ruby(value)
106
- return nil if value.nil?
107
- value.to_s
108
- end
109
- end
110
- end
111
-
112
-
113
-
114
- class FixnumConverter
115
- class << self
116
-
117
- def convert?(class_or_symbol)
118
- Fixnum == class_or_symbol || :fixnum == class_or_symbol || :numeric == class_or_symbol
119
- end
120
-
121
- def to_java(value)
122
- return nil if value.nil?
123
- value.to_i
124
- end
125
-
126
- def to_ruby(value)
127
- return nil if value.nil?
128
- value.to_i
129
- end
130
- end
131
- end
132
-
133
- class FloatConverter
134
- class << self
135
-
136
- def convert?(clazz_or_symbol)
137
- Float == clazz_or_symbol || :float == clazz_or_symbol
138
- end
139
-
140
- def to_java(value)
141
- return nil if value.nil?
142
- value.to_f
143
- end
144
-
145
- def to_ruby(value)
146
- return nil if value.nil?
147
- value.to_f
148
- end
149
- end
150
- end
151
-
152
- # Converts Date objects to Java long types. Must be timezone UTC.
153
- class DateConverter
154
- class << self
155
-
156
- def convert?(clazz_or_symbol)
157
- Date == clazz_or_symbol || :date == clazz_or_symbol
158
- end
159
-
160
- def to_java(value)
161
- return nil if value.nil?
162
- Time.utc(value.year, value.month, value.day).to_i
163
- end
164
-
165
- def to_ruby(value)
166
- return nil if value.nil?
167
- Time.at(value).utc.to_date
168
- end
169
- end
170
- end
171
-
172
- # Converts DateTime objects to and from Java long types. Must be timezone UTC.
173
- class DateTimeConverter
174
- class << self
175
-
176
- def convert?(clazz_or_symbol)
177
- DateTime == clazz_or_symbol || :datetime == clazz_or_symbol
178
- end
179
-
180
- # Converts the given DateTime (UTC) value to an Fixnum.
181
- # Only utc times are supported !
182
- def to_java(value)
183
- return nil if value.nil?
184
- if value.class == Date
185
- Time.utc(value.year, value.month, value.day, 0, 0, 0).to_i
186
- else
187
- Time.utc(value.year, value.month, value.day, value.hour, value.min, value.sec).to_i
188
- end
189
- end
190
-
191
- def to_ruby(value)
192
- return nil if value.nil?
193
- t = Time.at(value).utc
194
- DateTime.civil(t.year, t.month, t.day, t.hour, t.min, t.sec)
195
- end
196
- end
197
- end
198
-
199
- class TimeConverter
200
- class << self
201
-
202
- def convert?(clazz_or_symbol)
203
- Time == clazz_or_symbol || :time == clazz_or_symbol
204
- end
205
-
206
- # Converts the given DateTime (UTC) value to an Fixnum.
207
- # Only utc times are supported !
208
- def to_java(value)
209
- return nil if value.nil?
210
- if value.class == Date
211
- Time.utc(value.year, value.month, value.day, 0, 0, 0).to_i
212
- else
213
- value.utc.to_i
214
- end
215
- end
216
-
217
- def to_ruby(value)
218
- return nil if value.nil?
219
- Time.at(value).utc
220
- end
221
- end
222
- end
223
-
224
- class << self
225
-
226
- # Mostly for testing purpose, You can use this method in order to
227
- # add a converter while the neo4j has already started.
228
- def converters=(converters)
229
- @converters = converters
230
- end
231
-
232
- # Always returns a converter that handles to_ruby or to_java
233
- # if +enforce_type+ is set to false then it will raise in case of unknown type
234
- # otherwise it will return the DefaultConverter.
235
- def converter(type = nil, enforce_type = true)
236
- return DefaultConverter unless type
237
- @converters ||= begin
238
- Neo4j::TypeConverters.constants.find_all do |c|
239
- Neo4j::TypeConverters.const_get(c).respond_to?(:convert?)
240
- end.map do |c|
241
- Neo4j::TypeConverters.const_get(c)
242
- end
243
- end
244
- found = @converters.find {|c| c.convert?(type) }
245
- raise "The type #{type.inspect} is unknown. Use one of #{@converters.map{|c| c.name }.join(", ")} or create a custom type converter." if !found && enforce_type
246
- found or DefaultConverter
247
- end
248
-
249
- # Converts the given value to a Java type by using the registered converters.
250
- # It just looks at the class of the given value unless an attribute name is given.
251
- def convert(value, attribute = nil, klass = nil, enforce_type = true)
252
- converter(attribute_type(value, attribute, klass), enforce_type).to_java(value)
253
- end
254
-
255
- # Converts the given property (key, value) to Java if there is a type converter for given type.
256
- # The type must also be declared using Neo4j::NodeMixin#property property_name, :type => clazz
257
- # If no Converter is defined for this value then it simply returns the given value.
258
- def to_java(clazz, key, value)
259
- type = clazz._decl_props[key.to_sym] && clazz._decl_props[key.to_sym][:type]
260
- converter(type).to_java(value)
261
- end
262
-
263
- # Converts the given property (key, value) to Ruby if there is a type converter for given type.
264
- # If no Converter is defined for this value then it simply returns the given value.
265
- def to_ruby(clazz, key, value)
266
- type = clazz._decl_props[key.to_sym] && clazz._decl_props[key.to_sym][:type]
267
- converter(type).to_ruby(value)
268
- end
269
-
270
- private
271
- def attribute_type(value, attribute = nil, klass = nil)
272
- type = (attribute && klass) ? attribute_type_from_attribute_and_klass(value, attribute, klass) : nil
273
- type || attribute_type_from_value(value)
274
- end
275
-
276
- def attribute_type_from_attribute_and_klass(value, attribute, klass)
277
- if klass.respond_to?(:_decl_props)
278
- (klass._decl_props.has_key?(attribute) && klass._decl_props[attribute][:type]) ? klass._decl_props[attribute][:type] : nil
279
- end
280
- end
281
-
282
- def attribute_type_from_value(value)
283
- value.class
284
- end
285
- end
286
- end
287
- end