datamapper 0.3.2 → 0.9.3
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +0 -0
- data/Manifest.txt +5 -0
- data/README.txt +11 -0
- data/Rakefile +70 -0
- data/lib/datamapper.rb +8 -0
- metadata +152 -319
- data/CHANGELOG +0 -145
- data/FAQ +0 -96
- data/MIT-LICENSE +0 -22
- data/QUICKLINKS +0 -12
- data/README +0 -105
- data/environment.rb +0 -62
- data/example.rb +0 -156
- data/lib/data_mapper.rb +0 -88
- data/lib/data_mapper/adapters/abstract_adapter.rb +0 -43
- data/lib/data_mapper/adapters/data_object_adapter.rb +0 -480
- data/lib/data_mapper/adapters/mysql_adapter.rb +0 -72
- data/lib/data_mapper/adapters/postgresql_adapter.rb +0 -258
- data/lib/data_mapper/adapters/sql/coersion.rb +0 -134
- data/lib/data_mapper/adapters/sql/commands/load_command.rb +0 -545
- data/lib/data_mapper/adapters/sql/mappings/associations_set.rb +0 -34
- data/lib/data_mapper/adapters/sql/mappings/column.rb +0 -279
- data/lib/data_mapper/adapters/sql/mappings/conditions.rb +0 -172
- data/lib/data_mapper/adapters/sql/mappings/schema.rb +0 -60
- data/lib/data_mapper/adapters/sql/mappings/table.rb +0 -459
- data/lib/data_mapper/adapters/sql/quoting.rb +0 -24
- data/lib/data_mapper/adapters/sqlite3_adapter.rb +0 -159
- data/lib/data_mapper/associations.rb +0 -106
- data/lib/data_mapper/associations/belongs_to_association.rb +0 -160
- data/lib/data_mapper/associations/has_and_belongs_to_many_association.rb +0 -437
- data/lib/data_mapper/associations/has_many_association.rb +0 -283
- data/lib/data_mapper/associations/has_n_association.rb +0 -143
- data/lib/data_mapper/associations/reference.rb +0 -47
- data/lib/data_mapper/attributes.rb +0 -73
- data/lib/data_mapper/auto_migrations.rb +0 -36
- data/lib/data_mapper/base.rb +0 -17
- data/lib/data_mapper/callbacks.rb +0 -107
- data/lib/data_mapper/context.rb +0 -112
- data/lib/data_mapper/database.rb +0 -234
- data/lib/data_mapper/dependency_queue.rb +0 -28
- data/lib/data_mapper/embedded_value.rb +0 -145
- data/lib/data_mapper/identity_map.rb +0 -47
- data/lib/data_mapper/is/tree.rb +0 -121
- data/lib/data_mapper/migration.rb +0 -155
- data/lib/data_mapper/persistence.rb +0 -852
- data/lib/data_mapper/property.rb +0 -310
- data/lib/data_mapper/query.rb +0 -164
- data/lib/data_mapper/support/blank.rb +0 -35
- data/lib/data_mapper/support/connection_pool.rb +0 -117
- data/lib/data_mapper/support/enumerable.rb +0 -35
- data/lib/data_mapper/support/errors.rb +0 -16
- data/lib/data_mapper/support/inflector.rb +0 -265
- data/lib/data_mapper/support/object.rb +0 -54
- data/lib/data_mapper/support/serialization.rb +0 -96
- data/lib/data_mapper/support/silence.rb +0 -10
- data/lib/data_mapper/support/string.rb +0 -72
- data/lib/data_mapper/support/struct.rb +0 -7
- data/lib/data_mapper/support/symbol.rb +0 -82
- data/lib/data_mapper/support/typed_set.rb +0 -65
- data/lib/data_mapper/types/base.rb +0 -44
- data/lib/data_mapper/types/string.rb +0 -34
- data/lib/data_mapper/validatable_extensions/errors.rb +0 -12
- data/lib/data_mapper/validatable_extensions/macros.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validatable_instance_methods.rb +0 -62
- data/lib/data_mapper/validatable_extensions/validation_base.rb +0 -18
- data/lib/data_mapper/validatable_extensions/validations/formats/email.rb +0 -43
- data/lib/data_mapper/validatable_extensions/validations/validates_acceptance_of.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validations/validates_confirmation_of.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validations/validates_each.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validations/validates_format_of.rb +0 -28
- data/lib/data_mapper/validatable_extensions/validations/validates_length_of.rb +0 -15
- data/lib/data_mapper/validatable_extensions/validations/validates_numericality_of.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validations/validates_presence_of.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validations/validates_true_for.rb +0 -7
- data/lib/data_mapper/validatable_extensions/validations/validates_uniqueness_of.rb +0 -40
- data/lib/data_mapper/validations.rb +0 -20
- data/lib/data_mapper/validations/number_validator.rb +0 -40
- data/lib/data_mapper/validations/string_validator.rb +0 -20
- data/lib/data_mapper/validations/validator.rb +0 -13
- data/performance.rb +0 -307
- data/plugins/can_has_sphinx/LICENSE +0 -23
- data/plugins/can_has_sphinx/README +0 -4
- data/plugins/can_has_sphinx/REVISION +0 -1
- data/plugins/can_has_sphinx/Rakefile +0 -22
- data/plugins/can_has_sphinx/init.rb +0 -1
- data/plugins/can_has_sphinx/install.rb +0 -1
- data/plugins/can_has_sphinx/lib/acts_as_sphinx.rb +0 -123
- data/plugins/can_has_sphinx/lib/sphinx.rb +0 -460
- data/plugins/can_has_sphinx/scripts/sphinx.sh +0 -47
- data/plugins/can_has_sphinx/tasks/acts_as_sphinx_tasks.rake +0 -41
- data/profile_data_mapper.rb +0 -40
- data/rakefile.rb +0 -159
- data/spec/acts_as_tree_spec.rb +0 -67
- data/spec/adapters/data_object_adapter_spec.rb +0 -31
- data/spec/associations/belongs_to_association_spec.rb +0 -98
- data/spec/associations/has_and_belongs_to_many_association_spec.rb +0 -377
- data/spec/associations/has_many_association_spec.rb +0 -337
- data/spec/attributes_spec.rb +0 -52
- data/spec/auto_migrations_spec.rb +0 -101
- data/spec/callbacks_spec.rb +0 -186
- data/spec/can_has_sphinx.rb +0 -5
- data/spec/coersion_spec.rb +0 -41
- data/spec/column_spec.rb +0 -114
- data/spec/count_command_spec.rb +0 -45
- data/spec/database_spec.rb +0 -18
- data/spec/dataobjects_spec.rb +0 -27
- data/spec/delete_command_spec.rb +0 -11
- data/spec/dependency_spec.rb +0 -29
- data/spec/embedded_value_spec.rb +0 -161
- data/spec/fixtures/animals.yaml +0 -33
- data/spec/fixtures/animals_exhibits.yaml +0 -2
- data/spec/fixtures/careers.yaml +0 -5
- data/spec/fixtures/comments.yaml +0 -1
- data/spec/fixtures/exhibits.yaml +0 -90
- data/spec/fixtures/fruit.yaml +0 -6
- data/spec/fixtures/people.yaml +0 -37
- data/spec/fixtures/posts.yaml +0 -3
- data/spec/fixtures/projects.yaml +0 -13
- data/spec/fixtures/sections.yaml +0 -5
- data/spec/fixtures/serializers.yaml +0 -6
- data/spec/fixtures/tasks.yaml +0 -6
- data/spec/fixtures/tasks_tasks.yaml +0 -2
- data/spec/fixtures/tomatoes.yaml +0 -1
- data/spec/fixtures/users.yaml +0 -1
- data/spec/fixtures/zoos.yaml +0 -24
- data/spec/is_a_tree_spec.rb +0 -149
- data/spec/legacy_spec.rb +0 -16
- data/spec/load_command_spec.rb +0 -322
- data/spec/magic_columns_spec.rb +0 -26
- data/spec/migration_spec.rb +0 -267
- data/spec/mock_adapter.rb +0 -20
- data/spec/models/animal.rb +0 -12
- data/spec/models/candidate.rb +0 -8
- data/spec/models/career.rb +0 -7
- data/spec/models/chain.rb +0 -8
- data/spec/models/comment.rb +0 -6
- data/spec/models/exhibit.rb +0 -14
- data/spec/models/fence.rb +0 -7
- data/spec/models/fruit.rb +0 -8
- data/spec/models/job.rb +0 -8
- data/spec/models/person.rb +0 -30
- data/spec/models/post.rb +0 -14
- data/spec/models/project.rb +0 -41
- data/spec/models/sales_person.rb +0 -5
- data/spec/models/section.rb +0 -8
- data/spec/models/serializer.rb +0 -5
- data/spec/models/task.rb +0 -9
- data/spec/models/tomato.rb +0 -27
- data/spec/models/user.rb +0 -12
- data/spec/models/zoo.rb +0 -13
- data/spec/natural_key_spec.rb +0 -36
- data/spec/paranoia_spec.rb +0 -38
- data/spec/persistence_spec.rb +0 -479
- data/spec/postgres_spec.rb +0 -96
- data/spec/property_spec.rb +0 -151
- data/spec/query_spec.rb +0 -77
- data/spec/save_command_spec.rb +0 -94
- data/spec/schema_spec.rb +0 -8
- data/spec/serialize_spec.rb +0 -19
- data/spec/single_table_inheritance_spec.rb +0 -43
- data/spec/spec_helper.rb +0 -45
- data/spec/support/blank_spec.rb +0 -8
- data/spec/support/inflector_spec.rb +0 -41
- data/spec/support/object_spec.rb +0 -9
- data/spec/support/serialization_spec.rb +0 -61
- data/spec/support/silence_spec.rb +0 -15
- data/spec/support/string_spec.rb +0 -7
- data/spec/support/struct_spec.rb +0 -12
- data/spec/support/typed_set_spec.rb +0 -66
- data/spec/symbolic_operators_spec.rb +0 -27
- data/spec/table_spec.rb +0 -79
- data/spec/types/string.rb +0 -81
- data/spec/validates_confirmation_of_spec.rb +0 -55
- data/spec/validates_format_of_spec.rb +0 -78
- data/spec/validates_length_of_spec.rb +0 -117
- data/spec/validates_uniqueness_of_spec.rb +0 -92
- data/spec/validations/number_validator.rb +0 -59
- data/spec/validations/string_validator.rb +0 -14
- data/spec/validations_spec.rb +0 -141
- data/tasks/fixtures.rb +0 -53
data/spec/models/project.rb
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
class Project #< DataMapper::Base # please do not remove this
|
2
|
-
include DataMapper::Persistence
|
3
|
-
|
4
|
-
property :title, :string
|
5
|
-
property :description, :string
|
6
|
-
property :deleted_at, :datetime
|
7
|
-
|
8
|
-
has_many :sections
|
9
|
-
|
10
|
-
before_create :create_main_section
|
11
|
-
|
12
|
-
def tickets
|
13
|
-
return [] if sections.empty?
|
14
|
-
sections.map { |section| section.tickets }
|
15
|
-
end
|
16
|
-
|
17
|
-
|
18
|
-
def set_us_up_the_bomb=(val)
|
19
|
-
@set_us_up_the_bomb = !val.blank?
|
20
|
-
end
|
21
|
-
|
22
|
-
def set_up_for_bomb?
|
23
|
-
@set_us_up_the_bomb
|
24
|
-
end
|
25
|
-
|
26
|
-
def wery_sneaky?
|
27
|
-
@be_wery_sneaky
|
28
|
-
end
|
29
|
-
|
30
|
-
|
31
|
-
private
|
32
|
-
|
33
|
-
def create_main_section
|
34
|
-
sections << Section.find_or_create(:title => "Main") if sections.empty?
|
35
|
-
end
|
36
|
-
|
37
|
-
def be_wery_sneaky=(val)
|
38
|
-
@be_wery_sneaky = val
|
39
|
-
end
|
40
|
-
|
41
|
-
end
|
data/spec/models/sales_person.rb
DELETED
data/spec/models/section.rb
DELETED
data/spec/models/serializer.rb
DELETED
data/spec/models/task.rb
DELETED
@@ -1,9 +0,0 @@
|
|
1
|
-
class Task #< DataMapper::Base # please do not remove this
|
2
|
-
include DataMapper::Persistence
|
3
|
-
|
4
|
-
property :name, :string, :default => "No Name", :index => :unique
|
5
|
-
property :notes, :string
|
6
|
-
property :completed, :boolean, :default => false
|
7
|
-
|
8
|
-
has_and_belongs_to_many :tasks
|
9
|
-
end
|
data/spec/models/tomato.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
class Tomato
|
2
|
-
include DataMapper::Persistence
|
3
|
-
|
4
|
-
ATTRIBUTES << :bruised
|
5
|
-
|
6
|
-
def initialize(details = nil)
|
7
|
-
super
|
8
|
-
|
9
|
-
@name = 'Ugly'
|
10
|
-
@init_run = true
|
11
|
-
@bruised = true
|
12
|
-
end
|
13
|
-
|
14
|
-
def initialized?
|
15
|
-
@init_run
|
16
|
-
end
|
17
|
-
|
18
|
-
property :name, :string
|
19
|
-
|
20
|
-
def heal!
|
21
|
-
@bruised = false
|
22
|
-
end
|
23
|
-
|
24
|
-
def bruised?
|
25
|
-
@bruised
|
26
|
-
end
|
27
|
-
end
|
data/spec/models/user.rb
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
# This uses the inheritance method of declaring
|
2
|
-
# a model to boost spec coverage and ensure that the to
|
3
|
-
# ways of declaring a model are compatible within the
|
4
|
-
# same project and/or object graphs.
|
5
|
-
class User < DataMapper::Base
|
6
|
-
|
7
|
-
property :name, :string
|
8
|
-
property :email, :string, :format => :email_address
|
9
|
-
has_many :comments, :class => 'Comment', :foreign_key => 'user_id'
|
10
|
-
|
11
|
-
after_create { false }
|
12
|
-
end
|
data/spec/models/zoo.rb
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
class Zoo #< DataMapper::Base # please do not remove this
|
2
|
-
include DataMapper::Persistence
|
3
|
-
|
4
|
-
property :name, :string, :nullable => false, :default => "Zoo"
|
5
|
-
property :notes, :text
|
6
|
-
property :updated_at, :datetime
|
7
|
-
|
8
|
-
has_many :exhibits
|
9
|
-
|
10
|
-
def name=(val)
|
11
|
-
@name = (val == "Colorado Springs") ? "Cheyenne Mountain" : val
|
12
|
-
end
|
13
|
-
end
|
data/spec/natural_key_spec.rb
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + "/spec_helper"
|
2
|
-
|
3
|
-
describe('A Natural Key') do
|
4
|
-
|
5
|
-
it "should cause the Table to return a default foreign key composed of it's table and key column name" do
|
6
|
-
database.table(Person).default_foreign_key.should eql('person_id')
|
7
|
-
database.table(Career).default_foreign_key.should eql('career_name')
|
8
|
-
end
|
9
|
-
|
10
|
-
it "should load the object based on it's natural key" do
|
11
|
-
programmer = Career['Programmer']
|
12
|
-
programmer.should_not be_nil
|
13
|
-
programmer.name.should eql('Programmer')
|
14
|
-
programmer.attributes.should include(:name)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "should load an association based on the natural key" do
|
18
|
-
database do
|
19
|
-
programmer = Career['Programmer']
|
20
|
-
programmer.followers.should have(2).entries
|
21
|
-
|
22
|
-
sam = Person.first(:name => 'Sam')
|
23
|
-
scott = Person.first(:name => 'Scott')
|
24
|
-
|
25
|
-
programmer.followers.should include(sam)
|
26
|
-
programmer.followers.should include(scott)
|
27
|
-
|
28
|
-
peon = Career['Peon']
|
29
|
-
peon.followers.should have(1).entries
|
30
|
-
|
31
|
-
bob = Person.first(:name => 'Bob')
|
32
|
-
peon.followers.should include(bob)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|
data/spec/paranoia_spec.rb
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + "/spec_helper"
|
2
|
-
|
3
|
-
describe 'Paranoia' do
|
4
|
-
|
5
|
-
before(:all) do
|
6
|
-
class Scared #< DataMapper::Base # please do not remove this
|
7
|
-
include DataMapper::Persistence
|
8
|
-
|
9
|
-
property :name, :string
|
10
|
-
property :deleted_at, :datetime
|
11
|
-
end
|
12
|
-
|
13
|
-
Scared.auto_migrate!
|
14
|
-
end
|
15
|
-
|
16
|
-
after(:all) do
|
17
|
-
database.table(Scared).drop!
|
18
|
-
end
|
19
|
-
|
20
|
-
it "should not be found" do
|
21
|
-
cat = Scared.create(:name => 'bob')
|
22
|
-
|
23
|
-
database.query('SELECT name FROM scareds').should have(1).entries
|
24
|
-
|
25
|
-
cat2 = Scared.first(:name => 'bob')
|
26
|
-
cat2.should_not be_nil
|
27
|
-
cat2.should be_a_kind_of(Scared)
|
28
|
-
|
29
|
-
cat.destroy!
|
30
|
-
|
31
|
-
Scared.first(:name => 'bob').should be_nil
|
32
|
-
|
33
|
-
Scared.first.should be_nil
|
34
|
-
|
35
|
-
database.query('SELECT name FROM scareds').should have(1).entries
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|
data/spec/persistence_spec.rb
DELETED
@@ -1,479 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + "/spec_helper"
|
2
|
-
|
3
|
-
describe "DataMapper::Persistence::ConvenienceMethods::ClassMethods" do
|
4
|
-
|
5
|
-
before(:all) do
|
6
|
-
fixtures(:animals)
|
7
|
-
fixtures(:exhibits)
|
8
|
-
end
|
9
|
-
|
10
|
-
describe 'A record' do
|
11
|
-
it 'should save and return true if validations pass' do
|
12
|
-
count = Exhibit.count
|
13
|
-
bugs_and_more_bugs = Exhibit.new(:name => 'Bugs And More Bugs')
|
14
|
-
bugs_and_more_bugs.save.should be_true
|
15
|
-
Exhibit.count.should == count + 1
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'should return false on save if validations fail' do
|
19
|
-
count = Exhibit.count
|
20
|
-
bugs_and_more_bugs = Exhibit.new
|
21
|
-
bugs_and_more_bugs.save.should be_false
|
22
|
-
Exhibit.count.should == count
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'should reload its attributes' do
|
26
|
-
frog = Animal.first(:name => 'Frog')
|
27
|
-
frog.name = 'MegaFrog'
|
28
|
-
frog.name.should == 'MegaFrog'
|
29
|
-
frog.reload!
|
30
|
-
frog.name.should == 'Frog'
|
31
|
-
end
|
32
|
-
|
33
|
-
it "should prepare it's associations for reload" do
|
34
|
-
chippy = Animal.first(:name => 'Cup')
|
35
|
-
amazonia = Exhibit.first(:name => 'Amazonia')
|
36
|
-
amazonia.animals << chippy
|
37
|
-
amazonia.animals.should include(chippy)
|
38
|
-
amazonia.reload!
|
39
|
-
amazonia.animals.should_not include(chippy)
|
40
|
-
end
|
41
|
-
|
42
|
-
it 'should be destroyed!' do
|
43
|
-
capybara = Animal.create(:name => 'Capybara')
|
44
|
-
count = Animal.count
|
45
|
-
capybara.destroy!
|
46
|
-
Animal.first(:name => 'Capybara').should be_nil
|
47
|
-
Animal.count.should == count - 1
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
it 'should return the first match using find_or_create' do
|
52
|
-
count = Animal.count
|
53
|
-
frog = Animal.find_or_create(:name => 'Frog')
|
54
|
-
frog.name.should == 'Frog'
|
55
|
-
Animal.count.should == count
|
56
|
-
end
|
57
|
-
|
58
|
-
it 'should create a record if a match is not found with find_or_create' do
|
59
|
-
count = Animal.count
|
60
|
-
capybara = Animal.find_or_create(:name => 'Capybara')
|
61
|
-
capybara.name.should == 'Capybara'
|
62
|
-
Animal.count.should == count + 1
|
63
|
-
end
|
64
|
-
|
65
|
-
it 'should return all records' do
|
66
|
-
all_animals = Animal.all
|
67
|
-
all_animals.length.should == Animal.count
|
68
|
-
all_animals.each do |animal|
|
69
|
-
animal.class.should == Animal
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
it 'should return the first record' do
|
74
|
-
Animal.first.should == Animal.find(:first)
|
75
|
-
end
|
76
|
-
|
77
|
-
it 'should return a count of the records' do
|
78
|
-
Animal.count.should == Animal.find_by_sql("SELECT COUNT(*) FROM animals")[0]
|
79
|
-
end
|
80
|
-
|
81
|
-
it 'should delete all records' do
|
82
|
-
Animal.delete_all
|
83
|
-
Animal.count.should == 0
|
84
|
-
|
85
|
-
fixtures(:animals)
|
86
|
-
end
|
87
|
-
|
88
|
-
#it 'should be truncated' do
|
89
|
-
# Animal.truncate!
|
90
|
-
# Animal.count.should == 0
|
91
|
-
#
|
92
|
-
# fixtures(:animals)
|
93
|
-
#end
|
94
|
-
|
95
|
-
it 'should find a matching record given an id' do
|
96
|
-
Animal.find(1).name.should == 'Frog'
|
97
|
-
end
|
98
|
-
|
99
|
-
it 'should find all records' do
|
100
|
-
Animal.find(:all).length.should == Animal.count
|
101
|
-
end
|
102
|
-
|
103
|
-
it 'should find all matching records given some condition' do
|
104
|
-
Animal.find(:all, :conditions => ["name = ?", "Frog"])[0].name.should == 'Frog'
|
105
|
-
end
|
106
|
-
|
107
|
-
it 'should find the first matching record' do
|
108
|
-
Animal.find(:first).name.should == 'Frog'
|
109
|
-
end
|
110
|
-
|
111
|
-
it 'should find the first matching record given some condition' do
|
112
|
-
Animal.find(:first, :conditions => ["name = ?", "Frog"]).name.should == 'Frog'
|
113
|
-
end
|
114
|
-
|
115
|
-
it 'should select records using the supplied sql fragment' do
|
116
|
-
Animal.find_by_sql("SELECT name FROM animals WHERE name='Frog'")[0].should == 'Frog'
|
117
|
-
end
|
118
|
-
|
119
|
-
it 'should retrieve the indexed element' do
|
120
|
-
Animal[1].id.should == 1
|
121
|
-
end
|
122
|
-
|
123
|
-
it 'should create a new record' do
|
124
|
-
count = Animal.count
|
125
|
-
capybara = Animal.create(:name => 'Capybara')
|
126
|
-
capybara.name.should == 'Capybara'
|
127
|
-
Animal.count.should == count + 1
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
|
132
|
-
# Can't describe DataMapper::Persistence because
|
133
|
-
# rspec will include it for some crazy reason!
|
134
|
-
describe "DataMapper::Persistence" do
|
135
|
-
|
136
|
-
it "should raise ObjectNotFoundError for missing objects with the indexer finder" do
|
137
|
-
# pending "http://wm.lighthouseapp.com/projects/4819-datamapper/tickets/111-should-raise-objectnotfounderror-on-the-indexer-finder"
|
138
|
-
lambda { Zoo[900] }.should raise_error(DataMapper::ObjectNotFoundError)
|
139
|
-
end
|
140
|
-
|
141
|
-
it "should raise IncompleteModelDefinitionError for a model with no properties" do
|
142
|
-
lambda { class IncompleteZoo; include DataMapper::Persistence; end; IncompleteZoo.new }.should raise_error(DataMapper::Persistence::IncompleteModelDefinitionError)
|
143
|
-
end
|
144
|
-
|
145
|
-
it "attributes method should load all lazy-loaded values" do
|
146
|
-
Animal.first(:name => 'Cup').attributes[:notes].should == 'I am a Cup!'
|
147
|
-
end
|
148
|
-
|
149
|
-
it "mass assignment should call methods" do
|
150
|
-
class Animal
|
151
|
-
attr_reader :test
|
152
|
-
def test=(value)
|
153
|
-
@test = value + '!'
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
a = Animal.new(:test => 'testing')
|
158
|
-
a.test.should == 'testing!'
|
159
|
-
end
|
160
|
-
|
161
|
-
it "should be dirty" do
|
162
|
-
x = Person.create(:name => 'a')
|
163
|
-
x.should_not be_dirty
|
164
|
-
x.name = 'dslfay'
|
165
|
-
x.should be_dirty
|
166
|
-
end
|
167
|
-
|
168
|
-
it "should be dirty when set to nil" do
|
169
|
-
x = Person.create(:name => 'a')
|
170
|
-
x.should_not be_dirty
|
171
|
-
x.name = "asdfasfd"
|
172
|
-
x.should be_dirty
|
173
|
-
end
|
174
|
-
|
175
|
-
it "should return a diff" do
|
176
|
-
x = Person.new(:name => 'Sam', :age => 30, :occupation => 'Programmer')
|
177
|
-
y = Person.new(:name => 'Amy', :age => 21, :occupation => 'Programmer')
|
178
|
-
|
179
|
-
diff = (x ^ y)
|
180
|
-
diff.should include(:name)
|
181
|
-
diff.should include(:age)
|
182
|
-
diff[:name].should eql(['Sam', 'Amy'])
|
183
|
-
diff[:age].should eql([30, 21])
|
184
|
-
|
185
|
-
x.destroy!
|
186
|
-
y.destroy!
|
187
|
-
end
|
188
|
-
|
189
|
-
it "should update attributes" do
|
190
|
-
x = Person.create(:name => 'Sam')
|
191
|
-
x.update_attributes(:age => 30).should eql(true)
|
192
|
-
x.age.should eql(30)
|
193
|
-
x.should_not be_dirty
|
194
|
-
end
|
195
|
-
|
196
|
-
it "should return the table for a given model" do
|
197
|
-
Person.table.should be_a_kind_of(DataMapper::Adapters::Sql::Mappings::Table)
|
198
|
-
end
|
199
|
-
|
200
|
-
it "should support boolean accessors" do
|
201
|
-
dolphin = Animal.first(:name => 'Dolphin')
|
202
|
-
dolphin.should be_nice
|
203
|
-
end
|
204
|
-
|
205
|
-
it "should be comparable" do
|
206
|
-
p1 = Person.create(:name => 'Sam')
|
207
|
-
p2 = Person[p1.id]
|
208
|
-
|
209
|
-
p1.should == p2
|
210
|
-
end
|
211
|
-
|
212
|
-
# This is unnecessary. Use #dirty? to check for changes.
|
213
|
-
# it "should not be equal if attributes have changed" do
|
214
|
-
# p1 = Person.create(:name => 'Sam')
|
215
|
-
# p2 = Person[p1.id]
|
216
|
-
# p2.name = "Paul"
|
217
|
-
#
|
218
|
-
# p1.should_not == p2
|
219
|
-
# end
|
220
|
-
|
221
|
-
end
|
222
|
-
|
223
|
-
describe 'A new record' do
|
224
|
-
|
225
|
-
before(:each) do
|
226
|
-
@bob = Person.new(:name => 'Bob', :age => 30, :occupation => 'Sales')
|
227
|
-
end
|
228
|
-
|
229
|
-
it 'should be dirty' do
|
230
|
-
@bob.dirty?.should == true
|
231
|
-
end
|
232
|
-
|
233
|
-
it 'set attributes should be dirty' do
|
234
|
-
attributes = @bob.attributes.dup.reject { |k,v| k == :id }
|
235
|
-
@bob.dirty_attributes.should == { :name => 'Bob', :age => 30, :occupation => 'Sales' }
|
236
|
-
end
|
237
|
-
|
238
|
-
it 'should be marked as new' do
|
239
|
-
@bob.new_record?.should == true
|
240
|
-
end
|
241
|
-
|
242
|
-
it 'should have a nil id' do
|
243
|
-
@bob.id.should == nil
|
244
|
-
end
|
245
|
-
|
246
|
-
it "should not have dirty attributes when not dirty" do
|
247
|
-
x = Person.create(:name => 'a')
|
248
|
-
x.should_not be_dirty
|
249
|
-
x.dirty_attributes.should be_empty
|
250
|
-
end
|
251
|
-
|
252
|
-
it "should only list attributes that have changed in the dirty attributes hash" do
|
253
|
-
x = Person.create(:name => 'a')
|
254
|
-
x.name = "asdfr"
|
255
|
-
x.should be_dirty
|
256
|
-
x.dirty_attributes.keys.should == [:name]
|
257
|
-
end
|
258
|
-
|
259
|
-
it "should not have original_values when a new record" do
|
260
|
-
x = Person.new(:name => 'a')
|
261
|
-
x.original_values.should be_empty
|
262
|
-
end
|
263
|
-
|
264
|
-
it "should have original_values after saved" do
|
265
|
-
x = Person.new(:name => 'a')
|
266
|
-
x.save
|
267
|
-
x.original_values.should_not be_empty
|
268
|
-
x.original_values.keys.should include(:name)
|
269
|
-
x.original_values[:name].should == 'a'
|
270
|
-
end
|
271
|
-
|
272
|
-
it "should have original values when created" do
|
273
|
-
x = Person.create(:name => 'a')
|
274
|
-
x.original_values.should_not be_empty
|
275
|
-
x.original_values.keys.should include(:name)
|
276
|
-
x.original_values[:name].should == "a"
|
277
|
-
end
|
278
|
-
|
279
|
-
it "should have original values when loaded from the database" do
|
280
|
-
Person.create(:name => 'a')
|
281
|
-
x = Person.first(:name => 'a')
|
282
|
-
x.original_values.should_not be_empty
|
283
|
-
x.original_values.keys.should include(:name)
|
284
|
-
x.original_values[:name].should == "a"
|
285
|
-
end
|
286
|
-
|
287
|
-
it "should reset the original values when not new, changed then saved" do
|
288
|
-
x = Person.create(:name => 'a')
|
289
|
-
x.should_not be_new_record
|
290
|
-
x.original_values[:name].should == "a"
|
291
|
-
x.name = "b"
|
292
|
-
x.save
|
293
|
-
x.original_values[:name].should == "b"
|
294
|
-
end
|
295
|
-
|
296
|
-
it "should allow a value to be set to nil" do
|
297
|
-
x = Person.create(:name => 'a')
|
298
|
-
x.name = nil
|
299
|
-
x.save
|
300
|
-
x.reload!
|
301
|
-
x.name.should be_nil
|
302
|
-
end
|
303
|
-
|
304
|
-
end
|
305
|
-
|
306
|
-
describe 'Properties' do
|
307
|
-
|
308
|
-
it 'should default to public method visibility' do
|
309
|
-
class SoftwareEngineer #< DataMapper::Base # please do not remove this
|
310
|
-
include DataMapper::Persistence
|
311
|
-
|
312
|
-
set_table_name 'people'
|
313
|
-
property :name, :string
|
314
|
-
end
|
315
|
-
|
316
|
-
public_properties = SoftwareEngineer.public_instance_methods.select { |m| ["name", "name="].include?(m) }
|
317
|
-
public_properties.length.should == 2
|
318
|
-
end
|
319
|
-
|
320
|
-
it 'should respect protected property options' do
|
321
|
-
class SanitationEngineer #< DataMapper::Base # please do not remove this
|
322
|
-
include DataMapper::Persistence
|
323
|
-
|
324
|
-
set_table_name 'people'
|
325
|
-
property :name, :string, :reader => :protected
|
326
|
-
property :age, :integer, :writer => :protected
|
327
|
-
end
|
328
|
-
|
329
|
-
protected_properties = SanitationEngineer.protected_instance_methods.select { |m| ["name", "age="].include?(m) }
|
330
|
-
protected_properties.length.should == 2
|
331
|
-
end
|
332
|
-
|
333
|
-
it 'should respect private property options' do
|
334
|
-
class ElectricalEngineer #< DataMapper::Base # please do not remove this
|
335
|
-
include DataMapper::Persistence
|
336
|
-
|
337
|
-
set_table_name 'people'
|
338
|
-
property :name, :string, :reader => :private
|
339
|
-
property :age, :integer, :writer => :private
|
340
|
-
end
|
341
|
-
|
342
|
-
private_properties = ElectricalEngineer.private_instance_methods.select { |m| ["name", "age="].include?(m) }
|
343
|
-
private_properties.length.should == 2
|
344
|
-
end
|
345
|
-
|
346
|
-
it 'should set both reader and writer visibiliy when accessor option is passed' do
|
347
|
-
class TrainEngineer #< DataMapper::Base # please do not remove this
|
348
|
-
include DataMapper::Persistence
|
349
|
-
|
350
|
-
property :name, :string, :accessor => :private
|
351
|
-
end
|
352
|
-
|
353
|
-
private_properties = TrainEngineer.private_instance_methods.select { |m| ["name", "name="].include?(m) }
|
354
|
-
private_properties.length.should == 2
|
355
|
-
end
|
356
|
-
|
357
|
-
it 'should only be listed in attributes if they have public getters' do
|
358
|
-
class SalesEngineer #< DataMapper::Base # please do not remove this
|
359
|
-
include DataMapper::Persistence
|
360
|
-
|
361
|
-
set_table_name 'people'
|
362
|
-
property :name, :string
|
363
|
-
property :age, :integer, :reader => :private
|
364
|
-
end
|
365
|
-
|
366
|
-
@sam = SalesEngineer.first(:name => 'Sam')
|
367
|
-
# note: id default key gets a public reader by default (but writer is protected)
|
368
|
-
@sam.attributes.should == {:id => @sam.id, :name => @sam.name}
|
369
|
-
end
|
370
|
-
|
371
|
-
it 'should not allow mass assignment if private or protected' do
|
372
|
-
class ChemicalEngineer #< DataMapper::Base # please do not remove this
|
373
|
-
include DataMapper::Persistence
|
374
|
-
|
375
|
-
set_table_name 'people'
|
376
|
-
property :name, :string, :writer => :private
|
377
|
-
property :age, :integer
|
378
|
-
end
|
379
|
-
|
380
|
-
@sam = ChemicalEngineer.first(:name => 'Sam')
|
381
|
-
@sam.attributes = {:name => 'frank', :age => 101}
|
382
|
-
@sam.age.should == 101
|
383
|
-
@sam.name.should == 'Sam'
|
384
|
-
end
|
385
|
-
|
386
|
-
it 'should allow :protected to be passed as an alias for a public reader, protected writer' do
|
387
|
-
class CivilEngineer #< DataMapper::Base # please do not remove this
|
388
|
-
include DataMapper::Persistence
|
389
|
-
|
390
|
-
set_table_name 'people'
|
391
|
-
property :name, :string, :protected => true
|
392
|
-
end
|
393
|
-
|
394
|
-
CivilEngineer.public_instance_methods.should include("name")
|
395
|
-
CivilEngineer.protected_instance_methods.should include("name=")
|
396
|
-
end
|
397
|
-
|
398
|
-
it 'should allow :private to be passed as an alias for a public reader, private writer' do
|
399
|
-
class AudioEngineer #< DataMapper::Base # please do not remove this
|
400
|
-
include DataMapper::Persistence
|
401
|
-
|
402
|
-
set_table_name 'people'
|
403
|
-
property :name, :string, :private => true
|
404
|
-
end
|
405
|
-
|
406
|
-
AudioEngineer.public_instance_methods.should include("name")
|
407
|
-
AudioEngineer.private_instance_methods.should include("name=")
|
408
|
-
end
|
409
|
-
|
410
|
-
it 'should raise an error when invalid options are passsed' do
|
411
|
-
lambda do
|
412
|
-
class JumpyCow #< DataMapper::Base # please do not remove this
|
413
|
-
include DataMapper::Persistence
|
414
|
-
|
415
|
-
set_table_name 'animals'
|
416
|
-
property :name, :string, :laze => true
|
417
|
-
end
|
418
|
-
end.should raise_error(ArgumentError)
|
419
|
-
end
|
420
|
-
|
421
|
-
it 'should raise an error when the first argument to index isnt an array' do
|
422
|
-
lambda do
|
423
|
-
class JumpyCow #< DataMapper::Base # please do not remove this
|
424
|
-
include DataMapper::Persistence
|
425
|
-
|
426
|
-
set_table_name 'animals'
|
427
|
-
index :name, :parent
|
428
|
-
end
|
429
|
-
end.should raise_error(ArgumentError)
|
430
|
-
end
|
431
|
-
|
432
|
-
it "should return true on saving a new record" do
|
433
|
-
bob = User.new(:name => 'bob', :email => 'bob@example.com')
|
434
|
-
bob.save!.should == true
|
435
|
-
bob.destroy!
|
436
|
-
end
|
437
|
-
|
438
|
-
it "should assign to public setters" do
|
439
|
-
x = Project.new
|
440
|
-
x.attributes = { :set_us_up_the_bomb => true }
|
441
|
-
x.should be_set_up_for_bomb
|
442
|
-
end
|
443
|
-
|
444
|
-
it "should protect private setters" do
|
445
|
-
x = Project.new
|
446
|
-
x.attributes = { :be_wery_sneaky => true }
|
447
|
-
x.should_not be_wery_sneaky
|
448
|
-
end
|
449
|
-
|
450
|
-
it "should not call initialize on materialization" do
|
451
|
-
# pending "http://wm.lighthouseapp.com/projects/4819-datamapper/tickets/113-use-allocate-to-materialize-objects"
|
452
|
-
x = Tomato.new
|
453
|
-
x.should be_initialized
|
454
|
-
x.save!
|
455
|
-
x.name.should eql('Ugly')
|
456
|
-
x.should be_bruised
|
457
|
-
|
458
|
-
x.name = 'Bob'
|
459
|
-
x.save!
|
460
|
-
|
461
|
-
x2 = Tomato.first(:name => 'Bob')
|
462
|
-
x2.should_not be_bruised
|
463
|
-
x2.heal!
|
464
|
-
x2.should == x
|
465
|
-
x2.name.should eql('Bob')
|
466
|
-
x2.should_not be_initialized
|
467
|
-
|
468
|
-
x3 = Tomato.get(x.key)
|
469
|
-
x3.should_not be_bruised
|
470
|
-
x3.heal!
|
471
|
-
x3.should == x
|
472
|
-
x3.name.should eql('Bob')
|
473
|
-
x3.should_not be_initialized
|
474
|
-
end
|
475
|
-
|
476
|
-
it "should report persistence" do
|
477
|
-
Tomato.should be_persistent
|
478
|
-
end
|
479
|
-
end
|