jchris-couchrest 0.9.10 → 0.9.11

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/Rakefile CHANGED
@@ -5,7 +5,7 @@ require 'spec/rake/spectask'
5
5
 
6
6
  spec = Gem::Specification.new do |s|
7
7
  s.name = "couchrest"
8
- s.version = "0.9.10"
8
+ s.version = "0.9.11"
9
9
  s.date = "2008-09-11"
10
10
  s.summary = "Lean and RESTful interface to CouchDB."
11
11
  s.email = "jchris@grabb.it"
@@ -0,0 +1,138 @@
1
+ require 'rubygems'
2
+ require 'couchrest'
3
+
4
+ def show obj
5
+ puts obj.inspect
6
+ puts
7
+ end
8
+
9
+ CouchRest::Model.default_database = CouchRest.database!('couchrest-model-example')
10
+
11
+ class Author < CouchRest::Model
12
+ key_accessor :name
13
+ def drink_scotch
14
+ puts "... glug type glug ... I'm #{name} ... type glug glug ..."
15
+ end
16
+ end
17
+
18
+ class Post < CouchRest::Model
19
+ key_accessor :title, :body, :author
20
+
21
+ cast :author, :as => 'Author'
22
+
23
+ timestamps!
24
+ end
25
+
26
+ class Comment < CouchRest::Model
27
+ cast :commenter, :as => 'Author'
28
+
29
+ def post= post
30
+ self["post_id"] = post.id
31
+ end
32
+ def post
33
+ Post.get(self['post_id']) if self['post_id']
34
+ end
35
+
36
+ timestamps!
37
+ end
38
+
39
+ puts "Act I: CRUD"
40
+ puts
41
+ puts "(pause for dramatic effect)"
42
+ puts
43
+ sleep 2
44
+
45
+ puts "Create an author."
46
+ quentin = Author.new("name" => "Quentin Hazel")
47
+ show quentin
48
+
49
+ puts "Create a new post."
50
+ post = Post.new(:title => "First Post", :body => "Lorem ipsum dolor sit amet, consectetur adipisicing elit...")
51
+ show post
52
+
53
+ puts "Add the author to the post."
54
+ post.author = quentin
55
+ show post
56
+
57
+ puts "Save the post."
58
+ post.save
59
+ show post
60
+
61
+ puts "Load the post."
62
+ reloaded = Post.get(post.id)
63
+ show reloaded
64
+
65
+ puts "The author of the post is an instance of Author."
66
+ reloaded.author.drink_scotch
67
+
68
+ puts "\nAdd some comments to the post."
69
+ comment_one = Comment.new :text => "Blah blah blah", :commenter => {:name => "Joe Sixpack"}
70
+ comment_two = Comment.new :text => "Yeah yeah yeah", :commenter => {:name => "Jane Doe"}
71
+ comment_three = Comment.new :text => "Whatever...", :commenter => {:name => "John Stewart"}
72
+
73
+ # TODO - maybe add some magic here?
74
+ comment_one.post = post
75
+ comment_two.post = post
76
+ comment_three.post = post
77
+ comment_one.save
78
+ comment_two.save
79
+ comment_three.save
80
+
81
+ show comment_one
82
+ show comment_two
83
+ show comment_three
84
+
85
+ puts "We can load a post through its comment (no magic here)."
86
+ show post = comment_one.post
87
+
88
+ puts "Commenters are also authors."
89
+ comment_two['commenter'].drink_scotch
90
+ comment_one['commenter'].drink_scotch
91
+ comment_three['commenter'].drink_scotch
92
+
93
+ puts "\nLet's save an author to her own document."
94
+ jane = comment_two['commenter']
95
+ jane.save
96
+ show jane
97
+
98
+ puts "Oh, that's neat! Because Ruby passes hash valuee by reference, Jane's new id has been added to the comment she left."
99
+ show comment_two
100
+
101
+ puts "Of course, we'd better remember to save it."
102
+ comment_two.save
103
+ show comment_two
104
+
105
+ puts "Oooh, denormalized... feel the burn!"
106
+ puts
107
+ puts
108
+ puts
109
+ puts "Act II: Views"
110
+ puts
111
+ puts
112
+ sleep 2
113
+
114
+ puts "Let's find all the comments that go with our post."
115
+ puts "Our post has id #{post.id}, so lets find all the comments with that post_id."
116
+ puts
117
+
118
+ class Comment
119
+ view_by :post_id
120
+ end
121
+
122
+ comments = Comment.by_post_id :key => post.id
123
+ show comments
124
+
125
+ puts "That was too easy."
126
+ puts "We can even wrap it up in a finder on the Post class."
127
+ puts
128
+
129
+ class Post
130
+ def comments
131
+ Comment.by_post_id :key => id
132
+ end
133
+ end
134
+
135
+ show post.comments
136
+ puts "Gimme 5 minutes and I'll roll this into the framework. ;)"
137
+ puts
138
+ 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."
@@ -450,14 +450,13 @@ module CouchRest
450
450
  self.class.casts.each do |k,v|
451
451
  next unless self[k]
452
452
  target = v[:as]
453
- if target.is_a?(Array) && target[0].is_a?(Class)
453
+ if target.is_a?(Array)
454
+ klass = ::Extlib::Inflection.constantize(target[0])
454
455
  self[k] = self[k].collect do |value|
455
- target[0].new(value)
456
+ klass.new(value)
456
457
  end
457
- elsif target.is_a?(Class)
458
- self[k] = target.new(self[k])
459
458
  else
460
- raise ArgumentError, "Call like - cast :field, :as => MyClass - or - :as => [MyClass] if the field is an array."
459
+ self[k] = ::Extlib::Inflection.constantize(target).new(self[k])
461
460
  end
462
461
  end
463
462
  end
@@ -27,8 +27,8 @@ end
27
27
 
28
28
  class Course < CouchRest::Model
29
29
  key_accessor :title
30
- cast :questions, :as => [Question]
31
- cast :professor, :as => Person
30
+ cast :questions, :as => ['Question']
31
+ cast :professor, :as => 'Person'
32
32
  view_by :title
33
33
  end
34
34
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jchris-couchrest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.10
4
+ version: 0.9.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - J. Chris Anderson