Take the 2008 Git User's Survey and help out! [ hide ]

public
Rubygem
Fork of sam/dm-more
Description: Extras for DataMapper, including bridges to DataObjects::Migrations and Merb::DataMapper
Homepage: http://datamapper.org
Clone URL: git://github.com/cypher/dm-more.git
Search Repo:
Whitespace cleanup
myabc (author)
Tue May 13 05:07:43 -0700 2008
commit  ad1c629010118b42bbf9e83d9709f34078aec5ee
tree    a1d380499e2d494957f6e15bd506456964f0bbd0
parent  73da8be053bc188427d6a9efe1607cc31ddffd2f
...
18
19
20
21
22
 
...
18
19
20
 
21
22
0
@@ -18,4 +18,4 @@ nbproject
0
 .DS_Store
0
 rspec_report.html
0
 *.swp
0
-_Yardoc
0
\ No newline at end of file
0
+_Yardoc
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
0
@@ -1,22 +1,22 @@
0
-Copyright (c) 2007 Sam Smoot
0
-
0
-Permission is hereby granted, free of charge, to any person
0
-obtaining a copy of this software and associated documentation
0
-files (the "Software"), to deal in the Software without
0
-restriction, including without limitation the rights to use,
0
-copy, modify, merge, publish, distribute, sublicense, and/or sell
0
-copies of the Software, and to permit persons to whom the
0
-Software is furnished to do so, subject to the following
0
-conditions:
0
-
0
-The above copyright notice and this permission notice shall be
0
-included in all copies or substantial portions of the Software.
0
-
0
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
0
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
0
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
0
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
0
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0
-OTHER DEALINGS IN THE SOFTWARE.
0
\ No newline at end of file
0
+Copyright (c) 2007 Sam Smoot
0
+
0
+Permission is hereby granted, free of charge, to any person
0
+obtaining a copy of this software and associated documentation
0
+files (the "Software"), to deal in the Software without
0
+restriction, including without limitation the rights to use,
0
+copy, modify, merge, publish, distribute, sublicense, and/or sell
0
+copies of the Software, and to permit persons to whom the
0
+Software is furnished to do so, subject to the following
0
+conditions:
0
+
0
+The above copyright notice and this permission notice shall be
0
+included in all copies or substantial portions of the Software.
0
+
0
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
0
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
0
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
0
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
0
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0
+OTHER DEALINGS IN THE SOFTWARE.
0
...
7
8
9
10
11
...
7
8
9
 
 
0
@@ -7,5 +7,3 @@ Including:
0
 
0
  * Migrations
0
  * Validations
0
-
0
-
...
1
2
3
4
5
 
...
1
2
3
 
4
5
0
@@ -1,4 +1,4 @@
0
 dm-couchdb-adapter
0
 ==================
0
 
0
-A DataMapper adapter for CouchDB
0
\ No newline at end of file
0
+A DataMapper adapter for CouchDB
...
14
15
16
17
 
18
19
20
 
 
21
22
23
...
30
31
32
33
 
34
35
36
...
44
45
46
47
 
48
49
50
51
52
 
53
54
55
 
56
57
58
 
59
60
61
62
63
64
 
65
66
67
68
 
69
70
 
71
72
73
...
76
77
78
79
 
80
81
82
83
84
 
85
86
87
...
90
91
92
93
 
94
95
96
97
 
98
99
100
101
 
102
103
104
105
106
107
108
 
109
110
 
111
112
113
114
115
116
 
117
118
119
...
123
124
125
126
 
127
128
 
129
130
131
132
133
134
135
 
136
137
138
 
139
140
141
...
155
156
157
158
 
159
160
161
...
168
169
170
171
 
172
173
174
175
 
176
177
178
179
 
180
181
182
183
 
184
185
186
187
 
188
189
190
...
192
193
194
195
 
196
197
198
199
 
...
14
15
16
 
17
18
 
 
19
20
21
22
23
...
30
31
32
 
33
34
35
36
...
44
45
46
 
47
48
49
50
51
 
52
53
54
 
55
56
57
 
58
59
60
61
62
63
 
64
65
66
67
 
68
69
 
70
71
72
73
...
76
77
78
 
79
80
81
82
83
 
84
85
86
87
...
90
91
92
 
93
94
95
96
 
97
98
99
100
 
101
102
103
104
105
106
107
 
108
109
 
110
111
112
113
114
115
 
116
117
118
119
...
123
124
125
 
126
127
 
128
129
130
131
132
133
134
 
135
136
137
 
