datamapper 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. data/CHANGELOG +20 -1
  2. data/environment.rb +5 -3
  3. data/lib/data_mapper.rb +42 -23
  4. data/lib/data_mapper/adapters/data_object_adapter.rb +76 -73
  5. data/lib/data_mapper/adapters/mysql_adapter.rb +15 -1
  6. data/lib/data_mapper/adapters/postgresql_adapter.rb +1 -1
  7. data/lib/data_mapper/adapters/sql/commands/load_command.rb +242 -28
  8. data/lib/data_mapper/adapters/sql/mappings/column.rb +18 -1
  9. data/lib/data_mapper/adapters/sql/mappings/table.rb +20 -32
  10. data/lib/data_mapper/adapters/sql/quoting.rb +40 -7
  11. data/lib/data_mapper/adapters/sqlite3_adapter.rb +7 -1
  12. data/lib/data_mapper/associations/has_and_belongs_to_many_association.rb +6 -1
  13. data/lib/data_mapper/associations/has_many_association.rb +1 -1
  14. data/lib/data_mapper/context.rb +4 -2
  15. data/lib/data_mapper/database.rb +2 -12
  16. data/lib/data_mapper/support/active_record_impersonation.rb +1 -1
  17. data/lib/data_mapper/support/blank.rb +2 -2
  18. data/lib/data_mapper/support/serialization.rb +60 -4
  19. data/lib/data_mapper/support/string.rb +24 -2
  20. data/lib/data_mapper/validations/validation_errors.rb +6 -3
  21. data/performance.rb +20 -5
  22. data/plugins/dataobjects/Rakefile +2 -0
  23. data/plugins/dataobjects/do.rb +18 -8
  24. data/plugins/dataobjects/do_mysql.rb +57 -24
  25. data/plugins/dataobjects/do_postgres.rb +3 -7
  26. data/plugins/dataobjects/do_sqlite3.rb +18 -17
  27. data/plugins/dataobjects/swig_mysql/Makefile +146 -0
  28. data/plugins/dataobjects/swig_mysql/extconf.rb +13 -1
  29. data/plugins/dataobjects/swig_mysql/mkmf.log +24 -0
  30. data/plugins/dataobjects/swig_mysql/mysql_c.bundle +0 -0
  31. data/plugins/dataobjects/swig_mysql/mysql_c.c +303 -2501
  32. data/plugins/dataobjects/swig_mysql/mysql_c.i +63 -4
  33. data/plugins/dataobjects/swig_mysql/mysql_c.o +0 -0
  34. data/profile_data_mapper.rb +4 -4
  35. data/rakefile.rb +13 -7
  36. data/spec/acts_as_tree_spec.rb +2 -0
  37. data/spec/associations_spec.rb +12 -0
  38. data/spec/attributes_spec.rb +2 -0
  39. data/spec/base_spec.rb +2 -0
  40. data/spec/callbacks_spec.rb +2 -0
  41. data/spec/can_has_sphinx.rb +0 -1
  42. data/spec/coersion_spec.rb +10 -3
  43. data/spec/column_spec.rb +23 -0
  44. data/spec/conditions_spec.rb +18 -18
  45. data/spec/count_command_spec.rb +2 -0
  46. data/spec/dataobjects_spec.rb +26 -0
  47. data/spec/delete_command_spec.rb +2 -0
  48. data/spec/embedded_value_spec.rb +2 -0
  49. data/spec/fixtures/people.yaml +1 -1
  50. data/spec/fixtures/posts.yaml +3 -0
  51. data/spec/legacy_spec.rb +2 -0
  52. data/spec/load_command_spec.rb +28 -2
  53. data/spec/magic_columns_spec.rb +2 -0
  54. data/spec/models/person.rb +1 -1
  55. data/spec/models/post.rb +8 -0
  56. data/spec/query_spec.rb +2 -0
  57. data/spec/save_command_spec.rb +2 -0
  58. data/spec/schema_spec.rb +2 -0
  59. data/spec/serialization_spec.rb +58 -0
  60. data/spec/single_table_inheritance_spec.rb +2 -0
  61. data/spec/symbolic_operators_spec.rb +2 -0
  62. data/spec/validates_confirmation_of_spec.rb +2 -0
  63. data/spec/validates_format_of_spec.rb +2 -0
  64. data/spec/validates_length_of_spec.rb +2 -0
  65. data/spec/validates_uniqueness_of_spec.rb +2 -0
  66. data/spec/validations_spec.rb +2 -0
  67. data/tasks/fixtures.rb +15 -10
  68. metadata +10 -13
  69. data/lib/data_mapper/adapters/sql/commands/conditions.rb +0 -130
  70. data/lib/data_mapper/adapters/sql/commands/loader.rb +0 -99
  71. data/plugins/dataobjects/swig_mysql/do_mysql.bundle +0 -0
  72. data/spec/conversions_to_yaml_spec.rb +0 -17
