neo4r 0.0.3

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.
@@ -0,0 +1,10 @@
1
+ module Neo4r
2
+ # REST API proxy class
3
+ class RestWrapper
4
+ @@rest = Neography::Rest.new
5
+ def method_missing(name, *args, &block)
6
+ # Create rest object everytyime to avoid exception
7
+ @@rest.__send__ name, *args, &block
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,336 @@
1
+ module Neo4r
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>, <tt>index_as</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 writing your own marshalling converter:
11
+ #
12
+ # class Foo
13
+ # include Neo4r::NodeMixin
14
+ # property :thing, type: MyType
15
+ # end
16
+ #
17
+ # module Neo4r::TypeConverters
18
+ # class MyTypeConverter
19
+ # class << self
20
+ # def convert?(type)
21
+ # type == MyType
22
+ # end
23
+ #
24
+ # def to_java(val)
25
+ # "silly:#{val}"
26
+ # end
27
+ #
28
+ # def to_ruby(val)
29
+ # val.sub(/silly:/, '')
30
+ # end
31
+ #
32
+ # def index_as
33
+ # String
34
+ # end
35
+ # end
36
+ # end
37
+ # end
38
+ #
39
+ module TypeConverters
40
+
41
+ # The default converter to use if there isn't a specific converter for the type
42
+ class DefaultConverter
43
+ class << self
44
+
45
+ def to_java(value)
46
+ value
47
+ end
48
+
49
+ def to_ruby(value)
50
+ value
51
+ end
52
+
53
+ def index_as
54
+ String
55
+ end
56
+ end
57
+ end
58
+
59
+
60
+ class BooleanConverter
61
+ class << self
62
+
63
+ def convert?(class_or_symbol)
64
+ :boolean == class_or_symbol
65
+ end
66
+
67
+ def to_java(value)
68
+ return nil if value.nil?
69
+ !!value && value != '0'
70
+ end
71
+
72
+ def to_ruby(value)
73
+ return nil if value.nil?
74
+ !!value && value != '0'
75
+ end
76
+
77
+ def index_as
78
+ String
79
+ end
80
+ end
81
+
82
+ end
83
+
84
+ class SymbolConverter
85
+ class << self
86
+
87
+ def convert?(class_or_symbol)
88
+ :symbol == class_or_symbol || Symbol == class_or_symbol
89
+ end
90
+
91
+ def to_java(value)
92
+ return nil if value.nil?
93
+ value.to_s
94
+ end
95
+
96
+ def to_ruby(value)
97
+ return nil if value.nil?
98
+ value.to_sym
99
+ end
100
+
101
+ def index_as
102
+ String
103
+ end
104
+
105
+ end
106
+ end
107
+
108
+
109
+ class StringConverter
110
+ class << self
111
+
112
+ def convert?(class_or_symbol)
113
+ [String, :string, :text].include? class_or_symbol
114
+ end
115
+
116
+ def to_java(value)
117
+ return nil if value.nil?
118
+ Array === value ? value.map(&:to_s) : value.to_s
119
+ end
120
+
121
+ def to_ruby(value)
122
+ return nil if value.nil?
123
+ value
124
+ end
125
+
126
+ def index_as
127
+ String
128
+ end
129
+
130
+ end
131
+ end
132
+
133
+
134
+
135
+ class FixnumConverter
136
+ class << self
137
+
138
+ def convert?(class_or_symbol)
139
+ Fixnum == class_or_symbol || :fixnum == class_or_symbol || :numeric == class_or_symbol
140
+ end
141
+
142
+ def to_java(value)
143
+ return nil if value.nil?
144
+ Array === value ? value.map(&:to_i) : value.to_i
145
+ end
146
+
147
+ def to_ruby(value)
148
+ return nil if value.nil?
149
+ value#.to_i
150
+ end
151
+
152
+ def index_as
153
+ Fixnum
154
+ end
155
+
156
+ end
157
+ end
158
+
159
+ class FloatConverter
160
+ class << self
161
+
162
+ def convert?(clazz_or_symbol)
163
+ Float == clazz_or_symbol || :float == clazz_or_symbol
164
+ end
165
+
166
+ def to_java(value)
167
+ return nil if value.nil?
168
+ Array === value ? value.map(&:to_f) : value.to_f
169
+ end
170
+
171
+ def to_ruby(value)
172
+ return nil if value.nil?
173
+ value
174
+ end
175
+
176
+ def index_as
177
+ Float
178
+ end
179
+
180
+ end
181
+ end
182
+
183
+ # Converts Date objects to Java long types. Must be timezone UTC.
184
+ class DateConverter
185
+ class << self
186
+
187
+ def convert?(clazz_or_symbol)
188
+ Date == clazz_or_symbol || :date == clazz_or_symbol
189
+ end
190
+
191
+ def to_java(value)
192
+ return nil if value.nil?
193
+ Time.utc(value.year, value.month, value.day).to_i
194
+ end
195
+
196
+ def to_ruby(value)
197
+ return nil if value.nil?
198
+ Time.at(value).utc.to_date
199
+ end
200
+
201
+ def index_as
202
+ Fixnum
203
+ end
204
+
205
+ end
206
+ end
207
+
208
+ # Converts DateTime objects to and from Java long types. Must be timezone UTC.
209
+ class DateTimeConverter
210
+ class << self
211
+
212
+ def convert?(clazz_or_symbol)
213
+ DateTime == clazz_or_symbol || :datetime == clazz_or_symbol
214
+ end
215
+
216
+ # Converts the given DateTime (UTC) value to an Fixnum.
217
+ # DateTime values are automatically converted to UTC.
218
+ def to_java(value)
219
+ return nil if value.nil?
220
+ value = value.new_offset(0) if value.respond_to?(:new_offset)
221
+ if value.class == Date
222
+ Time.utc(value.year, value.month, value.day, 0, 0, 0).to_i
223
+ else
224
+ Time.utc(value.year, value.month, value.day, value.hour, value.min, value.sec).to_i
225
+ end
226
+ end
227
+
228
+ def to_ruby(value)
229
+ return nil if value.nil?
230
+ t = Time.at(value).utc
231
+ DateTime.civil(t.year, t.month, t.day, t.hour, t.min, t.sec)
232
+ end
233
+
234
+ def index_as
235
+ Fixnum
236
+ end
237
+
238
+ end
239
+ end
240
+
241
+ class TimeConverter
242
+ class << self
243
+
244
+ def convert?(clazz_or_symbol)
245
+ Time == clazz_or_symbol || :time == clazz_or_symbol
246
+ end
247
+
248
+ # Converts the given DateTime (UTC) value to an Fixnum.
249
+ # Only utc times are supported !
250
+ def to_java(value)
251
+ return nil if value.nil?
252
+ if value.is_a?(Date)
253
+ Time.utc(value.year, value.month, value.day, 0, 0, 0).to_i
254
+ elsif value.is_a?(String)
255
+ Time.parse(value).utc.to_i
256
+ else
257
+ value.utc.to_i
258
+ end
259
+ end
260
+
261
+ def to_ruby(value)
262
+ return nil if value.nil?
263
+ Time.at(value).utc
264
+ end
265
+
266
+ def index_as
267
+ Fixnum
268
+ end
269
+
270
+ end
271
+ end
272
+
273
+ class << self
274
+
275
+ # Mostly for testing purpose, You can use this method in order to
276
+ # add a converter while the neo4j has already started.
277
+ def converters=(converters)
278
+ @converters = converters
279
+ end
280
+
281
+ # Always returns a converter that handles to_ruby or to_java
282
+ # if +enforce_type+ is set to false then it will raise in case of unknown type
283
+ # otherwise it will return the DefaultConverter.
284
+ def converter(type = nil, enforce_type = true)
285
+ return DefaultConverter unless type
286
+ @converters ||= begin
287
+ Neo4r::TypeConverters.constants.find_all do |c|
288
+ Neo4r::TypeConverters.const_get(c).respond_to?(:convert?)
289
+ end.map do |c|
290
+ Neo4r::TypeConverters.const_get(c)
291
+ end
292
+ end
293
+ found = @converters.find {|c| c.convert?(type) }
294
+ 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
295
+ found or DefaultConverter
296
+ end
297
+
298
+ # Converts the given value to a Java type by using the registered converters.
299
+ # It just looks at the class of the given value unless an attribute name is given.
300
+ def convert(value, attribute = nil, klass = nil, enforce_type = true)
301
+ converter(attribute_type(value, attribute, klass), enforce_type).to_java(value)
302
+ end
303
+
304
+ # Converts the given property (key, value) to Java if there is a type converter for given type.
305
+ # The type must also be declared using Neo4r::NodeMixin#property property_name, type: clazz
306
+ # If no Converter is defined for this value then it simply returns the given value.
307
+ def to_java(clazz, key, value)
308
+ type = clazz._decl_props[key.to_sym] && clazz._decl_props[key.to_sym][:type]
309
+ converter(type).to_java(value)
310
+ end
311
+
312
+ # Converts the given property (key, value) to Ruby if there is a type converter for given type.
313
+ # If no Converter is defined for this value then it simply returns the given value.
314
+ def to_ruby(clazz, key, value)
315
+ type = clazz._decl_props[key.to_sym] && clazz._decl_props[key.to_sym][:type]
316
+ converter(type).to_ruby(value)
317
+ end
318
+
319
+ private
320
+ def attribute_type(value, attribute = nil, klass = nil)
321
+ type = (attribute && klass) ? attribute_type_from_attribute_and_klass(value, attribute, klass) : nil
322
+ type || attribute_type_from_value(value)
323
+ end
324
+
325
+ def attribute_type_from_attribute_and_klass(value, attribute, klass)
326
+ if klass.respond_to?(:_decl_props)
327
+ (klass._decl_props.has_key?(attribute) && klass._decl_props[attribute][:type]) ? klass._decl_props[attribute][:type] : nil
328
+ end
329
+ end
330
+
331
+ def attribute_type_from_value(value)
332
+ value.class
333
+ end
334
+ end
335
+ end
336
+ end
@@ -0,0 +1,3 @@
1
+ module Neo4r
2
+ VERSION = "0.0.3"
3
+ end
@@ -0,0 +1,20 @@
1
+ require 'will_paginate/collection'
2
+ require 'will_paginate/per_page'
3
+ require 'neo4r/paginated'
4
+
5
+ module Neo4r
6
+ # The module provides the common interface for the pagination on any Enumerable class.
7
+ module WillPaginate
8
+ include ::WillPaginate::CollectionMethods
9
+
10
+ def paginate(options = {})
11
+ page = options[:page].to_i || 1
12
+ per_page = options[:per_page].to_i || ::WillPaginate.per_page
13
+ ::WillPaginate::Collection.create(page, per_page) do |pager|
14
+ res = ::Neo4r::Paginated.create_from(self, page, per_page)
15
+ pager.replace res.to_a
16
+ pager.total_entries = res.total unless pager.total_entries
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'neo4r/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "neo4r"
8
+ spec.version = Neo4r::VERSION
9
+ spec.authors = ["Hiroyuki Sato"]
10
+ spec.email = ["hiroyuki_sato@spiber.jp"]
11
+ spec.summary = %q{A graph database for Ruby}
12
+ spec.description = %q{A Neo4j OGM (Object-Graph-Mapper) for use in Ruby on Rails and Rack frameworks heavily inspired by ActiveRecord.}
13
+ spec.homepage = "https://github.com/hiroponz/neo4r"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "pry"
24
+
25
+ spec.add_runtime_dependency "activemodel", "~>3.2"
26
+ spec.add_runtime_dependency "activesupport", "~>3.2"
27
+ spec.add_runtime_dependency "neography", "~>1.6"
28
+ spec.add_runtime_dependency "will_paginate", "~>3.0"
29
+ end
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: neo4r
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Hiroyuki Sato
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activemodel
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.2'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: neography
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.6'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.6'
97
+ - !ruby/object:Gem::Dependency
98
+ name: will_paginate
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ description: A Neo4j OGM (Object-Graph-Mapper) for use in Ruby on Rails and Rack frameworks
112
+ heavily inspired by ActiveRecord.
113
+ email:
114
+ - hiroyuki_sato@spiber.jp
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - lib/neo4r.rb
125
+ - lib/neo4r/attributes.rb
126
+ - lib/neo4r/finder.rb
127
+ - lib/neo4r/index_config_loader.rb
128
+ - lib/neo4r/node.rb
129
+ - lib/neo4r/node_relationship.rb
130
+ - lib/neo4r/node_traverser.rb
131
+ - lib/neo4r/paginated.rb
132
+ - lib/neo4r/property_container.rb
133
+ - lib/neo4r/relation.rb
134
+ - lib/neo4r/relationship.rb
135
+ - lib/neo4r/relationship_traverser.rb
136
+ - lib/neo4r/rest_wrapper.rb
137
+ - lib/neo4r/type_converters.rb
138
+ - lib/neo4r/version.rb
139
+ - lib/neo4r/will_paginate.rb
140
+ - neo4r.gemspec
141
+ homepage: https://github.com/hiroponz/neo4r
142
+ licenses:
143
+ - MIT
144
+ metadata: {}
145
+ post_install_message:
146
+ rdoc_options: []
147
+ require_paths:
148
+ - lib
149
+ required_ruby_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ required_rubygems_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ requirements: []
160
+ rubyforge_project:
161
+ rubygems_version: 2.4.1
162
+ signing_key:
163
+ specification_version: 4
164
+ summary: A graph database for Ruby
165
+ test_files: []