138
139
140
141
...
155
156
157
 
158
159
160
161
...
168
169
170
 
171
172
173
174
 
175
176
177
178
 
179
180
181
182
 
183
184
185
186
 
187
188
189
190
...
192
193
194
 
195
196
197
 
198
199
0
@@ -14,10 +14,10 @@ end
0
 
0
 module DataMapper
0
   module Resource
0
-
0
+
0
     def to_json(dirty = false)
0
-
0
- doc = (dirty ? self.dirty_attributes : self.class.properties).map do |property|
0
+
0
+ doc = (dirty ? self.dirty_attributes : self.class.properties).map do |property|
0
         [property.field, instance_variable_get(property.instance_variable_name)]
0
       end
0
 
0
@@ -30,7 +30,7 @@ end
0
 module DataMapper
0
   module Adapters
0
     class CouchdbAdapter < AbstractAdapter
0
-
0
+
0
       def create(repository, resource)
0
         result = http_post("/#{resource.class.storage_name(name)}/", resource.to_json(true))
0
         resource.instance_variable_set("@rev", result["rev"])
0
@@ -44,30 +44,30 @@ module DataMapper
0
           false
0
         end
0
       end
0
-
0
+
0
       def read(repository, resource, key)
0
         properties = resource.properties(repository.name).defaults
0
         properties_with_indexes = Hash[*properties.zip((0...properties.length).to_a).flatten]
0
         set = Collection.new(repository, resource, properties_with_indexes)
0
-
0
+
0
         doc = http_get("/#{resource.storage_name(name)}/#{key}")
0
         set.load(properties.map { |property| typecast(property.type, doc[property.field.to_s]) })
0
-
0
+
0
         set.first
0
       end
0
-
0
+
0
       def delete(repository, resource)
0
         key = resource.class.key(name).map { |property| resource.instance_variable_get(property.instance_variable_name) }
0
         result = http_delete("/#{resource.class.storage_name(name)}/#{key}?rev=#{resource.rev}")
0
         return result["ok"]
0
       end
0
-
0
+
0
       def update(repository, resource)
0
         key = resource.class.key(name).map { |property| resource.instance_variable_get(property.instance_variable_name) }
0
         result = http_put("/#{resource.class.storage_name(name)}/#{key}", resource.to_json)
0
-
0
+
0
         if result["ok"]
0
-
0
+
0
           key = resource.class.key(name)
0
           resource.instance_variable_set(key.first.instance_variable_name, result["id"])
0
           resource.instance_variable_set("@rev", result["rev"])
0
@@ -76,12 +76,12 @@ module DataMapper
0
           false
0
         end
0
       end
0
-
0
+
0
       def read_set(repository, query)
0
         doc = request do |http|
0
           http.request(build_javascript_request(query))
0
         end
0
-
0
+
0
         populate_set(repository, query.model, query.fields, doc["rows"])
0
       end
0
 
0
@@ -90,30 +90,30 @@ module DataMapper
0
         doc = http_get("/#{resource.storage_name(name)}/_view/#{resource.storage_name(name)}/#{proc_name}")
0
         populate_set(repository, resource, properties, doc["rows"])
0
       end
0
-
0
+
0
       def populate_set(repository, resource, properties, docs)
0
         properties_with_indexes = Hash[*properties.zip((0...properties.length).to_a).flatten]
0
         set = Collection.new(repository, resource, properties_with_indexes)
0
-
0
+
0
         docs.each do |doc|
0
           set.load(properties.map { |property| typecast(property.type, doc["value"][property.field.to_s]) })
0
         end
0
-
0
+
0
         set
0
       end
0
 
0
       def delete_set(repository, query)
0
         raise NotImplementedError
0
       end
0
-
0
+
0
       private
0
-
0
+
0
       def normalize_uri(uri_or_options)
0
         uri_or_options = URI.parse(uri_or_options) if String === uri_or_options
0
         uri_or_options.scheme = "http"
0
         uri_or_options
0
       end
0
-
0
+
0
       def typecast(type, value)
0
         return value if value.nil?
0
         case type.to_s
0
@@ -123,19 +123,19 @@ module DataMapper
0
         else value
0
         end
0
       end
0
-
0
+
0
       def build_javascript_request(query)
0
-
0
+
0
         if query.order.empty?
0
           key = "null"
0
         else
0
           key = query.order.map { |order| "doc.#{order.property.field}" }.join(", ")
0
           key = "[#{key}]"
0
         end
