attribool 0.0.3 → 0.1.0
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 +4 -4
- data/.github/workflows/ruby.yml +19 -8
- data/Gemfile.lock +1 -1
- data/README.md +40 -12
- data/attribool.gemspec +1 -1
- data/lib/attribool/version.rb +21 -3
- data/lib/attribool.rb +54 -56
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f04da5ae0d24a9e980b51a244c7d83ab90b6f4b3d288a49ab069397be78fe3d4
|
4
|
+
data.tar.gz: 5145d0dea86d0341d5bdbb64ae240647caca668eca6f72122bb1cf831a273ef3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c048e38a1a380785c5fdb7198913cb2aa686ea47f955ab649e004cf77e8e3fea08be1857a3d89e3da3647d788b0944cffceabdbbf0a5b85db560949426e60525
|
7
|
+
data.tar.gz: 3f522acba694671f5e8d7bf4a12221d9ac1edef9e538bf943683119c9346a68340550bee719db1167a9104ced50d9b2869e89245ec820ee5c4bae6542a6f00a2
|
data/.github/workflows/ruby.yml
CHANGED
@@ -1,3 +1,10 @@
|
|
1
|
+
# This workflow uses actions that are not certified by GitHub.
|
2
|
+
# They are provided by a third-party and are governed by
|
3
|
+
# separate terms of service, privacy policy, and support
|
4
|
+
# documentation.
|
5
|
+
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
|
6
|
+
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
|
7
|
+
|
1
8
|
name: Ruby
|
2
9
|
|
3
10
|
on:
|
@@ -10,15 +17,19 @@ jobs:
|
|
10
17
|
test:
|
11
18
|
|
12
19
|
runs-on: ubuntu-latest
|
20
|
+
strategy:
|
21
|
+
matrix:
|
22
|
+
ruby-version: ['2.6', '2.7', '3.0']
|
13
23
|
|
14
24
|
steps:
|
15
25
|
- uses: actions/checkout@v2
|
16
|
-
- name: Set up Ruby
|
17
|
-
|
26
|
+
- name: Set up Ruby
|
27
|
+
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
|
28
|
+
# change this to (see https://github.com/ruby/setup-ruby#versioning):
|
29
|
+
# uses: ruby/setup-ruby@v1
|
30
|
+
uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
|
18
31
|
with:
|
19
|
-
ruby-version:
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
bundle install
|
24
|
-
bundle exec rake test
|
32
|
+
ruby-version: ${{ matrix.ruby-version }}
|
33
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
34
|
+
- name: Run tests
|
35
|
+
run: bundle exec rake
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -3,22 +3,25 @@
|
|
3
3
|
[](https://badge.fury.io/rb/attribool)
|
4
4
|
[](https://opensource.org/licenses/MIT)
|
5
5
|
|
6
|
-
> Pre-release version. Everything works, but not everything is documented.
|
7
|
-
|
8
6
|
Ruby macros for creating boolean methods for attributes that may or may not be
|
9
|
-
booleans themselves. This is done via coercion based on truthiness.
|
10
|
-
|
11
|
-
|
12
|
-
`
|
7
|
+
booleans themselves. This is done via coercion based on truthiness.
|
8
|
+
|
9
|
+
For example, if you have an attribute of `@name`, and you want to know if
|
10
|
+
`@name` is not `nil`, you can declare `bool_reader :name`, which will define the
|
11
|
+
method `name?`. This method will return true if `@name` is truthy.
|
13
12
|
|
14
13
|
The `bool_reader` also comes with some handy options. For example, you can
|
15
14
|
[define a method name](#a-bool_reader-with-method-name-or-prefix) that makes
|
16
|
-
more sense.
|
15
|
+
more sense. Using the same example as above, if your attribute is `@name`, but
|
17
16
|
you'd like for your boolean method to be called `named?`, you can use
|
18
17
|
`bool_reader :name, method: :named?`.
|
19
18
|
[Conditionals](#a-bool_reader-with-method-name-and-conditional) can also be set
|
20
19
|
with lambdas via the `condition:` keyword argument.
|
21
20
|
|
21
|
+
The first argument is always the instance variable to check for truthiness.
|
22
|
+
Because of this, it is also valid syntax to use `bool_reader :@name`, if it
|
23
|
+
makes more sense to you.
|
24
|
+
|
22
25
|
Macros also exist for `bool_writer` and `bool_accessor`. When a writer
|
23
26
|
method is defined, the value will always be coerced into a boolean before
|
24
27
|
setting the attribute.
|
@@ -49,10 +52,12 @@ bundle exec rake install
|
|
49
52
|
require 'attribool'
|
50
53
|
|
51
54
|
class Person
|
52
|
-
|
55
|
+
extend Attribool
|
53
56
|
|
54
57
|
attr_accessor :name
|
55
58
|
bool_reader :name
|
59
|
+
# OR
|
60
|
+
bool_reader :@name
|
56
61
|
end
|
57
62
|
|
58
63
|
person = Person.new
|
@@ -71,7 +76,7 @@ person.name?
|
|
71
76
|
require 'attribool'
|
72
77
|
|
73
78
|
class Person
|
74
|
-
|
79
|
+
extend Attribool
|
75
80
|
|
76
81
|
attr_accessor :name
|
77
82
|
bool_reader :name, method: :named?
|
@@ -94,7 +99,7 @@ person.has_name?
|
|
94
99
|
require 'attribool'
|
95
100
|
|
96
101
|
class Person
|
97
|
-
|
102
|
+
extend Attribool
|
98
103
|
|
99
104
|
attr_accessor :age
|
100
105
|
# In the condition lambdas, the argument refers to the attribute's value.
|
@@ -119,7 +124,7 @@ person.adult?
|
|
119
124
|
require 'attribool'
|
120
125
|
|
121
126
|
class Person
|
122
|
-
|
127
|
+
extend Attribool
|
123
128
|
|
124
129
|
bool_accessor :living
|
125
130
|
end
|
@@ -134,6 +139,29 @@ person.living?
|
|
134
139
|
# Be aware -- if you pass anything truthy, it will be coerced to true!
|
135
140
|
```
|
136
141
|
|
142
|
+
#### Standard bool_writer
|
143
|
+
In most cases where you'd use a `bool_writer`, you'd probably want to just use
|
144
|
+
`bool_accessor`. This example is to show that, even when using `bool_accessor`,
|
145
|
+
the value is coerced to a boolean when the value is set by `bool_writer`.
|
146
|
+
```ruby
|
147
|
+
require 'attribool'
|
148
|
+
|
149
|
+
class Person
|
150
|
+
extend Attribool
|
151
|
+
|
152
|
+
attr_reader :living
|
153
|
+
bool_writer :living
|
154
|
+
end
|
155
|
+
|
156
|
+
person = Person.new
|
157
|
+
person.living
|
158
|
+
# nil
|
159
|
+
|
160
|
+
person.living = 'blah'
|
161
|
+
person.living
|
162
|
+
# true, because 'blah' was coerced into a boolean because strings are truthy.
|
163
|
+
```
|
164
|
+
|
137
165
|
## Reporting Bugs and Requesting Features
|
138
166
|
If you have an idea or find a bug, please [create an
|
139
167
|
issue](https://github.com/evanthegrayt/attribool/issues/new). Just make sure
|
@@ -144,4 +172,4 @@ Request.
|
|
144
172
|
I do these projects for fun, and I enjoy knowing that they're helpful to people.
|
145
173
|
Consider starring [the repository](https://github.com/evanthegrayt/attribool)
|
146
174
|
if you like it! If you love it, follow me [on
|
147
|
-
|
175
|
+
GitHub](https://github.com/evanthegrayt)!
|
data/attribool.gemspec
CHANGED
@@ -2,7 +2,7 @@ require_relative 'lib/attribool/version'
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |spec|
|
4
4
|
spec.name = 'attribool'
|
5
|
-
spec.version = Attribool::
|
5
|
+
spec.version = Attribool::VERSION
|
6
6
|
spec.authors = ['Evan Gray']
|
7
7
|
spec.email = 'evanthegrayt@vivaldi.net'
|
8
8
|
spec.license = 'MIT'
|
data/lib/attribool/version.rb
CHANGED
@@ -15,20 +15,38 @@ module Attribool
|
|
15
15
|
# Minor version.
|
16
16
|
#
|
17
17
|
# @return [Integer]
|
18
|
-
MINOR =
|
18
|
+
MINOR = 1
|
19
19
|
|
20
20
|
##
|
21
21
|
# Patch version.
|
22
22
|
#
|
23
23
|
# @return [Integer]
|
24
|
-
PATCH =
|
24
|
+
PATCH = 0
|
25
|
+
|
26
|
+
##
|
27
|
+
# Version as +[MAJOR, MINOR, PATCH]+
|
28
|
+
#
|
29
|
+
# @return [Array]
|
30
|
+
def self.to_a
|
31
|
+
[MAJOR, MINOR, PATCH]
|
32
|
+
end
|
25
33
|
|
26
34
|
##
|
27
35
|
# Version as +MAJOR.MINOR.PATCH+
|
28
36
|
#
|
29
37
|
# @return [String]
|
30
38
|
def self.to_s
|
31
|
-
|
39
|
+
to_a.join('.')
|
40
|
+
end
|
41
|
+
|
42
|
+
##
|
43
|
+
# Version as +{major: MAJOR, minor: MINOR, patch: PATCH}+
|
44
|
+
#
|
45
|
+
# @return [Hash]
|
46
|
+
def self.to_h
|
47
|
+
%i[major minor patch].zip(to_a).to_h
|
32
48
|
end
|
33
49
|
end
|
50
|
+
|
51
|
+
VERSION = Version.to_s.freeze
|
34
52
|
end
|
data/lib/attribool.rb
CHANGED
@@ -1,70 +1,68 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require_relative 'attribool/version'
|
4
|
+
|
3
5
|
##
|
4
6
|
# Adds macros and methods for dealing with boolean attributes.
|
5
7
|
module Attribool
|
6
|
-
def self.included(klass) # :nodoc:
|
7
|
-
klass.extend(ClassMethods)
|
8
|
-
end
|
9
|
-
|
10
8
|
##
|
11
|
-
#
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
)
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
raise TypeError, "#{attribute} is nil" if ivar.nil? && !allow_nil
|
9
|
+
# Creates a method that returns a boolean for attributes that may or may
|
10
|
+
# not be booleans themselves.
|
11
|
+
#
|
12
|
+
# @param [Symbol, String] attribute
|
13
|
+
#
|
14
|
+
# @kwarg [Boolean] allow_nil:
|
15
|
+
#
|
16
|
+
# @kwarg [Lambda] condition:
|
17
|
+
#
|
18
|
+
# @kwarg [Symbol, String] method:
|
19
|
+
#
|
20
|
+
# @kwarg [Symbol, String] prefix:
|
21
|
+
def bool_reader(
|
22
|
+
attribute,
|
23
|
+
allow_nil: true,
|
24
|
+
condition: nil,
|
25
|
+
prefix: nil,
|
26
|
+
method: nil
|
27
|
+
)
|
28
|
+
attribute = attribute.to_s
|
29
|
+
attribute = attribute.start_with?('@') ? attribute : "@#{attribute}"
|
30
|
+
method ||= "#{attribute.delete_prefix('@')}?"
|
31
|
+
method = "#{prefix}_#{method}" if prefix
|
32
|
+
define_method(method) do
|
33
|
+
ivar = instance_variable_get(attribute)
|
34
|
+
raise TypeError, "#{attribute} is nil" if ivar.nil? && !allow_nil
|
38
35
|
|
39
|
-
|
40
|
-
end
|
36
|
+
condition ? condition.call(ivar) : !!(ivar)
|
41
37
|
end
|
38
|
+
end
|
42
39
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
instance_variable_set("@#{attribute}", !!v)
|
40
|
+
##
|
41
|
+
# Creates a writer for boolean attributes. Always coerces to boolean based
|
42
|
+
# on truthiness.
|
43
|
+
#
|
44
|
+
# @param [Symbol, String] attribute
|
45
|
+
#
|
46
|
+
# @kwarg [Boolean] strict:
|
47
|
+
def bool_writer(attribute, strict: false)
|
48
|
+
attribute = attribute.to_s
|
49
|
+
attribute = attribute.start_with?('@') ? attribute : "@#{attribute}"
|
50
|
+
define_method("#{attribute.delete_prefix('@')}=") do |v|
|
51
|
+
if strict && ![TrueClass, FalseClass].include?(v.class)
|
52
|
+
raise ArgumentError, 'Argument must be a boolean'
|
57
53
|
end
|
58
|
-
end
|
59
54
|
|
60
|
-
|
61
|
-
# Creates a simple reader and writer for booleans. This should only be used
|
62
|
-
# when the attribute should only ever be +true+ or +false+.
|
63
|
-
#
|
64
|
-
# @param [Symbol, String] attribute
|
65
|
-
def bool_accessor(attribute)
|
66
|
-
bool_reader(attribute)
|
67
|
-
bool_writer(attribute)
|
55
|
+
instance_variable_set(attribute, !!v)
|
68
56
|
end
|
69
57
|
end
|
58
|
+
|
59
|
+
##
|
60
|
+
# Creates a simple reader and writer for booleans. This should only be used
|
61
|
+
# when the attribute should only ever be +true+ or +false+.
|
62
|
+
#
|
63
|
+
# @param [Symbol, String] attribute
|
64
|
+
def bool_accessor(attribute)
|
65
|
+
bool_reader(attribute)
|
66
|
+
bool_writer(attribute)
|
67
|
+
end
|
70
68
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attribool
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Evan Gray
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-12-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|