couch_potato 0.2.19 → 0.2.20

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## Changes
2
2
 
3
+ ### 0.2.20
4
+ * support for :boolean properties (jweiss)
5
+ * return the total_rows when querying a view (langalex)
6
+
3
7
  ### 0.2.19
4
8
  * added conditions to views (langalex)
5
9
 
data/README.md CHANGED
@@ -88,12 +88,13 @@ Properties can be typed:
88
88
  end
89
89
 
90
90
  In this case Address also implements CouchPotato::Persistence which means its JSON representation will be added to the user document.
91
- Couch Potato also has support for the basic types (right now only Fixnum is supported):
91
+ Couch Potato also has support for the basic types (right now Fixnum and :boolean are supported):
92
92
 
93
93
  class User
94
94
  include CouchPotato::Persistence
95
95
 
96
96
  property :age, :type => Fixnum
97
+ property :receive_newsletter, :type => :boolean
97
98
  end
98
99
 
99
100
  With this in place when you set the user's age as a String (e.g. using an hTML form) it will be converted into a Fixnum automatically.
@@ -202,7 +203,7 @@ You can also pass conditions as a JavaScript string:
202
203
  class User
203
204
  property :name
204
205
 
205
- view :completed, :key => :name, :conditions => 'doc.completed = true'
206
+ view :completed, :key => :name, :conditions => 'doc.completed === true'
206
207
  end
207
208
 