0
-
0
+
0
         request = Net::HTTP::Post.new("/#{query.model.storage_name(name)}/_temp_view")
0
         request["content-type"] = "text/javascript"
0
-
0
+
0
         if query.conditions.empty?
0
           request.body = "function(doc) { map(#{key}, doc); }"
0
         else
0
@@ -155,7 +155,7 @@ module DataMapper
0
         end
0
         request
0
       end
0
-
0
+
0
       def like_operator(value)
0
         case value
0
         when Regexp then value = value.source
0
@@ -168,23 +168,23 @@ module DataMapper
0
         end
0
         return ".match(/#{value}/)"
0
       end
0
-
0
+
0
       def http_put(uri, data = nil)
0
         request { |http| http.put(uri, data) }
0
       end
0
-
0
+
0
       def http_post(uri, data)
0
         request { |http| http.post(uri, data) }
0
       end
0
-
0
+
0
       def http_get(uri)
0
         request { |http| http.get(uri) }
0
       end
0
-
0
+
0
       def http_delete(uri)
0
         request { |http| http.delete(uri) }
0
       end
0
-
0
+
0
       def request(parse_result = true, &block)
0
         res = nil
0
         Net::HTTP.start(@uri.host, @uri.port) do |http|
0
@@ -192,7 +192,7 @@ module DataMapper
0
         end
0
         JSON.parse(res.body) if parse_result
0
       end
0
-
0
+
0
     end
0
   end
0
-end
0
\ No newline at end of file
0
+end
...
1
2
3
 
4
5
 
6
7
8
 
9
10
11
 
12
13
14
 
15
16
17
18
19
20
21
 
 
 
22
23
24
...
27
28
29
30
 
31
32
33
...
49
50
51
52
 
53
54
 
55
56
57
58
 
59
60
61
 
62
63
64
...
66
67
68
69
 
70
71
72
73
74
75
 
76
77
 
78
79
80
...
87
88
89
90
91
 
...
1
2
 
3
4
 
5
6
7
 
8
9
10
 
11
12
13
 
14
15
16
17
18
 
 
 
19
20
21
22
23
24
...
27
28
29
 
30
31
32
33
...
49
50
51
 
52
53
 
54
55
56
57
 
58
59
60
 
61
62
63
64
...
66
67
68
 
69
70
71
72
73
74
 
75
76
 
77
78
79
80
...
87
88
89
 
90
91
0
@@ -1,24 +1,24 @@
0
 # This provides a mechanism for accessing stored and indexed views
0
 # in the CouchDB database.
0
-#
0
+#
0
 # Here's a sample model:
0
-#
0
+#
0
 # class User
0
 # include DataMapper::Resource
0
-#
0
+#
0
 # property :id, String, :key => true, :field => :_id
0
 # property :rev, String, :field => :_rev
0
-#
0
+#
0
 # view :by_name
0
 # end
0
-#
0
+#
0
 # And here's the DM code to generate the view:
0
 #
0
 # view = Net::HTTP::Put.new("/users/_design/users") # /model_name/_design/model_name
0
 # view["content-type"] = "text/javascript"
0
-# view.body = {
0
-# "language" => "text/javascript",
0
-# "views" => {
0
+# view.body = {
0
+# "language" => "text/javascript",
0
+# "views" => {
0
 # # this filters out non-records, such as this
0
 # # _design document
0
 # "by_name" => "function(doc) { if(doc._id.charAt(0) != '_') { map(doc.name, doc); } }"
0
@@ -27,7 +27,7 @@
0
 # @adapter.send(:request, false) do |http|
0
 # http.request(view)
0
 # end
0
-#
0
+#
0
 
0
 module DataMapper
0
   class Repository
0
@@ -49,16 +49,16 @@ end
0
 
0
 module DataMapper
0
   class View
0
-
0
+
0
     attr_reader :model, :name
0
-
0
+
0
     def initialize(model, name)
0
       @model = model
0
       @name = name
0
-
0
+
0
       create_getter
0
     end
0
-
0
+
0
     def create_getter
0
       @model.class_eval <<-EOS, __FILE__, __LINE__
0
         def self.#{@name}(repository_name = self.repository.name)
0
@@ -66,15 +66,15 @@ module DataMapper
0
         end
0
       EOS
0
     end
0
-
0
+
0
   end
0
 end
0
 
0
 module DataMapper
0
   module Resource
0
-
0
+
0
     module ClassMethods
0
-
0
+
0
       def view(name)
0
         @views ||= Hash.new { |h,k| h[k] = {} }
