Skip to content

Commit

Permalink
Minor misc changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcarey committed Aug 14, 2009
1 parent 2e3bcf4 commit 8cf5fad
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 8 deletions.
1 change: 1 addition & 0 deletions lib/relaxdb/relaxdb.rb
Expand Up @@ -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={})
Expand Down
6 changes: 3 additions & 3 deletions lib/relaxdb/views.rb
Expand Up @@ -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);
}
}
Expand All @@ -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
Expand All @@ -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);
Expand Down
36 changes: 36 additions & 0 deletions 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"
13 changes: 10 additions & 3 deletions spec/document_spec.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions spec/relaxdb_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_models.rb
Expand Up @@ -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
Expand Down

0 comments on commit 8cf5fad

Please sign in to comment.