@@ -1,3 +1,5 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
1
3
  describe "Magic Columns" do
2
4
 
3
5
  it "should update updated_at on save" do
@@ -4,7 +4,7 @@ class Person < DataMapper::Base
4
4
  property :occupation, :string
5
5
  property :type, :class
6
6
  property :notes, :text
7
- property :date_of_birth, :datetime
7
+ property :date_of_birth, :date
8
8
 
9
9
  embed :address do
10
10
  property :street, :string
@@ -1,4 +1,12 @@
1
1
  class Post < DataMapper::Base
2
2
  property :title, :string
3
3
  property :created_at, :datetime
4
+
5
+ def next
6
+ Post.first(:created_at.gte => self.created_at, :id.gt => self.id, :order => "created_at, id")
7
+ end
8
+
9
+ def previous
10
+ Post.first(:created_at.lte => self.created_at, :id.lt => self.id, :order => "created_at DESC, id DESC")
11
+ end
4
12
  end
@@ -1,3 +1,5 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
1
3
  describe('A query') do
2
4
 
3
5
  it 'should return a simple Array of primitives' do
@@ -1,3 +1,5 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
1
3
  describe "Save Commands" do
2
4
 
3
5
  it "should create a new row" do
@@ -1,3 +1,5 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
1
3
  if ENV['ADAPTER'] == 'postgresql' && false
2
4
 
3
5
  describe DataMapper::Adapters::PostgresqlAdapter::Mappings::Table do
@@ -0,0 +1,58 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
3
+ describe DataMapper::Support::Serialization do
4
+
5
+ before(:all) do
6
+ fixtures(:animals)
7
+ end
8
+
9
+ it "should serialize to YAML" do
10
+ Animal.first(:name => 'Frog').to_yaml.should == <<-EOS.margin
11
+ ---
12
+ id: 1
13
+ name: Frog
14
+ notes: I am a Frog!
15
+
16
+ EOS
17
+ end
18
+
19
+ it "should serialize to XML" do
20
+ Animal.first(:name => 'Frog').to_xml.should == <<-EOS.compress_lines(false)
21
+ <animal id="1">
22
+ <name>Frog</name>
23
+ <notes>I am a Frog!</notes>
24
+ </animal>
25
+ EOS
26
+
27
+ san_diego_zoo = Zoo.first(:name => 'San Diego')
28
+ san_diego_zoo.to_xml.should == <<-EOS.compress_lines(false)
29
+ <zoo id="2">
30
+ <name>San Diego</name>
31
+ <notes/>
32
+ <updated_at>#{san_diego_zoo.updated_at}</updated_at>
33
+ </zoo>
34
+ EOS
35
+ end
36
+
37
+ it "should serialize to JSON" do
38
+
39
+ Animal.first(:name => 'Frog').to_json.should == <<-EOS.compress_lines
40
+ {
41
+ "id": 1,
42
+ "name": "Frog",
43
+ "notes": "I am a Frog!"
44
+ }
45
+ EOS
46
+
47
+ san_diego_zoo = Zoo.first(:name => 'San Diego')
48
+ san_diego_zoo.to_json.should == <<-EOS.compress_lines
49
+ {
50
+ "id": 2,
51
+ "name": "San Diego",
52
+ "notes": null,
53
+ "updated_at": #{san_diego_zoo.updated_at.to_json}
54
+ }
55
+ EOS
56
+ end
57
+
58
+ end
@@ -1,3 +1,5 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
1
3
  describe 'Single Table Inheritance' do
