orientdb-ar 0.0.3-jruby → 0.0.4-jruby

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -17,7 +17,7 @@ begin
17
17
  gem.required_rubygems_version = ">= 1.3.6"
18
18
  gem.rubyforge_project = "orientdb-ar"
19
19
 
20
- gem.add_dependency "orientdb", "0.0.11"
20
+ gem.add_dependency "orientdb", "0.0.15"
21
21
  gem.add_dependency "activemodel", ">= 3.0.3"
22
22
  gem.add_development_dependency "awesome_print"
23
23
  gem.add_development_dependency "rspec", ">= 2.4"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.3
1
+ 0.0.4
@@ -16,7 +16,7 @@ if ARGV.include?('test:db')
16
16
  FileUtils.remove_dir TEMP_DIR + '/databases', true
17
17
  puts ">> Creating OrientDB test database ..."
18
18
  FileUtils.mkdir_p TEST_DB_PATH
19
- OrientDB::AR::Base.connection = OrientDB::Database.new("local:#{TEST_DB_PATH}/test").create
19
+ OrientDB::AR::Base.connection = OrientDB::DocumentDatabase.new("local:#{TEST_DB_PATH}/test").create
20
20
  DB = OrientDB::AR::Base.connection
21
21
  puts ">> Connection : #{OrientDB::AR::Base.connection}"
22
22
 
@@ -5,19 +5,25 @@ module OrientDB::AR
5
5
 
6
6
  def attribute_names
7
7
  schema_names = self.class.fields.keys.map { |x| x.to_s }
8
- (schema_names + @odocument.field_names.map).uniq
8
+ field_names = @odocument.field_names.map { |x| x.to_s }
9
+ (schema_names + field_names).uniq
9
10
  end
10
11
 
11
12
  def attributes
12
13
  attribute_names.inject({}) { |h, attr| h[attr.to_s] = self[attr]; h }
13
14
  end
14
15
 
16
+ def to_orientdb
17
+ @odocument
18
+ end
19
+
15
20
  def [](attr)
16
21
  res = @odocument[attr]
17
- res.respond_to?(:jruby_value) ? res.jruby_value : res
22
+ res.respond_to?(:to_orientdb_ar) ? res.to_orientdb_ar : res
18
23
  end
19
24
 
20
25
  def []=(attr, value)
26
+ value = value.respond_to?(:to_orientdb) ? value.to_orientdb : value
21
27
  old_value = self[attr]
22
28
  return old_value if value == old_value
23
29
  attribute_will_change!(attr)
@@ -1,81 +1,18 @@
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
- require 'model/relations'
12
- require 'model/query'
13
-
14
1
  class OrientDB::AR::Base
15
- include ActiveModel::AttributeMethods
16
- include Comparable
17
-
18
- extend ActiveModel::Translation
19
- extend ActiveModel::Callbacks
20
2
 
21
- include OrientDB::AR::Attributes
22
- include OrientDB::AR::Conversion
23
- include OrientDB::AR::Validations
24
-
25
- include ActiveModel::Serializers::JSON
26
- include ActiveModel::Serializers::Xml
27
-
28
- define_model_callbacks :save, :delete
3
+ def self.embeddable?
4
+ false
5
+ end
29
6
 
30
- class_attribute :connection
7
+ include ActiveModel::AttributeMethods
8
+ include OrientDB::AR::DocumentMixin
31
9
 
32
10
  class_inheritable_hash :fields
33
11
  self.fields = ActiveSupport::OrderedHash.new
34
12
 
35
- attr_reader :odocument
36
-
37
- def initialize(fields = {})
38
- @odocument = self.class.new_document
39
- @changed_attributes = {}
40
- @errors = ActiveModel::Errors.new(self)
41
- fields.each { |k, v| send "#{k}=", v }
42
- end
43
-
44
- def field?(name)
45
- res = @odocument.field?(name)
46
- res
47
- end
48
-
49
- def respond_to?(method_name)
50
- # Simple field value lookup
51
- return true if field?(method_name)
52
- # Dirty
53
- return true if method_name.to_s =~ /(\w*)(_changed\?|_change|_will_change!|_was)$/ && field?($1)
54
- # Setter
55
- return true if method_name.to_s =~ /(.*?)=$/
56
- # Boolean
57
- return true if method_name.to_s =~ /(.*?)?$/ && field?($1)
58
- # Unknown pattern
59
- super
60
- end
13
+ class_attribute :connection
61
14
 
62
- def method_missing(method_name, *args, &blk)
63
- # Simple field value lookup
64
- return self[method_name] if field?(method_name)
65
- # Dirty
66
- if method_name.to_s =~ /(\w*)(_changed\?|_change|_will_change!|_was)$/ && field?($1)
67
- __send__("attribute#{$2}", $1)
68
- # Setter
69
- elsif method_name.to_s =~ /(.*?)=$/
70
- self[$1] = args.first
71
- # Boolean
72
- elsif method_name.to_s =~ /(.*?)?$/ && field?($1)
73
- !!self[$1]
74
- # Unknown pattern
75
- else
76
- super
77
- end
78
- end
15
+ define_model_callbacks :save, :delete
79
16
 
80
17
  def save
81
18
  _run_save_callbacks do
@@ -95,6 +32,14 @@ class OrientDB::AR::Base
95
32
  true
96
33
  end
97
34
 
35
+ def reload
36
+ raise "Not persisted, cannot reload" unless persisted?
37
+ @odocument = OrientDB::AR::Query.new(self.class).where('@rid' => rid.lit).first_result
38
+ @changed_attributes = { }
39
+ @errors = ActiveModel::Errors.new(self)
40
+ self
41
+ end
42
+
98
43
  def saved?