208
209
  The creation of views is based on view specification classes (see [CouchPotato::View::BaseViewSpec](http://rdoc.info/rdoc/langalex/couch_potato/blob/e8f0069e5529ad08a1bd1f02637ea8f1d6d0ab5b/CouchPotato/View/BaseViewSpec.html) and its descendants for more detailed documentation). The above code uses the ModelViewSpec class which is used to find models by their properties. For more sophisticated searches you can use other view specifications (either use the built-in or provide your own) by passing a type parameter:
@@ -337,6 +338,10 @@ In order for this to work you must have the `js` executable in your PATH. This i
337
338
 
338
339
  Please fix bugs, add more specs, implement new features by forking the github repo at http://github.com/langalex/couch_potato.
339
340
 
341
+ Issues are tracked at github: http://github.com/langalex/couch_potato/issues
342
+
343
+ There is a mailing list, just write to: couchpotato@librelis.com
344
+
340
345
  You can run all the specs by calling 'rake spec_unit' and 'rake spec_functional' in the root folder of Couch Potato. The specs require a running CouchDB instance at http://localhost:5984
341
346
 
342
347
  I will only accept patches that are covered by specs - sorry.
data/VERSION.yml CHANGED
@@ -1,5 +1,4 @@
1
1
  ---
2
- :build:
3
- :minor: 2
4
- :patch: 19
5
2
  :major: 0
3
+ :minor: 2
4
+ :patch: 20
@@ -12,14 +12,30 @@ module CouchPotato
12
12
  end
13
13
  end
14
14
 
15
+ # executes a view and return the results. you pass in a view spec
16
+ # which is usually a result of a SomePersistentClass.view call.
17
+ # also return the total_rows returned by CouchDB as an accessor on the results.
18
+ #
19
+ # Example:
20
+ #
21
+ # class User
22
+ # include CouchPotato::Persistence
23
+ # view :all, key: :created_at
24
+ # end
25
+ #
26
+ # CouchPotato.database.view(User.all) # => [user1, user2]
27
+ # CouchPotato.database.view(User.all).total_rows # => 2
28
+ #
15
29
  def view(spec)
16
30
  results = CouchPotato::View::ViewQuery.new(database,
17
31
  spec.design_document, spec.view_name, spec.map_function,
18
32
  spec.reduce_function).query_view!(spec.view_parameters)
19
- spec.process_results results
33
+ processed_results = spec.process_results results
34
+ processed_results.instance_eval "def total_rows; #{results['total_rows']}; end" if results['total_rows']
35
+ processed_results
20
36
  end
21
37
 
22
-
38
+ # saves a document. returns true on success, false on failure
23
39
  def save_document(document, validate = true)
24
40
  return true unless document.dirty?
25
41
  if document.new?
@@ -29,7 +45,8 @@ module CouchPotato
29
45
  end
30
46
  end
31
47
  alias_method :save, :save_document
32
-
48
+
49
+ # saves a document, raises a CouchPotato::Database::ValidationsFailedError on failure
33
50
  def save_document!(document)
34
51
  save_document(document) || raise(ValidationsFailedError.new(document.errors.full_messages))
35
52
  end
@@ -45,6 +62,7 @@ module CouchPotato
45
62
  end
46
63
  alias_method :destroy, :destroy_document
47
64
 
65
+ # loads a document by its id
48
66
  def load_document(id)
49
67
  raise "Can't load a document without an id (got nil)" if id.nil?
50
68
  begin
@@ -57,7 +75,7 @@ module CouchPotato
57
75
  end
58
76
  alias_method :load, :load_document
59
77
 
60
- def inspect
78
+ def inspect #:nodoc:
61
79
  "#<CouchPotato::Database>"
62
80
  end
63
81
 
@@ -2,6 +2,26 @@ module CouchPotato
2
2
  module Persistence
3
3
  class TypeCaster
4
4
  def cast(value, type)
5
+ if type == :boolean
6
+ cast_boolen(value)
7
+ else
8
+ cast_native(value, type)
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def cast_boolen(value)
15
+ if [FalseClass, TrueClass].include?(value.class) || value.nil?
16
+ value
17
+ elsif [0, '0'].include?(value)
18
+ false
19
+ else
20
+ true
21
+ end
22
+ end
23
+
24
+ def cast_native(value, type)
5
25
  if type && !value.instance_of?(type)
6
26
  if type == Fixnum
7
27
  value.to_s.scan(/\d/).join.to_i unless value.blank?
@@ -12,6 +32,7 @@ module CouchPotato
12
32
  value
13
33
  end
14
34
  end
35
+
15
36
  end
16
37
  end
17
38
  end
@@ -52,6 +52,11 @@ describe 'view' do
52
52
  it "should count zero documents" do
53
53
  CouchPotato.database.view(Build.count(:reduce => true)).should == 0
54
54
  end
55
+
56
+ it "should return the total_rows" do
57
+ CouchPotato.database.save_document Build.new(:state => 'success', :time => '2008-01-01')
58
+ CouchPotato.database.view(Build.count(:reduce => false)).total_rows.should == 1
59
+ end
55
60
 
56
61
  describe "properties defined" do
57
62
  it "should assign the configured properties" do
@@ -132,7 +137,7 @@ describe 'view' do
132
137
 
133
138
  describe "with array as key" do
134
139
  it "should create a map function with the composite key" do
135
- CouchPotato::View::ViewQuery.should_receive(:new).with(anything, anything, anything, string_matching(/emit\(\[doc\['time'\], doc\['state'\]\]/), anything).and_return(stub('view query').as_null_object)
140
+ CouchPotato::View::ViewQuery.should_receive(:new).with(anything, anything, anything, string_matching(/emit\(\[doc\['time'\], doc\['state'\]\]/), anything).and_return(stub('view query', :query_view! => {'rows' => []}))
136
141
  CouchPotato.database.view Build.key_array_timeline
137
142
  end
138
143
  end
@@ -6,4 +6,5 @@ class Address
6
6
  property :state
7
7
  property :zip
8
8
  property :country
9
+ property :verified, :type => :boolean
9
10
  end
@@ -141,6 +141,48 @@ describe 'properties' do
141
141
  p = CouchPotato.database.load_document p.id
142
142
  p.ship_address.should be_false
143
143
  end
144
+
145
+ describe "boolean properties" do
146
+ it "should persist '0' for false" do
147
+ a = Address.new
148
+ a.verified = '0'
149
+ CouchPotato.database.save_document! a
150
+ a = CouchPotato.database.load_document a.id
151
+ a.verified.should be_false
152
+ end
153
+
154
+ it "should persist 0 for false" do
155
+ a = Address.new
156
+ a.verified = 0
157
+ CouchPotato.database.save_document! a
158
+ a = CouchPotato.database.load_document a.id
159
+ a.verified.should be_false
160
+ end
161
+
162
+ it "should persist '1' for true" do
163
+ a = Address.new
164
+ a.verified = '1'
165
+ CouchPotato.database.save_document! a
166
+ a = CouchPotato.database.load_document a.id
167
+ a.verified.should be_true
168
+ end
169
+
170
+ it "should persist 1 for true" do
171
+ a = Address.new
172
+ a.verified = 1
173
+ CouchPotato.database.save_document! a
174
+ a = CouchPotato.database.load_document a.id
175
+ a.verified.should be_true
176
+ end
177
+
178
+ it "should leave nil as nil" do
179
+ a = Address.new
180
+ a.verified = nil
181
+ CouchPotato.database.save_document! a
182
+ a = CouchPotato.database.load_document a.id
183
+ a.verified.should be_nil
184
+ end
185
+ end
144
186
 
145
187
  describe "predicate" do
146
188
  it "should return true if property set" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: couch_potato
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.19
4
+ version: 0.2.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Lang
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-12-15 00:00:00 +01:00
12
+ date: 2010-01-11 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency