mattetti-couchrest 0.23 → 0.24
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/lib/couchrest/core/server.rb +5 -20
- data/lib/couchrest/mixins/design_doc.rb +6 -1
- data/lib/couchrest/mixins/document_queries.rb +0 -4
- data/lib/couchrest/mixins/properties.rb +2 -2
- data/lib/couchrest/mixins/views.rb +0 -6
- data/lib/couchrest/more/casted_model.rb +2 -1
- data/lib/couchrest/more/extended_document.rb +6 -1
- data/lib/couchrest/more/property.rb +2 -2
- data/lib/couchrest/support/rails.rb +35 -0
- data/lib/couchrest.rb +1 -1
- data/spec/couchrest/core/server_spec.rb +0 -30
- data/spec/couchrest/more/casted_model_spec.rb +9 -0
- data/spec/couchrest/more/extended_doc_spec.rb +29 -1
- data/spec/spec_helper.rb +12 -0
- metadata +3 -2
@@ -2,19 +2,9 @@ module CouchRest
|
|
2
2
|
class Server
|
3
3
|
attr_accessor :uri, :uuid_batch_count, :available_databases
|
4
4
|
def initialize(server = 'http://127.0.0.1:5984', uuid_batch_count = 1000)
|
5
|
-
|
6
|
-
when %r{\A(http://[^/]+)/(.*)\z}
|
7
|
-
@uri, @prefix = $1, $2
|
8
|
-
else
|
9
|
-
@uri, @prefix = server, ""
|
10
|
-
end
|
5
|
+
@uri = server
|
11
6
|
@uuid_batch_count = uuid_batch_count
|
12
7
|
end
|
13
|
-
|
14
|
-
# Add default prefix to database name
|
15
|
-
def expand(name)
|
16
|
-
@prefix + name
|
17
|
-
end
|
18
8
|
|
19
9
|
# Lists all "available" databases.
|
20
10
|
# An available database, is a database that was specified
|
@@ -42,7 +32,7 @@ module CouchRest
|
|
42
32
|
# @couch.available_database?(:default)
|
43
33
|
#
|
44
34
|
def available_database?(ref_or_name)
|
45
|
-
ref_or_name.is_a?(Symbol) ? available_databases.keys.include?(ref_or_name) : available_databases.values.map{|db| db.name}.include?(
|
35
|
+
ref_or_name.is_a?(Symbol) ? available_databases.keys.include?(ref_or_name) : available_databases.values.map{|db| db.name}.include?(ref_or_name)
|
46
36
|
end
|
47
37
|
|
48
38
|
def default_database=(name, create_unless_exists = true)
|
@@ -55,17 +45,12 @@ module CouchRest
|
|
55
45
|
|
56
46
|
# Lists all databases on the server
|
57
47
|
def databases
|
58
|
-
|
59
|
-
unless @prefix.empty?
|
60
|
-
pfx = @prefix.gsub('/','%2F')
|
61
|
-
dbs.reject! { |db| db.index(pfx) != 0 }
|
62
|
-
end
|
63
|
-
dbs
|
48
|
+
CouchRest.get "#{@uri}/_all_dbs"
|
64
49
|
end
|
65
50
|
|
66
51
|
# Returns a CouchRest::Database for the given name
|
67
52
|
def database(name)
|
68
|
-
CouchRest::Database.new(self,
|
53
|
+
CouchRest::Database.new(self, name)
|
69
54
|
end
|
70
55
|
|
71
56
|
# Creates the database if it doesn't exist
|
@@ -81,7 +66,7 @@ module CouchRest
|
|
81
66
|
|
82
67
|
# Create a database
|
83
68
|
def create_db(name)
|
84
|
-
CouchRest.put "#{@uri}/#{
|
69
|
+
CouchRest.put "#{@uri}/#{name}"
|
85
70
|
database(name)
|
86
71
|
end
|
87
72
|
|
@@ -9,6 +9,12 @@ module CouchRest
|
|
9
9
|
end
|
10
10
|
|
11
11
|
module ClassMethods
|
12
|
+
attr_accessor :design_doc, :design_doc_slug_cache, :design_doc_fresh
|
13
|
+
|
14
|
+
def design_doc
|
15
|
+
@design_doc ||= Design.new(default_design_doc)
|
16
|
+
end
|
17
|
+
|
12
18
|
def design_doc_id
|
13
19
|
"_design/#{design_doc_slug}"
|
14
20
|
end
|
@@ -16,7 +22,6 @@ module CouchRest
|
|
16
22
|
def design_doc_slug
|
17
23
|
return design_doc_slug_cache if (design_doc_slug_cache && design_doc_fresh)
|
18
24
|
funcs = []
|
19
|
-
self.design_doc ||= Design.new(default_design_doc)
|
20
25
|
design_doc['views'].each do |name, view|
|
21
26
|
funcs << "#{name}/#{view['map']}#{view['reduce']}"
|
22
27
|
end
|
@@ -12,10 +12,6 @@ module CouchRest
|
|
12
12
|
# name of the current class. Take the standard set of
|
13
13
|
# CouchRest::Database#view options.
|
14
14
|
def all(opts = {}, &block)
|
15
|
-
self.design_doc ||= Design.new(default_design_doc)
|
16
|
-
unless design_doc_fresh
|
17
|
-
refresh_design_doc
|
18
|
-
end
|
19
15
|
view(:all, opts, &block)
|
20
16
|
end
|
21
17
|
|
@@ -24,7 +24,7 @@ module CouchRest
|
|
24
24
|
self.class.properties.each do |property|
|
25
25
|
key = property.name.to_s
|
26
26
|
# let's make sure we have a default and we can assign the value
|
27
|
-
if property.default && (self.respond_to?("#{key}=") || self.key?(key))
|
27
|
+
if !property.default.nil? && (self.respond_to?("#{key}=") || self.key?(key))
|
28
28
|
if property.default.class == Proc
|
29
29
|
self[key] = property.default.call
|
30
30
|
else
|
@@ -126,4 +126,4 @@ module CouchRest
|
|
126
126
|
|
127
127
|
end
|
128
128
|
end
|
129
|
-
end
|
129
|
+
end
|
@@ -7,12 +7,6 @@ module CouchRest
|
|
7
7
|
end
|
8
8
|
|
9
9
|
module ClassMethods
|
10
|
-
attr_accessor :design_doc, :design_doc_slug_cache, :design_doc_fresh
|
11
|
-
|
12
|
-
def design_doc
|
13
|
-
@design_doc ||= Design.new(default_design_doc)
|
14
|
-
end
|
15
|
-
|
16
10
|
# Define a CouchDB view. The name of the view will be the concatenation
|
17
11
|
# of <tt>by</tt> and the keys joined by <tt>_and_</tt>
|
18
12
|
#
|
@@ -33,6 +33,11 @@ module CouchRest
|
|
33
33
|
|
34
34
|
def initialize(passed_keys={})
|
35
35
|
apply_defaults # defined in CouchRest::Mixins::Properties
|
36
|
+
passed_keys.each do |k,v|
|
37
|
+
if self.respond_to?("#{k}=")
|
38
|
+
self.send("#{k}=", passed_keys.delete(k))
|
39
|
+
end
|
40
|
+
end if passed_keys
|
36
41
|
super
|
37
42
|
cast_keys # defined in CouchRest::Mixins::Properties
|
38
43
|
unless self['_id'] && self['_rev']
|
@@ -212,4 +217,4 @@ module CouchRest
|
|
212
217
|
end
|
213
218
|
|
214
219
|
end
|
215
|
-
end
|
220
|
+
end
|
@@ -30,11 +30,11 @@ module CouchRest
|
|
30
30
|
@validation_format = options.delete(:format) if options[:format]
|
31
31
|
@read_only = options.delete(:read_only) if options[:read_only]
|
32
32
|
@alias = options.delete(:alias) if options[:alias]
|
33
|
-
@default = options.delete(:default)
|
33
|
+
@default = options.delete(:default) unless options[:default].nil?
|
34
34
|
@casted = options[:casted] ? true : false
|
35
35
|
@init_method = options[:send] ? options.delete(:send) : 'new'
|
36
36
|
@options = options
|
37
37
|
end
|
38
38
|
|
39
39
|
end
|
40
|
-
end
|
40
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# This file contains various hacks for Rails compatibility.
|
2
|
+
# To use, just require in environment.rb, like so:
|
3
|
+
#
|
4
|
+
# require 'couchrest/support/rails'
|
5
|
+
|
6
|
+
class Hash
|
7
|
+
# Hack so that CouchRest::Document, which descends from Hash,
|
8
|
+
# doesn't appear to Rails routing as a Hash of options
|
9
|
+
def self.===(other)
|
10
|
+
return false if self == Hash && other.is_a?(CouchRest::Document)
|
11
|
+
super
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
CouchRest::Document.class_eval do
|
17
|
+
# Hack so that CouchRest::Document, which descends from Hash,
|
18
|
+
# doesn't appear to Rails routing as a Hash of options
|
19
|
+
def is_a?(o)
|
20
|
+
return false if o == Hash
|
21
|
+
super
|
22
|
+
end
|
23
|
+
alias_method :kind_of?, :is_a?
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
require Pathname.new(File.dirname(__FILE__)).join('..', 'validation', 'validation_errors')
|
28
|
+
|
29
|
+
CouchRest::Validation::ValidationErrors.class_eval do
|
30
|
+
# Returns the total number of errors added. Two errors added to the same attribute will be counted as such.
|
31
|
+
# This method is called by error_messages_for
|
32
|
+
def count
|
33
|
+
errors.values.inject(0) { |error_count, errors_for_attribute| error_count + errors_for_attribute.size }
|
34
|
+
end
|
35
|
+
end
|
data/lib/couchrest.rb
CHANGED
@@ -28,7 +28,7 @@ require 'couchrest/monkeypatches'
|
|
28
28
|
|
29
29
|
# = CouchDB, close to the metal
|
30
30
|
module CouchRest
|
31
|
-
VERSION = '0.
|
31
|
+
VERSION = '0.24' unless self.const_defined?("VERSION")
|
32
32
|
|
33
33
|
autoload :Server, 'couchrest/core/server'
|
34
34
|
autoload :Database, 'couchrest/core/database'
|
@@ -2,36 +2,6 @@ require File.dirname(__FILE__) + '/../../spec_helper'
|
|
2
2
|
|
3
3
|
describe CouchRest::Server do
|
4
4
|
|
5
|
-
describe "named databases" do
|
6
|
-
it "should generate database without prefix" do
|
7
|
-
couch = CouchRest::Server.new "http://192.0.2.1:1234"
|
8
|
-
db = couch.database("foo")
|
9
|
-
db.name.should == "foo"
|
10
|
-
db.uri.should == "http://192.0.2.1:1234/foo"
|
11
|
-
end
|
12
|
-
|
13
|
-
it "should generate database with prefix" do
|
14
|
-
couch = CouchRest::Server.new "http://192.0.2.1:1234/dev"
|
15
|
-
db = couch.database("foo")
|
16
|
-
db.name.should == "devfoo"
|
17
|
-
db.uri.should == "http://192.0.2.1:1234/devfoo"
|
18
|
-
end
|
19
|
-
|
20
|
-
it "should generate database with prefix and slash" do
|
21
|
-
couch = CouchRest::Server.new "http://192.0.2.1:1234/dev/"
|
22
|
-
db = couch.database("foo")
|
23
|
-
db.name.should == "dev/foo"
|
24
|
-
db.uri.should == "http://192.0.2.1:1234/dev%2Ffoo"
|
25
|
-
end
|
26
|
-
|
27
|
-
it "should generate database with slashes" do
|
28
|
-
couch = CouchRest::Server.new "http://192.0.2.1:1234/dev/sample/"
|
29
|
-
db = couch.database("foo/bar")
|
30
|
-
db.name.should == "dev/sample/foo/bar"
|
31
|
-
db.uri.should == "http://192.0.2.1:1234/dev%2Fsample%2Ffoo%2Fbar"
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
5
|
describe "available databases" do
|
36
6
|
before(:each) do
|
37
7
|
@couch = CouchRest::Server.new
|
@@ -4,6 +4,7 @@ require File.join(FIXTURE_PATH, 'more', 'card')
|
|
4
4
|
class WithCastedModelMixin < Hash
|
5
5
|
include CouchRest::CastedModel
|
6
6
|
property :name
|
7
|
+
property :no_value
|
7
8
|
end
|
8
9
|
|
9
10
|
class DummyModel < CouchRest::ExtendedDocument
|
@@ -54,6 +55,14 @@ describe CouchRest::CastedModel do
|
|
54
55
|
it "should know who casted it" do
|
55
56
|
@casted_obj.casted_by.should == @obj
|
56
57
|
end
|
58
|
+
|
59
|
+
it "should return nil for the 'no_value' attribute" do
|
60
|
+
@casted_obj.no_value.should be_nil
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should return nil for the unknown attribute" do
|
64
|
+
@casted_obj["unknown"].should be_nil
|
65
|
+
end
|
57
66
|
end
|
58
67
|
|
59
68
|
describe "casted as an array of a different type" do
|
@@ -10,6 +10,7 @@ describe "ExtendedDocument" do
|
|
10
10
|
property :preset, :default => {:right => 10, :top_align => false}
|
11
11
|
property :set_by_proc, :default => Proc.new{Time.now}, :cast_as => 'Time'
|
12
12
|
property :tags, :default => []
|
13
|
+
property :false_default, :default => false
|
13
14
|
property :name
|
14
15
|
timestamps!
|
15
16
|
end
|
@@ -52,6 +53,19 @@ describe "ExtendedDocument" do
|
|
52
53
|
property :preset, :default => 'value'
|
53
54
|
property :has_no_default
|
54
55
|
end
|
56
|
+
|
57
|
+
class WithGetterAndSetterMethods < CouchRest::ExtendedDocument
|
58
|
+
use_database TEST_SERVER.default_database
|
59
|
+
|
60
|
+
property :other_arg
|
61
|
+
def arg
|
62
|
+
other_arg
|
63
|
+
end
|
64
|
+
|
65
|
+
def arg=(value)
|
66
|
+
self.other_arg = "foo-#{value}"
|
67
|
+
end
|
68
|
+
end
|
55
69
|
|
56
70
|
before(:each) do
|
57
71
|
@obj = WithDefaultValues.new
|
@@ -144,6 +158,11 @@ describe "ExtendedDocument" do
|
|
144
158
|
obj = WithDefaultValues.new(:tags => ['spec'])
|
145
159
|
obj.tags.should == ['spec']
|
146
160
|
end
|
161
|
+
|
162
|
+
it "should work with a default value of false" do
|
163
|
+
obj = WithDefaultValues.new
|
164
|
+
obj.false_default.should == false
|
165
|
+
end
|
147
166
|
end
|
148
167
|
|
149
168
|
describe "a doc with template values (CR::Model spec)" do
|
@@ -506,4 +525,13 @@ describe "ExtendedDocument" do
|
|
506
525
|
|
507
526
|
end
|
508
527
|
end
|
509
|
-
|
528
|
+
|
529
|
+
describe "getter and setter methods" do
|
530
|
+
it "should try to call the arg= method before setting :arg in the hash" do
|
531
|
+
@doc = WithGetterAndSetterMethods.new(:arg => "foo")
|
532
|
+
@doc['arg'].should be_nil
|
533
|
+
@doc[:arg].should be_nil
|
534
|
+
@doc.other_arg.should == "foo-foo"
|
535
|
+
end
|
536
|
+
end
|
537
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -23,4 +23,16 @@ def reset_test_db!
|
|
23
23
|
db = cr.database(TESTDB)
|
24
24
|
db.recreate! rescue nil
|
25
25
|
db
|
26
|
+
end
|
27
|
+
|
28
|
+
Spec::Runner.configure do |config|
|
29
|
+
config.before(:all) { reset_test_db! }
|
30
|
+
|
31
|
+
config.after(:all) do
|
32
|
+
cr = TEST_SERVER
|
33
|
+
test_dbs = cr.databases.select { |db| db =~ /^#{TESTDB}/ }
|
34
|
+
test_dbs.each do |db|
|
35
|
+
cr.database(db).delete! rescue nil
|
36
|
+
end
|
37
|
+
end
|
26
38
|
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.
|
4
|
+
version: "0.24"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- J. Chris Anderson
|
@@ -110,6 +110,7 @@ files:
|
|
110
110
|
- lib/couchrest/support
|
111
111
|
- lib/couchrest/support/blank.rb
|
112
112
|
- lib/couchrest/support/class.rb
|
113
|
+
- lib/couchrest/support/rails.rb
|
113
114
|
- lib/couchrest/validation
|
114
115
|
- lib/couchrest/validation/auto_validate.rb
|
115
116
|
- lib/couchrest/validation/contextual_validators.rb
|
@@ -194,7 +195,7 @@ requirements: []
|
|
194
195
|
rubyforge_project:
|
195
196
|
rubygems_version: 1.2.0
|
196
197
|
signing_key:
|
197
|
-
specification_version:
|
198
|
+
specification_version: 3
|
198
199
|
summary: Lean and RESTful interface to CouchDB.
|
199
200
|
test_files: []
|
200
201
|
|