0
         proc = View.new(self, name)
0
@@ -87,4 +87,4 @@ module DataMapper
0
       end
0
     end
0
   end
0
-end
0
\ No newline at end of file
0
+end
...
3
4
5
6
 
7
8
9
...
11
12
13
14
 
15
16
17
18
19
20
21
 
22
23
24
...
32
33
34
35
 
36
37
38
39
 
40
41
42
43
44
45
 
46
47
48
...
51
52
53
54
 
55
56
57
...
63
64
65
66
 
67
68
69
70
71
72
 
73
74
75
76
 
77
78
79
80
81
82
83
 
84
85
86
87
 
88
89
90
...
92
93
94
95
 
96
97
98
99
 
100
101
102
103
 
104
105
106
107
108
109
 
110
111
112
113
114
 
115
116
117
...
121
122
123
124
 
125
126
127
128
129
130
131
 
132
133
134
135
136
137
138
 
139
140
 
141
142
143
 
144
145
 
146
147
148
149
150
151
 
 
 
152
153
154
...
157
158
159
160
 
161
162
163
164
165
166
167
168
 
 
...
3
4
5
 
6
7
8
9
...
11
12
13
 
14
15
16
17
18
19
20
 
21
22
23
24
...
32
33
34
 
35
36
37
38
 
39
40
41
42
43
44
 
45
46
47
48
...
51
52
53
 
54
55
56
57
...
63
64
65
 
66
67
68
69
70
71
 
72
73
74
75
 
76
77
78
79
80
81
82
 
83
84
85
86
 
87
88
89
90
...
92
93
94
 
95
96
97
98
 
99
100
101
102
 
103
104
105
106
107
108
 
109
110
111
112
113
 
114
115
116
117
...
121
122
123
 
124
125
126
127
128
129
130
 
131
132
133
134
135
136
137
 
138
139
 
140
141
142
 
143
144
 
145
146
147
148
 
 
 
149
150
151
152
153
154
...
157
158
159
 
160
161
162
163
164
165
 
 
166
167
168
0
@@ -3,7 +3,7 @@ require Pathname(__FILE__).dirname.parent.expand_path + 'lib/couchdb_adapter'
0
 
0
 class User
0
   include DataMapper::Resource
0
-
0
+
0
   def self.default_repository_name
0
     :couchdb
0
   end
0
@@ -11,14 +11,14 @@ class User
0
   # required for CouchDB
0
   property :id, String, :key => true, :field => :_id
0
   property :rev, String, :field => :_rev
0
-
0
+
0
   # regular properties
0
   property :name, String
0
   property :age, Integer
0
   property :wealth, Float
0
   property :created_at, DateTime
0
   property :created_on, Date
0
-
0
+
0
   # creates methods for accessing stored/indexed views in the CouchDB database
0
   view :by_name
0
   view :by_age
0
@@ -32,17 +32,17 @@ describe "DataMapper::Adapters::CouchdbAdapter" do
0
     @adapter.send(:http_put, "/users/")
0
     create_procedures
0
   end
0
-
0
+
0
   after :all do
0
     @adapter.send(:http_delete, "/users/")
0
   end
0
-
0
+
0
   it "should create a record" do
0
     user = new_user
0
     user.save.should == true
0
     user.id.should_not == nil
0
   end
0
-
0
+
0
   it "should get a record" do
0
     created_user = new_user
0
     created_user.save
0
@@ -51,7 +51,7 @@ describe "DataMapper::Adapters::CouchdbAdapter" do
0
     user.name.should == "Jamie"
0
     user.age.should == 67
0
   end
0
-
0
+
0
   it "should update a record" do
0
     created_user = new_user
0
     created_user.save
0
@@ -63,28 +63,28 @@ describe "DataMapper::Adapters::CouchdbAdapter" do
0
     user.age.should == created_user.age
0
     user.id.should == created_user.id
0
   end
0
-
0
+
0
   it "should destroy a record" do
0
     created_user = new_user
0
     created_user.save
0
     created_user.destroy.should == true
0
   end
0
-
0
+
0
   it "should get all records" do
0
     User.all.size.should == 3
0
   end
0
-
0
+
0
   it "should get records by eql matcher" do
0
     new_user(:name => "John", :age => 50).save
0
     User.all(:name => "John").size.should == 1
0
     User.all(:age => 50).size.should == 1
0
     User.all(:wealth => 11.5).size.should == 4
0
   end
0
-
0
+
0
   it "should get records by not matcher" do
0
     User.all(:age.not => 50).size.should == 3
