mongoid-force_boolean 0.0.2
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.
- checksums.yaml +7 -0
- data/.gitignore +27 -0
- data/.rspec +1 -0
- data/Gemfile +6 -0
- data/README.md +55 -0
- data/Rakefile +12 -0
- data/lib/force_boolean.rb +45 -0
- data/lib/force_boolean/version.rb +5 -0
- data/mongoid-force_boolean.gemspec +18 -0
- data/spec/force_boolean_spec.rb +94 -0
- data/spec/spec_helper.rb +7 -0
- metadata +80 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 1c2d83d40e2327d22a75edf0e8748601dfbb367e
|
|
4
|
+
data.tar.gz: e494d58f5c40daa59f67c25b3d0cd0e30df996c7
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 37e1ecceb6c01da506c563e94ed59182b3d1fdba9ad326ec31e34af63b56afc943200c7be24b9b56de7ce7f13a0a028054e9724d49bca3b079fc6068e7c80d55
|
|
7
|
+
data.tar.gz: 75648b9afc95e4ac7f2d8fbcf1e92e951e0f71325564f17816f030b00dd077fec1e6e569896872dc3e2d19ea8e8ac8ab6fed4bb9377488461d68e47c501ee206
|
data/.gitignore
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# Because this is a gem, ignore Gemfile.lock:
|
|
2
|
+
|
|
3
|
+
Gemfile.lock
|
|
4
|
+
|
|
5
|
+
# And because this is Ruby, ignore the following
|
|
6
|
+
# (source: https://github.com/github/gitignore/blob/master/Ruby.gitignore):
|
|
7
|
+
|
|
8
|
+
*.gem
|
|
9
|
+
*.rbc
|
|
10
|
+
.bundle
|
|
11
|
+
.config
|
|
12
|
+
coverage
|
|
13
|
+
InstalledFiles
|
|
14
|
+
lib/bundler/man
|
|
15
|
+
pkg
|
|
16
|
+
rdoc
|
|
17
|
+
spec/reports
|
|
18
|
+
test/tmp
|
|
19
|
+
test/version_tmp
|
|
20
|
+
tmp
|
|
21
|
+
|
|
22
|
+
# YARD artifacts
|
|
23
|
+
.yardoc
|
|
24
|
+
_yardoc
|
|
25
|
+
doc/
|
|
26
|
+
tags
|
|
27
|
+
.rvmrc
|
data/.rspec
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# mongoid-force_boolean
|
|
2
|
+
|
|
3
|
+
Mongoid document boolean type field must be boolean.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
- git clone git@github.com:tumayun/mongoid-force_boolean.git
|
|
8
|
+
- cd mongoid-force_boolean
|
|
9
|
+
- gem build mongoid-force_boolean.gemspec
|
|
10
|
+
- gem install mongoid-force_boolean-x.x.x.gem
|
|
11
|
+
- Add this line to your application's Gemfile:
|
|
12
|
+
|
|
13
|
+
gem 'mongoid-force_boolean', require: 'force_boolean'
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
```ruby
|
|
19
|
+
class Post
|
|
20
|
+
include Mongoid::Document
|
|
21
|
+
include Mongoid::ForceBoolean
|
|
22
|
+
|
|
23
|
+
field :published, type: Boolean
|
|
24
|
+
field :title, type: String
|
|
25
|
+
field :body, type: String
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
post = Post.new(title: 'title', body: 'body', publushed: 0)
|
|
29
|
+
post.save #=> true
|
|
30
|
+
post.published #=> false
|
|
31
|
+
|
|
32
|
+
post.published = 1
|
|
33
|
+
post.save #=> true
|
|
34
|
+
post.published #=> true
|
|
35
|
+
|
|
36
|
+
post.published = false
|
|
37
|
+
post.save #=> true
|
|
38
|
+
post.published #=> false
|
|
39
|
+
|
|
40
|
+
post.published = true
|
|
41
|
+
post.save #=> true
|
|
42
|
+
post.published #=> true
|
|
43
|
+
|
|
44
|
+
post.published = 100
|
|
45
|
+
post.save #=> false
|
|
46
|
+
post.errors[:published] #=> ['must be boolean']
|
|
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 'Add some feature'`)
|
|
54
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
55
|
+
5. Create new Pull Reques
|
data/Rakefile
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
|
2
|
+
|
|
3
|
+
require 'bundler'
|
|
4
|
+
Bundler::GemHelper.install_tasks
|
|
5
|
+
|
|
6
|
+
require 'rspec/core/rake_task'
|
|
7
|
+
|
|
8
|
+
task :default => :spec
|
|
9
|
+
|
|
10
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
|
11
|
+
spec.pattern = "./spec/**/*_spec.rb"
|
|
12
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
module Mongoid
|
|
2
|
+
module ForceBoolean
|
|
3
|
+
extend ActiveSupport::Concern
|
|
4
|
+
class NotMongoidDocumentError < StandardError; end
|
|
5
|
+
|
|
6
|
+
included do
|
|
7
|
+
raise NotMongoidDocumentError, "The #{self.name} class is not Mongoid::Document." unless ancestors.include?(Mongoid::Document)
|
|
8
|
+
|
|
9
|
+
validate :force_boolean, if: :has_boolean_field?
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def force_boolean
|
|
13
|
+
self.class.boolean_fields.map do |field|
|
|
14
|
+
if (field_value = self.read_attribute(field)) && field_value != true && field_value != false
|
|
15
|
+
|
|
16
|
+
if field_value.to_s == '0'
|
|
17
|
+
self.write_attribute(field, false)
|
|
18
|
+
elsif field_value.to_s == '1'
|
|
19
|
+
self.write_attribute(field, true)
|
|
20
|
+
elsif !field_value.nil?
|
|
21
|
+
self.errors.add(field, 'must be boolean')
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
self.errors.empty?
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def has_boolean_field?
|
|
30
|
+
self.class.has_boolean_field?
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
module ClassMethods
|
|
34
|
+
|
|
35
|
+
def boolean_fields
|
|
36
|
+
return @boolean_fields if defined?(@boolean_fields)
|
|
37
|
+
@boolean_fields = fields.select { |_, field| field.type == ::Boolean }.keys
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def has_boolean_field?
|
|
41
|
+
!boolean_fields.empty?
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
$: << File.expand_path('../lib', __FILE__)
|
|
2
|
+
require 'force_boolean/version'
|
|
3
|
+
|
|
4
|
+
Gem::Specification.new do |gem|
|
|
5
|
+
gem.name = 'mongoid-force_boolean'
|
|
6
|
+
gem.version = Mongoid::ForceBoolean::VERSION
|
|
7
|
+
gem.authors = 'Tumayun'
|
|
8
|
+
gem.email = 'tumayun.2010@gmail.com'
|
|
9
|
+
gem.homepage = 'https://github.com/tumayun/mongoid-force_boolean'
|
|
10
|
+
gem.summary = 'Mongoid document boolean type field must be boolean.'
|
|
11
|
+
gem.description = 'Mongoid document boolean type field must be boolean.'
|
|
12
|
+
|
|
13
|
+
gem.files = `git ls-files`.split("\n")
|
|
14
|
+
gem.require_path = 'lib'
|
|
15
|
+
|
|
16
|
+
gem.add_dependency 'activesupport', '>= 3.0'
|
|
17
|
+
gem.add_dependency 'mongoid', '>= 3.1.4'
|
|
18
|
+
end
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
|
2
|
+
|
|
3
|
+
describe Mongoid::ForceBoolean do
|
|
4
|
+
|
|
5
|
+
class TestA
|
|
6
|
+
include Mongoid::Document
|
|
7
|
+
include Mongoid::ForceBoolean
|
|
8
|
+
|
|
9
|
+
field :published, type: Boolean
|
|
10
|
+
field :title, type: String
|
|
11
|
+
field :body, type: String
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class TestB
|
|
15
|
+
include Mongoid::Document
|
|
16
|
+
include Mongoid::ForceBoolean
|
|
17
|
+
|
|
18
|
+
field :title, type: String
|
|
19
|
+
field :body, type: String
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
class TestC
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
context 'included Mongoid::Document' do
|
|
26
|
+
it 'TestA.has_boolean_field? should be true' do
|
|
27
|
+
TestA.should be_has_boolean_field
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it 'TestA.boolean_fields should eq ["published"]' do
|
|
31
|
+
TestA.boolean_fields.sort.should == ["published"]
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
context 'TestA#force_boolean' do
|
|
35
|
+
|
|
36
|
+
it 'boolean field value is "0" should will become false' do
|
|
37
|
+
a = TestA.new(published: "0")
|
|
38
|
+
a.published.should == "0"
|
|
39
|
+
a.force_boolean
|
|
40
|
+
a.published.should == false
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it 'boolean field value is "1" should will become true' do
|
|
44
|
+
a = TestA.new(published: "1")
|
|
45
|
+
a.published.should == "1"
|
|
46
|
+
a.force_boolean
|
|
47
|
+
a.published.should == true
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
it 'boolean field value is false' do
|
|
51
|
+
a = TestA.new(published: false)
|
|
52
|
+
a.published.should == false
|
|
53
|
+
a.force_boolean
|
|
54
|
+
a.published.should == false
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it 'boolean field value is true' do
|
|
58
|
+
a = TestA.new(published: true)
|
|
59
|
+
a.published.should == true
|
|
60
|
+
a.force_boolean
|
|
61
|
+
a.published.should == true
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
it 'boolean field value is nil' do
|
|
65
|
+
a = TestA.new
|
|
66
|
+
a.published.should be_nil
|
|
67
|
+
a.force_boolean
|
|
68
|
+
a.published.should be_nil
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
it 'boolean field value is other values' do
|
|
72
|
+
a = TestA.new(published: 100)
|
|
73
|
+
a.published.should == 100
|
|
74
|
+
a.force_boolean
|
|
75
|
+
a.errors[:published].should == ["must be boolean"]
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
it 'TestB.has_boolean_field? should be false' do
|
|
80
|
+
TestB.should_not be_has_boolean_field
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
it 'TestB.boolean_fields should be empty' do
|
|
84
|
+
TestB.boolean_fields.sort.should be_empty
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
context 'no included Mongoid::Document' do
|
|
89
|
+
it 'should raise Mongoid::ForceBoolean::NotMongoidDocumentError' do
|
|
90
|
+
-> { TestC.send(:include, Mongoid::ForceBoolean) }.
|
|
91
|
+
should raise_error(Mongoid::ForceBoolean::NotMongoidDocumentError, "The TestC class is not Mongoid::Document.")
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: mongoid-force_boolean
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.2
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Tumayun
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2013-06-21 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: activesupport
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - '>='
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '3.0'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - '>='
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '3.0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: mongoid
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - '>='
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: 3.1.4
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - '>='
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: 3.1.4
|
|
41
|
+
description: Mongoid document boolean type field must be boolean.
|
|
42
|
+
email: tumayun.2010@gmail.com
|
|
43
|
+
executables: []
|
|
44
|
+
extensions: []
|
|
45
|
+
extra_rdoc_files: []
|
|
46
|
+
files:
|
|
47
|
+
- .gitignore
|
|
48
|
+
- .rspec
|
|
49
|
+
- Gemfile
|
|
50
|
+
- README.md
|
|
51
|
+
- Rakefile
|
|
52
|
+
- lib/force_boolean.rb
|
|
53
|
+
- lib/force_boolean/version.rb
|
|
54
|
+
- mongoid-force_boolean.gemspec
|
|
55
|
+
- spec/force_boolean_spec.rb
|
|
56
|
+
- spec/spec_helper.rb
|
|
57
|
+
homepage: https://github.com/tumayun/mongoid-force_boolean
|
|
58
|
+
licenses: []
|
|
59
|
+
metadata: {}
|
|
60
|
+
post_install_message:
|
|
61
|
+
rdoc_options: []
|
|
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
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
70
|
+
requirements:
|
|
71
|
+
- - '>='
|
|
72
|
+
- !ruby/object:Gem::Version
|
|
73
|
+
version: '0'
|
|
74
|
+
requirements: []
|
|
75
|
+
rubyforge_project:
|
|
76
|
+
rubygems_version: 2.0.0.rc.2
|
|
77
|
+
signing_key:
|
|
78
|
+
specification_version: 4
|
|
79
|
+
summary: Mongoid document boolean type field must be boolean.
|
|
80
|
+
test_files: []
|