bson_object_id_validations 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bson_object_id_validator.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Nick Hoffman
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # BsonObjectIdValidations
2
+
3
+ Provides an ActiveModel validation that checks if an attribute represents a legal BSON::ObjectId .
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'bson_object_id_validations'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install bson_object_id_validations
19
+
20
+
21
+ ## Usage
22
+
23
+ Validate that the specified attribute's value is a legal BSON::ObjectId .
24
+
25
+ ```ruby
26
+ class User
27
+ include ActiveModel::Validations
28
+
29
+ attr_accessor :mongo_id
30
+
31
+ validates :mongo_id, :legal_bson_object_id => true
32
+ end
33
+
34
+ user = User.new :mongo_id => "invalid"
35
+
36
+ user.valid?
37
+ => false
38
+
39
+ user.errors
40
+ => {:mongo_id=>["is an invalid BSON::ObjectId"]}
41
+
42
+ user.mongo_id = BSON::ObjectId.new.to_s
43
+
44
+ user.valid?
45
+ => true
46
+ ```
47
+
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it.
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`).
53
+ 3. Commit your changes (`git commit -am 'Added some feature'`).
54
+ 4. Push to your branch (`git push origin my-new-feature`).
55
+ 5. Send a pull request.
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core'
5
+ require 'rspec/core/rake_task'
6
+
7
+ RSpec::Core::RakeTask.new(:spec) do |spec|
8
+ spec.pattern = FileList['spec/**/*_spec.rb']
9
+ end
10
+
11
+ task :default => :spec
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/bson_object_id_validations/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Nick Hoffman"]
6
+ gem.email = ["nick@deadorange.com"]
7
+ gem.description = %q{Provides an ActiveModel validation that checks if an attribute represents a legal BSON::ObjectId .}
8
+ gem.summary = %q{Provides an ActiveModel validation that checks if an attribute represents a legal BSON::ObjectId .}
9
+ gem.homepage = "https://github.com/nickhoffman/bson_object_id_validations"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "bson_object_id_validations"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = BsonObjectIdValidations::VERSION
17
+
18
+ gem.add_runtime_dependency 'activemodel', '>= 3.0'
19
+ gem.add_runtime_dependency 'bson_ext', '>= 1.6'
20
+
21
+ gem.add_development_dependency 'rspec', '>= 2.8'
22
+ gem.add_development_dependency 'rake', '>= 0.9.0'
23
+ end
@@ -0,0 +1,7 @@
1
+ require 'active_model'
2
+ require 'bson'
3
+ require "bson_object_id_validations/version"
4
+ require "bson_object_id_validations/legal_bson_object_id_validator"
5
+
6
+ module BsonObjectIdValidations
7
+ end
@@ -0,0 +1,37 @@
1
+ module BsonObjectIdValidations
2
+ class LegalBsonObjectIdValidator < ActiveModel::EachValidator
3
+ @@error_message = 'is an invalid BSON::ObjectId'
4
+
5
+ # Validate that the specified attribute's value is a legal BSON::ObjectId .
6
+ #
7
+ # @example
8
+ # class User
9
+ # include ActiveModel::Validations
10
+ #
11
+ # attr_accessor :mongo_id
12
+ #
13
+ # validates :mongo_id, :legal_bson_object_id => true
14
+ # end
15
+ #
16
+ # user = User.new :mongo_id => "invalid"
17
+ #
18
+ # user.valid?
19
+ # => false
20
+ #
21
+ # user.errors
22
+ # => {:mongo_id=>["is an invalid BSON::ObjectId"]}
23
+ #
24
+ # user.mongo_id = BSON::ObjectId.new.to_s
25
+ #
26
+ # user.valid?
27
+ # => true
28
+ #
29
+ def validate_each(record, attribute, value)
30
+ error_message = options[:message] || @@error_message
31
+
32
+ unless ::BSON::ObjectId.legal? value
33
+ record.errors.add attribute, error_message
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module BsonObjectIdValidations
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,50 @@
1
+ require 'spec_helper'
2
+
3
+ describe BsonObjectIdValidations::LegalBsonObjectIdValidator do
4
+ class User
5
+ include ActiveModel::Validations
6
+ include BsonObjectIdValidations
7
+
8
+ attr_accessor :mongo_id_1
9
+ attr_accessor :mongo_id_2
10
+
11
+ validates :mongo_id_1, :legal_bson_object_id => true
12
+ validates :mongo_id_2, :legal_bson_object_id => {:message => 'a custom error message'}
13
+ end
14
+
15
+ it 'inherits from ActiveModel::EachValidator' do
16
+ described_class.superclass.should equal ActiveModel::EachValidator
17
+ end
18
+
19
+ describe 'class variables' do # {{{
20
+ it 'has @@error_message' do
21
+ described_class.class_variable_get(:@@error_message).
22
+ should == 'is an invalid BSON::ObjectId'
23
+ end
24
+ end # }}}
25
+
26
+ describe '#validate_each' do # {{{
27
+ let(:user) do
28
+ User.new :mongo_id_1 => 'invalid', :mongo_id_2 => 'invalid'
29
+ end
30
+
31
+ context 'when the attribute contains an invalid value' do # {{{
32
+ it 'adds an error on the attribute' do
33
+ user.valid?
34
+ user.errors[:mongo_id_1].count.should be 1
35
+ end
36
+
37
+ it 'uses the standard error message' do
38
+ user.valid?
39
+ user.errors[:mongo_id_1].should == ['is an invalid BSON::ObjectId']
40
+ end
41
+
42
+ context 'when a custom error message is provided' do # {{{
43
+ it 'uses the custom error message' do
44
+ user.valid?
45
+ user.errors[:mongo_id_2].should == ['a custom error message']
46
+ end
47
+ end # }}}
48
+ end # }}}
49
+ end # }}}
50
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe BsonObjectIdValidations do
4
+ it 'is at version 0.0.1' do
5
+ BsonObjectIdValidations::VERSION.should == '1.0.0'
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ require 'rspec'
2
+ require 'bson_object_id_validations'
3
+
4
+ RSpec.configure do |config|
5
+ config.treat_symbols_as_metadata_keys_with_true_values = true
6
+ config.run_all_when_everything_filtered = true
7
+ config.filter_run :focus
8
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bson_object_id_validations
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nick Hoffman
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-12 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activemodel
16
+ requirement: &80618720 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *80618720
25
+ - !ruby/object:Gem::Dependency
26
+ name: bson_ext
27
+ requirement: &80618300 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '1.6'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *80618300
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &80606080 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '2.8'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *80606080
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: &80605760 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 0.9.0
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *80605760
58
+ description: Provides an ActiveModel validation that checks if an attribute represents
59
+ a legal BSON::ObjectId .
60
+ email:
61
+ - nick@deadorange.com
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - .gitignore
67
+ - .rspec
68
+ - Gemfile
69
+ - LICENSE
70
+ - README.md
71
+ - Rakefile
72
+ - bson_object_id_validations.gemspec
73
+ - lib/bson_object_id_validations.rb
74
+ - lib/bson_object_id_validations/legal_bson_object_id_validator.rb
75
+ - lib/bson_object_id_validations/version.rb
76
+ - spec/bson_object_id_validations/legal_bson_object_id_validator_spec.rb
77
+ - spec/bson_object_id_validations_spec.rb
78
+ - spec/spec_helper.rb
79
+ homepage: https://github.com/nickhoffman/bson_object_id_validations
80
+ licenses: []
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ segments:
92
+ - 0
93
+ hash: 394965309
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ segments:
101
+ - 0
102
+ hash: 394965309
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 1.8.10
106
+ signing_key:
107
+ specification_version: 3
108
+ summary: Provides an ActiveModel validation that checks if an attribute represents
109
+ a legal BSON::ObjectId .
110
+ test_files:
111
+ - spec/bson_object_id_validations/legal_bson_object_id_validator_spec.rb
112
+ - spec/bson_object_id_validations_spec.rb
113
+ - spec/spec_helper.rb