2
4
 
3
5
  before(:all) do
@@ -1,3 +1,5 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
1
3
  describe Symbol::Operator do
2
4
 
3
5
  before(:all) do
@@ -1,3 +1,5 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
1
3
  describe DataMapper::Validations::ConfirmationValidator do
2
4
 
3
5
  before(:all) do
@@ -1,3 +1,5 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
1
3
  context DataMapper::Validations::FormatValidator do
2
4
 
3
5
  before(:all) do
@@ -1,3 +1,5 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
1
3
  describe DataMapper::Validations::LengthValidator do
2
4
 
3
5
  before(:all) do
@@ -1,3 +1,5 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
1
3
  describe DataMapper::Validations::UniqueValidator do
2
4
 
3
5
  before(:all) do
@@ -1,3 +1,5 @@
1
+ require File.dirname(__FILE__) + "/spec_helper"
2
+
1
3
  describe DataMapper::Validations do
2
4
 
3
5
  before(:all) do
@@ -5,35 +5,40 @@ namespace :dm do
5
5
  def fixtures_path
6
6
  return ENV['FIXTURE_PATH'] if ENV['FIXTURE_PATH']
7
7
 
8
- %w(db dev schema spec).find do |parent|
9
- test_for_dir = "#{DM_APP_ROOT}/#{parent}/fixtures"
10
- File.exists?(test_for_dir) ? test_for_dir : nil
8
+ fixture_path = %w(db dev schema spec).find do |parent|
9
+ File.exists?("#{DM_APP_ROOT}/#{parent}/fixtures")
11
10
  end
11
+
12
+ raise "Fixtures path not found." unless fixture_path
13
+
14
+ "#{DM_APP_ROOT}/#{fixture_path}/fixtures"
12
15
  end
13
16
 
14
17
  desc 'Dump database fixtures'
15
18
  task :dump do
19
+ ENV['AUTO_MIGRATE'] = 'false'
20
+ Rake::Task['environment'].invoke
16
21
  directory fixtures_path
17
- puts DataMapper::Base.subclasses.join("\n")
18
22
  DataMapper::Base.subclasses.each do |table|
19
23
  puts "Dumping #{table}"
20
- File.open( "#{fixtures_path}/#{table.to_s.underscore}.yml", "w") do |f|
21
- f.write YAML::dump(table.all.map{|r| r.attributes})
24
+ File.open( "#{fixtures_path}/#{Inflector.underscore(table.to_s)}.yml", "w+") do |file|
25
+ file.write YAML::dump(table.all)
22
26
  end
23
27
  end
24
28
  end
25
29
 
26
30
  desc 'Load database fixtures'
27
31
  task :load do
32
+ Rake::Task['environment'].invoke
28
33
  directory fixtures_path
29
34
  DataMapper::Base.subclasses.each do |table|
30
- file_name = "#{fixtures_path}/#{table.to_s.underscore}.yml"
35
+ file_name = "#{fixtures_path}/#{Inflector.underscore(table.to_s)}.yml"
31
36
  next unless File.exists?( file_name )
32
37
  puts "Loading #{table}"
33
38
  table.delete_all
34
- File.open( file_name, "r") do |f|
35
- YAML::load(f).each do |attrs|
36
- table.create attrs
39
+ File.open( file_name, "r") do |file|
40
+ YAML::load(file).each do |attributes|
41
+ table.create(attributes)
37
42
  end
38
43
  end
39
44
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: datamapper
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.0
7
- date: 2007-10-25 00:00:00 -05:00
6
+ version: 0.2.1
7
+ date: 2007-11-02 00:00:00 -05:00
8
8
  summary: An Object/Relational Mapper for Ruby
