attribute_boolean 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 57169cbf013b228b30041ef730210bc853015e82
4
+ data.tar.gz: b5571255127263ac9c222318fd86918248623899
5
+ SHA512:
6
+ metadata.gz: c9db8fccc47868be968354f2155525a7c85d621b1bda1662ea03b979336b308a3a366748d8d31cfb886d33c32151886854e8bc697cffb081c4274c3b52c5e81a
7
+ data.tar.gz: 7fe570f20d134186ac5e47d3a67d96212c53940414e6575d5dec77c26c767c2400c6570d60059e95b7c872f22a340f87dbda5df845b0e7a083bece1c1583fcad
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in attribute_boolean.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Alex McHale
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Alex McHale
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,106 @@
1
+ # Attribute Boolean
2
+
3
+ The purpose of this gem is to provide an `attr_boolean` shortcut to classes.
4
+ This shortcut creates accessors that, when assigned to with a variety of
5
+ "falsey" or "truthy" data, can be interpreted as intended.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'attribute_boolean'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install attribute_boolean
22
+
23
+ ## Usage
24
+
25
+ In a standard Ruby class, you might have something like this:
26
+
27
+ ```ruby
28
+ class Calculation
29
+
30
+ include AttributeBoolean
31
+
32
+ attr_boolean :complete
33
+
34
+ end
35
+ ```
36
+
37
+ This provides you with the `"#{attr}="` and `"#{attr}?"` methods:
38
+
39
+ ```ruby
40
+ c = Calculation.new
41
+
42
+ c.complete = "1"
43
+ c.complete? #=> true
44
+
45
+ c.complete = "0"
46
+ c.complete? #=> false
47
+
48
+ c.complete = "yes"
49
+ c.complete? #=> true
50
+
51
+ c.complete = "no"
52
+ c.complete? #=> false
53
+ ```
54
+
55
+ The default values that are interpreted as `false` is:
56
+
57
+ ```ruby
58
+ [ false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF', 'n', 'N', 'no', 'NO' ]
59
+ ```
60
+
61
+ This can be customized globally like this:
62
+
63
+ ```ruby
64
+ AttributeBoolean.false_values = [ "dunno", -1, :negative ]
65
+ AttributeBoolean.false_values += [ "no way", 0 ]
66
+ ```
67
+
68
+ Or the `false_values` list can be set individually per `attr_boolean` call.
69
+ Using this option will replace the global list for that attribute.
70
+
71
+ ```ruby
72
+ attr_boolean :complete, false_values: [ 'it is not complete', 'not completed', :nope ]
73
+ attr_boolean :in_progress, false_values: Set.new(:no, 'nope')
74
+ ```
75
+
76
+ Specify a default value for the attribute that will be used if it (1) hasn't
77
+ been set, or (2) has been set to nil. The value passed to `default` is
78
+ casted to a boolean using `!!default`.
79
+
80
+ ```ruby
81
+ attr_boolean :complete, default: true
82
+ ```
83
+
84
+ AttributeBoolean converts all symbols to strings when evaluating truthiness:
85
+
86
+ ```
87
+ obj.complete = :no
88
+ obj.complete? #=> false
89
+
90
+ obj.complete = :yes
91
+ obj.complete? #=> true
92
+ ```
93
+
94
+ ### Rails
95
+
96
+ If Rails is loaded when this gem loads, it will automatically inject the
97
+ `AttributeBoolean` module into `ActiveRecord::Base`, preventing the need to
98
+ call `include AttributeBoolean` on ActiveRecord classes.
99
+
100
+ ## Contributing
101
+
102
+ 1. Fork it ( https://github.com/[my-github-username]/attribute_boolean/fork )
103
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
104
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
105
+ 4. Push to the branch (`git push origin my-new-feature`)
106
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs.push "lib", "test"
6
+ t.test_files = FileList['test/*_test.rb']
7
+ t.verbose = true
8
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'attribute_boolean'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "attribute_boolean"
8
+ spec.version = AttributeBoolean::VERSION
9
+ spec.authors = ["Alex McHale"]
10
+ spec.email = ["alex@anticlever.com"]
11
+ spec.summary = %q{A gem for adding attr_boolean to objects}
12
+ spec.description = %q{A gem for adding attr_boolean to objects, assigning by various 'falsey' values}
13
+ spec.homepage = "https://github.com/alexmchale/attribute_boolean"
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.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
@@ -0,0 +1,86 @@
1
+ module AttributeBoolean
2
+
3
+ VERSION = "0.0.1"
4
+
5
+ module ClassMethods
6
+ def attr_boolean(*args)
7
+ options =
8
+ if args.last.kind_of?(Hash)
9
+ args.pop
10
+ else
11
+ {}
12
+ end
13
+
14
+ default_value =
15
+ if options.has_key?(:default)
16
+ !! options[:default]
17
+ elsif options.has_key?("default")
18
+ !! options["default"]
19
+ else
20
+ false
21
+ end
22
+
23
+ false_values =
24
+ if options.has_key?(:false_values)
25
+ options[:false_values]
26
+ elsif options.has_key?("false_values")
27
+ options["false_values"]
28
+ end
29
+
30
+ args.each do |name|
31
+ ivar = "@#{ name }"
32
+
33
+ define_method("#{ name }?") do
34
+ value = instance_variable_get(ivar)
35
+
36
+ if value == nil
37
+ default_value
38
+ else
39
+ !! value
40
+ end
41
+ end
42
+
43
+ define_method("#{ name }=") do |value|
44
+ is_true =
45
+ if value == nil
46
+ default_value
47
+ elsif false_values
48
+ ! false_values.include?(value.to_s)
49
+ else
50
+ ! AttributeBoolean.false_values.include?(value.to_s)
51
+ end
52
+
53
+ instance_variable_set(ivar, is_true)
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ def self.included(base)
60
+ base.send(:extend, ClassMethods)
61
+ end
62
+
63
+ def self.reset_false_values!
64
+ @false_values = [ false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF', 'n', 'N', 'no', 'NO' ]
65
+ end
66
+
67
+ def self.false_values
68
+ @false_values or reset_false_values!
69
+ end
70
+
71
+ def self.false_values=(false_values)
72
+ @false_values = false_values
73
+ end
74
+
75
+ # Mix AttributeBoolean into ActiveRecord::Base if Rails is loaded.
76
+ if defined? Rails::Railtie
77
+ class Railtie < Rails::Railtie
78
+ initializer "attribute_boolean.initialize" do
79
+ ActiveSupport.on_load(:active_record) do
80
+ ActiveRecord::Base.send(:include, AttributeBoolean)
81
+ end
82
+ end
83
+ end
84
+ end
85
+
86
+ end
@@ -0,0 +1,29 @@
1
+ require "test_helper"
2
+
3
+ class AttrBooleanTest < Minitest::Test
4
+
5
+ def setup
6
+ @class =
7
+ Class.new do
8
+ include AttributeBoolean
9
+ attr_boolean :field
10
+ end
11
+
12
+ @obj = @class.new
13
+ end
14
+
15
+ def test_truthiness
16
+ [ "y", "yes", true, 1, 2, :y ].each do |value|
17
+ @obj.field = value
18
+ assert_equal true, @obj.field?, "expected #{ value.inspect } to result in true"
19
+ end
20
+ end
21
+
22
+ def test_falsiness
23
+ [ false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF', 'n', 'N', 'no', 'NO' ].each do |value|
24
+ @obj.field = value
25
+ assert_equal false, @obj.field?, "expected #{ value.inspect } to result in false"
26
+ end
27
+ end
28
+
29
+ end
@@ -0,0 +1,36 @@
1
+ require "test_helper"
2
+
3
+ class DefaultTest < Minitest::Test
4
+
5
+ def setup
6
+ @class =
7
+ Class.new do
8
+ include AttributeBoolean
9
+ attr_boolean :default_false, default: false
10
+ attr_boolean :default_true, default: true
11
+ end
12
+
13
+ @obj = @class.new
14
+ end
15
+
16
+ def test_default_false
17
+ assert_equal false, @obj.default_false?, "expected default_false to default to false"
18
+
19
+ @obj.default_false = true
20
+ assert_equal true, @obj.default_false?, "expceted default_false to set to true"
21
+
22
+ @obj.default_false = nil
23
+ assert_equal false, @obj.default_false?, "expected default_false to revert to false"
24
+ end
25
+
26
+ def test_default_true
27
+ assert_equal true, @obj.default_true?, "expected default_true to default to true"
28
+
29
+ @obj.default_true = false
30
+ assert_equal false, @obj.default_true?, "expceted default_true to set to false"
31
+
32
+ @obj.default_true = nil
33
+ assert_equal true, @obj.default_true?, "expected default_true to revert to true"
34
+ end
35
+
36
+ end
@@ -0,0 +1,34 @@
1
+ require "test_helper"
2
+
3
+ class GlobalFalseValuesTest < Minitest::Test
4
+
5
+ def setup
6
+ @class =
7
+ Class.new do
8
+ include AttributeBoolean
9
+ attr_boolean :finished
10
+ end
11
+
12
+ @obj = @class.new
13
+ end
14
+
15
+ def test_custom_false
16
+ AttributeBoolean.false_values = [ "no way" ]
17
+
18
+ assert_equal [ "no way" ], AttributeBoolean.false_values
19
+
20
+ @obj.finished = false
21
+ assert_equal true, @obj.finished?, "returns true for values not listed in the global false values"
22
+
23
+ @obj.finished = "no way"
24
+ assert_equal false, @obj.finished?, "returns false for value listed in the global false values"
25
+
26
+ @obj.finished = :"no way"
27
+ assert_equal false, @obj.finished?, "also returns false for symbolized versions of those values"
28
+ end
29
+
30
+ def teardown
31
+ AttributeBoolean.reset_false_values!
32
+ end
33
+
34
+ end
@@ -0,0 +1,26 @@
1
+ require "test_helper"
2
+
3
+ class LocalFalseValuesTest < Minitest::Test
4
+
5
+ def setup
6
+ @class =
7
+ Class.new do
8
+ include AttributeBoolean
9
+ attr_boolean :finished, false_values: [ "nope", "nut uh" ]
10
+ end
11
+
12
+ @obj = @class.new
13
+ end
14
+
15
+ def test_custom_false
16
+ @obj.finished = false
17
+ assert_equal true, @obj.finished?, "returns true for values not listed in the global false values"
18
+
19
+ @obj.finished = "nut uh"
20
+ assert_equal false, @obj.finished?, "returns false for value listed in the global false values"
21
+
22
+ @obj.finished = :"nut uh"
23
+ assert_equal false, @obj.finished?, "also returns false for symbolized versions of those values"
24
+ end
25
+
26
+ end
@@ -0,0 +1,4 @@
1
+ require "minitest/autorun"
2
+ require "minitest/pride"
3
+
4
+ require "attribute_boolean"
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attribute_boolean
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alex McHale
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: A gem for adding attr_boolean to objects, assigning by various 'falsey'
42
+ values
43
+ email:
44
+ - alex@anticlever.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - LICENSE
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - attribute_boolean.gemspec
56
+ - lib/attribute_boolean.rb
57
+ - test/attr_boolean_test.rb
58
+ - test/default_test.rb
59
+ - test/global_false_values_test.rb
60
+ - test/local_false_values_test.rb
61
+ - test/test_helper.rb
62
+ homepage: https://github.com/alexmchale/attribute_boolean
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.2.2
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: A gem for adding attr_boolean to objects
86
+ test_files:
87
+ - test/attr_boolean_test.rb
88
+ - test/default_test.rb
89
+ - test/global_false_values_test.rb
90
+ - test/local_false_values_test.rb
91
+ - test/test_helper.rb
92
+ has_rdoc: