paulcarey-relaxdb 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -1,5 +1,7 @@
1
1
  h3. What's New?
2
2
 
3
+ * 2009-08-15
4
+ ** A few minor tweaks and patches push the version to 0.3.4, compatible with CouchDB 0.9.1.
3
5
  * 2009-05-27
4
6
  ** Added minimal support for data migrations. Although CouchDB's nature removes the necessity for migrations, certain knowledge that all objects possess a particular property can simplify client logic. This desire for simplification is the rationale behind this change.
5
7
  * 2009-04-19
data/Rakefile CHANGED
@@ -4,7 +4,7 @@ require 'spec/rake/spectask'
4
4
 
5
5
  PLUGIN = "relaxdb"
6
6
  NAME = "relaxdb"
7
- GEM_VERSION = "0.3.3"
7
+ GEM_VERSION = "0.3.4"
8
8
  AUTHOR = "Paul Carey"
9
9
  EMAIL = "paul.p.carey@gmail.com"
10
10
  HOMEPAGE = "http://github.com/paulcarey/relaxdb/"
@@ -24,7 +24,7 @@ module RelaxDB
24
24
  class_inheritable_accessor :__view_by_list__
25
25
  self.__view_by_list__ = []
26
26
 
27
- class_inheritable_accessor :belongs_to_rels, :reder => true
27
+ class_inheritable_accessor :belongs_to_rels, :reader => true
28
28
  self.belongs_to_rels = {}
29
29
 
30
30
  def self.property(prop, opts={})
@@ -306,12 +306,13 @@ module RelaxDB
306
306
  alias_method :id, :to_param
307
307
 
308
308
  def set_timestamps
309
+ now = Time.now
309
310
  if new_document? && respond_to?(:created_at)
310
311
  # Don't override it if it's already been set
311
- @created_at = Time.now if @created_at.nil?
312
+ @created_at = now if @created_at.nil?
312
313
  end
313
314
 
314
- @updated_at = Time.now if respond_to?(:updated_at)
315
+ @updated_at = now if respond_to?(:updated_at)
315
316
  end
316
317
 
317
318
  def create_or_get_proxy(klass, relationship, opts=nil)
@@ -372,7 +373,7 @@ module RelaxDB
372
373
  @has_many_rels << relationship
373
374
 
374
375
  if RelaxDB.create_views?
375
- target_class = opts[:class]
376
+ target_class = opts[:class] || relationship.to_s.singularize.camel_case
376
377
  relationship_as_viewed_by_target = (opts[:known_as] || self.name.snake_case).to_s
377
378
  ViewCreator.has_n(self.name, relationship, target_class, relationship_as_viewed_by_target).save
378
379
  end
@@ -50,19 +50,28 @@ module RelaxDB
50
50
  end
51
51
 
52
52
  def create_next(doc, view_keys)
53
- next_key = view_keys.map { |a| a.is_a?(Symbol) ? doc.send(a) : a }
53
+ next_key = view_keys_to_vals doc, view_keys
54
54
  next_key = next_key.length == 1 ? next_key[0] : next_key
55
55
  next_key_docid = doc._id
56
56
  { :startkey => next_key, :startkey_docid => next_key_docid, :descending => @orig_paginate_params.descending }
57
57
  end
58
58
 
59
59
  def create_prev(doc, view_keys)
60
- prev_key = view_keys.map { |a| a.is_a?(Symbol) ? doc.send(a) : a }
60
+ prev_key = view_keys_to_vals doc, view_keys
61
61
  prev_key = prev_key.length == 1 ? prev_key[0] : prev_key
62
62
  prev_key_docid = doc._id
63
63
  prev_params = { :startkey => prev_key, :startkey_docid => prev_key_docid, :descending => !@orig_paginate_params.descending }
64
64
  end
65
65
 
