couchrest-uniqueness-validation 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +22 -0
- data/LICENSE +20 -0
- data/README.rdoc +41 -0
- data/Rakefile +46 -0
- data/VERSION +1 -0
- data/lib/couchrest-uniqueness-validation.rb +76 -0
- data/spec/couchrest-uniqueness-validation_spec.rb +13 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +25 -0
- metadata +85 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 alex
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
= couchrest-uniqueness-validation
|
2
|
+
|
3
|
+
Validates that the specified attribute is unique across documents with the same
|
4
|
+
couchrest-type using a view.
|
5
|
+
|
6
|
+
@note
|
7
|
+
You have to define a design doc view.
|
8
|
+
@see http://rdoc.info/rdoc/couchrest/couchrest/blob/1b34fe4b60694683e98866a51c2109c1885f7e42/CouchRest/Mixins/Views/ClassMethods.html#view_by-instance_method for more details about views.
|
9
|
+
|
10
|
+
@example [Usage]
|
11
|
+
|
12
|
+
class User
|
13
|
+
|
14
|
+
property :nickname
|
15
|
+
property :login
|
16
|
+
|
17
|
+
view_by :nickname
|
18
|
+
|
19
|
+
# assumes that a view :by_nickname is present
|
20
|
+
validates_uniqueness_of :nickname
|
21
|
+
|
22
|
+
# uses a different from default view
|
23
|
+
validates_uniqueness_of :login, :view => 'my_custom_view'
|
24
|
+
|
25
|
+
# a call to valid? will return false unless no other document exists with
|
26
|
+
# the same couchrest-type, nickname and login
|
27
|
+
end
|
28
|
+
|
29
|
+
== Note on Patches/Pull Requests
|
30
|
+
|
31
|
+
* Fork the project.
|
32
|
+
* Make your feature addition or bug fix.
|
33
|
+
* Add tests for it. This is important so I don't break it in a
|
34
|
+
future version unintentionally.
|
35
|
+
* Commit, do not mess with rakefile, version, or history.
|
36
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
37
|
+
* Send me a pull request. Bonus points for topic branches.
|
38
|
+
|
39
|
+
== Copyright
|
40
|
+
|
41
|
+
Copyright (c) 2010 alex@digns.com. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "couchrest-uniqueness-validation"
|
8
|
+
gem.summary = %Q{adds validates_uniqueness_of validator to CouchRest::Validation}
|
9
|
+
gem.description = %Q{adds uniquness validation by using a design doc view}
|
10
|
+
gem.email = "alex@digns.com"
|
11
|
+
gem.homepage = "http://github.com/crhym3/couchrest-uniqueness-validation"
|
12
|
+
gem.authors = ["alex"]
|
13
|
+
gem.add_dependency("couchrest", ">= 0.34")
|
14
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
|
+
spec.libs << 'lib' << 'spec'
|
25
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
end
|
27
|
+
|
28
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
29
|
+
spec.libs << 'lib' << 'spec'
|
30
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
31
|
+
spec.rcov = true
|
32
|
+
end
|
33
|
+
|
34
|
+
task :spec => :check_dependencies
|
35
|
+
|
36
|
+
task :default => :spec
|
37
|
+
|
38
|
+
require 'rake/rdoctask'
|
39
|
+
Rake::RDocTask.new do |rdoc|
|
40
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
41
|
+
|
42
|
+
rdoc.rdoc_dir = 'rdoc'
|
43
|
+
rdoc.title = "couchrest-uniqueness-validation #{version}"
|
44
|
+
rdoc.rdoc_files.include('README*')
|
45
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
46
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# Based on original validators in CouchRest::Validation
|
2
|
+
module CouchRest
|
3
|
+
module Validation
|
4
|
+
|
5
|
+
class UniquenessValidator < GenericValidator
|
6
|
+
def initialize(field_name, options = {})
|
7
|
+
super
|
8
|
+
@field_name, @options = field_name, options
|
9
|
+
@options[:view] ||= "by_#{@field_name}".to_sym
|
10
|
+
end
|
11
|
+
|
12
|
+
def call(target)
|
13
|
+
return true if unique?(target)
|
14
|
+
|
15
|
+
error_message = @options[:message] || ValidationErrors.default_error_message(:taken, field_name)
|
16
|
+
add_error(target, error_message, field_name)
|
17
|
+
false
|
18
|
+
end
|
19
|
+
|
20
|
+
protected
|
21
|
+
|
22
|
+
def unique?(target)
|
23
|
+
value = target.validation_property_value(@field_name)
|
24
|
+
existing_docs = target.class.view(@options[:view], :key => value, :limit => 1, :include_docs => false)
|
25
|
+
|
26
|
+
# normal case when target.new_document? == true and
|
27
|
+
# no other document exists
|
28
|
+
return true if existing_docs.empty?
|
29
|
+
|
30
|
+
# we got here because either another document does exist or
|
31
|
+
# we're validating an existing document
|
32
|
+
existing_docs.reject! { |d| d['id'] == target.id } unless target.new_document?
|
33
|
+
existing_docs.empty?
|
34
|
+
end
|
35
|
+
end # class UniquenessValidator
|
36
|
+
|
37
|
+
module ValidatesUniqueness
|
38
|
+
|
39
|
+
##
|
40
|
+
# Validates that the specified attribute is unique across documents with the same
|
41
|
+
# couchrest-type using a view.
|
42
|
+
#
|
43
|
+
# @note
|
44
|
+
# You have to define a design doc view.
|
45
|
+
# @see http://rdoc.info/rdoc/couchrest/couchrest/blob/1b34fe4b60694683e98866a51c2109c1885f7e42/CouchRest/Mixins/Views/ClassMethods.html#view_by-instance_method for more details about views.
|
46
|
+
#
|
47
|
+
# @example [Usage]
|
48
|
+
#
|
49
|
+
# class User
|
50
|
+
#
|
51
|
+
# property :nickname
|
52
|
+
# property :login
|
53
|
+
#
|
54
|
+
# view_by :nickname
|
55
|
+
#
|
56
|
+
# # assumes that a view :by_nickname is present
|
57
|
+
# validates_uniqueness_of :nickname
|
58
|
+
#
|
59
|
+
# # uses a different from default view
|
60
|
+
# validates_uniqueness_of :login, :view => 'my_custom_view'
|
61
|
+
#
|
62
|
+
# # a call to valid? will return false unless no other document exists with
|
63
|
+
# # the same couchrest-type, nickname and login
|
64
|
+
# end
|
65
|
+
def validates_uniqueness_of(*fields)
|
66
|
+
opts = opts_from_validator_args(fields)
|
67
|
+
add_validator_to_context(opts, fields, CouchRest::Validation::UniquenessValidator)
|
68
|
+
end
|
69
|
+
|
70
|
+
end # module ValidatesUniqueness
|
71
|
+
|
72
|
+
module ClassMethods
|
73
|
+
include CouchRest::Validation::ValidatesUniqueness
|
74
|
+
end
|
75
|
+
end # module Validation
|
76
|
+
end # module CouchRest
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe CouchRest::Validation::ValidatesUniqueness do
|
4
|
+
|
5
|
+
class SomeExtDoc < CouchRest::ExtendedDocument
|
6
|
+
include CouchRest::Validation
|
7
|
+
use_database SPEC_COUCH
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should provide validates_uniqueness_of method" do
|
11
|
+
SomeExtDoc.should respond_to :validates_uniqueness_of
|
12
|
+
end
|
13
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
require 'rubygems'
|
4
|
+
require 'couchrest'
|
5
|
+
require 'couchrest/mixins/validation'
|
6
|
+
require 'couchrest-uniqueness-validation'
|
7
|
+
require 'spec'
|
8
|
+
require 'spec/autorun'
|
9
|
+
|
10
|
+
unless defined?(SPEC_COUCH)
|
11
|
+
COUCH_URL = "http://127.0.0.1:5984"
|
12
|
+
COUCH_NAME = 'couchrest-test'
|
13
|
+
|
14
|
+
SPEC_COUCH = CouchRest.database!("#{COUCH_URL}/#{COUCH_NAME}")
|
15
|
+
end
|
16
|
+
|
17
|
+
Spec::Runner.configure do |config|
|
18
|
+
config.before(:all) {
|
19
|
+
SPEC_COUCH.recreate! rescue nil
|
20
|
+
}
|
21
|
+
|
22
|
+
config.after(:all) do
|
23
|
+
SPEC_COUCH.delete! rescue nil
|
24
|
+
end
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: couchrest-uniqueness-validation
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- alex
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-17 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: couchrest
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0.34"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.2.9
|
34
|
+
version:
|
35
|
+
description: adds uniquness validation by using a design doc view
|
36
|
+
email: alex@digns.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- LICENSE
|
43
|
+
- README.rdoc
|
44
|
+
files:
|
45
|
+
- .document
|
46
|
+
- .gitignore
|
47
|
+
- LICENSE
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- VERSION
|
51
|
+
- lib/couchrest-uniqueness-validation.rb
|
52
|
+
- spec/couchrest-uniqueness-validation_spec.rb
|
53
|
+
- spec/spec.opts
|
54
|
+
- spec/spec_helper.rb
|
55
|
+
has_rdoc: true
|
56
|
+
homepage: http://github.com/crhym3/couchrest-uniqueness-validation
|
57
|
+
licenses: []
|
58
|
+
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options:
|
61
|
+
- --charset=UTF-8
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
version:
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: "0"
|
75
|
+
version:
|
76
|
+
requirements: []
|
77
|
+
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 1.3.5
|
80
|
+
signing_key:
|
81
|
+
specification_version: 3
|
82
|
+
summary: adds validates_uniqueness_of validator to CouchRest::Validation
|
83
|
+
test_files:
|
84
|
+
- spec/couchrest-uniqueness-validation_spec.rb
|
85
|
+
- spec/spec_helper.rb
|