9
9
  require_paths:
10
10
  - lib
@@ -43,9 +43,7 @@ files:
43
43
  - lib/data_mapper/adapters/mysql_adapter.rb
44
44
  - lib/data_mapper/adapters/postgresql_adapter.rb
45
45
  - lib/data_mapper/adapters/sql/coersion.rb
46
- - lib/data_mapper/adapters/sql/commands/conditions.rb
47
46
  - lib/data_mapper/adapters/sql/commands/load_command.rb
48
- - lib/data_mapper/adapters/sql/commands/loader.rb
49
47
  - lib/data_mapper/adapters/sql/mappings/associations_set.rb
50
48
  - lib/data_mapper/adapters/sql/mappings/column.rb
51
49
  - lib/data_mapper/adapters/sql/mappings/schema.rb
@@ -92,9 +90,10 @@ files:
92
90
  - spec/callbacks_spec.rb
93
91
  - spec/can_has_sphinx.rb
94
92
  - spec/coersion_spec.rb
93
+ - spec/column_spec.rb
95
94
  - spec/conditions_spec.rb
96
- - spec/conversions_to_yaml_spec.rb
97
95
  - spec/count_command_spec.rb
96
+ - spec/dataobjects_spec.rb
98
97
  - spec/delete_command_spec.rb
99
98
  - spec/embedded_value_spec.rb
100
99
  - spec/legacy_spec.rb
@@ -114,6 +113,7 @@ files:
114
113
  - spec/query_spec.rb
115
114
  - spec/save_command_spec.rb
116
115
  - spec/schema_spec.rb
116
+ - spec/serialization_spec.rb
117
117
  - spec/single_table_inheritance_spec.rb
118
118
  - spec/spec_helper.rb
119
119
  - spec/symbolic_operators_spec.rb
@@ -127,6 +127,7 @@ files:
127
127
  - spec/fixtures/exhibits.yaml
128
128
  - spec/fixtures/fruit.yaml
129
129
  - spec/fixtures/people.yaml
130
+ - spec/fixtures/posts.yaml
130
131
  - spec/fixtures/zoos.yaml
131
132
  - tasks/drivers.rb
132
133
  - tasks/fixtures.rb
@@ -155,10 +156,13 @@ files:
155
156
  - plugins/dataobjects/spec/do_spec.rb
156
157
  - plugins/dataobjects/spec/spec_helper.rb
157
158
  - plugins/dataobjects/swig_mysql
158
- - plugins/dataobjects/swig_mysql/do_mysql.bundle
159
159
  - plugins/dataobjects/swig_mysql/extconf.rb
160
+ - plugins/dataobjects/swig_mysql/Makefile
161
+ - plugins/dataobjects/swig_mysql/mkmf.log
162
+ - plugins/dataobjects/swig_mysql/mysql_c.bundle
160
163
  - plugins/dataobjects/swig_mysql/mysql_c.c
161
164
  - plugins/dataobjects/swig_mysql/mysql_c.i
165
+ - plugins/dataobjects/swig_mysql/mysql_c.o
162
166
  - plugins/dataobjects/swig_mysql/mysql_supp.i
163
167
  - plugins/dataobjects/swig_postgres
164
168
  - plugins/dataobjects/swig_postgres/extconf.rb
@@ -193,9 +197,7 @@ extra_rdoc_files:
193
197
  - lib/data_mapper/adapters/mysql_adapter.rb
194
198
  - lib/data_mapper/adapters/postgresql_adapter.rb
195
199
  - lib/data_mapper/adapters/sql/coersion.rb
196
- - lib/data_mapper/adapters/sql/commands/conditions.rb
197
200
  - lib/data_mapper/adapters/sql/commands/load_command.rb
198
- - lib/data_mapper/adapters/sql/commands/loader.rb
199
201
  - lib/data_mapper/adapters/sql/mappings/associations_set.rb
200
202
  - lib/data_mapper/adapters/sql/mappings/column.rb
