couchrest_model 1.0.0.beta7 → 1.0.0.beta8

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.
@@ -1,144 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'couchrest')
2
-
3
- def show obj
4
- puts obj.inspect
5
- puts
6
- end
7
-
8
- SERVER = CouchRest.new
9
- SERVER.default_database = 'couchrest-extendeddoc-example'
10
-
11
- class Author < CouchRest::ExtendedDocument
12
- use_database SERVER.default_database
13
- property :name
14
-
15
- def drink_scotch
16
- puts "... glug type glug ... I'm #{name} ... type glug glug ..."
17
- end
18
- end
19
-
20
- class Post < CouchRest::ExtendedDocument
21
- use_database SERVER.default_database
22
-
23
- property :title
24
- property :body
25
- property :author, :cast_as => 'Author'
26
-
27
- timestamps!
28
- end
29
-
30
- class Comment < CouchRest::ExtendedDocument
31
- use_database SERVER.default_database
32
-
33
- property :commenter, :cast_as => 'Author'
34
- timestamps!
35
-
36
- def post= post
37
- self["post_id"] = post.id
38
- end
39
- def post
40
- Post.get(self['post_id']) if self['post_id']
41
- end
42
-
43
- end
44
-
45
- puts "Act I: CRUD"
46
- puts
47
- puts "(pause for dramatic effect)"
48
- puts
49
- sleep 2
50
-
51
- puts "Create an author."
52
- quentin = Author.new("name" => "Quentin Hazel")
53
- show quentin
54
-
55
- puts "Create a new post."
56
- post = Post.new(:title => "First Post", :body => "Lorem ipsum dolor sit amet, consectetur adipisicing elit...")
57
- show post
58
-
59
- puts "Add the author to the post."
60
- post.author = quentin
61
- show post
62
-
63
- puts "Save the post."
64
- post.save
65
- show post
66
-
67
- puts "Load the post."
68
- reloaded = Post.get(post.id)
69
- show reloaded
70
-
71
- puts "The author of the post is an instance of Author."
72
- reloaded.author.drink_scotch
73
-
74
- puts "\nAdd some comments to the post."
75
- comment_one = Comment.new :text => "Blah blah blah", :commenter => {:name => "Joe Sixpack"}
76
- comment_two = Comment.new :text => "Yeah yeah yeah", :commenter => {:name => "Jane Doe"}
77
- comment_three = Comment.new :text => "Whatever...", :commenter => {:name => "John Stewart"}
78
-
79
- # TODO - maybe add some magic here?
80
- comment_one.post = post
81
- comment_two.post = post
82
- comment_three.post = post
83
- comment_one.save
84
- comment_two.save
85
- comment_three.save
86
-
87
- show comment_one
88
- show comment_two
89
- show comment_three
90
-
91
- puts "We can load a post through its comment (no magic here)."
92
- show post = comment_one.post
93
-
94
- puts "Commenters are also authors."
95
- comment_two['commenter'].drink_scotch
96
- comment_one['commenter'].drink_scotch
97
- comment_three['commenter'].drink_scotch
98
-
99
- puts "\nLet's save an author to her own document."
100
- jane = comment_two['commenter']
101
- jane.save
102
- show jane
103
-
104
- puts "Oh, that's neat! Because Ruby passes hash valuee by reference, Jane's new id has been added to the comment she left."
105
- show comment_two
106
-
107
- puts "Of course, we'd better remember to save it."
108
- comment_two.save
109
- show comment_two
110
-
111
- puts "Oooh, denormalized... feel the burn!"
112
- puts
113
- puts
114
- puts
115
- puts "Act II: Views"
116
- puts
117
- puts
118
- sleep 2
119
-
120
- puts "Let's find all the comments that go with our post."
121
- puts "Our post has id #{post.id}, so lets find all the comments with that post_id."
122
- puts
123
-
124
- class Comment
125
- view_by :post_id
126
- end
127
-
128
- comments = Comment.by_post_id :key => post.id
129
- show comments
130
-
131
- puts "That was too easy."
132
- puts "We can even wrap it up in a finder on the Post class."
133
- puts
134
-
135
- class Post
136
- def comments
137
- Comment.by_post_id :key => id
138
- end
139
- end
140
-
141
- show post.comments
142
- puts "Gimme 5 minutes and I'll roll this into the framework. ;)"
143
- puts
144
- puts "There is a lot more that can be done with views, but a lot of the interesting stuff is joins, which of course range across types. We'll pick up where we left off, next time."
data/utils/remap.rb DELETED
@@ -1,27 +0,0 @@
1
- require 'rubygems'
2
- require 'couchrest'
3
-
4
- # set the source db and map view
5
- source = CouchRest.new("http://127.0.0.1:5984").database('source-db')
6
- source_view = 'mydesign/view-map'
7
-
8
- # set the target db
9
- target = CouchRest.new("http://127.0.0.1:5984").database('target-db')
10
-
11
-
12
- pager = CouchRest::Pager.new(source)
13
-
14
- # pager will yield once per uniq key in the source view
15
-
16
- pager.key_reduce(source_view, 10000) do |key, values|
17
- # create a doc from the key and the values
18
- example_doc = {
19
- :key => key,
20
- :values => values.uniq
21
- }
22
-
23
- target.save(example_doc)
24
-
25
- # keep us up to date with progress
26
- puts k if (rand > 0.9)
27
- end
data/utils/subset.rb DELETED
@@ -1,30 +0,0 @@
1
- require 'rubygems'
2
- require 'couchrest'
3
-
4
- # subset.rb replicates a percentage of a database to a fresh database.
5
- # use it to create a smaller dataset on which to prototype views.
6
-
7
- # specify the source database
8
- source = CouchRest.new("http://127.0.0.1:5984").database('source-db')
9
-
10
- # specify the target database
11
- target = CouchRest.new("http://127.0.0.1:5984").database('target-db')
12
-
13
- # pager efficiently yields all view rows
14
- pager = CouchRest::Pager.new(source)
15
-
16
- pager.all_docs(1000) do |rows|
17
- docs = rows.collect do |r|
18
- # the percentage of docs to clone
19
- next if rand > 0.1
20
- doc = source.get(r['id'])
21
- doc.delete('_rev')
22
- doc
23
- end.compact
24
- puts docs.length
25
- next if docs.empty?
26
-
27
- puts docs.first['_id']
28
- target.bulk_save(docs)
29
- end
30
-