orientdb-ar 0.0.4-jruby → 0.0.5-jruby

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,65 +1,31 @@
1
1
  module OrientDB::AR
2
2
  module Relations
3
3
 
4
- def embedds_one(klass, options = {})
5
- klass = klass_for klass
6
- name = options[:name].to_s || field_name_for(klass, true)
7
-
8
- field name, [OrientDB::FIELD_TYPES[:embedded], klass.oclass]
9
-
10
- class_eval <<-eorb, __FILE__, __LINE__ + 1
11
- def #{name} # def address
12
- self[:#{name}] # self[:address]
13
- end # end
14
- eorb
15
-
16
- class_eval <<-eorb, __FILE__, __LINE__ + 1
17
- def #{name}=(value) # def address=(value)
18
- self[:#{name}] = value # self[:address] = value.odocument
19
- #{name} # address
20
- end # end
21
- eorb
22
- end
23
-
24
- def embedds_many(klass, options = {})
25
- klass = klass_for klass
26
- name = options[:name].to_s || field_name_for(klass, false)
27
-
28
- field name, [OrientDB::FIELD_TYPES[:embedded_list], klass.oclass]
29
-
30
- class_eval <<-eorb, __FILE__, __LINE__ + 1
31
- def #{name} # def addresses
32
- self[:#{name}] # self[:addresses]
33
- end # end
34
- eorb
35
-
36
- class_eval <<-eorb, __FILE__, __LINE__ + 1
37
- def #{name}=(value) # def addresses=(value)
38
- self[:#{name}] # self[:addresses]
39
- end # end
40
- eorb
4
+ private
41
5
 
42
- class_eval <<-eorb, __FILE__, __LINE__ + 1
43
- def add_#{name.singularize}(value) # def add_address(value)
44
- self[:#{name}] ||= [] # self[:addresses] ||= []
45
- self[:#{name}] << value # self[:addresses] << value
46
- #{name} # addresses
47
- end # end
48
- eorb
6
+ def check_rel_options(klass_name, options, plurality, default = nil)
7
+ klass_name = klass_name_for klass_name
8
+ options[:plurality] ||= plurality
9
+ options[:name] ||= field_name_for klass_name, plurality
10
+ options[:name] = options[:name].to_s
11
+ options[:default] ||= default
12
+ [klass_name, options]
49
13
  end
50
14
 
51
- private
52
-
53
- def klass_for(klass)
54
- return klass if klass.class.name == 'Class'
55
- klass.to_s.singularize.camelize.constantize
56
- rescue
57
- raise "Problem getting klass for [#{klass}]"
15
+ def klass_name_for(klass_name)
16
+ return klass_name.name if klass_name.class.name == 'Class'
17
+ klass_name.to_s
58
18
  end
59
19
 
60
- def field_name_for(klass, singular)
61
- klass.to_s.underscore.send(singular ? :singularize : :pluralize).gsub('/', '__')
20
+ def field_name_for(klass, plurality)
21
+ plurality_meth = plurality.to_s + 'ize'
22
+ klass.to_s.underscore.send(plurality_meth).gsub('/', '__')
62
23
  end
63
24
 
64
25
  end
65
- end
26
+ end
27
+
28
+ require 'orientdb-ar/relations/embedds_one'
29
+ require 'orientdb-ar/relations/embedds_many'
30
+ require 'orientdb-ar/relations/links_one'
31
+ require 'orientdb-ar/relations/links_many'
@@ -0,0 +1,40 @@
1
+ module OrientDB::AR
2
+ module Relations
3
+
4
+ def embedds_many(klass_name, options = { })
5
+ klass_name, options = check_rel_options klass_name, options, :plural, []
6
+ name = options[:name]
7
+
8
+ relationships[name] = options.merge :type => :embedds_many, :class_name => klass_name
9
+
10
+ field name, [OrientDB::FIELD_TYPES[:embedded_list], OrientDB::AR::Base.oclass_name_for(klass_name)]
11
+
12
+ class_eval <<-eorb, __FILE__, __LINE__ + 1
13
+ def #{name} # def address
14
+ self[:#{name}] || #{options[:default].inspect} # self[:address] || nil
15
+ end # end
16
+ eorb
17
+
18
+ class_eval <<-eorb, __FILE__, __LINE__ + 1
19
+ def #{name}=(value) # def addresses=(value)
20
+ self[:#{name}] = value # self[:addresses] = value
21
+ end # end
22
+ eorb
23
+
24
+ class_eval <<-eorb, __FILE__, __LINE__ + 1
25
+ def add_#{name.singularize}(value) # def add_address(value)
26
+ self[:#{name}] ||= #{options[:default].inspect} # self[:addresses] ||= []
27
+ self[:#{name}] << self.class.to_orientdb(value) # self[:addresses] << self.class.to_orientdb(value)
28
+ #{name} # addresses
29
+ end # end
30
+ eorb
31
+
32
+ class_eval <<-eorb, __FILE__, __LINE__ + 1
33
+ def build_#{name}(fields = {}) # def build_address(fields = {})
34
+ add_#{name} #{klass_name}.new fields # self[:addresses] = Address.new fields
35
+ end # end
36
+ eorb
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,33 @@
1
+ module OrientDB::AR
2
+ module Relations
3
+
4
+ def embedds_one(klass_name, options = { })
5
+ klass_name, options = check_rel_options klass_name, options, :singular
6
+ name = options[:name]
7
+
8
+ relationships[name] = options.merge :type => :embedds_one, :class_name => klass_name
9
+
10
+ field name, [OrientDB::FIELD_TYPES[:embedded], OrientDB::AR::Base.oclass_name_for(klass_name)]
11
+
12
+ class_eval <<-eorb, __FILE__, __LINE__ + 1
13
+ def #{name} # def address
14
+ self[:#{name}] || #{options[:default].inspect} # self[:address] || nil
15
+ end # end
16
+ eorb
17
+
18
+ class_eval <<-eorb, __FILE__, __LINE__ + 1
19
+ def #{name}=(value) # def addresses=(value)
20
+ self[:#{name}] = value # self[:addresses] = value
21
+ end # end
22
+ eorb
23
+
24
+ class_eval <<-eorb, __FILE__, __LINE__ + 1
25
+ def build_#{name}(fields = {}) # def build_address(fields = {})
26
+ self[:#{name}] = #{klass_name}.new fields # self[:addresses] = Address.new fields
27
+ #{name} # address
28
+ end # end
29
+ eorb
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,46 @@
1
+ module OrientDB::AR
2
+ module Relations
3
+
4
+ def links_many(klass_name, options = { })
5
+ klass_name, options = check_rel_options klass_name, options, :plural, []
6
+ name = options[:name]
7
+
8
+ relationships[name] = options.merge :type => :links_many, :class_name => klass_name
9
+
10
+ field name, [OrientDB::FIELD_TYPES[:link_list], OrientDB::AR::Base.oclass_name_for(klass_name)]
11
+
12
+ class_eval <<-eorb, __FILE__, __LINE__ + 1
13
+ def #{name} # def address
14
+ self[:#{name}] || #{options[:default].inspect} # self[:address] || nil
15
+ end # end
16
+ eorb
17
+
18
+ class_eval <<-eorb, __FILE__, __LINE__ + 1
19
+ def #{name}=(value) # def addresses=(value)
20
+ self[:#{name}] = value # self[:addresses] = value
21
+ end # end
22
+ eorb
23
+
24
+ class_eval <<-eorb, __FILE__, __LINE__ + 1
25
+ def add_#{name.singularize}(value) # def add_address(value)
26
+ self[:#{name}] ||= #{options[:default].inspect} # self[:addresses] ||= []
27
+ self[:#{name}] << self.class.to_orientdb(value) # self[:addresses] << self.class.to_orientdb(value)
28
+ #{name} # addresses
29
+ end # end
30
+ eorb
31
+
32
+ class_eval <<-eorb, __FILE__, __LINE__ + 1
33
+ def build_#{name}(fields = {}) # def build_address(fields = {})
34
+ add_#{name} #{klass_name}.new fields # self[:addresses] = Address.new fields
35
+ end # end
36
+ eorb
37
+
38
+ class_eval <<-eorb, __FILE__, __LINE__ + 1
39
+ def create_#{name}(fields = {}) # def create_address(fields = {})
40
+ add_#{name} #{klass_name}.create fields # self[:addresses] = Address.create fields
41
+ end # end
42
+ eorb
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,41 @@
1
+ module OrientDB::AR
2
+ module Relations
3
+
4
+ def links_one(klass_name, options = { })
5
+ klass_name, options = check_rel_options klass_name, options, :singular
6
+ puts "links_one : #{klass_name} : #{options.inspect}"
7
+ name = options[:name]
8
+
9
+ relationships[name] = options.merge :type => :links_one, :class_name => klass_name
10
+
11
+ field name, [OrientDB::FIELD_TYPES[:link], OrientDB::AR::Base.oclass_name_for(klass_name)]
12
+
13
+ class_eval <<-eorb, __FILE__, __LINE__ + 1
14
+ def #{name} # def address
15
+ self[:#{name}] || #{options[:default].inspect} # self[:address] || nil
16
+ end # end
17
+ eorb
18
+
19
+ class_eval <<-eorb, __FILE__, __LINE__ + 1
20
+ def #{name}=(value) # def addresses=(value)
21
+ self[:#{name}] = value # self[:addresses] = value
22
+ end # end
23
+ eorb
24
+
25
+ class_eval <<-eorb, __FILE__, __LINE__ + 1
26
+ def build_#{name}(fields = {}) # def build_address(fields = {})
27
+ self[:#{name}] = #{klass_name}.new fields # self[:addresses] = Address.create fields
28
+ #{name} # address
29
+ end # end
30
+ eorb
31
+
32
+ class_eval <<-eorb, __FILE__, __LINE__ + 1
33
+ def create_#{name}(fields = {}) # def create_address(fields = {})
34
+ self[:#{name}] = #{klass_name}.create fields # self[:addresses] = Address.create fields
35
+ #{name} # address
36
+ end # end
37
+ eorb
38
+ end
39
+
40
+ end
41
+ end
@@ -2,6 +2,12 @@ class OrientDB::AR::Query
2
2
 
3
3
  attr_accessor :model, :query
4
4
 
5
+ def self.for_oclass(name, query = OrientDB::SQL::Query.new)
6
+ obj = new OrientDB::AR::Base, query
7
+ obj.from OrientDB::AR::Base.oclass_name_for name
8
+ obj
9
+ end
10
+
5
11
  def initialize(model, query = OrientDB::SQL::Query.new)
6
12
  @model, @query = model, query
7
13
  @query.from model.oclass_name
data/orientdb-ar.gemspec CHANGED
@@ -5,16 +5,15 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{orientdb-ar}
8
- s.version = "0.0.4"
8
+ s.version = "0.0.5"
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
- s.authors = ["Adrian Madrid"]
13
- s.date = %q{2011-01-26}
14
- s.default_executable = %q{orientdbar_console}
12
+ s.authors = [%q{Adrian Madrid}]
13
+ s.date = %q{2012-01-12}
15
14
  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"]
15
+ s.email = [%q{aemadrid@gmail.com}]
16
+ s.executables = [%q{orientdbar_console}]
18
17
  s.extra_rdoc_files = [
19
18
  "LICENSE",
20
19
  "README.rdoc"
@@ -36,12 +35,15 @@ Gem::Specification.new do |s|
36
35
  "lib/orientdb-ar/embedded.rb",
37
36
  "lib/orientdb-ar/ext.rb",
38
37
  "lib/orientdb-ar/relations.rb",
38
+ "lib/orientdb-ar/relations/embedds_many.rb",
39
+ "lib/orientdb-ar/relations/embedds_one.rb",
40
+ "lib/orientdb-ar/relations/links_many.rb",
41
+ "lib/orientdb-ar/relations/links_one.rb",
39
42
  "lib/orientdb-ar/sql.rb",
40
43
  "lib/orientdb-ar/sql/delete.rb",
41
44
  "lib/orientdb-ar/sql/insert.rb",
42
45
  "lib/orientdb-ar/sql/query.rb",
43
46
  "lib/orientdb-ar/sql/update.rb",
44
- "lib/orientdb-ar/validations.rb",
45
47
  "orientdb-ar.gemspec",
46
48
  "spec/base_spec.rb",
47
49
  "spec/dirty_spec.rb",
@@ -51,36 +53,42 @@ Gem::Specification.new do |s|
51
53
  "spec/models/address.rb",
52
54
  "spec/models/customer.rb",
53
55
  "spec/models/flo_admin.rb",
56
+ "spec/models/invoice.rb",
57
+ "spec/models/invoice_line.rb",
54
58
  "spec/models/person.rb",
55
59
  "spec/models/phone_number.rb",
60
+ "spec/models/product.rb",
56
61
  "spec/models/simple_person.rb",
57
62
  "spec/serialization_spec.rb",
58
63
  "spec/spec.opts",
59
64
  "spec/spec_helper.rb"
60
65
  ]
61
66
  s.homepage = %q{http://rubygems.org/gems/orientdb}
62
- s.require_paths = ["lib"]
67
+ s.require_paths = [%q{lib}]
63
68
  s.rubyforge_project = %q{orientdb-ar}
64
- s.rubygems_version = %q{1.4.2}
69
+ s.rubygems_version = %q{1.8.9}
65
70
  s.summary = %q{ActiveRecord-like persistency through OrientDB in JRuby}
66
71
 
67
72
  if s.respond_to? :specification_version then
68
73
  s.specification_version = 3
69
74
 
70
75
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
71
- s.add_runtime_dependency(%q<orientdb>, ["= 0.0.15"])
76
+ s.add_runtime_dependency(%q<orientdb>, [">= 0.0.20"])
72
77
  s.add_runtime_dependency(%q<activemodel>, [">= 3.0.3"])
78
+ s.add_runtime_dependency(%q<jnunemaker-validatable>, ["= 1.8.4"])
73
79
  s.add_development_dependency(%q<awesome_print>, [">= 0"])
74
80
  s.add_development_dependency(%q<rspec>, [">= 2.4"])
75
81
  else
76
- s.add_dependency(%q<orientdb>, ["= 0.0.15"])
82
+ s.add_dependency(%q<orientdb>, [">= 0.0.20"])
77
83
  s.add_dependency(%q<activemodel>, [">= 3.0.3"])
84
+ s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.4"])
78
85
  s.add_dependency(%q<awesome_print>, [">= 0"])
79
86
  s.add_dependency(%q<rspec>, [">= 2.4"])
80
87
  end
81
88
  else
82
- s.add_dependency(%q<orientdb>, ["= 0.0.15"])
89
+ s.add_dependency(%q<orientdb>, [">= 0.0.20"])
83
90
  s.add_dependency(%q<activemodel>, [">= 3.0.3"])
91
+ s.add_dependency(%q<jnunemaker-validatable>, ["= 1.8.4"])
84
92
  s.add_dependency(%q<awesome_print>, [">= 0"])
85
93
  s.add_dependency(%q<rspec>, [">= 2.4"])
86
94
  end
data/spec/dirty_spec.rb CHANGED
@@ -1,33 +1,30 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
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
3
+ describe "ActiveModel::Dirty" do
4
+
5
+ it "should comply" do
6
+ @person = Person.create :name => "Uncle Bob", :age => 45
7
+ @person.changed?.should == false
8
+
9
+ @person.name = 'Bob'
10
+ @person.changed?.should == true
11
+ @person.name_changed?.should == true
12
+ @person.name_was.should == 'Uncle Bob'
13
+ @person.name_change.should == ['Uncle Bob', 'Bob']
14
+ @person.name = 'Bill'
15
+ @person.name_change.should == ['Uncle Bob', 'Bill']
16
+
17
+ @person.save
18
+ @person.changed?.should == false
19
+ @person.name_changed?.should == false
20
+
21
+ @person.name = 'Bill'
22
+ @person.name_changed?.should == false
23
+ @person.name_change.should == nil
24
+
25
+ @person.name = 'Bob'
26
+ @person.changed.should == ['name']
27
+ @person.changes.should == { 'name' => ['Bill', 'Bob'] }
31
28
  end
32
29
 
33
30
  end
@@ -17,10 +17,10 @@ module RspecRailsMatchers
17
17
  it { should respond_to(:to_param) }
18
18
 
19
19
  it { should respond_to(:valid?) }
20
- its(:valid?) { should be_a_boolean }
20
+ #its(:valid?) { should be_a_boolean }
21
21
 
22
22
  it { should respond_to(:persisted?) }
23
- its(:persisted?) { should be_a_boolean }
23
+ #its(:persisted?) { should be_a_boolean }
24
24
 
25
25
  its(:class) { should respond_to(:model_name) }
26
26
 
data/spec/lint_spec.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
- describe "ActiveModel" do
3
+ describe "ActiveModel::Lint" do
4
+ include RspecRailsMatchers::Behavior::Lint
4
5
 
5
6
  describe Person do
6
7
  it_should_behave_like AnActiveModel
@@ -41,7 +42,7 @@ describe "ActiveModel" do
41
42
  @person.serializable_hash.should == {"name" => nil}
42
43
  @person.as_json.should == {"name" => nil}
43
44
  @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
+ @person.to_xml.should == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<simple-person>\n <name nil=\"true\"></name>\n</simple-person>\n"
45
46
 
46
47
  @person.name = "Bob"
47
48
  @person.serializable_hash.should == {"name" => "Bob"}
data/spec/model_spec.rb CHANGED
@@ -2,100 +2,230 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "Model" do
4
4
 
5
- it "should have a valid connection" do
6
- Person.connection.should be_a_kind_of OrientDB::DocumentDatabase
5
+ describe :connection do
6
+ it "should have a valid connection" do
7
+ Person.connection.should be_a_kind_of OrientDB::DocumentDatabase
8
+ end
7
9
  end
8
10
 
9
- it "should have the right schema" do
10
- Person.new.should be_a_kind_of Person
11
- Person.oclass_name.should == 'Person'
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] }
11
+ describe :schema do
12
+ it "should have the right schema" do
13
+ Person.new.should be_a_kind_of Person
14
+ Person.oclass_name.should == 'Person'
15
+ Person.fields.keys.should == [:name, :age, :tags]
16
+ Person.fields[:name].should == { :type => :string, :not_null => true }
17
+ Person.fields[:age].should == { :type => :int }
18
+ Person.fields[:tags].should == { :type => [:list, :string] }
19
+ end
16
20
  end
17
21
 
18
- it "should create working models" do
19
- p = Person.create :name => "Tester Testing", :age => 45, :tags => %w{ tech_support rocks }
22
+ describe :attributes do
23
+ it "should create working models" do
24
+ p = Person.create :name => "Tester Testing", :age => 45, :tags => %w{ tech_support rocks }
20
25
 
21
- p.name.should == "Tester Testing"
22
- p.age.should == 45
23
- p.tags.should == %w{ tech_support rocks }
26
+ p.name.should == "Tester Testing"
27
+ p.age.should == 45
28
+ p.tags.should == %w{ tech_support rocks }
24
29
 
25
- p2 = Person.first :name => "Tester Testing"
26
- p2.name.should == p.name
27
- p2.age.should == p.age
28
- p2.tags.map.should == p.tags
30
+ p2 = Person.first :name => "Tester Testing"
31
+ p2.name.should == p.name
32
+ p2.age.should == p.age
33
+ p2.tags.map.should == p.tags
34
+ end
29
35
  end
30
36
 
31
- it "should handle embedded models as relations" do
32
- Customer.clear
33
-
34
- a = Address.new :street => "123 S Main", :city => "Salt Lake", :state => "UT", :country => "USA"
35
- p1 = PhoneNumber.new :number => "123456789"
36
- p2 = PhoneNumber.new :number => "987654321"
37
- c1 = Customer.create :name => "Albert Einstein", :number => 1, :address => a, :phones => [p1, p2]
38
-
39
- c2 = Customer.first
40
- c1.should == c2
37
+ describe :embedded do
38
+ it "should handle embedds_one models as relations" do
39
+ Customer.clear
40
+ Customer.count.should <= 0
41
+
42
+ a = Address.new :street => "123 S Main", :city => "Salt Lake", :state => "UT", :country => "USA"
43
+ c1 = Customer.create :name => "Albert Einstein", :number => 1, :address => a, :age => 35
44
+ c1.saved?.should == true
45
+ c1.address.should == a
46
+ c1.phones.should == []
47
+
48
+ c2 = Customer.first
49
+ c1.should == c2
50
+ c2.address.should == a
51
+ c2.phones.should == []
52
+ end
53
+
54
+ it "should handle embedds_many models as relations" do
55
+ Customer.clear
56
+ Customer.count.should <= 0
57
+
58
+ p1 = PhoneNumber.new :number => "123456789"
59
+ p2 = PhoneNumber.new :number => "987654321"
60
+ c1 = Customer.create :name => "Albert Einstein", :number => 1, :phones => [p1, p2], :age => 35
61
+ c1.saved?.should == true
62
+ c1.address.should == nil
63
+ c1.phones.should == [p1, p2]
64
+
65
+ c2 = Customer.first
66
+ c1.should == c2
67
+ c2.address.should == nil
68
+ c2.phones.should == [p1, p2]
69
+ end
70
+
71
+ it "should handle mixed embedded models as relations" do
72
+ Customer.clear
73
+ Customer.count.should <= 0
74
+
75
+ a = Address.new :street => "123 S Main", :city => "Salt Lake", :state => "UT", :country => "USA"
76
+ p1 = PhoneNumber.new :number => "123456789"
77
+ p2 = PhoneNumber.new :number => "987654321"
78
+ c1 = Customer.create :name => "Albert Einstein", :number => 1, :address => a, :phones => [p1, p2], :age => 35
79
+ c1.saved?.should == true
80
+ c1.address.should == a
81
+ c1.phones.should == [p1, p2]
82
+
83
+ c2 = Customer.first
84
+ c1.should == c2
85
+ c2.address.should == a
86
+ c2.phones.should == [p1, p2]
87
+ end
41
88
  end
42
89
 
43
- it "should find models" do
44
- OrientDB::SQL.monkey_patch! Symbol
45
- Person.clear
46
-
47
- p1 = Person.create :name => "Hans Solo", :age => 38, :tags => %w{ fighter pilot }
48
- p2 = Person.create :name => "Yoda", :age => 387, :tags => %w{ genius jedi }
49
- p3 = Person.create :name => "Luke Skywalker", :age => 28, :tags => %w{ jedi fighter pilot }
50
- p4 = Person.create :name => "Princess Leia", :age => 28, :tags => %w{ fighter royalty }
51
- p5 = Person.create :name => "Chewbaca", :age => 53, :tags => %w{ fighter brave hairy }
52
-
53
- Person.where(:name.like('%w%')).all.should == [p3, p5]
54
- Person.where(:age.gt(28), :age.lt(75)).all.should == [p1, p5]
55
- Person.where("'jedi' IN tags").all.should == [p2, p3]
56
- Person.where("'fighter' IN tags", :age.lte(28)).order(:name.desc).all.first.should == p4
90
+ describe :linked do
91
+ it "should handle mixed linked models as relations" do
92
+ Invoice.clear; InvoiceLine.clear; Product.clear; Customer.clear
93
+
94
+ c1 = Customer.create :name => "Generous Buyer", :age => 38, :number => 1001
95
+ p1 = Product.create :sku => "h1", :title => "Hammer", :price => 7.25
96
+ p2 = Product.create :sku => "n2", :title => "9in Nail", :price => 0.12
97
+ l1 = InvoiceLine.create :product => p1, :quantity => 1, :price => p1.price
98
+ l2 = InvoiceLine.create :product => p2, :quantity => 10, :price => p2.price
99
+ total = l1.quantity * l1.price + l2.quantity * l2.price
100
+ i1 = Invoice.create :number => 417, :customer => c1, :sold_on => Date.today, :total => total, :lines => [l1, l2]
101
+ l3 = InvoiceLine.create :product => p1, :quantity => 2, :price => p1.price
102
+ i3 = Invoice.create :number => 418, :customer => c1, :sold_on => Date.today, :lines => [l3]
103
+ l4 = InvoiceLine.create :product => p2, :quantity => 3, :price => p2.price
104
+ i4 = Invoice.create :number => 419, :customer => c1, :sold_on => Date.today, :lines => [l4]
105
+ i1.saved?.should == true
106
+ i1.customer.should == c1
107
+ i1.lines.should == [l1, l2]
108
+ i1.lines[0].product.should == p1
109
+ i1.lines[1].product.should == p2
110
+
111
+ i2 = Invoice.first
112
+ i2.customer.should == c1
113
+ i2.lines.map { |x| x.rid }.should == [l1.rid, l2.rid]
114
+ i2.lines[0].product.rid.should == p1.rid
115
+ i2.lines[1].product.rid.should == p2.rid
116
+ i2.rid.should == i1.rid
117
+ end
57
118
  end
58
119
 
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]
120
+ describe "SQL" do
121
+ describe "query" do
122
+ it "should find models" do
123
+ OrientDB::SQL.monkey_patch! Symbol
124
+ Person.clear
125
+ Person.count.should == 0
126
+
127
+ p1 = Person.create :name => "Hans Solo", :age => 38, :tags => %w{ fighter pilot }
128
+ p2 = Person.create :name => "Yoda", :age => 387, :tags => %w{ genius jedi }
129
+ p3 = Person.create :name => "Luke Skywalker", :age => 28, :tags => %w{ jedi fighter pilot }
130
+ p4 = Person.create :name => "Princess Leia", :age => 28, :tags => %w{ fighter royalty }
131
+ p5 = Person.create :name => "Chewbaca", :age => 53, :tags => %w{ fighter brave hairy }
132
+
133
+ Person.where(:name.like('%w%')).all.should == [p3, p5]
134
+ Person.where(:age.gt(28), :age.lt(75)).all.should == [p1, p5]
135
+ Person.where("'jedi' IN tags").all.should == [p2, p3]
136
+ Person.where("'fighter' IN tags", :age.lte(28)).order(:name.desc).all.first.should == p4
137
+ end
138
+ end
139
+
140
+ describe "update" do
141
+ it "should update models" do
142
+ Person.clear
143
+ Person.count.should == 0
144
+
145
+ p1 = Person.create :name => "Hans Solo", :age => 38, :tags => %w{ fighter pilot }
146
+ p2 = Person.create :name => "Yoda", :age => 387, :tags => %w{ genius jedi }
147
+ p3 = Person.create :name => "Luke Skywalker", :age => 28, :tags => %w{ jedi fighter pilot }
148
+
149
+ Person.where(:name => "Hans Solo").update(:name => "Hans Meister")
150
+ Person.where(:name => "Hans Meister").all.map { |x| x.rid }.should == [p1.rid]
151
+ p1.reload
152
+ Person.where(:name => "Hans Meister").all.should == [p1]
153
+
154
+
155
+ Person.update(:age => 45)
156
+ Person.where(:age => 45).all.map { |x| x.name }.should == [p1.name, p2.name, p3.name]
157
+ end
158
+ end
159
+
160
+ describe "delete" do
161
+ it "should delete models" do
162
+ Person.delete
163
+ Person.count.should == 0
164
+
165
+ Person.create :name => "Hans Solo", :age => 38, :tags => %w{ fighter pilot }
166
+ Person.create :name => "Yoda", :age => 387, :tags => %w{ genius jedi }
167
+ Person.create :name => "Luke Skywalker", :age => 28, :tags => %w{ jedi fighter pilot }
168
+
169
+ Person.count.should == 3
170
+
171
+ Person.where(:name => "Hans Solo").delete
172
+ Person.count.should == 2
173
+
174
+ Person.delete
175
+ Person.count.should == 0
176
+ end
177
+ end
178
+
179
+ describe "insert" do
180
+ it "should insert models" do
181
+ Person.delete
182
+ Person.count.should == 0
183
+
184
+ p1 = Person.insert :name => "Hans Solo", :age => 38, :tags => %w{ fighter pilot }
185
+ Person.count.should == 1
186
+
187
+ p2 = Person.first
188
+ p1.should == p2
189
+ end
190
+ end
74
191
  end
75
192
 
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
193
+ describe :validations do
194
+ it "should validate models correctly" do
195
+ Person.delete
196
+ Person.count.should == 0
197
+
198
+ p1 = Person.new
199
+ p1.save.should == false
200
+ p1.errors.size.should == 2
201
+ p1.errors.keys.should == [:name, :age]
202
+ p1.errors[:name].should == ["can't be empty"]
203
+ p1.errors[:age].should == ["can't be empty"]
204
+
205
+ p1.name = "Tester"
206
+ p1.valid?.should == false
207
+ p1.errors.size.should == 1
208
+ p1.errors[:age].should == ["can't be empty"]
209
+
210
+ p1.age = 25
211
+ p1.valid?.should == true
212
+ p1.errors.size.should == 0
213
+
214
+ p1.save.should == true
215
+ end
90
216
  end
91
217
 
92
- it "should insert models" do
93
- Person.clear
218
+ describe :validate_embedded do
219
+ it "should validate properly with embedded documents" do
220
+ Customer.clear
94
221
 
95
- p1 = Person.insert :name => "Hans Solo", :age => 38, :tags => %w{ fighter pilot }
96
- Person.count.should == 1
222
+ c1 = Customer.new :name => "Tester", :age => 37
223
+ c1.valid?.should == true
97
224
 
98
- p2 = Person.first
99
- p1.should == p2
225
+ c1.build_address :street => "4647 Pagentry", :city => "Sandy"
226
+ c1.valid?.should == false
227
+ c1.errors.size.should == 1
228
+ c1.errors[:address].should == ["#-1:-1 State can't be empty"]
229
+ end
100
230
  end
101
231
  end