attribool 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +24 -0
- data/.gitignore +1 -0
- data/README.md +26 -6
- data/Rakefile +1 -1
- data/attribool.gemspec +2 -4
- data/lib/attribool.rb +6 -32
- data/lib/attribool/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 04a8612ea59018c8c1ace211692e9100676136ac77613627b8e8b0b295cf3bae
|
4
|
+
data.tar.gz: fe39cccfee3277bd92028940a54313e10861445a1b486d4791942875a3cb918a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 762bd93ec17802176022253614871443f0e388892558d455b697b90e69d26be00917f6cd5b803f105e6ace168985e85a4c14305059e92886500bbb3f035efcff
|
7
|
+
data.tar.gz: 1b125fffc61d42dc9a8b63b77b625f57786ca03c44a2045a7bb1685bb5ea1d6c8e36ffa5fa6104b14fde943e79bedcfb8d5b92da88a59c1b0dd1f692b67098d2
|
@@ -0,0 +1,24 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [ master ]
|
6
|
+
pull_request:
|
7
|
+
branches: [ master ]
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
test:
|
11
|
+
|
12
|
+
runs-on: ubuntu-latest
|
13
|
+
|
14
|
+
steps:
|
15
|
+
- uses: actions/checkout@v2
|
16
|
+
- name: Set up Ruby 2.6
|
17
|
+
uses: actions/setup-ruby@v1
|
18
|
+
with:
|
19
|
+
ruby-version: 2.6.x
|
20
|
+
- name: Build and test with Rake
|
21
|
+
run: |
|
22
|
+
gem install bundler
|
23
|
+
bundle install
|
24
|
+
bundle exec rake test
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,10 @@
|
|
1
1
|
# Attribool
|
2
|
+
[![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fevanthegrayt%2Fattribool%2Fbadge%3Fref%3Dmaster&style=flat)](https://actions-badge.atrox.dev/evanthegrayt/attribool/goto?ref=master)
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/attribool.svg)](https://badge.fury.io/rb/attribool)
|
4
|
+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
|
5
|
+
|
6
|
+
> Pre-release version. Everything works, but not everything is documented.
|
7
|
+
|
2
8
|
Ruby macros for creating boolean methods for attributes that may or may not be
|
3
9
|
booleans themselves. This is done via coercion based on truthiness. For example,
|
4
10
|
if you have an attribute of `@name`, and you want to know if `@name` is not
|
@@ -17,6 +23,8 @@ Macros also exist for `bool_writer` and `bool_accessor`. When a writer
|
|
17
23
|
method is defined, the value will always be coerced into a boolean before
|
18
24
|
setting the attribute.
|
19
25
|
|
26
|
+
You can read the documentation [here](https://evanthegrayt.github.io/attribool/).
|
27
|
+
|
20
28
|
## Installation
|
21
29
|
#### Via Gemfile
|
22
30
|
```ruby
|
@@ -41,7 +49,7 @@ bundle exec rake install
|
|
41
49
|
require 'attribool'
|
42
50
|
|
43
51
|
class Person
|
44
|
-
|
52
|
+
include Attribool
|
45
53
|
|
46
54
|
attr_accessor :name
|
47
55
|
bool_reader :name
|
@@ -63,11 +71,11 @@ person.name?
|
|
63
71
|
require 'attribool'
|
64
72
|
|
65
73
|
class Person
|
66
|
-
|
74
|
+
include Attribool
|
67
75
|
|
68
76
|
attr_accessor :name
|
69
77
|
bool_reader :name, method: :named?
|
70
|
-
bool_reader :name, prefix: :has
|
78
|
+
bool_reader :name, prefix: :has
|
71
79
|
end
|
72
80
|
|
73
81
|
person = Person.new
|
@@ -86,7 +94,7 @@ person.has_name?
|
|
86
94
|
require 'attribool'
|
87
95
|
|
88
96
|
class Person
|
89
|
-
|
97
|
+
include Attribool
|
90
98
|
|
91
99
|
attr_accessor :age
|
92
100
|
# In the condition lambdas, the argument refers to the attribute's value.
|
@@ -111,7 +119,7 @@ person.adult?
|
|
111
119
|
require 'attribool'
|
112
120
|
|
113
121
|
class Person
|
114
|
-
|
122
|
+
include Attribool
|
115
123
|
|
116
124
|
bool_accessor :living
|
117
125
|
end
|
@@ -123,5 +131,17 @@ person.living?
|
|
123
131
|
person.living = true
|
124
132
|
person.living?
|
125
133
|
# true, because @living is true.
|
126
|
-
# Be aware -- if you pass
|
134
|
+
# Be aware -- if you pass anything truthy, it will be coerced to true!
|
127
135
|
```
|
136
|
+
|
137
|
+
## Reporting Bugs and Requesting Features
|
138
|
+
If you have an idea or find a bug, please [create an
|
139
|
+
issue](https://github.com/evanthegrayt/attribool/issues/new). Just make sure
|
140
|
+
the topic doesn't already exist. Better yet, you can always submit a Pull
|
141
|
+
Request.
|
142
|
+
|
143
|
+
## Self-Promotion
|
144
|
+
I do these projects for fun, and I enjoy knowing that they're helpful to people.
|
145
|
+
Consider starring [the repository](https://github.com/evanthegrayt/attribool)
|
146
|
+
if you like it! If you love it, follow me [on
|
147
|
+
Github](https://github.com/evanthegrayt)!
|
data/Rakefile
CHANGED
data/attribool.gemspec
CHANGED
@@ -19,10 +19,8 @@ Gem::Specification.new do |spec|
|
|
19
19
|
|
20
20
|
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
21
21
|
spec.metadata['homepage_uri'] = spec.homepage
|
22
|
-
spec.metadata['source_code_uri'] =
|
23
|
-
|
24
|
-
spec.metadata['documentation_uri'] =
|
25
|
-
'https://evanthegrayt.github.io/attribool/doc/index.html'
|
22
|
+
spec.metadata['source_code_uri'] = 'https://github.com/evanthegrayt/attribool'
|
23
|
+
spec.metadata['documentation_uri'] = 'https://evanthegrayt.github.io/attribool/'
|
26
24
|
|
27
25
|
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
28
26
|
f.match(%r{^(test|spec|features)/})
|
data/lib/attribool.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
##
|
4
4
|
# Adds macros and methods for dealing with boolean attributes.
|
5
5
|
module Attribool
|
6
|
-
def self.included(klass)
|
6
|
+
def self.included(klass) # :nodoc:
|
7
7
|
klass.extend(ClassMethods)
|
8
8
|
end
|
9
9
|
|
@@ -36,7 +36,7 @@ module Attribool
|
|
36
36
|
ivar = instance_variable_get("@#{attribute}")
|
37
37
|
raise TypeError, "#{attribute} is nil" if ivar.nil? && !allow_nil
|
38
38
|
|
39
|
-
condition ? condition.call(ivar) :
|
39
|
+
condition ? condition.call(ivar) : !!(ivar)
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
@@ -49,9 +49,11 @@ module Attribool
|
|
49
49
|
# @kwarg [Boolean] strict:
|
50
50
|
def bool_writer(attribute, strict: false)
|
51
51
|
define_method("#{attribute}=") do |v|
|
52
|
-
|
52
|
+
if strict && ![TrueClass, FalseClass].include?(v.class)
|
53
|
+
raise ArgumentError, 'Argument must be a boolean'
|
54
|
+
end
|
53
55
|
|
54
|
-
instance_variable_set("@#{attribute}",
|
56
|
+
instance_variable_set("@#{attribute}", !!v)
|
55
57
|
end
|
56
58
|
end
|
57
59
|
|
@@ -65,32 +67,4 @@ module Attribool
|
|
65
67
|
bool_writer(attribute)
|
66
68
|
end
|
67
69
|
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
70
|
end
|
data/lib/attribool/version.rb
CHANGED
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.0.2
|
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-05-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -70,6 +70,7 @@ executables: []
|
|
70
70
|
extensions: []
|
71
71
|
extra_rdoc_files: []
|
72
72
|
files:
|
73
|
+
- ".github/workflows/ruby.yml"
|
73
74
|
- ".gitignore"
|
74
75
|
- Gemfile
|
75
76
|
- Gemfile.lock
|
@@ -86,7 +87,7 @@ metadata:
|
|
86
87
|
allowed_push_host: https://rubygems.org
|
87
88
|
homepage_uri: https://github.com/evanthegrayt/attribool
|
88
89
|
source_code_uri: https://github.com/evanthegrayt/attribool
|
89
|
-
documentation_uri: https://evanthegrayt.github.io/attribool/
|
90
|
+
documentation_uri: https://evanthegrayt.github.io/attribool/
|
90
91
|
post_install_message:
|
91
92
|
rdoc_options: []
|
92
93
|
require_paths:
|