201
203
  - lib/data_mapper/adapters/sql/mappings/schema.rb
@@ -237,21 +239,16 @@ extra_rdoc_files:
237
239
  - lib/data_mapper.rb
238
240
  - tasks/drivers.rb
239
241
  - tasks/fixtures.rb
240
- - plugins/can_has_sphinx
241
242
  - plugins/can_has_sphinx/init.rb
242
243
  - plugins/can_has_sphinx/install.rb
243
- - plugins/can_has_sphinx/lib
244
244
  - plugins/can_has_sphinx/lib/acts_as_sphinx.rb
245
245
  - plugins/can_has_sphinx/lib/sphinx.rb
246
246
  - plugins/can_has_sphinx/LICENSE
247
247
  - plugins/can_has_sphinx/Rakefile
248
248
  - plugins/can_has_sphinx/README
249
249
  - plugins/can_has_sphinx/REVISION
250
- - plugins/can_has_sphinx/scripts
251
250
  - plugins/can_has_sphinx/scripts/sphinx.sh
252
- - plugins/can_has_sphinx/tasks
253
251
  - plugins/can_has_sphinx/tasks/acts_as_sphinx_tasks.rake
254
- - plugins/dataobjects
255
252
  - plugins/dataobjects/do.rb
256
253
  - plugins/dataobjects/do_mysql.rb
257
254
  - plugins/dataobjects/do_postgres.rb
