zdzolton-cambric 0.6.0 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +41 -2
- data/VERSION +1 -1
- data/cambric.gemspec +2 -2
- data/lib/cambric/assume_design_doc_name.rb +11 -0
- data/spec/cambric/assume_design_doc_name_spec.rb +30 -2
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -52,15 +52,54 @@ by the application.
|
|
52
52
|
# Except, you do NOT need to re-specify the design doc name when
|
53
53
|
# calling CouchRest::Database#view —since you're only using one!
|
54
54
|
tweet_db.view 'by_follower_and_created_at', :limit => 1
|
55
|
+
|
56
|
+
# Also, we can auto-cast doc from map-only views for you!
|
57
|
+
tweet_db.get_docs_from_view :by_follower_and_created_at, :cast_as => 'type'
|
58
|
+
# Or:
|
59
|
+
tweet_db.get_docs_from_view :by_follower_and_created_at, :cast_as => Tweet
|
60
|
+
|
61
|
+
4) Write unit tests for your JavaScript map-reduce functions:
|
62
|
+
|
63
|
+
class CambricViewTest < ActiveSupport::TestCase
|
64
|
+
include Cambric::TestHelpers
|
65
|
+
|
66
|
+
test "should map a user to be keyed by who he follows" do
|
67
|
+
kv_pairs = execute_map :users, # Database name
|
68
|
+
:followers, # View name
|
69
|
+
'_id' => 'poddle', 'following' => ['jack', 'russel'] # Document attributes
|
70
|
+
|
71
|
+
assert_equal 2, kv_pairs.size
|
72
|
+
assert_equal 'jack', kv_pairs[0]['key']
|
73
|
+
assert_equal 'poddle', kv_pairs[0]['value']
|
74
|
+
assert_equal 'russel', kv_pairs[1]['key']
|
75
|
+
assert_equal 'poddle', kv_pairs[1]['value']
|
76
|
+
end
|
77
|
+
|
78
|
+
test "should reduce to a count of followers for a user" do
|
79
|
+
count = execute_reduce :users, # Database name
|
80
|
+
:followers, # View name
|
81
|
+
:values => ['poddle', 'larry'] # :keys don't matter for this reduce
|
82
|
+
assert_equal 2, count
|
83
|
+
end
|
84
|
+
|
85
|
+
test "should re-reduce counts of followers for a user" do
|
86
|
+
count = execute_reduce :users, # Database name
|
87
|
+
:followers, # View name
|
88
|
+
:values => [2, 5, 1],
|
89
|
+
:rereduce => true # defaults to false
|
90
|
+
assert_equal 8, count
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
55
94
|
|
56
|
-
|
95
|
+
5) Push view/design doc changes to CouchDB:
|
57
96
|
|
58
97
|
Cambric.push_design_docs
|
59
98
|
|
60
99
|
# Alternatively, you can just call this, since it wont create
|
61
100
|
# your database, if they already exist:
|
62
101
|
Cambric.prepare_database
|
63
|
-
|
102
|
+
|
64
103
|
== Installation
|
65
104
|
|
66
105
|
* Install gems:
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.6.
|
1
|
+
0.6.2
|
data/cambric.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{cambric}
|
5
|
-
s.version = "0.6.
|
5
|
+
s.version = "0.6.2"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Zachary Zolton", "Geoff Buesing"]
|
9
|
-
s.date = %q{2009-06-
|
9
|
+
s.date = %q{2009-06-05}
|
10
10
|
s.email = %q{zachary.zolton@gmail.com}
|
11
11
|
s.extra_rdoc_files = [
|
12
12
|
"LICENSE",
|
@@ -10,6 +10,17 @@ module Cambric
|
|
10
10
|
def cambric_design_doc
|
11
11
|
get "_design/#{@cambric_design_doc_name}"
|
12
12
|
end
|
13
|
+
|
14
|
+
def get_docs_from_view name, options={}
|
15
|
+
cast_as = options.delete(:cast_as) || options.delete('cast_as')
|
16
|
+
results = view name, options.merge(:reduce => false, :include_docs => true)
|
17
|
+
mapper = case cast_as
|
18
|
+
when String then lambda { |r| CouchRest.constantize(r['doc'][cast_as]).new(r['doc']) }
|
19
|
+
when Class then lambda { |r| cast_as.new(r['doc']) }
|
20
|
+
else lambda { |r| r['doc'] }
|
21
|
+
end
|
22
|
+
results['rows'].map &mapper
|
23
|
+
end
|
13
24
|
|
14
25
|
end
|
15
26
|
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper'))
|
2
2
|
|
3
3
|
describe Cambric::AssumeDesignDocName do
|
4
|
-
|
5
4
|
describe "after pushing design docs" do
|
5
|
+
|
6
6
|
before :all do
|
7
7
|
configure_twitter_clone
|
8
8
|
Cambric.prepare_databases!
|
@@ -17,6 +17,34 @@ describe Cambric::AssumeDesignDocName do
|
|
17
17
|
it "should get the design doc specified by configuration" do
|
18
18
|
Cambric[:tweets].cambric_design_doc.should_not be_nil
|
19
19
|
end
|
20
|
+
|
21
|
+
describe "Casting documents from map-only query results" do
|
22
|
+
before :all do
|
23
|
+
class User < CouchRest::Document; end
|
24
|
+
Cambric[:users].bulk_save [
|
25
|
+
User.new('_id' => 'trevor', 'following' => %w(bob geoff scott brian zach), 'type' => 'User'),
|
26
|
+
User.new('_id' => 'scott', 'following' => %w(bob geoff trevor brian zach), 'type' => 'User')
|
27
|
+
]
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should cast to specified type, when cast_as is a type" do
|
31
|
+
followers = Cambric[:users].get_docs_from_view :followers, :cast_as => User, :key => 'bob'
|
32
|
+
followers.should have(2).elements
|
33
|
+
followers.each{ |f| f.should be_a(User) }
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should cast to type per-doc when cast_as is a string" do
|
37
|
+
followers = Cambric[:users].get_docs_from_view :followers, :cast_as => 'type', :key => 'bob'
|
38
|
+
followers.should have(2).elements
|
39
|
+
followers.each{ |f| f.should be_a(User) }
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should default to returning the plain doc hashes if cast_as is omitted" do
|
43
|
+
followers = Cambric[:users].get_docs_from_view :followers, :key => 'bob'
|
44
|
+
followers.should have(2).elements
|
45
|
+
followers.each{ |f| f.should be_a(Hash) }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
20
49
|
end
|
21
|
-
|
22
50
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zdzolton-cambric
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zachary Zolton
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2009-06-
|
13
|
+
date: 2009-06-05 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|