attribool 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 +7 -0
- data/.gitignore +2 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +31 -0
- data/LICENSE +21 -0
- data/README.md +127 -0
- data/Rakefile +21 -0
- data/attribool.gemspec +36 -0
- data/lib/attribool.rb +96 -0
- data/lib/attribool/version.rb +34 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 469b94667c5be65a2901c9049f8290711c823fc7d08226140471c4c8820e66df
|
4
|
+
data.tar.gz: 526c90658fc7f1ac712383083a5c38ced3088f3ed91fc32af3a6801e7c0352cc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 90be0ebf1c939bf765d0dd3848cf273b00606d4e00335418b1fc44de239ffb5f2042032525a22f9b205b9b10b6ff71126ba0a3c01449d2957fe328f4be354359
|
7
|
+
data.tar.gz: 48dbb1b70a57413523a962d29432343e5c7117f06ceae8063a7de1a5306595bab5f469db638a25bed1074efee037982e4a776c75334168e9d6e0064359e9e240
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
attribool (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
docile (1.3.5)
|
10
|
+
power_assert (2.0.0)
|
11
|
+
rake (13.0.3)
|
12
|
+
simplecov (0.21.2)
|
13
|
+
docile (~> 1.1)
|
14
|
+
simplecov-html (~> 0.11)
|
15
|
+
simplecov_json_formatter (~> 0.1)
|
16
|
+
simplecov-html (0.12.3)
|
17
|
+
simplecov_json_formatter (0.1.2)
|
18
|
+
test-unit (3.4.0)
|
19
|
+
power_assert
|
20
|
+
|
21
|
+
PLATFORMS
|
22
|
+
ruby
|
23
|
+
|
24
|
+
DEPENDENCIES
|
25
|
+
attribool!
|
26
|
+
rake (~> 13.0, >= 13.0.1)
|
27
|
+
simplecov
|
28
|
+
test-unit (~> 3.3, >= 3.3.5)
|
29
|
+
|
30
|
+
BUNDLED WITH
|
31
|
+
2.1.4
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2021 Evan Gray
|
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/README.md
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
# Attribool
|
2
|
+
Ruby macros for creating boolean methods for attributes that may or may not be
|
3
|
+
booleans themselves. This is done via coercion based on truthiness. For example,
|
4
|
+
if you have an attribute of `@name`, and you want to know if `@name` is not
|
5
|
+
`nil`, you can declare `bool_reader :name`, which will define the method
|
6
|
+
`name?`. This method will return true if `@name` is truthy.
|
7
|
+
|
8
|
+
The `bool_reader` also comes with some handy options. For example, you can
|
9
|
+
[define a method name](#a-bool_reader-with-method-name-or-prefix) that makes
|
10
|
+
more sense. Using the same example, as above, if your attribute is `@name`, but
|
11
|
+
you'd like for your boolean method to be called `named?`, you can use
|
12
|
+
`bool_reader :name, method: :named?`.
|
13
|
+
[Conditionals](#a-bool_reader-with-method-name-and-conditional) can also be set
|
14
|
+
with lambdas via the `condition:` keyword argument.
|
15
|
+
|
16
|
+
Macros also exist for `bool_writer` and `bool_accessor`. When a writer
|
17
|
+
method is defined, the value will always be coerced into a boolean before
|
18
|
+
setting the attribute.
|
19
|
+
|
20
|
+
## Installation
|
21
|
+
#### Via Gemfile
|
22
|
+
```ruby
|
23
|
+
gem 'attribool'
|
24
|
+
```
|
25
|
+
|
26
|
+
#### Via rubygems
|
27
|
+
```sh
|
28
|
+
gem install attribool
|
29
|
+
```
|
30
|
+
|
31
|
+
#### From source
|
32
|
+
```sh
|
33
|
+
git clone https://github.com/evanthegrayt/attribool.git
|
34
|
+
cd attribool
|
35
|
+
bundle exec rake install
|
36
|
+
```
|
37
|
+
|
38
|
+
## Examples
|
39
|
+
#### Standard bool_reader
|
40
|
+
```ruby
|
41
|
+
require 'attribool'
|
42
|
+
|
43
|
+
class Person
|
44
|
+
extend Attribool
|
45
|
+
|
46
|
+
attr_accessor :name
|
47
|
+
bool_reader :name
|
48
|
+
end
|
49
|
+
|
50
|
+
person = Person.new
|
51
|
+
person.name?
|
52
|
+
# false, because @name is nil.
|
53
|
+
|
54
|
+
person.name = 'John Smith'
|
55
|
+
person.name
|
56
|
+
# "John Smith"
|
57
|
+
person.name?
|
58
|
+
# true, because @name is truthy.
|
59
|
+
```
|
60
|
+
|
61
|
+
#### A bool_reader with method name or prefix
|
62
|
+
```ruby
|
63
|
+
require 'attribool'
|
64
|
+
|
65
|
+
class Person
|
66
|
+
extend Attribool
|
67
|
+
|
68
|
+
attr_accessor :name
|
69
|
+
bool_reader :name, method: :named?
|
70
|
+
bool_reader :name, prefix: :has?
|
71
|
+
end
|
72
|
+
|
73
|
+
person = Person.new
|
74
|
+
person.named?
|
75
|
+
person.has_name?
|
76
|
+
# false, because @name is nil.
|
77
|
+
|
78
|
+
person.name = 'John Smith'
|
79
|
+
person.named?
|
80
|
+
person.has_name?
|
81
|
+
# true, because @name is truthy.
|
82
|
+
```
|
83
|
+
|
84
|
+
#### A bool_reader with method name and conditional
|
85
|
+
```ruby
|
86
|
+
require 'attribool'
|
87
|
+
|
88
|
+
class Person
|
89
|
+
extend Attribool
|
90
|
+
|
91
|
+
attr_accessor :age
|
92
|
+
# In the condition lambdas, the argument refers to the attribute's value.
|
93
|
+
bool_reader :age, method: :adult?, condition: ->(a) { a.to_i >= 18 }
|
94
|
+
end
|
95
|
+
|
96
|
+
person = Person.new
|
97
|
+
person.adult?
|
98
|
+
# false, because @age is nil, which the to_i converts to 0.
|
99
|
+
|
100
|
+
person.age = 10
|
101
|
+
person.adult?
|
102
|
+
# false, because @age is less than 18.
|
103
|
+
|
104
|
+
person.age = 20
|
105
|
+
person.adult?
|
106
|
+
# true, because @age is greater than 18.
|
107
|
+
```
|
108
|
+
|
109
|
+
#### Standard bool_accessor
|
110
|
+
```ruby
|
111
|
+
require 'attribool'
|
112
|
+
|
113
|
+
class Person
|
114
|
+
extend Attribool
|
115
|
+
|
116
|
+
bool_accessor :living
|
117
|
+
end
|
118
|
+
|
119
|
+
person = Person.new
|
120
|
+
person.living?
|
121
|
+
# false, because @living is nil.
|
122
|
+
|
123
|
+
person.living = true
|
124
|
+
person.living?
|
125
|
+
# true, because @living is true.
|
126
|
+
# Be aware -- if you pass a string, it will be coerced to true!
|
127
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/attribool'
|
4
|
+
require 'bundler/gem_tasks'
|
5
|
+
require 'rdoc/task'
|
6
|
+
require 'rake/testtask'
|
7
|
+
|
8
|
+
Rake::TestTask.new do |t|
|
9
|
+
t.libs = ['lib']
|
10
|
+
t.warning = true
|
11
|
+
t.verbose = true
|
12
|
+
t.test_files = FileList['test/**/*_test.rb']
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new do |rdoc|
|
16
|
+
rdoc.main = 'README.md'
|
17
|
+
rdoc.rdoc_dir = 'doc'
|
18
|
+
rdoc.rdoc_files.include('README.md', 'lib/**/*.rb')
|
19
|
+
end
|
20
|
+
|
21
|
+
task default: :test
|
data/attribool.gemspec
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require_relative 'lib/attribool/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = 'attribool'
|
5
|
+
spec.version = Attribool::Version.to_s
|
6
|
+
spec.authors = ['Evan Gray']
|
7
|
+
spec.email = 'evanthegrayt@vivaldi.net'
|
8
|
+
spec.license = 'MIT'
|
9
|
+
spec.date = Time.now.strftime('%Y-%m-%d')
|
10
|
+
|
11
|
+
spec.summary = %(Macros for creating boolean attribute methods)
|
12
|
+
spec.description = %(Macros for creating boolean attribute methods)
|
13
|
+
spec.homepage = 'https://github.com/evanthegrayt/attribool'
|
14
|
+
|
15
|
+
unless spec.respond_to?(:metadata)
|
16
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
17
|
+
'public gem pushes.'
|
18
|
+
end
|
19
|
+
|
20
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
21
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
22
|
+
spec.metadata['source_code_uri'] =
|
23
|
+
'https://github.com/evanthegrayt/attribool'
|
24
|
+
spec.metadata['documentation_uri'] =
|
25
|
+
'https://evanthegrayt.github.io/attribool/doc/index.html'
|
26
|
+
|
27
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
28
|
+
f.match(%r{^(test|spec|features)/})
|
29
|
+
end
|
30
|
+
spec.bindir = 'bin'
|
31
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
32
|
+
spec.require_paths = ['lib']
|
33
|
+
spec.add_development_dependency 'rake', '~> 13.0', '>= 13.0.1'
|
34
|
+
spec.add_development_dependency 'simplecov'
|
35
|
+
spec.add_development_dependency 'test-unit', '~> 3.3', '>= 3.3.5'
|
36
|
+
end
|
data/lib/attribool.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
##
|
4
|
+
# Adds macros and methods for dealing with boolean attributes.
|
5
|
+
module Attribool
|
6
|
+
def self.included(klass)
|
7
|
+
klass.extend(ClassMethods)
|
8
|
+
end
|
9
|
+
|
10
|
+
##
|
11
|
+
# Class methods to be added to the class when included.
|
12
|
+
module ClassMethods
|
13
|
+
##
|
14
|
+
# Creates a method that returns a boolean for attributes that may or may
|
15
|
+
# not be booleans themselves.
|
16
|
+
#
|
17
|
+
# @param [Symbol, String] attribute
|
18
|
+
#
|
19
|
+
# @kwarg [Boolean] allow_nil:
|
20
|
+
#
|
21
|
+
# @kwarg [Lambda] condition:
|
22
|
+
#
|
23
|
+
# @kwarg [Symbol, String] method:
|
24
|
+
#
|
25
|
+
# @kwarg [Symbol, String] prefix:
|
26
|
+
def bool_reader(
|
27
|
+
attribute,
|
28
|
+
allow_nil: true,
|
29
|
+
condition: nil,
|
30
|
+
prefix: nil,
|
31
|
+
method: nil
|
32
|
+
)
|
33
|
+
method ||= "#{attribute}?"
|
34
|
+
method = "#{prefix}_#{method}" if prefix
|
35
|
+
define_method(method) do
|
36
|
+
ivar = instance_variable_get("@#{attribute}")
|
37
|
+
raise TypeError, "#{attribute} is nil" if ivar.nil? && !allow_nil
|
38
|
+
|
39
|
+
condition ? condition.call(ivar) : to_boolean(ivar)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
##
|
44
|
+
# Creates a writer for boolean attributes. Always coerces to boolean based
|
45
|
+
# on truthiness.
|
46
|
+
#
|
47
|
+
# @param [Symbol, String] attribute
|
48
|
+
#
|
49
|
+
# @kwarg [Boolean] strict:
|
50
|
+
def bool_writer(attribute, strict: false)
|
51
|
+
define_method("#{attribute}=") do |v|
|
52
|
+
validate_boolean(v) if strict
|
53
|
+
|
54
|
+
instance_variable_set("@#{attribute}", to_boolean(v))
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
##
|
59
|
+
# Creates a simple reader and writer for booleans. This should only be used
|
60
|
+
# when the attribute should only ever be +true+ or +false+.
|
61
|
+
#
|
62
|
+
# @param [Symbol, String] attribute
|
63
|
+
def bool_accessor(attribute)
|
64
|
+
bool_reader(attribute)
|
65
|
+
bool_writer(attribute)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
##
|
70
|
+
# Is the argument a boolean?
|
71
|
+
#
|
72
|
+
# @param [Object] attribute
|
73
|
+
#
|
74
|
+
# @return [Boolean]
|
75
|
+
def boolean?(attribute)
|
76
|
+
[TrueClass, FalseClass].include?(attribute.class)
|
77
|
+
end
|
78
|
+
|
79
|
+
##
|
80
|
+
# Converts an Object to a boolean.
|
81
|
+
#
|
82
|
+
# @param [Object] attribute
|
83
|
+
#
|
84
|
+
# @return [Boolean]
|
85
|
+
def to_boolean(attribute)
|
86
|
+
!!attribute
|
87
|
+
end
|
88
|
+
|
89
|
+
private
|
90
|
+
|
91
|
+
##
|
92
|
+
# Raises if argument is not a boolean.
|
93
|
+
def validate_boolean(attribute) # :nodoc:
|
94
|
+
raise ArgumentError, 'Argument must be a boolean' unless boolean?(attribute)
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Attribool
|
4
|
+
##
|
5
|
+
# Module that contains all gem version information. Follows semantic
|
6
|
+
# versioning. Read: https://semver.org/
|
7
|
+
module Version
|
8
|
+
##
|
9
|
+
# Major version.
|
10
|
+
#
|
11
|
+
# @return [Integer]
|
12
|
+
MAJOR = 0
|
13
|
+
|
14
|
+
##
|
15
|
+
# Minor version.
|
16
|
+
#
|
17
|
+
# @return [Integer]
|
18
|
+
MINOR = 0
|
19
|
+
|
20
|
+
##
|
21
|
+
# Patch version.
|
22
|
+
#
|
23
|
+
# @return [Integer]
|
24
|
+
PATCH = 1
|
25
|
+
|
26
|
+
##
|
27
|
+
# Version as +MAJOR.MINOR.PATCH+
|
28
|
+
#
|
29
|
+
# @return [String]
|
30
|
+
def self.to_s
|
31
|
+
"#{MAJOR}.#{MINOR}.#{PATCH}"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: attribool
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Evan Gray
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-04-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '13.0'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 13.0.1
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '13.0'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 13.0.1
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: simplecov
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: test-unit
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '3.3'
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 3.3.5
|
57
|
+
type: :development
|
58
|
+
prerelease: false
|
59
|
+
version_requirements: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - "~>"
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '3.3'
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: 3.3.5
|
67
|
+
description: Macros for creating boolean attribute methods
|
68
|
+
email: evanthegrayt@vivaldi.net
|
69
|
+
executables: []
|
70
|
+
extensions: []
|
71
|
+
extra_rdoc_files: []
|
72
|
+
files:
|
73
|
+
- ".gitignore"
|
74
|
+
- Gemfile
|
75
|
+
- Gemfile.lock
|
76
|
+
- LICENSE
|
77
|
+
- README.md
|
78
|
+
- Rakefile
|
79
|
+
- attribool.gemspec
|
80
|
+
- lib/attribool.rb
|
81
|
+
- lib/attribool/version.rb
|
82
|
+
homepage: https://github.com/evanthegrayt/attribool
|
83
|
+
licenses:
|
84
|
+
- MIT
|
85
|
+
metadata:
|
86
|
+
allowed_push_host: https://rubygems.org
|
87
|
+
homepage_uri: https://github.com/evanthegrayt/attribool
|
88
|
+
source_code_uri: https://github.com/evanthegrayt/attribool
|
89
|
+
documentation_uri: https://evanthegrayt.github.io/attribool/doc/index.html
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubygems_version: 3.2.3
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: Macros for creating boolean attribute methods
|
109
|
+
test_files: []
|