99
44
  @saved || @odocument.rid != '-1:-1'
100
45
  end
@@ -107,18 +52,6 @@ class OrientDB::AR::Base
107
52
  saved? && !deleted?
108
53
  end
109
54
 
110
- def inspect
111
- attrs = attributes.map { |k, v| "#{k}:#{v.inspect}" }.join(' ')
112
- super_klass = self.class.descends_from_base? ? '' : "(#{self.class.superclass.name})"
113
- %{#<#{self.class.name}#{super_klass}:#{@odocument.rid} #{attrs}>}
114
- end
115
-
116
- alias :to_s :inspect
117
-
118
- def <=>(other)
119
- to_s <=> other.to_s
120
- end
121
-
122
55
  class << self
123
56
 
124
57
  include OrientDB::AR::Relations
@@ -142,10 +75,6 @@ class OrientDB::AR::Base
142
75
  @oclass
143
76
  end
144
77
 
145
- def embedded?
146
- false
147
- end
148
-
149
78
  def field(name, type, options = {})
150
79
  name = name.to_sym
151
80
  if fields.key? name
@@ -166,11 +95,6 @@ class OrientDB::AR::Base
166
95
  self
167
96
  end
168
97
 
169
- def new_document(fields = {})
170
- oclass
171
- OrientDB::Document.new connection, oclass_name, fields
172
- end
173
-
174
98
  def create(fields = {})
175
99
  obj = new fields
176
100
  obj.save
@@ -207,19 +131,24 @@ class OrientDB::AR::Base
207
131
  OrientDB::AR::Query.new(self).where(conditions).first
208
132
  end
209
133
 
210
- def clear
211
- oclass.truncate
134
+ def update(*args)
135
+ OrientDB::AR::Update.new(self).values(*args).run
212
136
  end
213
137
 
214
- def new_from_doc(doc)
215
- klass = doc.getClassName.constantize
216
- obj = klass.new
217
- obj.instance_variable_set "@odocument", doc
218
- obj
138
+ def delete(*args)
139
+ OrientDB::AR::Delete.new(self).where(*args).run
140
+ end
141
+
142
+ def insert(*args)
143
+ from_orientdb OrientDB::AR::Insert.new(self).fields(*args).run
144
+ end
145
+
146
+ def count
147
+ oclass.count
219
148
  end
220
149
 
221
- def new_from_docs(docs)
222
- docs.map { |doc| new_from_doc doc }
150
+ def clear
151
+ oclass.truncate
223
152
  end
224
153
  end
225
154
 
File without changes
@@ -0,0 +1,144 @@
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 'active_support/core_ext/class/inheritable_attributes'
7
+ require 'orientdb'
8
+
9
+ require 'orientdb-ar/conversion'
10
+ require 'orientdb-ar/attributes'
11
+ require 'orientdb-ar/validations'
12
+ require 'orientdb-ar/relations'
13
+ require 'orientdb-ar/sql'
14
+ module OrientDB::AR::DocumentMixin
15
+
16
+ include Comparable
17
+
18
+ include OrientDB::AR::Attributes
19
+ include OrientDB::AR::Conversion
20
+ include OrientDB::AR::Validations
21
+
22
+ def self.included(base)
23
+ base.extend ActiveModel::Translation
24
+ base.extend ActiveModel::Callbacks unless base.embeddable?
25
+
26
+ base.send :include, ActiveModel::Serializers::JSON
27
+ base.send :include, ActiveModel::Serializers::Xml
28
+
29
+ base.extend OrientDB::AR::DocumentMixin::ClassMethods
30
+ end
31
+
32
+ attr_reader :odocument
33
+
34
+ def initialize(fields = { })
35
+ @odocument = self.class.new_document
36
+ @changed_attributes = { }
37
+ @errors = ActiveModel::Errors.new(self)
38
+ fields.each { |k, v| send "#{k}=", v }
39
+ end
40
+
41
+ def field?(name)
42
+ @odocument.field?(name)
43
+ end
44
+
45
+ def respond_to?(method_name)
46
+ # Simple field value lookup
47
+ return true if field?(method_name)
48
+ # Dirty
49
+ return true if method_name.to_s =~ /(\w*)(_changed\?|_change|_will_change!|_was)$/ && field?($1)
50
+ # Setter
51
+ return true if method_name.to_s =~ /(.*?)=$/
52
+ # Boolean
53
+ return true if method_name.to_s =~ /(.*?)?$/ && field?($1)
54
+ # Unknown pattern
55
+ super
56
+ end
57
+
58
+ def method_missing(method_name, *args, &blk)
59
+ # Simple field value lookup
60
+ return self[method_name] if field?(method_name)
61
+ # Dirty
62
+ if method_name.to_s =~ /(\w*)(_changed\?|_change|_will_change!|_was)$/ && field?($1)
63
+ __send__("attribute#{$2}", $1)
64
+ # Setter
65
+ elsif method_name.to_s =~ /(.*?)=$/
66
+ self[$1] = args.first
67
+ # Boolean
68
+ elsif method_name.to_s =~ /(.*?)?$/ && field?($1)
69
+ !!self[$1]
70
+ # Unknown pattern
71
+ else
72
+ super
73
+ end
74
+ end
75
+
76
+ def embedded?
77
+ self.class.embeddable?
78
+ end
79
+
80
+ def connection
81
+ self.class.connection
82
+ end
83
+
84
+ def oclass
85
+ self.class.oclass
86
+ end
87
+
88
+ def oclass_name
89
+ self.class.oclass_name
90
+ end
91
+
92
+ def rid
93
+ @odocument.rid
94
+ end
95
+
96
+ def inspect
97
+ attrs = attributes.map { |k, v| "#{k}:#{v.inspect}" }.join(' ')
98
+ super_klass = self.class.descends_from_base? ? '' : "(#{self.class.superclass.name})"
99
+ %{#<#{self.class.name}#{super_klass}:#{@odocument.rid} #{attrs}>}
100
+ end
101
+
102
+ alias :to_s :inspect
103
+
104
+ def <=>(other)
105
+ to_s <=> other.to_s
106
+ end
107
+
108
+ module ClassMethods
109
+
110
+ attr_writer :oclass_name
111
+
112
+ def oclass_name
113
+ @oclass_name ||= name.to_s.gsub('::', '__')
114
+ end
115
+
116
+ def field(name, type, options = { })
117
+ name = name.to_sym
118
+ if fields.key? name
119
+ puts "Already defined field [#{name}]"
120
+ else
121
+ fields[name] = { :type => type }.update options
122
+ end
123
+ end
124
+
125
+ def new_document
126
+ OrientDB::Document.new connection, oclass_name
127
+ end
128
+
129
+ def from_orientdb(value)
130
+ value.respond_to?(:to_orientdb_ar) ? value.to_orientdb_ar : value
131
+ end
132
+
133
+ def to_orientdb(value)
134
+ if value.respond_to?(:to_orientdb)
135
+ value.to_orientdb
136
+ elsif value.respond_to?(:jruby_value)
137
+ value.jruby_value
138
+ else
139
+ value
140
+ end
141
+ end
142
+ end
143
+
144
+ end
@@ -0,0 +1,110 @@
1
+ class OrientDB::AR::Embedded
2
+
3
+ def self.embeddable?
4
+ true
5
+ end
6
+
7
+ include ActiveModel::AttributeMethods
8
+ include OrientDB::AR::DocumentMixin
9
+
10
+ class_inheritable_hash :fields
11
+ self.fields = ActiveSupport::OrderedHash.new
12
+
13
+ def save
14
+ raise "Not implemented on Embedded models"
15
+ end
16
+
17
+ def delete
18
+ raise "Not implemented on Embedded models"
19
+ end
20
+
21
+ def reload
22
+ raise "Not implemented on Embedded models"
23
+ end
24
+
25
+ def saved?
26
+ false
27
+ end
28
+
29
+ def deleted?
30
+ false
31
+ end
32
+
33
+ def persisted?
34
+ false
35
+ end
36
+
37
+ class << self
38
+
39
+ def connection
40
+ OrientDB::AR::Base.connection
41
+ end
42
+
43
+ def oclass
44
+ @oclass ||= connection.get_or_create_class oclass_name, fields.dup
45
+ end
46
+
47
+ def descends_from_base?
48
+ superclass && superclass == OrientDB::AR::Embedded
49
+ end
50
+
51
+ def schema!
52
+ raise "Not implemented on Embedded models"
53
+ end
54
+
55
+ def create(fields = {})
56
+ raise "Not implemented on Embedded models"
57
+ end
58
+
59
+ def select(*args)
60
+ raise "Not implemented on Embedded models"
61
+ end
62
+
63
+ alias :columns :select
64
+
65
+ def where(*args)
66
+ raise "Not implemented on Embedded models"
67
+ end
68
+
69
+ def order(*args)
70
+ raise "Not implemented on Embedded models"
71
+ end
72
+
73
+ def limit(max_records)
74
+ raise "Not implemented on Embedded models"
75
+ end
76
+
77
+ def range(lower_rid, upper_rid = nil)
78
+ raise "Not implemented on Embedded models"
79
+ end
80
+
81
+ def all(conditions = {})
82
+ raise "Not implemented on Embedded models"
83
+ end
84
+
85
+ def first(conditions = {})
86
+ raise "Not implemented on Embedded models"
87
+ end
88
+
89
+ def update(*args)
90
+ raise "Not implemented on Embedded models"
91
+ end
92
+
93
+ def delete(*args)
94
+ raise "Not implemented on Embedded models"
95
+ end
96
+
97
+ def insert(*args)
98
+ raise "Not implemented on Embedded models"
99
+ end
100
+
101
+ def count
102
+ raise "Not implemented on Embedded models"
103
+ end
104
+
105
+ def clear
106
+ raise "Not implemented on Embedded models"
107
+ end
108
+ end
109
+
110
+ end
@@ -0,0 +1,58 @@
1
+ class OrientDB::Document
2
+ def to_orientdb_ar
3
+ klass = getClassName.gsub('__', '::').constantize
4
+ obj = klass.new
5
+ obj.instance_variable_set "@odocument", self
6
+ obj
7
+ end
8
+ end
9
+
10
+ class OrientDB::RecordList
11
+ def to_orientdb_ar
12
+ map { |x| x.respond_to?(:to_orientdb_ar) ? x.to_orientdb_ar : x }
13
+ end
14
+ end
15
+
16
+ class OrientDB::RecordMap
17
+ def to_orientdb_ar
18
+ inject({ }) { |h, (k, v)| h[k] = v.respond_to?(:to_orientdb_ar) ? v.to_orientdb_ar : v; h }
19
+ end
20
+ end
21
+
22
+ class OrientDB::RecordSet
23
+ def to_orientdb_ar
24
+ map { |x| x.respond_to?(:to_orientdb_ar) ? x.to_orientdb_ar : x }
25
+ end
26
+ end
27
+
28
+ class NilClass
29
+ def to_orientdb_ar
30
+ self
31
+ end
32
+ end
33
+
34
+ class Array
35
+ def to_orientdb
36
+ map { |x| x.respond_to?(:to_orientdb) ? x.to_orientdb : x }
37
+ end
38
+
39
+ def to_orientdb_ar
40
+ map { |x| x.respond_to?(:to_orientdb_ar) ? x.to_orientdb_ar : x }
41
+ end
42
+ end
43
+
44
+ class Hash
45
+ def to_orientdb
46
+ inject(java.util.HashMap.new) { |h, (k, v)| h[k.to_s] = v.respond_to?(:to_orientdb) ? v.to_orientdb : v; h }
47
+ end
48
+
49
+ def to_orientdb_ar
50
+ inject({ }) { |h, (k, v)| h[k] = v.respond_to?(:to_orientdb_ar) ? v.to_orientdb_ar : v; h }
51
+ end
52
+ end
53
+
54
+ class Java::JavaUtil::ArrayList
55
+ def to_orientdb_ar
56
+ map { |x| x.respond_to?(:to_orientdb_ar) ? x.to_orientdb_ar : x }
57
+ end
58
+ end
@@ -1,53 +1,48 @@
1
1
  module OrientDB::AR
2
2
  module Relations
3
3
 
4
- def has_one(klass, options = {})
4
+ def embedds_one(klass, options = {})
5
5
  klass = klass_for klass
6
6
  name = options[:name].to_s || field_name_for(klass, true)
7
7
 
8
- field_type = klass.embedded? ? :embedded : :link
9
- field name, [OrientDB::FIELD_TYPES[field_type], klass.oclass]
8
+ field name, [OrientDB::FIELD_TYPES[:embedded], klass.oclass]
10
9
 
11
10
  class_eval <<-eorb, __FILE__, __LINE__ + 1
12
11
  def #{name} # def address
13
- doc = odocument[:#{name}] # doc = odocument[:address]
14
- doc ? #{klass.name}.new_from_doc(doc) : nil # doc ? Address.new_from_doc(doc) : nil
12
+ self[:#{name}] # self[:address]
15
13
  end # end
16
14
  eorb
17
- #
15
+
18
16
  class_eval <<-eorb, __FILE__, __LINE__ + 1
19
17
  def #{name}=(value) # def address=(value)
20
- raise "Invalid value for [#{name}]" unless value.is_a?(#{klass}) # raise "Invalid value for [address]" unless value.is_a?(Address)
21
- odocument[:#{name}] = value.odocument # odocument[:address] = value.odocument
18
+ self[:#{name}] = value # self[:address] = value.odocument
22
19
  #{name} # address
23
20
  end # end
24
21
  eorb
25
22
  end
26
23
 
27
- def has_many(klass, options = {})
24
+ def embedds_many(klass, options = {})
28
25
  klass = klass_for klass
29
26
  name = options[:name].to_s || field_name_for(klass, false)
30
27
 
31
- field_type = klass.embedded? ? :embedded : :link
32
- field name, [OrientDB::FIELD_TYPES[field_type], klass.oclass]
28
+ field name, [OrientDB::FIELD_TYPES[:embedded_list], klass.oclass]
33
29
 
34
30
  class_eval <<-eorb, __FILE__, __LINE__ + 1
35
31
  def #{name} # def addresses
36
- docs = odocument[:#{name}] # docs = odocument[:addresses]
37
- docs ? #{klass.name}.new_from_docs(docs) : nil # docs ? Address.new_from_docs(doc) : nil
32
+ self[:#{name}] # self[:addresses]
38
33
  end # end
39
34
  eorb
40
35
 
41
36
  class_eval <<-eorb, __FILE__, __LINE__ + 1
42
37
  def #{name}=(value) # def addresses=(value)
43
- odocument[:#{name}] = value.map{|x| x.odocument } # odocument[:addresses] = value.map{|x| x.odocument }
44
- #{name} # addresses
38
+ self[:#{name}] # self[:addresses]
45
39
  end # end
46
40
  eorb
47
41
 
48
42
  class_eval <<-eorb, __FILE__, __LINE__ + 1
49
- def add_#{name.singularize}(value) # def add_address(value)
50
- odocument[:#{name}] << value.odocument # odocument[:addresses] << value.odocument
43
+ def add_#{name.singularize}(value) # def add_address(value)
44
+ self[:#{name}] ||= [] # self[:addresses] ||= []
45
+ self[:#{name}] << value # self[:addresses] << value
51
46
  #{name} # addresses
52
47
  end # end
53
48
  eorb
@@ -0,0 +1,33 @@
1
+ class OrientDB::AR::Delete
2
+
3
+ attr_accessor :model, :command
4
+
5
+ def initialize(model, command = OrientDB::SQL::Delete.new)
6
+ @model, @command = model, command
7
+ @command.oclass model.oclass_name
8
+ end
9
+
10
+ %w{ oclass oclass! cluster cluster! where where! and or and_not or_not }.each do |name|
11
+ define_method(name) do |*args|
12
+ command.send name, *args
13
+ self
14
+ end
15
+ end
16
+
17
+ def self.from_query(query)
18
+ obj = new query.model
19
+ obj.command.instance_variable_set "@conditions", query.query.instance_variable_get("@conditions")
20
+ obj
21
+ end
22
+
23
+ def run
24
+ model.connection.run_command command.to_s
25
+ end
26
+
27
+ def inspect
28
+ %{#<OrientDB::AR::Delete:#{model.name} command="#{command.to_s}">}
29
+ end
30
+
31
+ alias :to_s :inspect
32
+
33
+ end
@@ -0,0 +1,27 @@
1
+ class OrientDB::AR::Insert
2
+
3
+ attr_accessor :model, :command
4
+
5
+ def initialize(model, command = OrientDB::SQL::Insert.new)
6
+ @model, @command = model, command
7
+ @command.oclass model.oclass_name
8
+ end
9
+
10
+ %w{ oclass oclass! cluster cluster! fields fields! values values! }.each do |name|
11
+ define_method(name) do |*args|
12
+ command.send name, *args
13
+ self
14
+ end
15
+ end
16
+
17
+ def run
18
+ model.connection.run_command command.to_s
19
+ end
20
+
21
+ def inspect
22
+ %{#<OrientDB::AR::Insert:#{model.name} command="#{command.to_s}">}
23
+ end
24
+
25
+ alias :to_s :inspect
26
+
27
+ end
@@ -4,7 +4,7 @@ class OrientDB::AR::Query
4
4
 
5
5
  def initialize(model, query = OrientDB::SQL::Query.new)
6
6
  @model, @query = model, query
7
- @query.from model.name
7
+ @query.from model.oclass_name
8
8
  end
9
9
 
10
10
  %w{ select select! where where! and or and_not or_not order order! limit limit! range range! }.each do |name|
@@ -15,15 +15,27 @@ class OrientDB::AR::Query
15
15
  end
16
16
 
17
17
  def all
18
- model.connection.query(query).map { |doc| model.new_from_doc doc }
18
+ model.connection.all(query).map { |doc| doc.to_orientdb_ar }
19
19
  end
20
20
 
21
21
  def first
22
- model.new_from_doc model.connection.first(query)
22
+ model.connection.first(query).to_orientdb_ar
23
23
  end
24
24
 
25
25
  def results
26
- model.connection.query(query).map
26
+ model.connection.all(query).map
27
+ end
28
+
29
+ def first_result
30
+ model.connection.first(query)
31
+ end
32
+
33
+ def update(*args)
34
+ OrientDB::AR::Update.from_query(self, *args).run
35
+ end
36
+
37
+ def delete
38
+ OrientDB::AR::Delete.from_query(self).run
27
39
  end
28
40
 
29
41
  def inspect
@@ -0,0 +1,34 @@
1
+ class OrientDB::AR::Update
2
+
3
+ attr_accessor :model, :command
4
+
5
+ def initialize(model, command = OrientDB::SQL::Update.new)
6
+ @model, @command = model, command
7
+ @command.oclass model.oclass_name
8
+ end
9
+
10
+ %w{ oclass oclass! cluster cluster! action fields fields! values values! where where! and or and_not or_not }.each do |name|
11
+ define_method(name) do |*args|
12
+ command.send name, *args
13
+ self
14
+ end
15
+ end
16
+
17
+ def run
18
+ model.connection.run_command command.to_s
19
+ end
20
+
21
+ def self.from_query(query, *args)
22
+ obj = new query.model
23
+ obj.command.instance_variable_set "@conditions", query.query.instance_variable_get("@conditions")
24
+ obj.command.fields *args
25
+ obj
26
+ end
27
+
28
+ def inspect
29
+ %{#<OrientDB::AR::Update:#{model.name} command="#{command.to_s}">}
30
+ end
31
+
32
+ alias :to_s :inspect
33
+
34
+ end
@@ -0,0 +1,4 @@
1
+ require 'orientdb-ar/sql/query'
2
+ require 'orientdb-ar/sql/update'
3
+ require 'orientdb-ar/sql/delete'
4
+ require 'orientdb-ar/sql/insert'
File without changes
data/lib/orientdb-ar.rb CHANGED
@@ -5,5 +5,9 @@ module OrientDB
5
5
  end
6
6
  end
7
7
 
8
- require 'model/base'
9
- require 'model/embedded'
8
+ require 'orientdb-ar/document_mixin'
9
+ require 'orientdb-ar/base'
10
+ require 'orientdb-ar/embedded'
11
+ require 'orientdb-ar/ext'
12
+
13
+ OrientDB::SQL.monkey_patch!
data/orientdb-ar.gemspec CHANGED
@@ -5,12 +5,12 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{orientdb-ar}
8
- s.version = "0.0.3"
8
+ s.version = "0.0.4"
9
9
  s.platform = %q{jruby}
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 1.3.6") if s.respond_to? :required_rubygems_version=
12
12
  s.authors = ["Adrian Madrid"]
13
- s.date = %q{2011-01-18}
13
+ s.date = %q{2011-01-26}
14
14
  s.default_executable = %q{orientdbar_console}
15
15
  s.description = %q{Active Model wrappers to persist Ruby objects under OrientDB in JRuby.}
16
16
  s.email = ["aemadrid@gmail.com"]
@@ -28,14 +28,20 @@ Gem::Specification.new do |s|
28
28
  "Rakefile",
29
29
  "VERSION",
30
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/query.rb",
36
- "lib/model/relations.rb",
37
- "lib/model/validations.rb",
38
31
  "lib/orientdb-ar.rb",
32
+ "lib/orientdb-ar/attributes.rb",
33
+ "lib/orientdb-ar/base.rb",
34
+ "lib/orientdb-ar/conversion.rb",
35
+ "lib/orientdb-ar/document_mixin.rb",
36
+ "lib/orientdb-ar/embedded.rb",
37
+ "lib/orientdb-ar/ext.rb",
38
+ "lib/orientdb-ar/relations.rb",
39
+ "lib/orientdb-ar/sql.rb",
40
+ "lib/orientdb-ar/sql/delete.rb",
41
+ "lib/orientdb-ar/sql/insert.rb",
42
+ "lib/orientdb-ar/sql/query.rb",
43
+ "lib/orientdb-ar/sql/update.rb",
44
+ "lib/orientdb-ar/validations.rb",
39
45
  "orientdb-ar.gemspec",
40
46
  "spec/base_spec.rb",
41
47
  "spec/dirty_spec.rb",
@@ -55,26 +61,25 @@ Gem::Specification.new do |s|
55
61
  s.homepage = %q{http://rubygems.org/gems/orientdb}
56
62
  s.require_paths = ["lib"]
57
63
  s.rubyforge_project = %q{orientdb-ar}
58
- s.rubygems_version = %q{1.3.6}
64
+ s.rubygems_version = %q{1.4.2}
59
65
  s.summary = %q{ActiveRecord-like persistency through OrientDB in JRuby}
60
66
 
61
67
  if s.respond_to? :specification_version then
62
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
63
68
  s.specification_version = 3
64
69
 
65
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
66
- s.add_runtime_dependency(%q<orientdb>, ["= 0.0.11"])
70
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
71
+ s.add_runtime_dependency(%q<orientdb>, ["= 0.0.15"])
67
72
  s.add_runtime_dependency(%q<activemodel>, [">= 3.0.3"])
68
73
  s.add_development_dependency(%q<awesome_print>, [">= 0"])
69
74
  s.add_development_dependency(%q<rspec>, [">= 2.4"])
70
75
  else
71
- s.add_dependency(%q<orientdb>, ["= 0.0.11"])
76
+ s.add_dependency(%q<orientdb>, ["= 0.0.15"])
72
77
  s.add_dependency(%q<activemodel>, [">= 3.0.3"])
73
78
  s.add_dependency(%q<awesome_print>, [">= 0"])
74
79
  s.add_dependency(%q<rspec>, [">= 2.4"])
75
80
  end
76
81
  else
77
- s.add_dependency(%q<orientdb>, ["= 0.0.11"])
82
+ s.add_dependency(%q<orientdb>, ["= 0.0.15"])
78
83
  s.add_dependency(%q<activemodel>, [">= 3.0.3"])
79
84
  s.add_dependency(%q<awesome_print>, [">= 0"])
80
85
  s.add_dependency(%q<rspec>, [">= 2.4"])
data/spec/base_spec.rb CHANGED
@@ -2,6 +2,6 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "Base" do
4
4
  it "should have a valid connection" do
5
- OrientDB::AR::Base.connection.should be_a_kind_of OrientDB::Database
5
+ OrientDB::AR::Base.connection.should be_a_kind_of OrientDB::DocumentDatabase
6
6
  end
7
7
  end
data/spec/model_spec.rb CHANGED
@@ -3,16 +3,16 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
3
  describe "Model" do
4
4
 
5
5
  it "should have a valid connection" do
6
- Person.connection.should be_a_kind_of OrientDB::Database
6
+ Person.connection.should be_a_kind_of OrientDB::DocumentDatabase
7
7
  end
8
8
 
9
9
  it "should have the right schema" do
10
10
  Person.new.should be_a_kind_of Person
11
11
  Person.oclass_name.should == 'Person'
12
12
  Person.fields.keys.should == [:name, :age, :tags]
13
- Person.fields[:name].should == {:type => :string, :not_null => true}
14
- Person.fields[:age].should == {:type => :int}
15
- Person.fields[:tags].should == {:type => [:list, :string]}
13
+ Person.fields[:name].should == { :type => :string, :not_null => true }
14
+ Person.fields[:age].should == { :type => :int }
15
+ Person.fields[:tags].should == { :type => [:list, :string] }
16
16
  end
17
17
 
18
18
  it "should create working models" do
@@ -31,7 +31,7 @@ describe "Model" do
31
31
  it "should handle embedded models as relations" do
32
32
  Customer.clear
33
33
 
34
- a = Address.new :street => "123 S Main", :city => "Salt Lake", :state => "UT", :country => "USA"
34
+ a = Address.new :street => "123 S Main", :city => "Salt Lake", :state => "UT", :country => "USA"
35
35
  p1 = PhoneNumber.new :number => "123456789"
36
36
  p2 = PhoneNumber.new :number => "987654321"
37
37
  c1 = Customer.create :name => "Albert Einstein", :number => 1, :address => a, :phones => [p1, p2]
@@ -56,4 +56,46 @@ describe "Model" do
56
56
  Person.where("'fighter' IN tags", :age.lte(28)).order(:name.desc).all.first.should == p4
57
57
  end
58
58
 
59
+ it "should update models" do
60
+ Person.clear
61
+
62
+ p1 = Person.create :name => "Hans Solo", :age => 38, :tags => %w{ fighter pilot }
63
+ p2 = Person.create :name => "Yoda", :age => 387, :tags => %w{ genius jedi }
64
+ p3 = Person.create :name => "Luke Skywalker", :age => 28, :tags => %w{ jedi fighter pilot }
65
+
66
+ Person.where(:name => "Hans Solo").update(:name => "Hans Meister")
67
+ Person.where(:name => "Hans Meister").all.map { |x| x.rid }.should == [p1.rid]
68
+ p1.reload
69
+ Person.where(:name => "Hans Meister").all.should == [p1]
70
+
71
+
72
+ Person.update(:age => 45)
73
+ Person.where(:age => 45).all.map { |x| x.name }.should == [p1.name, p2.name, p3.name]
74
+ end
75
+
76
+ it "should delete models" do
77
+ Person.clear
78
+
79
+ Person.create :name => "Hans Solo", :age => 38, :tags => %w{ fighter pilot }
80
+ Person.create :name => "Yoda", :age => 387, :tags => %w{ genius jedi }
81
+ Person.create :name => "Luke Skywalker", :age => 28, :tags => %w{ jedi fighter pilot }
82
+
83
+ Person.count.should == 3
84
+
85
+ Person.where(:name => "Hans Solo").delete
86
+ Person.count.should == 2
87
+
88
+ Person.delete
89
+ Person.count.should == 0
90
+ end
91
+
92
+ it "should insert models" do
93
+ Person.clear
94
+
95
+ p1 = Person.insert :name => "Hans Solo", :age => 38, :tags => %w{ fighter pilot }
96
+ Person.count.should == 1
97
+
98
+ p2 = Person.first
99
+ p1.should == p2
100
+ end
59
101
  end
@@ -5,4 +5,3 @@ class Address < OrientDB::AR::Embedded
5
5
  field :zip, :string
6
6
  field :country, :string
7
7
  end
8
- Address.schema!
@@ -1,6 +1,6 @@
1
1
  class Customer < Person
2
2
  field :number, :int, :not_null => true
3
- has_one Address, :name => :address
4
- has_many PhoneNumber, :name => :phones
3
+ embedds_one Address, :name => :address
4
+ embedds_many PhoneNumber, :name => :phones
5
5
  end
6
6
  Customer.schema!
@@ -2,5 +2,4 @@ class PhoneNumber < OrientDB::AR::Embedded
2
2
  field :type, :string
3
3
  field :number, :string
4
4
  field :extension, :string
5
- end
6
- PhoneNumber.schema!
5
+ end
data/spec/spec_helper.rb CHANGED
@@ -23,7 +23,7 @@ unless defined?(SPEC_HELPER_LOADED)
23
23
  FileUtils.remove_dir TEMP_DIR + '/test', true
24
24
  puts ">> Creating OrientDB database..."
25
25
  FileUtils.mkdir_p TEST_DB_PATH
26
- OrientDB::AR::Base.connection = OrientDB::Database.new("local:#{TEST_DB_PATH}/test").create
26
+ OrientDB::AR::Base.connection = OrientDB::DocumentDatabase.new("local:#{TEST_DB_PATH}/test").create
27
27
  puts ">> Connection : #{OrientDB::AR::Base.connection}"
28
28
 
29
29
  require SPEC_ROOT + '/models/person'
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: orientdb-ar
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 3
9
- version: 0.0.3
4
+ prerelease:
5
+ version: 0.0.4
10
6
  platform: jruby
11
7
  authors:
12
8
  - Adrian Madrid
@@ -14,34 +10,28 @@ autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
12
 
17
- date: 2011-01-18 00:00:00 -07:00
13
+ date: 2011-01-26 00:00:00 -07:00
18
14
  default_executable: orientdbar_console
19
15
  dependencies:
20
16
  - !ruby/object:Gem::Dependency
21
17
  name: orientdb
22
18
  prerelease: false
23
19
  requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
24
21
  requirements:
25
22
  - - "="
26
23
  - !ruby/object:Gem::Version
27
- segments:
28
- - 0
29
- - 0
30
- - 11
31
- version: 0.0.11
24
+ version: 0.0.15
32
25
  type: :runtime
33
26
  version_requirements: *id001
34
27
  - !ruby/object:Gem::Dependency
35
28
  name: activemodel
36
29
  prerelease: false
37
30
  requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
38
32
  requirements:
39
33
  - - ">="
40
34
  - !ruby/object:Gem::Version
41
- segments:
42
- - 3
43
- - 0
44
- - 3
45
35
  version: 3.0.3
46
36
  type: :runtime
47
37
  version_requirements: *id002
@@ -49,11 +39,10 @@ dependencies:
49
39
  name: awesome_print
50
40
  prerelease: false
51
41
  requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
52
43
  requirements:
53
44
  - - ">="
54
45
  - !ruby/object:Gem::Version
55
- segments:
56
- - 0
57
46
  version: "0"
58
47
  type: :development
59
48
  version_requirements: *id003
@@ -61,12 +50,10 @@ dependencies:
61
50
  name: rspec
62
51
  prerelease: false
63
52
  requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
64
54
  requirements:
65
55
  - - ">="
66
56
  - !ruby/object:Gem::Version
67
- segments:
68
- - 2
69
- - 4
70
57
  version: "2.4"
71
58
  type: :development
72
59
  version_requirements: *id004
@@ -89,14 +76,20 @@ files:
89
76
  - Rakefile
90
77
  - VERSION
91
78
  - 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/query.rb
97
- - lib/model/relations.rb
98
- - lib/model/validations.rb
99
79
  - lib/orientdb-ar.rb
80
+ - lib/orientdb-ar/attributes.rb
81
+ - lib/orientdb-ar/base.rb
82
+ - lib/orientdb-ar/conversion.rb
83
+ - lib/orientdb-ar/document_mixin.rb
84
+ - lib/orientdb-ar/embedded.rb
85
+ - lib/orientdb-ar/ext.rb
86
+ - lib/orientdb-ar/relations.rb
87
+ - lib/orientdb-ar/sql.rb
88
+ - lib/orientdb-ar/sql/delete.rb
89
+ - lib/orientdb-ar/sql/insert.rb
90
+ - lib/orientdb-ar/sql/query.rb
91
+ - lib/orientdb-ar/sql/update.rb
92
+ - lib/orientdb-ar/validations.rb
100
93
  - orientdb-ar.gemspec
101
94
  - spec/base_spec.rb
102
95
  - spec/dirty_spec.rb
@@ -122,25 +115,21 @@ rdoc_options: []
122
115
  require_paths:
123
116
  - lib
124
117
  required_ruby_version: !ruby/object:Gem::Requirement
118
+ none: false
125
119
  requirements:
126
120
  - - ">="
127
121
  - !ruby/object:Gem::Version
128
- segments:
129
- - 0
130
122
  version: "0"
131
123
  required_rubygems_version: !ruby/object:Gem::Requirement
124
+ none: false
132
125
  requirements:
133
126
  - - ">="
134
127
  - !ruby/object:Gem::Version
135
- segments:
136
- - 1
137
- - 3
138
- - 6
139
128
  version: 1.3.6
140
129
  requirements: []
141
130
 
142
131
  rubyforge_project: orientdb-ar
143
- rubygems_version: 1.3.6
132
+ rubygems_version: 1.4.2
144
133
  signing_key:
145
134
  specification_version: 3
146
135
  summary: ActiveRecord-like persistency through OrientDB in JRuby
@@ -1,197 +0,0 @@
1
- class OrientDB::AR::Embedded
2
- include ActiveModel::AttributeMethods
3
- include Comparable
4
-
5
- extend ActiveModel::Translation
6
-
7
- include OrientDB::AR::Attributes
8
- include OrientDB::AR::Conversion
9
- include OrientDB::AR::Validations
10
-
11
- include ActiveModel::Serializers::JSON
12
- include ActiveModel::Serializers::Xml
13
-
14
- class_inheritable_hash :fields
15
- self.fields = ActiveSupport::OrderedHash.new
16
-
17
- attr_reader :odocument
18
-
19
- def initialize(fields = {})
20
- @odocument = self.class.new_document fields
21
- @changed_attributes = {}
22
- @errors = ActiveModel::Errors.new(self)
23
- end
24
-
25
- def field?(name)
26
- res = @odocument.field?(name)
27
- res
28
- end
29
-
30
- def respond_to?(method_name)
31
- # Simple field value lookup
32
- return true if field?(method_name)
33
- # Dirty
34
- return true if method_name.to_s =~ /(\w*)(_changed\?|_change|_will_change!|_was)$/ && field?($1)
35
- # Setter
36
- return true if method_name.to_s =~ /(.*?)=$/
37
- # Boolean
38
- return true if method_name.to_s =~ /(.*?)?$/ && field?($1)
39
- # Unknown pattern
40
- super
41
- end
42
-
43
- def method_missing(method_name, *args, &blk)
44
- # Simple field value lookup
45
- return self[method_name] if field?(method_name)
46
- # Dirty
47
- if method_name.to_s =~ /(\w*)(_changed\?|_change|_will_change!|_was)$/ && field?($1)
48
- __send__("attribute#{$2}", $1)
49
- # Setter
50
- elsif method_name.to_s =~ /(.*?)=$/
51
- self[$1] = args.first
52
- # Boolean
53
- elsif method_name.to_s =~ /(.*?)?$/ && field?($1)
54
- !!self[$1]
55
- # Unknown pattern
56
- else
57
- super
58
- end
59
- end
60
-
61
- def save
62
- raise "Not implemented on Embedded models"
63
- end
64
-
65
- def delete
66
- raise "Not implemented on Embedded models"
67
- end
68
-
69
- def saved?
70
- false
71
- end
72
-
73
- def deleted?
74
- false
75
- end
76
-
77
- def persisted?
78
- false
79
- end
80
-
81
- def inspect
82
- attrs = attributes.map { |k, v| "#{k}:#{v.inspect}" }.join(' ')
83
- super_klass = self.class.descends_from_embedded? ? '' : "(#{self.class.superclass.name})"
84
- %{#<#{self.class.name}#{super_klass}:#{@odocument.rid} #{attrs}>}
85
- end
86
-
87
- alias :to_s :inspect
88
-
89
- def <=>(other)
90
- to_s <=> other.to_s
91
- end
92
-
93
- class << self
94
-
95
- def connection
96
- OrientDB::AR::Base.connection
97
- end
98
-
99
- attr_writer :oclass_name
100
-
101
- def oclass_name
102
- @oclass_name ||= name.to_s
103
- end
104
-
105
- def oclass
106
- unless defined?(@oclass)
107
- options = {}
108
- unless descends_from_embedded?
109
- super_oclass = superclass.oclass
110
- options[:super] = super_oclass
111
- options[:use_cluster] = super_oclass.cluster_ids.first
112
- end
113
- @oclass = connection.get_or_create_class oclass_name, options
114
- end
115
- @oclass
116
- end
117
-
118
- def embedded?
119
- true
120
- end
121
-
122
- def field(name, type, options = {})
123
- name = name.to_sym
124
- if fields.key? name
125
- puts "Already defined field [#{name}]"
126
- else
127
- fields[name] = {:type => type}.update options
128
- end
129
- end
130
-
131
- def descends_from_embedded?
132
- superclass && superclass == OrientDB::AR::Embedded
133
- end
134
-
135
- def schema!
136
- fields.each do |field, options|
137
- oclass.add field, options[:type], options.except(:type)
138
- end
139
- self
140
- end
141
-
142
- def new_document(fields = {})
143
- oclass
144
- OrientDB::Document.new connection, oclass_name, fields
145
- end
146
-
147
- def create(fields = {})
148
- raise "Not implemented on Embedded models"
149
- end
150
-
151
- def select(*args)
152
- raise "Not implemented on Embedded models"
153
- end
154
-
155
- alias :columns :select
156
-
157
- def where(*args)
158
- raise "Not implemented on Embedded models"
159
- end
160
-
161
- def order(*args)
162
- raise "Not implemented on Embedded models"
163
- end
164
-
165
- def limit(max_records)
166
- raise "Not implemented on Embedded models"
167
- end
168
-
169
- def range(lower_rid, upper_rid = nil)
170
- raise "Not implemented on Embedded models"
171
- end
172
-
173
- def all(conditions = {})
174
- raise "Not implemented on Embedded models"
175
- end
176
-
177
- def first(conditions = {})
178
- raise "Not implemented on Embedded models"
179
- end
180
-
181
- def clear
182
- raise "Not implemented on Embedded models"
183
- end
184
-
185
- def new_from_doc(doc)
186
- klass = doc.getClassName.constantize
187
- obj = klass.new
188
- obj.instance_variable_set "@odocument", doc
189
- obj
190
- end
191
-
192
- def new_from_docs(docs)
193
- docs.map { |doc| new_from_doc doc }
194
- end
195
- end
196
-
197
- end