66
+ def view_keys_to_vals doc, view_keys
67
+ view_keys.map do |k|
68
+ if k.is_a?(Symbol) then doc.send(k)
69
+ elsif k.is_a?(Proc) then k.call(doc)
70
+ else k
71
+ end
72
+ end
73
+ end
74
+
66
75
  def orig_offset(view_name)
67
76
  if @paginate_params.order_inverted?
68
77
  params = {:startkey => @orig_paginate_params.endkey, :descending => !@orig_paginate_params.descending}
@@ -114,6 +114,7 @@ module RelaxDB
114
114
  #
115
115
  # Examples:
116
116
  # RelaxDB.load "foo", :conflicts => true
117
+ # RelaxDB.load "foo", :revs => true
117
118
  # RelaxDB.load ["foo", "bar"]
118
119
  #
119
120
  def load(ids, atts={})
data/lib/relaxdb/views.rb CHANGED
@@ -25,7 +25,7 @@ module RelaxDB
25
25
  map = <<-QUERY
26
26
  function(doc) {
27
27
  var class_match = #{kls_check kls}
28
- if(class_match && #{prop_check}) {
28
+ if (class_match && #{prop_check}) {
29
29
  emit(#{key}, doc);
30
30
  }
31
31
  }
@@ -39,7 +39,7 @@ module RelaxDB
39
39
  def self.has_n(client_class, relationship, target_class, relationship_to_client)
40
40
  map = <<-QUERY
41
41
  function(doc) {
42
- if(doc.relaxdb_class == "#{target_class}" && doc.#{relationship_to_client}_id)
42
+ if (doc.relaxdb_class == "#{target_class}" && doc.#{relationship_to_client}_id)
43
43
  emit(doc.#{relationship_to_client}_id, doc);
44
44
  }
45
45
  QUERY
@@ -51,7 +51,7 @@ module RelaxDB
51
51
  def self.references_many(client_class, relationship, target_class, peers)
52
52
  map = <<-QUERY
53
53
  function(doc) {
54
- if(doc.relaxdb_class == "#{target_class}" && doc.#{peers}) {
54
+ if (doc.relaxdb_class == "#{target_class}" && doc.#{peers}) {
55
55
  var i;
56
56
  for(i = 0; i < doc.#{peers}.length; i++) {
57
57
  emit(doc.#{peers}[i], doc);
@@ -8,7 +8,7 @@ describe RelaxDB::BelongsToProxy do
8
8
  end
9
9
 
10
10
  describe "belongs_to" do
11
-
11
+
12
12
  it "should return nil when accessed before assignment" do
13
13
  r = Rating.new
14
14
  r.photo.should == nil
@@ -165,11 +165,19 @@ describe RelaxDB::Document do
165
165
 
166
166
  describe "user defined property writer" do
167
167
 
168
- it "should not be used" do
168
+ it "should not be used to modify state" do
169
169
  o = BespokeWriter.new(:val => 101).save
170
170
  o = RelaxDB.load o._id
171
171
  o.val.should == 81
172
172
  end
173
+
174
+ it "may be used if effectively idempotent" do
175
+ o = BespokeWriter.new(:tt => "2009/04/01").save
176
+ RelaxDB.reload(o).tt.should == Time.parse("2009/04/01")
177
+
178
+ o = BespokeWriter.new(:tt => Time.now).save
179
+ RelaxDB.reload(o).tt.should be_close(Time.now, 1)
180
+ end
173
181
 
174
182
  end
175
183
 
@@ -254,8 +262,7 @@ describe RelaxDB::Document do
254
262
  end
255
263
 
256
264
  it "should return an empty array when no instances exist" do
257
- Atom.all.should be_an_instance_of(Array)
258
- Atom.all.should be_empty
265
+ Atom.all.should == []
259
266
  end
260
267
 
261
268
  end
@@ -8,6 +8,35 @@ describe RelaxDB::HasManyProxy do
8
8
  end
9
9
 
10
10
  describe "has_many" do
11
+
12
+
13
+ describe "target_class in the generated view" do
14
+ it "should infer the class name from the relationship if not supplied" do
15
+ view = mock(:view).as_null_object
16
+ RelaxDB::ViewCreator.should_receive(:has_n).with(
17
+ "", # client_class
18
+ :foo_bars, # relationship
19
+ "FooBar", # target_class
20
+ "" # relationship_to_client
21
+ ).and_return view
22
+ klass = Class.new(RelaxDB::Document) do
23
+ has_many :foo_bars
24
+ end
25
+ end
26
+
27
+ it "should use the class name if supplied" do
28
+ view = mock(:view).as_null_object
29
+ RelaxDB::ViewCreator.should_receive(:has_n).with(
30
+ "", # client_class
31
+ :foo_bars, # relationship
32
+ "Bar", # target_class
33
+ "" # relationship_to_client
34
+ ).and_return view
35
+ klass = Class.new(RelaxDB::Document) do
36
+ has_many :foo_bars, :class => "Bar"
37
+ end
38
+ end
39
+ end
11
40
 
12
41
  it "should be considered enumerable" do
13
42
  u = User.new.save
@@ -346,6 +346,15 @@ describe "RelaxDB Pagination" do
346
346
  navigate_b_series query
347
347
  end
348
348
 
349
+ it "should pass using lambdas as view_keys" do
350
+ query = lambda do |page_params|
351
+ RelaxDB.paginate_view "Letter_by_letter_and_number", :page_params => page_params,
352
+ :startkey => ["b"], :endkey => ["b", {}], :limit => 2,
353
+ :attributes => ["b", lambda { |l| l.number } ]
354
+ end
355
+ navigate_b_series query
356
+ end
357
+
349
358
  end
350
359
 
351
360
  end
data/spec/relaxdb_spec.rb CHANGED
@@ -217,8 +217,9 @@ describe RelaxDB do
217
217
  ns = (0...100).map { rand(1_000_000_000).to_s }
218
218
  objs = ns.map { |n| Primitives.new :_id => n }
219
219
  RelaxDB.bulk_save! *objs
220
+ ns = ns.reverse
220
221
  objs = RelaxDB.load! ns
221
- (0...100).each do |i|
222
+ 99.downto(0) do |i|
222
223
  ns[i].should == objs[i]._id
223
224
  end
224
225
  end
@@ -249,7 +250,7 @@ describe RelaxDB do
249
250
  it "should request a view and return an array" do
250
251
  RelaxDB::DesignDocument.get(RelaxDB.dd).add_view("simple", "map", map_func).save
251
252
  data = RelaxDB.view("simple")
252
- data.should be_instance_of(Array)
253
+ data.should == []
253
254
  end
254
255
 
255
256
  it "may accept query params" do
data/spec/spec_models.rb CHANGED
@@ -35,7 +35,9 @@ end
35
35
 
36
36
  class BespokeWriter < RelaxDB::Document
37
37
  property :val
38
+ property :tt
38
39
  def val=(v); @val = v - 10; end
40
+ def tt=(t); @tt = t.is_a?(String) ? Time.parse(t) : t; end
39
41
  end
40
42
 
41
43
  class Invite < RelaxDB::Document
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paulcarey-relaxdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Carey
@@ -9,7 +9,7 @@ autorequire: relaxdb
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-05-27 00:00:00 -07:00
12
+ date: 2009-08-15 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -107,6 +107,7 @@ files:
107
107
  - spec/view_spec.rb
108
108
  has_rdoc: false
109
109
  homepage: http://github.com/paulcarey/relaxdb/
110
+ licenses:
110
111
  post_install_message:
111
112
  rdoc_options: []
112
113
 
@@ -127,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
128
  requirements: []
128
129
 
129
130
  rubyforge_project:
130
- rubygems_version: 1.2.0
131
+ rubygems_version: 1.3.5
131
132
  signing_key:
132
133
  specification_version: 2
133
134
  summary: RelaxDB provides a simple interface to CouchDB