@@ -1,130 +0,0 @@
1
- module DataMapper
2
- module Adapters
3
- module Sql
4
- module Commands
5
-
6
- class Conditions
7
-
8
- def initialize(adapter, loader, conditions_hash)
9
- @adapter, @loader = adapter, loader
10
- @conditions = parse_conditions(conditions_hash)
11
- end
12
-
13
- def empty?
14
- @conditions.empty?
15
- end
16
-
17
- def to_parameterized_sql
18
- sql = []
19
- parameters = []
20
-
21
- @conditions.each do |condition|
22
- case condition
23
- when String then sql << condition
24
- when Array then
25
- sql << condition.shift
26
- parameters += condition
27
- else
28
- raise "Unable to parse condition: #{condition.inspect}" if condition
29
- end
30
- end
31
-
32
- parameters.unshift("(#{sql.join(') AND (')})")
33
- end
34
-
35
- private
36
-
37
- class ConditionsError < StandardError
38
-
39
- attr_reader :inner_error
40
-
41
- def initialize(clause, value, inner_error)
42
- @clause, @value, @inner_error = clause, value, inner_error
43
- end
44
-
45
- def message
46
- "Conditions (:clause => #{@clause.inspect}, :value => #{@value.inspect}) failed: #{@inner_error}"
47
- end
48
-
49
- def backtrace
50
- @inner_error.backtrace
51
- end
52
-
53
- end
54
-
55
- def expression_to_sql(clause, value, collector)
56
- qualify_columns = @loader.qualify_columns?
57
-
58
- case clause
59
- when Symbol::Operator then
60
- operator = case clause.type
61
- when :gt then '>'
62
- when :gte then '>='
63
- when :lt then '<'
64
- when :lte then '<='
65
- when :not then inequality_operator(value)
66
- when :eql then equality_operator(value)
67
- when :like then equality_operator(value, 'LIKE')
68
- when :in then equality_operator(value)
69
- else raise ArgumentError.new('Operator type not supported')
70
- end
71
- collector << ["#{primary_class_table[clause].to_sql(qualify_columns)} #{operator} ?", value]
72
- when Symbol then
73
- collector << ["#{primary_class_table[clause].to_sql(qualify_columns)} #{equality_operator(value)} ?", value]
74
- when String then
75
- collector << [clause, value]
76
- when Mappings::Column then
77
- collector << ["#{clause.to_sql(qualify_columns)} #{equality_operator(value)} ?", value]
78
- else raise "CAN HAS CRASH? #{clause.inspect}"
79
- end
80
- rescue => e
81
- raise ConditionsError.new(clause, value, e)
82
- end
83
-
84
- def equality_operator(value, default = '=')
85
- case value
86
- when NilClass then 'IS'
87
- when Array then 'IN'
88
- else default
89
- end
90
- end
91
-
92
- def inequality_operator(value, default = '<>')
93
- case value
94
- when NilClass then 'IS NOT'
95
- when Array then 'NOT IN'
96
- else default
97
- end
98
- end
99
-
100
- def parse_conditions(conditions_hash)
101
- collection = []
102
-
103
- case x = conditions_hash.delete(:conditions)
104
- when Array then
105
- clause = x.shift
106
- expression_to_sql(clause, x, collection)
107
- when Hash then
108
- x.each_pair do |key,value|
109
- expression_to_sql(key, value, collection)
110
- end
111
- else
112
- raise "Unable to parse conditions: #{x.inspect}" if x
113
- end
114
-
115
- conditions_hash.each_pair do |key,value|
116
- expression_to_sql(key, value, collection)
117
- end
118
-
119
- collection
120
- end
121
-
122
- def primary_class_table
123
- @primary_class_table || (@primary_class_table = @loader.send(:primary_class_table))
124
- end
125
- end
126
-
127
- end
128
- end
129
- end
130
- end
@@ -1,99 +0,0 @@
1
- module DataMapper
2
- module Adapters
3
- module Sql
4
- module Commands
5
-
6
- class Loader
7
-
8
- def initialize(load_command, klass)
9
- @load_command, @klass = load_command, klass
10
- @columns = {}
11
- @key = nil
12
- @key_index = nil
13
- @type_override_present = false
14
- @type_override_index = nil
15
- @type_override = nil
16
- @session = load_command.session
17
- @reload = load_command.reload?
18
- @set = []
19
- end
20
-
21
- def add_column(column, index)
22
- if column.key?
23
- @key = column
24
- @key_index = index
25
- end
26
-
27
- if column.type == :class
28
- @type_override_present = true
29
- @type_override_index = index
30
- @type_override = column
31
- end
32
-
33
- @columns[index] = column
34
-
35
- self
36
- end
37
-
38
- def materialize(values)
39
-
40
- instance_id = @key.type_cast_value(values[@key_index])
41
- instance = if @type_override_present
42
- create_instance(instance_id, @type_override.type_cast_value(values[@type_override_index]))
43
- else
44
- create_instance(instance_id)
45
- end
46
-
47
- @klass.callbacks.execute(:before_materialize, instance)
48
-
49
- original_hashes = instance.original_hashes
50
-
51
- @columns.each_pair do |index, column|
52
- # This may be a little confusing, but we're
53
- # setting both the original-hash value, and the
54
- # instance-variable through method chaining to avoid
55
- # lots of extra short-lived local variables.
56
- original_hashes[column.name] = instance.instance_variable_set(
57
- column.instance_variable_name,
58
- column.type_cast_value(values[index])
59
- ).hash
60
- end
61
-
62
- instance.instance_variable_set(:@loaded_set, @set)
63
- @set << instance
64
-
65
- @klass.callbacks.execute(:after_materialize, instance)
66
-
67
- return instance
68
- end
69
-
70
- def loaded_set
71
- @set
72
- end
73
-
74
- private
75
-
76
- def create_instance(instance_id, instance_type = @klass)
77
- instance = @session.identity_map.get(@klass, instance_id)
78
-
79
- if instance.nil? || @reload
80
- instance = instance_type.new() if instance.nil?
81
- instance.instance_variable_set(:@__key, instance_id)
82
- instance.instance_variable_set(:@new_record, false)
83
- @session.identity_map.set(instance)
84
- elsif instance.new_record?
85
- instance.instance_variable_set(:@__key, instance_id)
86
- instance.instance_variable_set(:@new_record, false)
87
- end
88
-
89
- instance.session = @session
90
-
91
- return instance
92
- end
93
-
94
- end
95
-
96
- end # module Commands
97
- end # module Sql
98
- end # module Adapters
99
- end # module DataMapper