maybe_so 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.travis.yml +15 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +76 -0
- data/Rakefile +2 -0
- data/lib/maybe_so/active_model/boolean_attribute.rb +41 -0
- data/lib/maybe_so/active_model.rb +19 -0
- data/lib/maybe_so/core_ext/false_class.rb +11 -0
- data/lib/maybe_so/core_ext/fixnum.rb +7 -0
- data/lib/maybe_so/core_ext/nil_class.rb +7 -0
- data/lib/maybe_so/core_ext/string.rb +7 -0
- data/lib/maybe_so/core_ext/true_class.rb +11 -0
- data/lib/maybe_so/core_ext.rb +5 -0
- data/lib/maybe_so/version.rb +3 -0
- data/lib/maybe_so.rb +13 -0
- data/maybe_so.gemspec +25 -0
- data/spec/boolean_attribute_spec.rb +61 -0
- data/spec/false_class_spec.rb +15 -0
- data/spec/fixnum_spec.rb +23 -0
- data/spec/nil_class_spec.rb +9 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/string_spec.rb +19 -0
- data/spec/true_class_spec.rb +15 -0
- metadata +145 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f804350c62df7af2cf73422bfaa3a9cc219ce8a5
|
4
|
+
data.tar.gz: 0f5a5092a9149b39f1660f820f0cdd5ec1151a24
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0b7105d8f3f7e5a756b4169687f7d5f820f219f5660ddf5c22ff949a0c017ceed22f71e73f787e9d7c084d51882f71e3f239f2a61da9b915074f102a49f4b090
|
7
|
+
data.tar.gz: 6aa63ce3a34660f2ba0b01e0741774ff87b67ad7d861570a70dec5e88e79cf8fcd1a5f69a78347a1d984afd198e6b332f4b72c91ff5efe0b3714293c97dabe87
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
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
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 James Miller & Tanner Mares
|
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,76 @@
|
|
1
|
+
# Maybe So
|
2
|
+
|
3
|
+
Ever get annoyed that ActiveRecord blows up if you set a boolean column to nil? Have you ever wanted to allow custom "truthy" values for booleans rather than just `true`, `false`, `0`, and `1`? Wish you could call `to_bool` on random objects and have it return something reasonable?
|
4
|
+
|
5
|
+
Maybe So handles this for you!
|
6
|
+
|
7
|
+
Tested on the following Rubies: MRI 1.9.3, 2.0.0, 2.1.x, JRuby (1.7.2 and newer).
|
8
|
+
|
9
|
+
[![Build Status](https://secure.travis-ci.org/ministrycentered/maybe_so.svg?branch=master)](http://travis-ci.org/ministrycentered/maybe_so)
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'maybe_so'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install maybe_so
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
### ActiveRecord
|
28
|
+
|
29
|
+
Let's say you have an ActiveRecord attribute called `hidden` that you want to only be 0 or 1, never nil.
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
add_column :products, :hidden, :boolean, default: false, null: false
|
33
|
+
```
|
34
|
+
|
35
|
+
Add the `boolean_attribute` class method on your class.
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
class Product < ActiveRecord::Base
|
39
|
+
boolean_attribute :hidden
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
By default, Maybe So accepts the following case insensitive values as `true`. Everything else is `false`. You can override these values with the `truthy_values` option (see below).
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
true, "true", "Y", "Yes", 1, "1", "T"
|
47
|
+
```
|
48
|
+
|
49
|
+
### ActiveModel
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
class Product
|
53
|
+
include ActiveModel::Model
|
54
|
+
|
55
|
+
boolean_attribute :hidden
|
56
|
+
end
|
57
|
+
```
|
58
|
+
|
59
|
+
### Configuration options
|
60
|
+
|
61
|
+
`boolean_attributes` accepts an options hash for the following:
|
62
|
+
|
63
|
+
- `truthy_values`: array of values (will be coerced into strings when checking) to be considered truthy
|
64
|
+
- `skip_validator`: this will not add the default `validates_inclusion_of` to ensure records are not valid unless they are set to `true` or `false`
|
65
|
+
|
66
|
+
Example custom configuration:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
class Product < ActiveRecord::Base
|
70
|
+
boolean_attribute :hidden, truthy_values: ["Si"], skip_validator: true
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
## Copyright
|
75
|
+
|
76
|
+
Copyright (c) 2014 James Miller and Tanner Mares
|
data/Rakefile
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
module MaybeSo
|
2
|
+
module ActiveModel
|
3
|
+
module BooleanAttribute
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.extend(ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def boolean_attribute(attribute, options = {})
|
11
|
+
truthy_values = options.delete(:truthy_values) || MaybeSo::DEFAULT_TRUTHY_VALUES
|
12
|
+
skip_validator = options.delete(:skip_validator)
|
13
|
+
|
14
|
+
# Override the attribute's ActiveModel or ActiveRecord setter to always set a boolean
|
15
|
+
#
|
16
|
+
# Example:
|
17
|
+
# def hidden=(hidden)
|
18
|
+
# ...
|
19
|
+
# end
|
20
|
+
define_method "#{attribute}=" do |value|
|
21
|
+
boolean = truthy_values.include? value.to_s.downcase
|
22
|
+
if respond_to? :[]= # Use ActiveRecord setter
|
23
|
+
self[attribute.to_sym] = boolean
|
24
|
+
else
|
25
|
+
instance_variable_set("@#{attribute}", boolean)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Add a validator that ensures only `true` or `false` are ever written to
|
30
|
+
# the database, regardless of whether or not the setter above was used for
|
31
|
+
# assignment.
|
32
|
+
unless skip_validator
|
33
|
+
instance_eval do
|
34
|
+
validates_inclusion_of attribute.to_sym, in: [true, false]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require "maybe_so/active_model/boolean_attribute"
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
module Model
|
5
|
+
class << self
|
6
|
+
def included_with_boolean_attribute(base)
|
7
|
+
included_without_boolean_attribute(base)
|
8
|
+
base.class_eval do
|
9
|
+
include MaybeSo::ActiveModel::BooleanAttribute
|
10
|
+
end
|
11
|
+
end
|
12
|
+
alias_method_chain :included, :boolean_attribute
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
if defined?(::ActiveRecord)
|
18
|
+
ActiveRecord::Base.send(:include, MaybeSo::ActiveModel::BooleanAttribute)
|
19
|
+
end
|
data/lib/maybe_so.rb
ADDED
data/maybe_so.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'maybe_so/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "maybe_so"
|
8
|
+
spec.version = MaybeSo::VERSION
|
9
|
+
spec.authors = ["James Miller", "Tanner Mares"]
|
10
|
+
spec.email = ["bensie@gmail.com", "tannermares@gmail.com"]
|
11
|
+
spec.summary = %q{Ruby boolean type casting}
|
12
|
+
spec.description = %q{Ruby boolean type casting}
|
13
|
+
spec.homepage = "https://github.com/ministrycentered/maybe_so"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake", '~> 10'
|
23
|
+
spec.add_development_dependency "rspec", ">= 3.0.0", "< 4"
|
24
|
+
spec.add_development_dependency "activemodel", ">= 3.2.0", "< 5"
|
25
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe MaybeSo::ActiveModel::BooleanAttribute do
|
4
|
+
before do
|
5
|
+
@klass = Class.new
|
6
|
+
@klass.send(:include, ActiveModel::Model)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "has the #boolean_attribute method on the class" do
|
10
|
+
expect(@klass).to respond_to(:boolean_attribute)
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#boolean_attribute" do
|
14
|
+
describe "attribute setter override" do
|
15
|
+
it "overrides the setter" do
|
16
|
+
@klass.boolean_attribute :hidden
|
17
|
+
expect(@klass.instance_methods).to include(:hidden=)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "adds the attribute validator" do
|
21
|
+
@klass.boolean_attribute :hidden
|
22
|
+
expect(@klass.validators.map(&:attributes).flatten).to include(:hidden)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "skips the validator if arg is provided" do
|
26
|
+
@klass.boolean_attribute :hidden, skip_validator: true
|
27
|
+
expect(@klass.validators.map(&:attributes).flatten).to_not include(:hidden)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "when a boolean attribute is defined" do
|
32
|
+
before do
|
33
|
+
@klass.send(:attr_reader, :hidden)
|
34
|
+
@klass.boolean_attribute :hidden
|
35
|
+
@record = @klass.new
|
36
|
+
end
|
37
|
+
|
38
|
+
it "accepts truthy values and reads them as booleans" do
|
39
|
+
@record.hidden = "T"
|
40
|
+
expect(@record.hidden).to eq(true)
|
41
|
+
|
42
|
+
@record.hidden = "Yes"
|
43
|
+
expect(@record.hidden).to eq(true)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "accepts falsy values and reads them as booleans" do
|
47
|
+
@record.hidden = "F"
|
48
|
+
expect(@record.hidden).to eq(false)
|
49
|
+
|
50
|
+
@record.hidden = "No"
|
51
|
+
expect(@record.hidden).to eq(false)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "coerces nil to false" do
|
55
|
+
@record.hidden = nil
|
56
|
+
expect(@record.hidden).to eq(false)
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/spec/fixnum_spec.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Fixnum do
|
4
|
+
describe "#to_bool" do
|
5
|
+
context "when 1" do
|
6
|
+
it "returns true" do
|
7
|
+
expect(1.to_bool).to eq(true)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
context "when 0" do
|
12
|
+
it "returns false" do
|
13
|
+
expect(0.to_bool).to eq(false)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context "when some other number" do
|
18
|
+
it "returns false" do
|
19
|
+
expect(42.to_bool).to eq(false)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/spec/string_spec.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe String do
|
4
|
+
describe "#to_bool" do
|
5
|
+
MaybeSo::DEFAULT_TRUTHY_VALUES.each do |truth|
|
6
|
+
context "when #{truth}" do
|
7
|
+
it "returns true" do
|
8
|
+
expect(truth.to_bool).to eq(true)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context "when any other string value" do
|
14
|
+
it "returns false" do
|
15
|
+
expect("foooo".to_bool).to eq(false)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: maybe_so
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- James Miller
|
8
|
+
- Tanner Mares
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-07-29 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.6'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.6'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '10'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '10'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rspec
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 3.0.0
|
49
|
+
- - "<"
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '4'
|
52
|
+
type: :development
|
53
|
+
prerelease: false
|
54
|
+
version_requirements: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: 3.0.0
|
59
|
+
- - "<"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '4'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: activemodel
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.2.0
|
69
|
+
- - "<"
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
version: '5'
|
72
|
+
type: :development
|
73
|
+
prerelease: false
|
74
|
+
version_requirements: !ruby/object:Gem::Requirement
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: 3.2.0
|
79
|
+
- - "<"
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '5'
|
82
|
+
description: Ruby boolean type casting
|
83
|
+
email:
|
84
|
+
- bensie@gmail.com
|
85
|
+
- tannermares@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".travis.yml"
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- lib/maybe_so.rb
|
97
|
+
- lib/maybe_so/active_model.rb
|
98
|
+
- lib/maybe_so/active_model/boolean_attribute.rb
|
99
|
+
- lib/maybe_so/core_ext.rb
|
100
|
+
- lib/maybe_so/core_ext/false_class.rb
|
101
|
+
- lib/maybe_so/core_ext/fixnum.rb
|
102
|
+
- lib/maybe_so/core_ext/nil_class.rb
|
103
|
+
- lib/maybe_so/core_ext/string.rb
|
104
|
+
- lib/maybe_so/core_ext/true_class.rb
|
105
|
+
- lib/maybe_so/version.rb
|
106
|
+
- maybe_so.gemspec
|
107
|
+
- spec/boolean_attribute_spec.rb
|
108
|
+
- spec/false_class_spec.rb
|
109
|
+
- spec/fixnum_spec.rb
|
110
|
+
- spec/nil_class_spec.rb
|
111
|
+
- spec/spec_helper.rb
|
112
|
+
- spec/string_spec.rb
|
113
|
+
- spec/true_class_spec.rb
|
114
|
+
homepage: https://github.com/ministrycentered/maybe_so
|
115
|
+
licenses:
|
116
|
+
- MIT
|
117
|
+
metadata: {}
|
118
|
+
post_install_message:
|
119
|
+
rdoc_options: []
|
120
|
+
require_paths:
|
121
|
+
- lib
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
requirements: []
|
133
|
+
rubyforge_project:
|
134
|
+
rubygems_version: 2.2.2
|
135
|
+
signing_key:
|
136
|
+
specification_version: 4
|
137
|
+
summary: Ruby boolean type casting
|
138
|
+
test_files:
|
139
|
+
- spec/boolean_attribute_spec.rb
|
140
|
+
- spec/false_class_spec.rb
|
141
|
+
- spec/fixnum_spec.rb
|
142
|
+
- spec/nil_class_spec.rb
|
143
|
+
- spec/spec_helper.rb
|
144
|
+
- spec/string_spec.rb
|
145
|
+
- spec/true_class_spec.rb
|