diff --git a/lib/relaxdb/relaxdb.rb b/lib/relaxdb/relaxdb.rb index 4ee4457..0c3b751 100644 --- a/lib/relaxdb/relaxdb.rb +++ b/lib/relaxdb/relaxdb.rb @@ -114,6 +114,7 @@ def reload(obj) # # Examples: # RelaxDB.load "foo", :conflicts => true + # RelaxDB.load "foo", :revs => true # RelaxDB.load ["foo", "bar"] # def load(ids, atts={}) diff --git a/lib/relaxdb/views.rb b/lib/relaxdb/views.rb index 83d2305..2f6ff50 100644 --- a/lib/relaxdb/views.rb +++ b/lib/relaxdb/views.rb @@ -25,7 +25,7 @@ def self.by_att_list(kls, *atts) map = <<-QUERY function(doc) { var class_match = #{kls_check kls} - if(class_match && #{prop_check}) { + if (class_match && #{prop_check}) { emit(#{key}, doc); } } @@ -39,7 +39,7 @@ def self.by_att_list(kls, *atts) def self.has_n(client_class, relationship, target_class, relationship_to_client) map = <<-QUERY function(doc) { - if(doc.relaxdb_class == "#{target_class}" && doc.#{relationship_to_client}_id) + if (doc.relaxdb_class == "#{target_class}" && doc.#{relationship_to_client}_id) emit(doc.#{relationship_to_client}_id, doc); } QUERY @@ -51,7 +51,7 @@ def self.has_n(client_class, relationship, target_class, relationship_to_client) def self.references_many(client_class, relationship, target_class, peers) map = <<-QUERY function(doc) { - if(doc.relaxdb_class == "#{target_class}" && doc.#{peers}) { + if (doc.relaxdb_class == "#{target_class}" && doc.#{peers}) { var i; for(i = 0; i < doc.#{peers}.length; i++) { emit(doc.#{peers}[i], doc); diff --git a/scratch/key_query_demo.rb b/scratch/key_query_demo.rb new file mode 100644 index 0000000..919925f --- /dev/null +++ b/scratch/key_query_demo.rb @@ -0,0 +1,36 @@ +# Silence parenthesis warnings with ruby -W0 scratch/keys.rb + +# +# A couple of simple illustrations of how keys affect queries +# + +$:.unshift(File.dirname(__FILE__) + "/../lib") +require 'relaxdb' +require File.dirname(__FILE__) + "/../spec/spec_models" + + +RelaxDB.configure :host => "localhost", :port => 5984, :design_doc => "spec_doc", :logger => Logger.new(STDOUT) +class RdbFormatter; def call(sv, time, progname, msg); puts msg; end; end +RelaxDB.logger.formatter = RdbFormatter.new +RelaxDB.delete_db "relaxdb_spec" rescue :ok +RelaxDB.use_db "relaxdb_spec" +RelaxDB.replicate_db "relaxdb_spec_base", "relaxdb_spec" + +def p ps + ps.each { |p| puts("%3s : %3d" % [p._id, p.num]) } + puts +end + +h = {1 => 1, 2 => 2, 3 => 2, 4 => 3, 5 => 3, 6 => 3} +ps = (1..6).map { |i| Primitives.new(:_id => i.to_s, :num => h[i]) } +RelaxDB.bulk_save *ps +puts + +p Primitives.all +p Primitives.by_num +p Primitives.by_num :startkey => 3, :limit => 1 +p Primitives.by_num :startkey => 3 +p Primitives.by_num :startkey => 3, :startkey_docid => "5" +p Primitives.by_num :startkey_docid => "5" +p Primitives.all :startkey_docid => "2" +p Primitives.all :startkey => "4" \ No newline at end of file diff --git a/spec/document_spec.rb b/spec/document_spec.rb index 701f2f8..979513e 100644 --- a/spec/document_spec.rb +++ b/spec/document_spec.rb @@ -165,11 +165,19 @@ describe "user defined property writer" do - it "should not be used" do + it "should not be used to modify state" do o = BespokeWriter.new(:val => 101).save o = RelaxDB.load o._id o.val.should == 81 end + + it "may be used if effectively idempotent" do + o = BespokeWriter.new(:tt => "2009/04/01").save + RelaxDB.reload(o).tt.should == Time.parse("2009/04/01") + + o = BespokeWriter.new(:tt => Time.now).save + RelaxDB.reload(o).tt.should be_close(Time.now, 1) + end end @@ -254,8 +262,7 @@ end it "should return an empty array when no instances exist" do - Atom.all.should be_an_instance_of(Array) - Atom.all.should be_empty + Atom.all.should == [] end end diff --git a/spec/relaxdb_spec.rb b/spec/relaxdb_spec.rb index 1abfcf6..f80fb12 100644 --- a/spec/relaxdb_spec.rb +++ b/spec/relaxdb_spec.rb @@ -217,8 +217,9 @@ class ReplicaTest < RelaxDB::Document; end ns = (0...100).map { rand(1_000_000_000).to_s } objs = ns.map { |n| Primitives.new :_id => n } RelaxDB.bulk_save! *objs + ns = ns.reverse objs = RelaxDB.load! ns - (0...100).each do |i| + 99.downto(0) do |i| ns[i].should == objs[i]._id end end @@ -249,7 +250,7 @@ class ReplicaTest < RelaxDB::Document; end it "should request a view and return an array" do RelaxDB::DesignDocument.get(RelaxDB.dd).add_view("simple", "map", map_func).save data = RelaxDB.view("simple") - data.should be_instance_of(Array) + data.should == [] end it "may accept query params" do diff --git a/spec/spec_models.rb b/spec/spec_models.rb index 361f5e3..e715fc3 100644 --- a/spec/spec_models.rb +++ b/spec/spec_models.rb @@ -35,7 +35,9 @@ def val; @val + 5; end class BespokeWriter < RelaxDB::Document property :val + property :tt def val=(v); @val = v - 10; end + def tt=(t); @tt = t.is_a?(String) ? Time.parse(t) : t; end end class Invite < RelaxDB::Document