mattetti-couchrest 0.24 → 0.25

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.md CHANGED
@@ -8,6 +8,8 @@ to CouchDB's API endpoints so you don't have to.
8
8
 
9
9
  CouchRest is designed to make a simple base for application and framework-specific object oriented APIs. CouchRest is Object-Mapper agnostic, the parsed JSON it returns from CouchDB shows up as subclasses of Ruby's Hash. Naked JSON, just as it was mean to be.
10
10
 
11
+ Note: CouchRest only support CouchDB 0.9.0 or newer.
12
+
11
13
  ## Easy Install
12
14
 
13
15
  Easy Install is moving to RubyForge, heads up for the gem.
data/Rakefile CHANGED
@@ -29,7 +29,6 @@ spec = Gem::Specification.new do |s|
29
29
  Dir["spec/tmp"]
30
30
  s.extra_rdoc_files = %w( README.md LICENSE THANKS.md )
31
31
  s.require_path = "lib"
32
- s.add_dependency("json", ">= 1.1.2")
33
32
  s.add_dependency("rest-client", ">= 0.5")
34
33
  s.add_dependency("mime-types", ">= 1.15")
35
34
  end
data/lib/couchrest.rb CHANGED
@@ -12,10 +12,12 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- require "rubygems"
16
- gem 'json'
17
- require 'json'
18
- gem 'rest-client'
15
+ require 'rubygems'
16
+ begin
17
+ require 'json'
18
+ rescue LoadError
19
+ raise "You need install and require your own compatible json library since couchrest rest couldn't load the json/json_pure gem" unless Kernel.const_defined?("JSON")
20
+ end
19
21
  require 'rest_client'
20
22
 
21
23
  $:.unshift File.dirname(__FILE__) unless
@@ -28,7 +30,7 @@ require 'couchrest/monkeypatches'
28
30
 
29
31
  # = CouchDB, close to the metal
30
32
  module CouchRest
31
- VERSION = '0.24' unless self.const_defined?("VERSION")
33
+ VERSION = '0.25' unless self.const_defined?("VERSION")
32
34
 
33
35
  autoload :Server, 'couchrest/core/server'
34
36
  autoload :Database, 'couchrest/core/database'
@@ -44,6 +44,12 @@ module CouchRest
44
44
  end
45
45
  end
46
46
 
47
+ # load a set of documents by passing an array of ids
48
+ def get_bulk(ids)
49
+ documents(:keys => ids, :include_docs => true)
50
+ end
51
+ alias :bulk_load :get_bulk
52
+
47
53
  # POST a temporary view function to CouchDB for querying. This is not
48
54
  # recommended, as you don't get any performance benefit from CouchDB's
49
55
  # materialized views. Can be quite slow on large databases.
@@ -115,7 +115,22 @@ module CouchRest
115
115
  # Check if a resource is valid in a given context
116
116
  #
117
117
  def valid?(context = :default)
118
- self.class.validators.execute(context, self)
118
+ result = self.class.validators.execute(context, self)
119
+ result && validate_casted_arrays
120
+ end
121
+
122
+ # checking on casted objects
123
+ def validate_casted_arrays
124
+ result = true
125
+ array_casted_properties = self.class.properties.select { |property| property.casted && property.type.instance_of?(Array) }
126
+ array_casted_properties.each do |property|
127
+ casted_values = self.send(property.name)
128
+ next unless casted_values.respond_to?(:each) && casted_values.first.respond_to?(:valid?)
129
+ casted_values.each do |value|
130
+ result = (result && value.valid?) if value.respond_to?(:valid?)
131
+ end
132
+ end
133
+ result
119
134
  end
120
135
 
121
136
  # Begin a recursive walk of the model checking validity
@@ -599,6 +599,11 @@ describe CouchRest::Database do
599
599
  ds = @db.documents(:startkey => 'doc0', :endkey => 'doc3', :include_docs => true)
600
600
  ds['rows'][0]['doc']['another'].should == "doc"
601
601
  end
602
+ it "should have the bulk_load macro" do
603
+ rs = @db.bulk_load ["doc0", "doc7"]
604
+ rs['rows'].length.should == 2
605
+ rs['rows'][0]['doc']['another'].should == "doc"
606
+ end
602
607
  end
603
608
 
604
609
 
@@ -1,5 +1,10 @@
1
+ # encoding: utf-8
2
+
1
3
  require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
2
4
  require File.join(FIXTURE_PATH, 'more', 'card')
5
+ require File.join(FIXTURE_PATH, 'more', 'cat')
6
+ require File.join(FIXTURE_PATH, 'more', 'person')
7
+
3
8
 
4
9
  class WithCastedModelMixin < Hash
5
10
  include CouchRest::CastedModel
@@ -103,5 +108,35 @@ describe CouchRest::CastedModel do
103
108
  end
104
109
 
105
110
  end
111
+
112
+ describe "saving document with array of casted models and validation" do
113
+ before :each do
114
+ @cat = Cat.new
115
+ @cat.save
116
+ end
117
+
118
+ it "should save" do
119
+ toy = CatToy.new :name => "Mouse"
120
+ @cat.toys.push(toy)
121
+ @cat.save.should be_true
122
+ end
123
+
124
+ it "should fail because name is not present" do
125
+ toy = CatToy.new
126
+ @cat.toys.push(toy)
127
+ @cat.should_not be_valid
128
+ @cat.save.should be_false
129
+ end
130
+
131
+ it "should not fail if the casted model doesn't have validation" do
132
+ Cat.property :masters, :cast_as => ['Person'], :default => []
133
+ Cat.validates_present :name
134
+ cat = Cat.new(:name => 'kitty')
135
+ cat.should be_valid
136
+ cat.masters.push Person.new
137
+ cat.should be_valid
138
+ end
139
+
140
+ end
106
141
 
107
142
  end
@@ -0,0 +1,18 @@
1
+ class Cat < CouchRest::ExtendedDocument
2
+ include ::CouchRest::Validation
3
+
4
+ # Set the default database to use
5
+ use_database TEST_SERVER.default_database
6
+
7
+ property :name
8
+ property :toys, :cast_as => ['CatToy'], :default => []
9
+ end
10
+
11
+ class CatToy < Hash
12
+ include ::CouchRest::CastedModel
13
+ include ::CouchRest::Validation
14
+
15
+ property :name
16
+
17
+ validates_present :name
18
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mattetti-couchrest
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.24"
4
+ version: "0.25"
5
5
  platform: ruby
6
6
  authors:
7
7
  - J. Chris Anderson
@@ -13,16 +13,6 @@ cert_chain: []
13
13
  date: 2008-11-22 00:00:00 -08:00
14
14
  default_executable:
15
15
  dependencies:
16
- - !ruby/object:Gem::Dependency
17
- name: json
18
- type: :runtime
19
- version_requirement:
20
- version_requirements: !ruby/object:Gem::Requirement
21
- requirements:
22
- - - ">="
23
- - !ruby/object:Gem::Version
24
- version: 1.1.2
25
- version:
26
16
  - !ruby/object:Gem::Dependency
27
17
  name: rest-client
28
18
  type: :runtime
@@ -154,6 +144,7 @@ files:
154
144
  - spec/fixtures/more
155
145
  - spec/fixtures/more/article.rb
156
146
  - spec/fixtures/more/card.rb
147
+ - spec/fixtures/more/cat.rb
157
148
  - spec/fixtures/more/course.rb
158
149
  - spec/fixtures/more/event.rb
159
150
  - spec/fixtures/more/invoice.rb