0
   end
0
-
0
+
0
   it "should get records by gt matcher" do
0
     User.all(:age.gt => 50).size.should == 3
0
   end
0
@@ -92,26 +92,26 @@ describe "DataMapper::Adapters::CouchdbAdapter" do
0
   it "should get records by gte matcher" do
0
     User.all(:age.gte => 50).size.should == 4
0
   end
0
-
0
+
0
   it "should get records by lt matcher" do
0
     User.all(:age.lt => 50).size.should == 0
0
   end
0
-
0
+
0
   it "should get records by lte matcher" do
0
     User.all(:age.lte => 50).size.should == 1
0
   end
0
-
0
+
0
   it "should get records by the like matcher" do
0
     User.all(:name.like => "Jo").size.should == 0
0
     User.all(:name.like => "Jo%").size.should == 1
0
     User.all(:name.like => /^Jam/).size.should == 2
0
   end
0
-
0
+
0
   it "should get records with multiple matchers" do
0
     new_user(:name => "John", :age => 30).save
0
     User.all(:name => "John", :age.lt => 50).size.should == 1
0
   end
0
-
0
+
0
   it "should order records" do
0
     new_user(:name => "Aaron", :age => 30).save
0
     new_user(:name => "Aaron").save
0
@@ -121,34 +121,34 @@ describe "DataMapper::Adapters::CouchdbAdapter" do
0
     users[0].age.should == 30
0
     users[1].age.should == 67
0
   end
0
-
0
+
0
   it "should handle DateTime" do
0
     user = new_user
0
     user.save
0
     time = user.created_at
0
     User[user.id].created_at.should == time
0
   end
0
-
0
+
0
   it "should handle Date" do
0
     user = new_user
0
     user.save
0
     date = user.created_on
0
     User[user.id].created_on.should == date
0
   end
0
-
0
+
0
   it "should be able to call stored views" do
0
-
0
+
0
     User.by_name.first.should == User.all(:order => [:name]).first
0
     User.by_age.first.should == User.all(:order => [:age]).first
0
-
0
+
0
   end
0
-
0
+
0
   def create_procedures
0
     view = Net::HTTP::Put.new("/users/_design/users")
0
     view["content-type"] = "text/javascript"
0
- view.body = {
0
- "language" => "text/javascript",
0
- "views" => {
0
+ view.body = {
0
+ "language" => "text/javascript",
0
+ "views" => {
0
         "by_name" => "function(doc) { if(doc._id.charAt(0) != '_') { map(doc.name, doc); } }",
0
         "by_age" => "function(doc) { if(doc._id.charAt(0) != '_') { map(doc.age, doc); } }"
0
       }
0
@@ -157,11 +157,11 @@ describe "DataMapper::Adapters::CouchdbAdapter" do
0
       http.request(view)
0
     end
0
   end
0
-
0
+
0
   def new_user(options = {})
0
     default_options = { :name => "Jamie", :age => 67, :wealth => 11.5 }
0
     default_options.merge!(options)
0
     User.new(default_options)
0
   end
0
-
0
-end
0
\ No newline at end of file
0
+
0
+end
...
10
11
12
13
 
14
15
16
17
 
18
19
20
21
22
 
23
24
25
26
27
 
28
29
30
...
41
42
43
44
45
 
...
10
11
12
 
13
14
15
16
 
17
18
19
20
21
 
22
23
24
25
26
 
27
28
29
30
...
41
42
43
 
44
45
0
@@ -10,21 +10,21 @@ describe "DataMapper::View" do
0
       property :open, TrueClass
0
     end
0
   end
0
-
0
+
0
   it "should have a view method" do
0
     Zoo.should respond_to(:view)
0
   end
0
-
0
+
0
   it "should store a view when called" do
0
     Zoo.view :by_name
0
     Zoo.views.keys.should include(:by_name)
0
   end
0
-
0
+
0
   it "should initialize a new Procedure instance" do
0
     proc = Zoo.view :by_name_desc
0
     proc.should be_an_instance_of(DataMapper::View)
0
   end
0
-
0
+
0
   it "should create a getter method" do
0
     Zoo.view :open
0
     Zoo.should respond_to(:open)
0
@@ -41,4 +41,4 @@ describe "DataMapper::Adapters::AbstractAdapter" do
0
   it "should have a view method" do
0
     DataMapper::Adapters::AbstractAdapter.instance_methods.should include("view")
0
   end
0
-end
0
\ No newline at end of file
0
+end

Comments

    No one has commented yet.