orientdb-ar 0.0.2-jruby

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.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gemspec ADDED
@@ -0,0 +1,85 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = nil
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Adrian Madrid"]
12
+ s.date = %q{2011-01-14}
13
+ s.default_executable = %q{orientdbar_console}
14
+ s.description = %q{TODO: longer description of your gem}
15
+ s.email = %q{aemadrid@gmail.com}
16
+ s.executables = ["orientdbar_console"]
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".gemspec",
24
+ "LICENSE",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "bin/orientdbar_console",
29
+ "lib/model/attributes.rb",
30
+ "lib/model/base.rb",
31
+ "lib/model/conversion.rb",
32
+ "lib/model/embedded.rb",
33
+ "lib/model/validations.rb",
34
+ "lib/orientdb-ar.rb",
35
+ "orientdb-ar.gemspec",
36
+ "spec/base_spec.rb",
37
+ "spec/dirty_spec.rb",
38
+ "spec/lint_behavior.rb",
39
+ "spec/lint_spec.rb",
40
+ "spec/model_spec.rb",
41
+ "spec/models/address.rb",
42
+ "spec/models/customer.rb",
43
+ "spec/models/flo_admin.rb",
44
+ "spec/models/person.rb",
45
+ "spec/models/simple_person.rb",
46
+ "spec/serialization_spec.rb",
47
+ "spec/spec.opts",
48
+ "spec/spec_helper.rb"
49
+ ]
50
+ s.homepage = %q{http://github.com/aemadrid/orientdb-ar}
51
+ s.require_paths = ["lib"]
52
+ s.rubygems_version = %q{1.3.6}
53
+ s.summary = %q{TODO: one-line summary of your gem}
54
+ s.test_files = [
55
+ "spec/base_spec.rb",
56
+ "spec/dirty_spec.rb",
57
+ "spec/lint_behavior.rb",
58
+ "spec/lint_spec.rb",
59
+ "spec/model_spec.rb",
60
+ "spec/models/address.rb",
61
+ "spec/models/customer.rb",
62
+ "spec/models/flo_admin.rb",
63
+ "spec/models/person.rb",
64
+ "spec/models/simple_person.rb",
65
+ "spec/serialization_spec.rb",
66
+ "spec/spec_helper.rb"
67
+ ]
68
+
69
+ if s.respond_to? :specification_version then
70
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
71
+ s.specification_version = 3
72
+
73
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
74
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
75
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
76
+ else
77
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
78
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
79
+ end
80
+ else
81
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
82
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
83
+ end
84
+ end
85
+
data/.gitignore ADDED
@@ -0,0 +1,26 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## RUBYMINE
17
+ .idea
18
+
19
+ ## PROJECT::GENERAL
20
+ coverage
21
+ rdoc
22
+ pkg
23
+ tmp
24
+ .rvmrc
25
+
26
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Adrian Madrid
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = orientdb-ar
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Adrian Madrid. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,58 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
5
+
6
+ begin
7
+ require 'jeweler'
8
+ Jeweler::Tasks.new do |gem|
9
+ gem.name = "orientdb-ar"
10
+ gem.platform = "jruby"
11
+ gem.authors = ["Adrian Madrid"]
12
+ gem.email = ["aemadrid@gmail.com"]
13
+ gem.homepage = "http://rubygems.org/gems/orientdb"
14
+ gem.summary = "ActiveRecord-like persistency through OrientDB in JRuby"
15
+ gem.description = "Active Model wrappers to persist Ruby objects under OrientDB in JRuby."
16
+
17
+ gem.required_rubygems_version = ">= 1.3.6"
18
+ gem.rubyforge_project = "orientdb-ar"
19
+
20
+ gem.add_dependency "orientdb", "0.0.10"
21
+ gem.add_dependency "activemodel", ">= 3.0.3"
22
+ gem.add_development_dependency "awesome_print"
23
+ gem.add_development_dependency "rspec", ">= 2.4"
24
+
25
+ gem.files = `git ls-files`.split("\n")
26
+ gem.test_files = Dir["test/test*.rb"]
27
+ gem.executables = `git ls-files`.split("\n").map { |f| f =~ /^bin\/(.*)/ ? $1 : nil }.compact
28
+ gem.require_path = 'lib'
29
+ end
30
+ Jeweler::GemcutterTasks.new
31
+ rescue LoadError
32
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
33
+ end
34
+
35
+ require 'rspec/core/rake_task'
36
+ RSpec::Core::RakeTask.new(:spec)
37
+
38
+ desc "Run all examples using rcov"
39
+ RSpec::Core::RakeTask.new :rcov => :cleanup_rcov_files do |t|
40
+ t.rcov = true
41
+ t.rcov_opts = %[-Ilib -Ispec --exclude "spec/*,gems/*" --text-report --sort coverage --aggregate coverage.data]
42
+ end
43
+
44
+ task :cleanup_rcov_files do
45
+ rm_rf 'coverage.data'
46
+ end
47
+
48
+ task :spec => :check_dependencies
49
+
50
+ task :default => :spec
51
+
52
+ require 'rake/rdoctask'
53
+ Rake::RDocTask.new do |rdoc|
54
+ rdoc.rdoc_dir = 'rdoc'
55
+ rdoc.title = "orientdb #{version}"
56
+ rdoc.rdoc_files.include('README*')
57
+ rdoc.rdoc_files.include('lib/**/*.rb')
58
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env jruby
2
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/orientdb-ar")
3
+
4
+ if ARGV.include?('test:db')
5
+ GEM_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
6
+ SPEC_ROOT = GEM_ROOT + '/spec'
7
+ TEMP_DIR = File.expand_path Dir.pwd + '/tmp'
8
+
9
+ TEST_DB_PATH = "#{TEMP_DIR}/databases/db_#{rand(999) + 1}"
10
+
11
+ puts ">> GEM_ROOT : #{GEM_ROOT}"
12
+ puts ">> TEST_DB PATH : #{TEST_DB_PATH}"
13
+
14
+ require 'fileutils'
15
+ puts ">> Removing tmp directory #{TEMP_DIR} ..."
16
+ FileUtils.remove_dir TEMP_DIR + '/databases', true
17
+ puts ">> Creating OrientDB test database ..."
18
+ FileUtils.mkdir_p TEST_DB_PATH
19
+ OrientDB::AR::Base.connection = OrientDB::Database.new("local:#{TEST_DB_PATH}/test").create
20
+ DB = OrientDB::AR::Base.connection
21
+ puts ">> Connection : #{OrientDB::AR::Base.connection}"
22
+
23
+ require SPEC_ROOT + '/models/person'
24
+ require SPEC_ROOT + '/models/simple_person'
25
+ require SPEC_ROOT + '/models/address'
26
+ require SPEC_ROOT + '/models/customer'
27
+ require SPEC_ROOT + '/models/flo_admin'
28
+ end
29
+
30
+ require 'irb'
31
+ ARGV.clear
32
+ IRB.start(__FILE__)
@@ -0,0 +1,78 @@
1
+ require 'active_support/core_ext/hash/indifferent_access'
2
+
3
+ module OrientDB::AR
4
+ module Attributes
5
+
6
+ def attribute_names
7
+ schema_names = self.class.fields.keys.map { |x| x.to_s }
8
+ (schema_names + @odocument.field_names.map).uniq
9
+ end
10
+
11
+ def attributes
12
+ attribute_names.inject({}) { |h, attr| h[attr.to_s] = self[attr]; h }
13
+ end
14
+
15
+ def [](attr)
16
+ res = @odocument[attr]
17
+ res.respond_to?(:jruby_value) ? res.jruby_value : res
18
+ end
19
+
20
+ def []=(attr, value)
21
+ old_value = self[attr]
22
+ return old_value if value == old_value
23
+ attribute_will_change!(attr)
24
+ @odocument[attr] = value
25
+ end
26
+
27
+ def changed?
28
+ !changed_attributes.empty?
29
+ end
30
+
31
+ def changed
32
+ changed_attributes.keys
33
+ end
34
+
35
+ def changes
36
+ changed.inject(HashWithIndifferentAccess.new) { |h, attr| h[attr] = attribute_change(attr); h }
37
+ end
38
+
39
+ def previous_changes
40
+ @previously_changed
41
+ end
42
+
43
+ def changed_attributes
44
+ @changed_attributes ||= HashWithIndifferentAccess.new
45
+ end
46
+
47
+ private
48
+
49
+ def attribute_changed?(attr)
50
+ changed_attributes.include?(attr)
51
+ end
52
+
53
+ def attribute_change(attr)
54
+ [changed_attributes[attr], @odocument[attr]] if attribute_changed?(attr)
55
+ end
56
+
57
+ def attribute_was(attr)
58
+ attribute_changed?(attr) ? changed_attributes[attr] : @odocument[attr]
59
+ end
60
+
61
+ def attribute_will_change!(attr)
62
+ return if attribute_changed?(attr)
63
+
64
+ begin
65
+ value = @odocument[attr]
66
+ value = value.duplicable? ? value.clone : value
67
+ rescue TypeError, NoMethodError
68
+ end
69
+
70
+ changed_attributes[attr] = value
71
+ end
72
+
73
+ def reset_attribute!(attr)
74
+ @odocument[attr] = changed_attributes[attr] if attribute_changed?(attr)
75
+ end
76
+
77
+ end
78
+ end
data/lib/model/base.rb ADDED
@@ -0,0 +1,250 @@
1
+ require 'rubygems'
2
+ require 'active_model'
3
+ require 'active_support/core_ext/class/attribute_accessors'
4
+ require 'active_support/core_ext/kernel'
5
+ require 'active_support/core_ext/class/attribute'
6
+ require 'orientdb'
7
+
8
+ require 'model/conversion'
9
+ require 'model/attributes'
10
+ require 'model/validations'
11
+
12
+ class OrientDB::AR::Base
13
+ include ActiveModel::AttributeMethods
14
+ include Comparable
15
+
16
+ extend ActiveModel::Translation
17
+ extend ActiveModel::Callbacks
18
+
19
+ include OrientDB::AR::Attributes
20
+ include OrientDB::AR::Conversion
21
+ include OrientDB::AR::Validations
22
+
23
+ include ActiveModel::Serializers::JSON
24
+ include ActiveModel::Serializers::Xml
25
+
26
+ define_model_callbacks :save, :delete
27
+
28
+ class_attribute :connection
29
+
30
+ class_inheritable_hash :fields
31
+ self.fields = ActiveSupport::OrderedHash.new
32
+
33
+ attr_reader :odocument
34
+
35
+ def initialize(fields = {})
36
+ @odocument = self.class.new_document fields
37
+ @changed_attributes = {}
38
+ @errors = ActiveModel::Errors.new(self)
39
+ end
40
+
41
+ def field?(name)
42
+ res = @odocument.field?(name)
43
+ res
44
+ end
45
+
46
+ def respond_to?(method_name)
47
+ # Simple field value lookup
48
+ return true if field?(method_name)
49
+ # Dirty
50
+ return true if method_name.to_s =~ /(\w*)(_changed\?|_change|_will_change!|_was)$/ && field?($1)
51
+ # Setter
52
+ return true if method_name.to_s =~ /(.*?)=$/
53
+ # Boolean
54
+ return true if method_name.to_s =~ /(.*?)?$/ && field?($1)
55
+ # Unknown pattern
56
+ super
57
+ end
58
+
59
+ def method_missing(method_name, *args, &blk)
60
+ # Simple field value lookup
61
+ return self[method_name] if field?(method_name)
62
+ # Dirty
63
+ if method_name.to_s =~ /(\w*)(_changed\?|_change|_will_change!|_was)$/ && field?($1)
64
+ __send__("attribute#{$2}", $1)
65
+ # Setter
66
+ elsif method_name.to_s =~ /(.*?)=$/
67
+ self[$1] = args.first
68
+ # Boolean
69
+ elsif method_name.to_s =~ /(.*?)?$/ && field?($1)
70
+ !!self[$1]
71
+ # Unknown pattern
72
+ else
73
+ super
74
+ end
75
+ end
76
+
77
+ def save
78
+ _run_save_callbacks do
79
+ @odocument.save
80
+ @saved = true
81
+ @previously_changed = @changed_attributes
82
+ @changed_attributes.clear
83
+ end
84
+ true
85
+ end
86
+
87
+ def delete
88
+ _run_delete_callbacks do
89
+ @odocument.delete
90
+ @deleted = true
91
+ end
92
+ true
93
+ end
94
+
95
+ def saved?
96
+ @saved || @odocument.rid != '-1:-1'
97
+ end
98
+
99
+ def deleted?
100
+ @deleted ||= false
101
+ end
102
+
103
+ def persisted?
104
+ saved? && !deleted?
105
+ end
106
+
107
+ def inspect
108
+ attrs = attributes.map { |k, v| "#{k}:#{v.inspect}" }.join(' ')
109
+ super_klass = self.class.descends_from_base? ? '' : "(#{self.class.superclass.name})"
110
+ %{#<#{self.class.name}#{super_klass}:#{@odocument.rid} #{attrs}>}
111
+ end
112
+
113
+ alias :to_s :inspect
114
+
115
+ def <=>(other)
116
+ to_s <=> other.to_s
117
+ end
118
+
119
+ class << self
120
+
121
+ attr_writer :oclass_name
122
+
123
+ def oclass_name
124
+ @oclass_name ||= name.to_s
125
+ end
126
+
127
+ def oclass
128
+ unless defined?(@oclass)
129
+ options = {}
130
+ unless descends_from_base?
131
+ super_oclass = superclass.oclass
132
+ options[:super] = super_oclass
133
+ options[:use_cluster] = super_oclass.cluster_ids.first
134
+ end
135
+ @oclass = connection.get_or_create_class oclass_name, options
136
+ end
137
+ @oclass
138
+ end
139
+
140
+ def field(name, type, options = {})
141
+ name = name.to_sym
142
+ if fields.key? name
143
+ puts "Already defined field [#{name}]"
144
+ else
145
+ fields[name] = {:type => type}.update options
146
+ end
147
+ end
148
+
149
+ def descends_from_base?
150
+ superclass && superclass == OrientDB::AR::Base
151
+ end
152
+
153
+ def schema!
154
+ fields.each do |field, options|
155
+ oclass.add field, options[:type], options.except(:type)
156
+ end
157
+ end
158
+
159
+ def new_document(fields = {})
160
+ oclass
161
+ OrientDB::Document.new connection, oclass_name, fields
162
+ end
163
+
164
+ def create(fields = {})
165
+ obj = new fields
166
+ obj.save
167
+ obj
168
+ end
169
+
170
+ def select(*args)
171
+ OrientDB::AR::Query.new(self).select(*args)
172
+ end
173
+
174
+ alias :columns :select
175
+
176
+ def where(*args)
177
+ OrientDB::AR::Query.new(self).where(*args)
178
+ end
179
+
180
+ def order(*args)
181
+ OrientDB::AR::Query.new(self).order(*args)
182
+ end
183
+
184
+ def limit(max_records)
185
+ OrientDB::AR::Query.new(self).limit(max_records)
186
+ end
187
+
188
+ def range(lower_rid, upper_rid = nil)
189
+ OrientDB::AR::Query.new(self).range(lower_rid, upper_rid)
190
+ end
191
+
192
+ def all(conditions = {})
193
+ OrientDB::AR::Query.new(self).where(conditions).all
194
+ end
195
+
196
+ def first(conditions = {})
197
+ OrientDB::AR::Query.new(self).where(conditions).first
198
+ end
199
+
200
+ def clear
201
+ oclass.truncate
202
+ end
203
+
204
+ def new_from_doc(doc)
205
+ klass = doc.getClassName.constantize
206
+ obj = klass.new
207
+ obj.instance_variable_set "@odocument", doc
208
+ obj
209
+ end
210
+ end
211
+
212
+ end
213
+
214
+ class OrientDB::AR::Query
215
+
216
+ attr_accessor :model, :query
217
+
218
+ def initialize(model, query = OrientDB::SQL::Query.new)
219
+ @model, @query = model, query
220
+ @query.from model.name
221
+ end
222
+
223
+ %w{ select select! where where! and or and_not or_not order order! limit limit! range range! }.each do |name|
224
+ define_method(name) do |*args|
225
+ query.send name, *args
226
+ self
227
+ end
228
+ end
229
+
230
+ def all
231
+ model.connection.query(query).map { |doc| model.new_from_doc doc }
232
+ end
233
+
234
+ def first
235
+ model.new_from_doc model.connection.first(query)
236
+ end
237
+
238
+ def results
239
+ model.connection.query(query).map
240
+ end
241
+
242
+ def inspect
243
+ %{#<OrientDB::AR::Query:#{model.name} query="#{query.to_s}">}
244
+ end
245
+
246
+ alias :to_s :inspect
247
+
248
+ end
249
+
250
+ OrientDB::AR::Base.include_root_in_json = false
@@ -0,0 +1,17 @@
1
+ module OrientDB::AR
2
+ module Conversion
3
+
4
+ def to_model
5
+ self
6
+ end
7
+
8
+ def to_key
9
+ persisted? ? @odocument.rid.split(':') : nil
10
+ end
11
+
12
+ def to_param
13
+ persisted? ? to_key.join('-') : nil
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,83 @@
1
+ require 'model/attributes'
2
+ require 'model/validations'
3
+
4
+ class OrientDB::AR::Embedded
5
+ extend ActiveModel::Translation
6
+
7
+ include OrientDB::AR::Attributes
8
+ include OrientDB::AR::Conversion
9
+ include OrientDB::AR::Validations
10
+
11
+ def initialize(fields = {})
12
+ @odocument = self.class.new_document fields
13
+ @changed_attributes = {}
14
+ @errors = ActiveModel::Errors.new(self)
15
+ end
16
+
17
+ def field?(name)
18
+ @odocument.field?(name)
19
+ end
20
+
21
+ def respond_to?(method_name)
22
+ # Simple field value lookup
23
+ return true if field?(method_name)
24
+ # Dirty
25
+ return true if method_name.to_s =~ /(\w*)(_changed\?|_change|_will_change!|_was)$/ && field?($1)
26
+ # Setter
27
+ return true if method_name.to_s =~ /(.*?)=$/
28
+ # Boolean
29
+ return true if method_name.to_s =~ /(.*?)?$/ && field?($1)
30
+ # Unknown pattern
31
+ super
32
+ end
33
+
34
+ def method_missing(method_name, *args, &blk)
35
+ # Simple field value lookup
36
+ return self[method_name] if field?(method_name)
37
+ # Dirty
38
+ if method_name.to_s =~ /(\w*)(_changed\?|_change|_will_change!|_was)$/ && field?($1)
39
+ __send__("attribute#{$2}", $1)
40
+ # Setter
41
+ elsif method_name.to_s =~ /(.*?)=$/
42
+ self[$1] = args.first
43
+ # Boolean
44
+ elsif method_name.to_s =~ /(.*?)?$/ && field?($1)
45
+ !!self[$1]
46
+ # Unknown pattern
47
+ else
48
+ super
49
+ end
50
+ end
51
+
52
+ class << self
53
+
54
+ attr_writer :oclass_name
55
+
56
+ def oclass_name
57
+ @oclass_name ||= name.downcase.gsub('::', '_')
58
+ end
59
+
60
+ def oclass
61
+ @oclass ||= connection.get_or_create_class oclass_name
62
+ end
63
+
64
+ def fields
65
+ @fields ||= ActiveSupport::OrderedHash.new
66
+ end
67
+
68
+ def field(name, type, options = {})
69
+ name = name.to_sym
70
+ if fields.key? name
71
+ puts "Already defined field [#{name}]"
72
+ else
73
+ fields[name] = {:type => type}.update options
74
+ end
75
+ end
76
+
77
+ def new_document(fields = {})
78
+ oclass
79
+ OrientDB::Document.new connection, oclass_name, fields
80
+ end
81
+
82
+ end
83
+ end
@@ -0,0 +1,11 @@
1
+ module OrientDB::AR
2
+ module Validations
3
+
4
+ attr_reader :errors
5
+
6
+ def valid?
7
+ true
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+
3
+ module OrientDB
4
+ module AR
5
+ end
6
+ end
7
+
8
+ require 'model/base'
9
+ require 'model/embedded'
@@ -0,0 +1,80 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{orientdb-ar}
8
+ s.version = "0.0.2"
9
+ s.platform = %q{jruby}
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.3.6") if s.respond_to? :required_rubygems_version=
12
+ s.authors = ["Adrian Madrid"]
13
+ s.date = %q{2011-01-17}
14
+ s.default_executable = %q{orientdbar_console}
15
+ s.description = %q{Active Model wrappers to persist Ruby objects under OrientDB in JRuby.}
16
+ s.email = ["aemadrid@gmail.com"]
17
+ s.executables = ["orientdbar_console"]
18
+ s.extra_rdoc_files = [
19
+ "LICENSE",
20
+ "README.rdoc"
21
+ ]
22
+ s.files = [
23
+ ".document",
24
+ ".gemspec",
25
+ ".gitignore",
26
+ "LICENSE",
27
+ "README.rdoc",
28
+ "Rakefile",
29
+ "VERSION",
30
+ "bin/orientdbar_console",
31
+ "lib/model/attributes.rb",
32
+ "lib/model/base.rb",
33
+ "lib/model/conversion.rb",
34
+ "lib/model/embedded.rb",
35
+ "lib/model/validations.rb",
36
+ "lib/orientdb-ar.rb",
37
+ "orientdb-ar.gemspec",
38
+ "spec/base_spec.rb",
39
+ "spec/dirty_spec.rb",
40
+ "spec/lint_behavior.rb",
41
+ "spec/lint_spec.rb",
42
+ "spec/model_spec.rb",
43
+ "spec/models/address.rb",
44
+ "spec/models/customer.rb",
45
+ "spec/models/flo_admin.rb",
46
+ "spec/models/person.rb",
47
+ "spec/models/simple_person.rb",
48
+ "spec/serialization_spec.rb",
49
+ "spec/spec.opts",
50
+ "spec/spec_helper.rb"
51
+ ]
52
+ s.homepage = %q{http://rubygems.org/gems/orientdb}
53
+ s.require_paths = ["lib"]
54
+ s.rubyforge_project = %q{orientdb-ar}
55
+ s.rubygems_version = %q{1.3.6}
56
+ s.summary = %q{ActiveRecord-like persistency through OrientDB in JRuby}
57
+
58
+ if s.respond_to? :specification_version then
59
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
60
+ s.specification_version = 3
61
+
62
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
63
+ s.add_runtime_dependency(%q<orientdb>, ["= 0.0.10"])
64
+ s.add_runtime_dependency(%q<activemodel>, [">= 3.0.3"])
65
+ s.add_development_dependency(%q<awesome_print>, [">= 0"])
66
+ s.add_development_dependency(%q<rspec>, [">= 2.4"])
67
+ else
68
+ s.add_dependency(%q<orientdb>, ["= 0.0.10"])
69
+ s.add_dependency(%q<activemodel>, [">= 3.0.3"])
70
+ s.add_dependency(%q<awesome_print>, [">= 0"])
71
+ s.add_dependency(%q<rspec>, [">= 2.4"])
72
+ end
73
+ else
74
+ s.add_dependency(%q<orientdb>, ["= 0.0.10"])
75
+ s.add_dependency(%q<activemodel>, [">= 3.0.3"])
76
+ s.add_dependency(%q<awesome_print>, [">= 0"])
77
+ s.add_dependency(%q<rspec>, [">= 2.4"])
78
+ end
79
+ end
80
+
data/spec/base_spec.rb ADDED
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Base" do
4
+ it "should have a valid connection" do
5
+ OrientDB::AR::Base.connection.should be_a_kind_of OrientDB::Database
6
+ end
7
+ end
@@ -0,0 +1,33 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "ActiveModel" do
4
+
5
+ describe "Dirty" do
6
+
7
+ it "should comply" do
8
+ @person = Person.create :name => "Uncle Bob", :age => 45
9
+ @person.changed?.should == false
10
+
11
+ @person.name = 'Bob'
12
+ @person.changed?.should == true
13
+ @person.name_changed?.should == true
14
+ @person.name_was.should == 'Uncle Bob'
15
+ @person.name_change.should == ['Uncle Bob', 'Bob']
16
+ @person.name = 'Bill'
17
+ @person.name_change.should == ['Uncle Bob', 'Bill']
18
+
19
+ @person.save
20
+ @person.changed?.should == false
21
+ @person.name_changed?.should == false
22
+
23
+ @person.name = 'Bill'
24
+ @person.name_changed?.should == false
25
+ @person.name_change.should == nil
26
+
27
+ @person.name = 'Bob'
28
+ @person.changed.should == ['name']
29
+ @person.changes.should == {'name' => ['Bill', 'Bob']}
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,69 @@
1
+ # Stolen from https://github.com/sinefunc/rspec-rails-matchers/raw/master/lib/rspec_rails_matchers/behavior/lint.rb
2
+
3
+ module RspecRailsMatchers
4
+ module Behavior
5
+ module Lint
6
+ share_as :AnActiveModel do
7
+ def be_a_boolean
8
+ Rspec::Matchers::Matcher.new :be_a_boolean do
9
+ match do |value|
10
+ [ true, false ].include?( value )
11
+ end
12
+ end
13
+ end
14
+
15
+ it { should respond_to(:to_model) }
16
+ it { should respond_to(:to_key) }
17
+ it { should respond_to(:to_param) }
18
+
19
+ it { should respond_to(:valid?) }
20
+ its(:valid?) { should be_a_boolean }
21
+
22
+ it { should respond_to(:persisted?) }
23
+ its(:persisted?) { should be_a_boolean }
24
+
25
+ its(:class) { should respond_to(:model_name) }
26
+
27
+ context "the model name" do
28
+ def subject
29
+ super.class.model_name
30
+ end
31
+
32
+ it { should be_kind_of(String) }
33
+ its(:human) { should be_kind_of(String) }
34
+ its(:partial_path) { should be_kind_of(String) }
35
+ its(:singular) { should be_kind_of(String) }
36
+ its(:plural) { should be_kind_of(String) }
37
+ end
38
+
39
+ context "when its not persisted" do
40
+ def subject
41
+ obj = super.dup
42
+ obj.stub!(:persisted?).and_return(false)
43
+ obj
44
+ end
45
+
46
+ its(:to_key) { should be_nil }
47
+ its(:to_param) { should be_nil }
48
+ end
49
+
50
+ it { should respond_to(:errors) }
51
+
52
+ context "errors" do
53
+ def subject
54
+ super.errors
55
+ end
56
+
57
+ it { should respond_to(:[]) }
58
+ it { should respond_to(:full_messages) }
59
+
60
+ context "[:hello]" do
61
+ it { subject[:hello].should be_an_instance_of(Array) }
62
+
63
+ its(:full_messages) { should be_an_instance_of(Array) }
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
data/spec/lint_spec.rb ADDED
@@ -0,0 +1,55 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "ActiveModel" do
4
+
5
+ describe Person do
6
+ it_should_behave_like AnActiveModel
7
+ end
8
+
9
+ describe "Dirty" do
10
+
11
+ it "should comply" do
12
+ @person = Person.create :name => "Uncle Bob", :age => 45
13
+ @person.changed?.should == false
14
+
15
+ @person.name = 'Bob'
16
+ @person.changed?.should == true
17
+ @person.name_changed?.should == true
18
+ @person.name_was.should == 'Uncle Bob'
19
+ @person.name_change.should == ['Uncle Bob', 'Bob']
20
+ @person.name = 'Bill'
21
+ @person.name_change.should == ['Uncle Bob', 'Bill']
22
+
23
+ @person.save
24
+ @person.changed?.should == false
25
+ @person.name_changed?.should == false
26
+
27
+ @person.name = 'Bill'
28
+ @person.name_changed?.should == false
29
+ @person.name_change.should == nil
30
+
31
+ @person.name = 'Bob'
32
+ @person.changed.should == ['name']
33
+ @person.changes.should == {'name' => ['Bill', 'Bob']}
34
+ end
35
+ end
36
+
37
+ describe "Serialization" do
38
+
39
+ it "should comply" do
40
+ @person = SimplePerson.new
41
+ @person.serializable_hash.should == {"name" => nil}
42
+ @person.as_json.should == {"name" => nil}
43
+ @person.to_json.should == %{{"name":null}}
44
+ @person.to_xml.should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<simple-person>\n <name type=\"yaml\" nil=\"true\"></name>\n</simple-person>\n"
45
+
46
+ @person.name = "Bob"
47
+ @person.serializable_hash.should == {"name" => "Bob"}
48
+ @person.as_json.should == {"name" => "Bob"}
49
+ @person.to_json.should == %{{"name":"Bob"}}
50
+ @person.to_xml.should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<simple-person>\n <name>Bob</name>\n</simple-person>\n"
51
+ end
52
+
53
+ end
54
+
55
+ end
@@ -0,0 +1,45 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Model" do
4
+ it "should have a valid connection" do
5
+ Person.connection.should be_a_kind_of OrientDB::Database
6
+ end
7
+
8
+ it "should have the right schema" do
9
+ Person.new.should be_a_kind_of Person
10
+ Person.oclass_name.should == 'Person'
11
+ Person.fields.keys.should == [:name, :age, :tags]
12
+ Person.fields[:name].should == {:type => :string, :not_null => true}
13
+ Person.fields[:age].should == {:type => :int}
14
+ Person.fields[:tags].should == {:type => [:list, :string]}
15
+ end
16
+
17
+ it "should create working models" do
18
+ p = Person.create :name => "Tester Testing", :age => 45, :tags => %w{ tech_support rocks }
19
+
20
+ p.name.should == "Tester Testing"
21
+ p.age.should == 45
22
+ p.tags.should == %w{ tech_support rocks }
23
+
24
+ p2 = Person.first :name => "Tester Testing"
25
+ p2.name.should == p.name
26
+ p2.age.should == p.age
27
+ p2.tags.map.should == p.tags
28
+ end
29
+
30
+ it "should find models" do
31
+ OrientDB::SQL.monkey_patch! Symbol
32
+ Person.clear
33
+
34
+ p1 = Person.create :name => "Hans Solo", :age => 38, :tags => %w{ fighter pilot }
35
+ p2 = Person.create :name => "Yoda", :age => 387, :tags => %w{ genius jedi }
36
+ p3 = Person.create :name => "Luke Skywalker", :age => 28, :tags => %w{ jedi fighter pilot }
37
+ p4 = Person.create :name => "Princess Leia", :age => 28, :tags => %w{ fighter royalty }
38
+ p5 = Person.create :name => "Chewbaca", :age => 53, :tags => %w{ fighter brave hairy }
39
+
40
+ Person.where(:name.like('%w%')).all.should == [p3, p5]
41
+ Person.where(:age.gt(28), :age.lt(75)).all.should == [p1, p5]
42
+ Person.where("'jedi' IN tags").all.should == [p2, p3]
43
+ Person.where("'fighter' IN tags", :age.lte(28)).order(:name.desc).all.first.should == p4
44
+ end
45
+ end
@@ -0,0 +1,7 @@
1
+ class Address < OrientDB::AR::Embedded
2
+ field :street, :string
3
+ field :city, :string
4
+ field :state, :string
5
+ field :zip, :string
6
+ field :country, :string
7
+ end
@@ -0,0 +1,5 @@
1
+ class Customer < Person
2
+ field :number, :int, :not_null => true
3
+ # field :address, Address
4
+ end
5
+ Customer.schema!
@@ -0,0 +1,7 @@
1
+ module Flo
2
+ class Admin < ::Person
3
+ field :login, :string
4
+ field :password, :string
5
+ end
6
+ end
7
+ Flo::Admin.schema!
@@ -0,0 +1,6 @@
1
+ class Person < OrientDB::AR::Base
2
+ field :name, :string, :not_null => true
3
+ field :age, :int
4
+ field :tags, [:list, :string]
5
+ end
6
+ Person.schema!
@@ -0,0 +1,5 @@
1
+ class SimplePerson < OrientDB::AR::Base
2
+ field :name, :string
3
+ end
4
+
5
+ SimplePerson.schema!
@@ -0,0 +1,23 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "ActiveModel" do
4
+
5
+ describe "Serialization" do
6
+
7
+ it "should comply" do
8
+ @person = SimplePerson.new
9
+ @person.serializable_hash.should == {"name" => nil}
10
+ @person.as_json.should == {"name" => nil}
11
+ @person.to_json.should == %{{"name":null}}
12
+ @person.to_xml.should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<simple-person>\n <name type=\"yaml\" nil=\"true\"></name>\n</simple-person>\n"
13
+
14
+ @person.name = "Bob"
15
+ @person.serializable_hash.should == {"name" => "Bob"}
16
+ @person.as_json.should == {"name" => "Bob"}
17
+ @person.to_json.should == %{{"name":"Bob"}}
18
+ @person.to_xml.should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<simple-person>\n <name>Bob</name>\n</simple-person>\n"
19
+ end
20
+
21
+ end
22
+
23
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,47 @@
1
+ unless defined?(SPEC_HELPER_LOADED)
2
+
3
+ GEM_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
4
+ LIB_ROOT = GEM_ROOT + '/lib'
5
+ SPEC_ROOT = GEM_ROOT + '/spec'
6
+ TEMP_DIR = GEM_ROOT + '/tmp'
7
+
8
+ puts ">> GEM_ROOT : #{GEM_ROOT}"
9
+
10
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
11
+ $LOAD_PATH.unshift(LIB_ROOT) unless $LOAD_PATH.include?(LIB_ROOT)
12
+
13
+ require 'orientdb-ar'
14
+ require 'rspec'
15
+ #require 'rspec/autorun'
16
+ require 'fileutils'
17
+
18
+ TEST_DB_PATH = "#{TEMP_DIR}/test/db_#{rand(999) + 1}"
19
+ puts ">> TEST_DB PATH : #{TEST_DB_PATH}"
20
+
21
+ require 'fileutils'
22
+ puts ">> Removing tmp directory #{TEMP_DIR} ..."
23
+ FileUtils.remove_dir TEMP_DIR + '/test', true
24
+ puts ">> Creating OrientDB database..."
25
+ FileUtils.mkdir_p TEST_DB_PATH
26
+ OrientDB::AR::Base.connection = OrientDB::Database.new("local:#{TEST_DB_PATH}/test").create
27
+ puts ">> Connection : #{OrientDB::AR::Base.connection}"
28
+
29
+ require SPEC_ROOT + '/models/person'
30
+ require SPEC_ROOT + '/models/simple_person'
31
+ require SPEC_ROOT + '/models/address'
32
+ require SPEC_ROOT + '/models/customer'
33
+
34
+ require SPEC_ROOT + '/lint_behavior'
35
+
36
+ module Helpers
37
+ end
38
+
39
+ RSpec.configure do |config|
40
+ config.include Helpers
41
+ config.include RspecRailsMatchers::Behavior::Lint
42
+
43
+ config.color_enabled = true
44
+ end
45
+
46
+ SPEC_HELPER_LOADED = true
47
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: orientdb-ar
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 2
9
+ version: 0.0.2
10
+ platform: jruby
11
+ authors:
12
+ - Adrian Madrid
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-17 00:00:00 -07:00
18
+ default_executable: orientdbar_console
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: orientdb
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 0
30
+ - 10
31
+ version: 0.0.10
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: activemodel
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 3
43
+ - 0
44
+ - 3
45
+ version: 3.0.3
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: awesome_print
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ type: :development
59
+ version_requirements: *id003
60
+ - !ruby/object:Gem::Dependency
61
+ name: rspec
62
+ prerelease: false
63
+ requirement: &id004 !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 2
69
+ - 4
70
+ version: "2.4"
71
+ type: :development
72
+ version_requirements: *id004
73
+ description: Active Model wrappers to persist Ruby objects under OrientDB in JRuby.
74
+ email:
75
+ - aemadrid@gmail.com
76
+ executables:
77
+ - orientdbar_console
78
+ extensions: []
79
+
80
+ extra_rdoc_files:
81
+ - LICENSE
82
+ - README.rdoc
83
+ files:
84
+ - .document
85
+ - .gemspec
86
+ - .gitignore
87
+ - LICENSE
88
+ - README.rdoc
89
+ - Rakefile
90
+ - VERSION
91
+ - bin/orientdbar_console
92
+ - lib/model/attributes.rb
93
+ - lib/model/base.rb
94
+ - lib/model/conversion.rb
95
+ - lib/model/embedded.rb
96
+ - lib/model/validations.rb
97
+ - lib/orientdb-ar.rb
98
+ - orientdb-ar.gemspec
99
+ - spec/base_spec.rb
100
+ - spec/dirty_spec.rb
101
+ - spec/lint_behavior.rb
102
+ - spec/lint_spec.rb
103
+ - spec/model_spec.rb
104
+ - spec/models/address.rb
105
+ - spec/models/customer.rb
106
+ - spec/models/flo_admin.rb
107
+ - spec/models/person.rb
108
+ - spec/models/simple_person.rb
109
+ - spec/serialization_spec.rb
110
+ - spec/spec.opts
111
+ - spec/spec_helper.rb
112
+ has_rdoc: true
113
+ homepage: http://rubygems.org/gems/orientdb
114
+ licenses: []
115
+
116
+ post_install_message:
117
+ rdoc_options: []
118
+
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ segments:
126
+ - 0
127
+ version: "0"
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ segments:
133
+ - 1
134
+ - 3
135
+ - 6
136
+ version: 1.3.6
137
+ requirements: []
138
+
139
+ rubyforge_project: orientdb-ar
140
+ rubygems_version: 1.3.6
141
+ signing_key:
142
+ specification_version: 3
143
+ summary: ActiveRecord-like persistency through OrientDB in JRuby
144
+ test_files: []
145
+