attribool 1.0.0 → 1.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 +4 -4
- data/lib/attribool/attribute.rb +56 -4
- data/lib/attribool/version.rb +5 -1
- data/lib/attribool.rb +5 -5
- metadata +2 -3
- data/lib/attribool/method_name.rb +0 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e18815dc8107a42c84084e2c35614d8111f7ae827224b4b78cbb3e920ff9c970
|
4
|
+
data.tar.gz: 84642d3baca03e19bf84794ed88f2696d1f639801b79f55cd9fc2636309f8061
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3984a29d0285e92995b01d36ba9fbe0fe4919b9800116bff642ff5e7f0ee8e3fe1c4444a5bb1f3494f87e41228c31b4f0a7eec086ae3f3e5f07f255a5943ba41
|
7
|
+
data.tar.gz: 68246fd9004eb745078a8399ee4dc160ba2b06535a12fad81173958053c738c523fe5df152e266e2103e74f84715f9ca0bb309726eb30159e313f45efd9a8761
|
data/lib/attribool/attribute.rb
CHANGED
@@ -1,10 +1,62 @@
|
|
1
|
+
##
|
2
|
+
# Abstraction for an attribute to determine its name, reader, writer, and
|
3
|
+
# instance variable name.
|
1
4
|
class Attribool::Attribute
|
5
|
+
##
|
6
|
+
# The name of the attribute, without the leading "@".
|
7
|
+
#
|
8
|
+
# @return [String]
|
2
9
|
attr_reader :name
|
10
|
+
|
11
|
+
##
|
12
|
+
# The name of the instance variable for the attribute, with the leading "@".
|
13
|
+
#
|
14
|
+
# @return [String]
|
3
15
|
attr_reader :ivar
|
4
16
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
17
|
+
##
|
18
|
+
# The name of the reader for the attribute.
|
19
|
+
#
|
20
|
+
# @return [String]
|
21
|
+
attr_reader :reader
|
22
|
+
|
23
|
+
##
|
24
|
+
# The name of the writer for the attribute.
|
25
|
+
#
|
26
|
+
# @return [String]
|
27
|
+
attr_reader :writer
|
28
|
+
|
29
|
+
##
|
30
|
+
# Ensures that if multiple attributes are being defined, and +method_name+ is
|
31
|
+
# provided, that +method_name+ is a +Proc+.
|
32
|
+
#
|
33
|
+
# @param [Integer] number_of_attributes
|
34
|
+
#
|
35
|
+
# @param [String, Symbol, Proc] method_name
|
36
|
+
def self.validate_method_name(number_of_attributes, method_name)
|
37
|
+
if number_of_attributes > 1 && method_name && !method_name.is_a?(Proc)
|
38
|
+
raise ArgumentError, "Must use a Proc when creating multiple methods"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
##
|
43
|
+
# Create an Attribute. The attribute can either be a String or a Symbol, and
|
44
|
+
# can also start with an "@", or not.
|
45
|
+
#
|
46
|
+
# @param [String, Symbol] attribute
|
47
|
+
#
|
48
|
+
# @param [String, Symbol, Proc, nil] reader_name
|
49
|
+
def initialize(attribute, reader_name = nil)
|
50
|
+
attribute.to_s.tap do |a|
|
51
|
+
@ivar = a.start_with?('@') ? a : "@#{a}"
|
52
|
+
@name = @ivar.delete_prefix('@')
|
53
|
+
@reader =
|
54
|
+
case reader_name
|
55
|
+
when Proc then reader_name.call(name)
|
56
|
+
when nil then "#{name}?"
|
57
|
+
else reader_name.to_s
|
58
|
+
end
|
59
|
+
@writer = "#{name}="
|
60
|
+
end
|
9
61
|
end
|
10
62
|
end
|
data/lib/attribool/version.rb
CHANGED
@@ -21,7 +21,7 @@ module Attribool
|
|
21
21
|
# Patch version.
|
22
22
|
#
|
23
23
|
# @return [Integer]
|
24
|
-
PATCH =
|
24
|
+
PATCH = 1
|
25
25
|
|
26
26
|
##
|
27
27
|
# Version as +[MAJOR, MINOR, PATCH]+
|
@@ -48,5 +48,9 @@ module Attribool
|
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
|
+
##
|
52
|
+
# The version, as a string.
|
53
|
+
#
|
54
|
+
# @return [String]
|
51
55
|
VERSION = Version.to_s.freeze
|
52
56
|
end
|
data/lib/attribool.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative 'attribool/version'
|
4
|
-
require_relative 'attribool/method_name'
|
5
4
|
require_relative 'attribool/attribute'
|
6
5
|
|
7
6
|
##
|
@@ -24,9 +23,10 @@ module Attribool
|
|
24
23
|
condition: nil,
|
25
24
|
method_name: nil
|
26
25
|
)
|
27
|
-
|
28
|
-
|
29
|
-
|
26
|
+
Attribute.validate_method_name(attributes.size, method_name)
|
27
|
+
|
28
|
+
attributes.map { |a| Attribute.new(a, method_name) }.each do |attribute|
|
29
|
+
define_method(attribute.reader) do
|
30
30
|
value = instance_variable_get(attribute.ivar)
|
31
31
|
raise TypeError, "#{attribute.ivar} is nil" if value.nil? && !allow_nil
|
32
32
|
|
@@ -44,7 +44,7 @@ module Attribool
|
|
44
44
|
# @kwarg [Boolean] strict
|
45
45
|
def bool_writer(*attributes, strict: false)
|
46
46
|
attributes.map { |a| Attribute.new(a) }.each do |attribute|
|
47
|
-
define_method(
|
47
|
+
define_method(attribute.writer) do |v|
|
48
48
|
if strict && ![TrueClass, FalseClass].include?(v.class)
|
49
49
|
raise ArgumentError, 'Argument must be a boolean'
|
50
50
|
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: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Evan Gray
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-01-
|
11
|
+
date: 2022-01-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -80,7 +80,6 @@ files:
|
|
80
80
|
- attribool.gemspec
|
81
81
|
- lib/attribool.rb
|
82
82
|
- lib/attribool/attribute.rb
|
83
|
-
- lib/attribool/method_name.rb
|
84
83
|
- lib/attribool/version.rb
|
85
84
|
homepage: https://github.com/evanthegrayt/attribool
|
86
85
|
licenses:
|
@@ -1,18 +0,0 @@
|
|
1
|
-
class Attribool::MethodName
|
2
|
-
attr_reader :name
|
3
|
-
|
4
|
-
def self.validate(method_name, number_of_attributes)
|
5
|
-
if number_of_attributes > 1 && method_name && !method_name.is_a?(Proc)
|
6
|
-
raise ArgumentError, "Must use a Proc when creating multiple methods"
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
def initialize(attribute, method_name)
|
11
|
-
@name =
|
12
|
-
case method_name
|
13
|
-
when Proc then method_name.call(attribute)
|
14
|
-
when nil then "#{attribute}?"
|
15
|
-
else method_name
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|