couchobject 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +10 -0
- data/Manifest.txt +30 -6
- data/README.txt +580 -42
- data/TODO +2 -2
- data/config/hoe.rb +1 -1
- data/lib/couch_object.rb +7 -2
- data/lib/couch_object/database.rb +19 -34
- data/lib/couch_object/document.rb +13 -6
- data/lib/couch_object/error_classes.rb +110 -0
- data/lib/couch_object/persistable.rb +954 -36
- data/lib/couch_object/persistable/has_many_relations_array.rb +91 -0
- data/lib/couch_object/persistable/meta_classes.rb +568 -0
- data/lib/couch_object/persistable/overloaded_methods.rb +209 -0
- data/lib/couch_object/server.rb +1 -1
- data/lib/couch_object/utils.rb +44 -0
- data/lib/couch_object/version.rb +1 -1
- data/lib/couch_object/view.rb +129 -6
- data/script/console +0 -0
- data/script/destroy +0 -0
- data/script/generate +0 -0
- data/script/txt2html +0 -0
- data/spec/database_spec.rb +23 -31
- data/spec/database_spec.rb.orig +173 -0
- data/spec/document_spec.rb +21 -3
- data/spec/integration/database_integration_spec.rb +46 -15
- data/spec/integration/integration_helper.rb +3 -3
- data/spec/persistable/callback.rb +44 -0
- data/spec/persistable/callback_spec.rb +44 -0
- data/spec/persistable/cloning.rb +77 -0
- data/spec/persistable/cloning_spec.rb +77 -0
- data/spec/persistable/comparing_objects.rb +350 -0
- data/spec/persistable/comparing_objects_spec.rb +350 -0
- data/spec/persistable/deleting.rb +113 -0
- data/spec/persistable/deleting_spec.rb +113 -0
- data/spec/persistable/error_messages.rb +32 -0
- data/spec/persistable/error_messages_spec.rb +32 -0
- data/spec/persistable/loading.rb +339 -0
- data/spec/persistable/loading_spec.rb +339 -0
- data/spec/persistable/new_methods.rb +70 -0
- data/spec/persistable/new_methods_spec.rb +70 -0
- data/spec/persistable/persistable_helper.rb +194 -0
- data/spec/persistable/relations.rb +470 -0
- data/spec/persistable/relations_spec.rb +470 -0
- data/spec/persistable/saving.rb +137 -0
- data/spec/persistable/saving_spec.rb +137 -0
- data/spec/persistable/setting_storage_location.rb +65 -0
- data/spec/persistable/setting_storage_location_spec.rb +65 -0
- data/spec/persistable/timestamps.rb +76 -0
- data/spec/persistable/timestamps_spec.rb +76 -0
- data/spec/persistable/unsaved_changes.rb +211 -0
- data/spec/persistable/unsaved_changes_spec.rb +211 -0
- data/spec/server_spec.rb +5 -5
- data/spec/utils_spec.rb +60 -0
- data/spec/view_spec.rb +40 -7
- data/website/index.html +22 -7
- data/website/index.txt +13 -5
- metadata +93 -61
- data/bin/couch_ruby_view_requestor +0 -81
- data/lib/couch_object/model.rb +0 -5
- data/lib/couch_object/proc_condition.rb +0 -14
- data/spec/model_spec.rb +0 -5
- data/spec/persistable_spec.rb +0 -91
- data/spec/proc_condition_spec.rb +0 -26
@@ -1,81 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby -wKU
|
2
|
-
|
3
|
-
require 'rubygems'
|
4
|
-
require 'json'
|
5
|
-
|
6
|
-
# Highly experimental view requestor for CouchDb
|
7
|
-
#
|
8
|
-
# >> pp db.post("_temp_view", "proc{|doc| if doc['foo'] =~ /ba/; return doc;end }")
|
9
|
-
# #<CouchObject::Response:0x616944
|
10
|
-
# @parsed_body=
|
11
|
-
# {"rows"=>
|
12
|
-
# [{"_rev"=>928806717,
|
13
|
-
# "_id"=>"28D568C5992CBD2B4711F57225A19517",
|
14
|
-
# "value"=>
|
15
|
-
# {"_id"=>"28D568C5992CBD2B4711F57225A19517",
|
16
|
-
# "_rev"=>928806717,
|
17
|
-
# "foo"=>"baz"}},
|
18
|
-
# {"_rev"=>-1696868121,
|
19
|
-
# "_id"=>"601D858DB2E298EFC4BBA92A11760D1E",
|
20
|
-
# "value"=>
|
21
|
-
# {"_id"=>"601D858DB2E298EFC4BBA92A11760D1E",
|
22
|
-
# "_rev"=>-1696868121,
|
23
|
-
# "foo"=>"bar"}},
|
24
|
-
# {"_rev"=>-2093091288,
|
25
|
-
# "_id"=>"CABCEB3F2C8B70B3FE24A03FF6AB7A1E",
|
26
|
-
# "value"=>
|
27
|
-
# {"_id"=>"CABCEB3F2C8B70B3FE24A03FF6AB7A1E",
|
28
|
-
# "_rev"=>-2093091288,
|
29
|
-
# "foo"=>"bar"}}],
|
30
|
-
# "offset"=>0,
|
31
|
-
# "total_rows"=>3,
|
32
|
-
# "view"=>"_temp_view:proc{|doc| if doc['foo'] =~ /ba/; return doc;end }"},
|
33
|
-
# @response=#<Net::HTTPOK 200 OK readbody=true>>
|
34
|
-
|
35
|
-
$callables = []
|
36
|
-
|
37
|
-
while cmd = ARGF.gets
|
38
|
-
#$stderr.puts "@@@==> got: #{cmd.inspect}"
|
39
|
-
cmd = JSON.parse(cmd)
|
40
|
-
case cmd[0]
|
41
|
-
when "reset"
|
42
|
-
$callables = []
|
43
|
-
puts "true"
|
44
|
-
STDOUT.flush
|
45
|
-
when "add_map_fun"
|
46
|
-
# second arg is a string that will compile to a function
|
47
|
-
callable = eval(cmd[1])
|
48
|
-
if callable.respond_to?(:call)
|
49
|
-
$callables << callable
|
50
|
-
puts "true"
|
51
|
-
else
|
52
|
-
puts JSON.unparse(["error", "String must respond_to #call, eg proc{|doc| doc.title == 'foo' }"])
|
53
|
-
end
|
54
|
-
STDOUT.flush
|
55
|
-
when "map_doc"
|
56
|
-
results = []
|
57
|
-
doc = cmd[1]
|
58
|
-
doc.freeze
|
59
|
-
$callables.each do |callable|
|
60
|
-
begin
|
61
|
-
result = callable.call(doc)
|
62
|
-
if result.nil?
|
63
|
-
results << 0 # indicate no match
|
64
|
-
elsif result == 0 # not sure if this one is correct
|
65
|
-
results << {"value"=>0}
|
66
|
-
else
|
67
|
-
results << result
|
68
|
-
end
|
69
|
-
rescue => e
|
70
|
-
# An error mapping the document. Indicate no match.
|
71
|
-
results << 0
|
72
|
-
end
|
73
|
-
end
|
74
|
-
puts JSON.unparse(results)
|
75
|
-
STDOUT.flush
|
76
|
-
else
|
77
|
-
puts "error"
|
78
|
-
STDOUT.flush
|
79
|
-
exit(1)
|
80
|
-
end
|
81
|
-
end
|
data/lib/couch_object/model.rb
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
module CouchObject
|
2
|
-
class ProcCondition
|
3
|
-
def initialize(&block)
|
4
|
-
unless block.arity == 1
|
5
|
-
raise ArgumentError, "wrong number of arguments (#{block.arity+1} for 1)"
|
6
|
-
end
|
7
|
-
@block = block
|
8
|
-
end
|
9
|
-
|
10
|
-
def to_ruby
|
11
|
-
@block.to_ruby.squeeze(" ")
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
data/spec/model_spec.rb
DELETED
data/spec/persistable_spec.rb
DELETED
@@ -1,91 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
-
|
3
|
-
class Bike
|
4
|
-
include CouchObject::Persistable
|
5
|
-
|
6
|
-
def initialize
|
7
|
-
@wheels = 2
|
8
|
-
end
|
9
|
-
attr_accessor :wheels
|
10
|
-
|
11
|
-
def to_couch
|
12
|
-
{:wheels => @wheels}
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.from_couch(attributes)
|
16
|
-
bike = new
|
17
|
-
bike.wheels = attributes["wheels"]
|
18
|
-
bike
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
describe CouchObject::Persistable, "when mixed into a Class" do
|
23
|
-
before(:each) do
|
24
|
-
@bike = Bike.new
|
25
|
-
@db = mock("mock db")
|
26
|
-
|
27
|
-
@empty_response = {}
|
28
|
-
@ok_response = {"ok" => true}
|
29
|
-
@document_response = {
|
30
|
-
"_id" => "123BAC",
|
31
|
-
"_rev" => "946B7D1C",
|
32
|
-
"attributes" => {
|
33
|
-
"wheels" => 3
|
34
|
-
}
|
35
|
-
}
|
36
|
-
end
|
37
|
-
|
38
|
-
it "should give the class a save method that saves the object" do
|
39
|
-
CouchObject::Database.should_receive(:open).and_return(@db)
|
40
|
-
@db.should_receive(:post).with("", @bike.to_json).and_return(@empty_response)
|
41
|
-
@bike.save("foo")
|
42
|
-
end
|
43
|
-
|
44
|
-
it "should raise if no to_couch on class" do
|
45
|
-
klass = Class.new{ include CouchObject::Persistable }
|
46
|
-
proc{ klass.new.to_json }.should raise_error(CouchObject::Persistable::NoToCouchMethodError)
|
47
|
-
end
|
48
|
-
|
49
|
-
it "should raise if no to_couch on class" do
|
50
|
-
klass = Class.new{
|
51
|
-
include CouchObject::Persistable
|
52
|
-
def to_couch() end
|
53
|
-
}
|
54
|
-
proc{ klass.get_by_id("foo", "bar") }.should raise_error(CouchObject::Persistable::NoFromCouchMethodError)
|
55
|
-
end
|
56
|
-
|
57
|
-
it "should return the doc id on successfull save" do
|
58
|
-
CouchObject::Database.should_receive(:open).and_return(@db)
|
59
|
-
@db.should_receive(:post).with("", @bike.to_json).and_return(@document_response)
|
60
|
-
@bike.save("foo")["_id"].should == "123BAC"
|
61
|
-
end
|
62
|
-
|
63
|
-
it "should assign the returned id to itself on successful save" do
|
64
|
-
CouchObject::Database.should_receive(:open).and_return(@db)
|
65
|
-
@db.should_receive(:post).with("", @bike.to_json).and_return(@document_response)
|
66
|
-
@bike.save("foo")
|
67
|
-
@bike.id.should == "123BAC"
|
68
|
-
end
|
69
|
-
|
70
|
-
it "should know if itself is a new object" do
|
71
|
-
CouchObject::Database.should_receive(:open).and_return(@db)
|
72
|
-
@db.should_receive(:post).with("", @bike.to_json).and_return(@document_response)
|
73
|
-
@bike.new?.should == true
|
74
|
-
@bike.save("foo")
|
75
|
-
@bike.new?.should == false
|
76
|
-
end
|
77
|
-
|
78
|
-
it "should get document by id" do
|
79
|
-
CouchObject::Database.should_receive(:open).and_return(@db)
|
80
|
-
@db.should_receive(:get).with("123BAC").and_return(@document_response)
|
81
|
-
Bike.get_by_id("foo", "123BAC")
|
82
|
-
end
|
83
|
-
|
84
|
-
it "should instantiate a new object based on their #to_couch" do
|
85
|
-
CouchObject::Database.should_receive(:open).and_return(@db)
|
86
|
-
@db.should_receive(:get).with("123BAC").and_return(@document_response)
|
87
|
-
bike = Bike.get_by_id("foo", "123BAC")
|
88
|
-
bike.class.should == Bike
|
89
|
-
bike.wheels.should == 3
|
90
|
-
end
|
91
|
-
end
|
data/spec/proc_condition_spec.rb
DELETED
@@ -1,26 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
-
|
3
|
-
describe CouchObject::ProcCondition do
|
4
|
-
|
5
|
-
it "should accept a block only" do
|
6
|
-
proc{
|
7
|
-
CouchObject::ProcCondition.new do |x|
|
8
|
-
x
|
9
|
-
end
|
10
|
-
}.should_not raise_error
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should require a block argument" do
|
14
|
-
proc{
|
15
|
-
CouchObject::ProcCondition.new do
|
16
|
-
"bar"
|
17
|
-
end
|
18
|
-
}.should raise_error(ArgumentError)
|
19
|
-
end
|
20
|
-
|
21
|
-
it "should convert itself to ruby" do
|
22
|
-
CouchObject::ProcCondition.new do |x|
|
23
|
-
x
|
24
|
-
end.to_ruby.should == "proc { |x|\n x\n}"
|
25
|
-
end
|
26
|
-
end
|