attribool 0.1.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f04da5ae0d24a9e980b51a244c7d83ab90b6f4b3d288a49ab069397be78fe3d4
4
- data.tar.gz: 5145d0dea86d0341d5bdbb64ae240647caca668eca6f72122bb1cf831a273ef3
3
+ metadata.gz: dc735929535da353af1b584656aafb3ba6ba46b7996af32ae37dedc30bcfb0a2
4
+ data.tar.gz: 116a6a7fd8f9803773d10a6fff0b294f73bb9f5eb872db573f11273f8fc1ddcb
5
5
  SHA512:
6
- metadata.gz: c048e38a1a380785c5fdb7198913cb2aa686ea47f955ab649e004cf77e8e3fea08be1857a3d89e3da3647d788b0944cffceabdbbf0a5b85db560949426e60525
7
- data.tar.gz: 3f522acba694671f5e8d7bf4a12221d9ac1edef9e538bf943683119c9346a68340550bee719db1167a9104ced50d9b2869e89245ec820ee5c4bae6542a6f00a2
6
+ metadata.gz: 73fc2ab7f625fdfbb7577cda39c60eb86f6f53511fa685b2c0b86a79c90d22876123fcd34db74a76deb25eabc223cf891964fac36f962732af87a0c0c9aabcb0
7
+ data.tar.gz: ec6dc2496637c4c21c3c252a4c02fa1d574b4e4e033b74bd9019d5a8c286febba0990cd6d5659cc15be20b6a6c6dc3428f23a8bcb11ded8ed6a884403f78f682
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- attribool (0.1.0)
4
+ attribool (1.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -4,17 +4,18 @@
4
4
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
5
 
6
6
  Ruby macros for creating boolean methods for attributes that may or may not be
7
- booleans themselves. This is done via coercion based on truthiness.
7
+ booleans themselves. This is done via either coercion based on truthiness, or a
8
+ user-defined condition.
8
9
 
9
10
  For example, if you have an attribute of `@name`, and you want to know if
10
11
  `@name` is not `nil`, you can declare `bool_reader :name`, which will define the
11
12
  method `name?`. This method will return true if `@name` is truthy.
12
13
 
13
14
  The `bool_reader` also comes with some handy options. For example, you can
14
- [define a method name](#a-bool_reader-with-method-name-or-prefix) that makes
15
+ [define a method name](#a-bool_reader-with-method-name) that makes
15
16
  more sense. Using the same example as above, if your attribute is `@name`, but
16
17
  you'd like for your boolean method to be called `named?`, you can use
17
- `bool_reader :name, method: :named?`.
18
+ `bool_reader :name, method_name: :named?`.
18
19
  [Conditionals](#a-bool_reader-with-method-name-and-conditional) can also be set
19
20
  with lambdas via the `condition:` keyword argument.
20
21
 
@@ -57,7 +58,9 @@ class Person
57
58
  attr_accessor :name
58
59
  bool_reader :name
59
60
  # OR
60
- bool_reader :@name
61
+ # bool_reader :@name
62
+ # bool_reader 'name'
63
+ # bool_reader '@name'
61
64
  end
62
65
 
63
66
  person = Person.new
@@ -71,7 +74,7 @@ person.name?
71
74
  # true, because @name is truthy.
72
75
  ```
73
76
 
74
- #### A bool_reader with method name or prefix
77
+ #### A bool_reader with method name
75
78
  ```ruby
76
79
  require 'attribool'
77
80
 
@@ -79,18 +82,15 @@ class Person
79
82
  extend Attribool
80
83
 
81
84
  attr_accessor :name
82
- bool_reader :name, method: :named?
83
- bool_reader :name, prefix: :has
85
+ bool_reader :name, method_name: :named?
84
86
  end
85
87
 
86
88
  person = Person.new
87
89
  person.named?
88
- person.has_name?
89
90
  # false, because @name is nil.
90
91
 
91
92
  person.name = 'John Smith'
92
93
  person.named?
93
- person.has_name?
94
94
  # true, because @name is truthy.
95
95
  ```
96
96
 
@@ -103,7 +103,7 @@ class Person
103
103
 
104
104
  attr_accessor :age
105
105
  # In the condition lambdas, the argument refers to the attribute's value.
106
- bool_reader :age, method: :adult?, condition: ->(a) { a.to_i >= 18 }
106
+ bool_reader :age, method_name: :adult?, condition: ->(a) { a.to_i >= 18 }
107
107
  end
108
108
 
109
109
  person = Person.new
@@ -119,6 +119,34 @@ person.adult?
119
119
  # true, because @age is greater than 18.
120
120
  ```
121
121
 
122
+ #### Assigning more than one bool_reader with a method name at once
123
+ ```ruby
124
+ require 'attribool'
125
+
126
+ class Person
127
+ extend Attribool
128
+
129
+ attr_accessor :name, :age
130
+ # When creating multiple readers at once, if you want to specify a
131
+ # method_name, you must provide a Proc as the argument, where the attribute
132
+ # name is the argument.
133
+ bool_reader :age, :name, method_name: ->(m) { "has_#{m}?" }
134
+ end
135
+
136
+ person = Person.new
137
+ person.has_age?
138
+ person.has_name?
139
+ # Both false, because @age and @name are nil.
140
+
141
+ person.age = 10
142
+ person.has_age?
143
+ # true, because @age is not nil.
144
+
145
+ person.name = 'Bob'
146
+ person.has_name?
147
+ # true, because @name is not nil.
148
+ ```
149
+
122
150
  #### Standard bool_accessor
123
151
  ```ruby
124
152
  require 'attribool'
@@ -0,0 +1,10 @@
1
+ class Attribool::Attribute
2
+ attr_reader :name
3
+ attr_reader :ivar
4
+
5
+ def initialize(attribute)
6
+ attribute = attribute.to_s
7
+ @ivar = attribute.start_with?('@') ? attribute : "@#{attribute}"
8
+ @name = @ivar.delete_prefix('@')
9
+ end
10
+ end
@@ -0,0 +1,18 @@
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
@@ -9,13 +9,13 @@ module Attribool
9
9
  # Major version.
10
10
  #
11
11
  # @return [Integer]
12
- MAJOR = 0
12
+ MAJOR = 1
13
13
 
14
14
  ##
15
15
  # Minor version.
16
16
  #
17
17
  # @return [Integer]
18
- MINOR = 1
18
+ MINOR = 0
19
19
 
20
20
  ##
21
21
  # Patch version.
data/lib/attribool.rb CHANGED
@@ -1,39 +1,37 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'attribool/version'
4
+ require_relative 'attribool/method_name'
5
+ require_relative 'attribool/attribute'
4
6
 
5
7
  ##
6
- # Adds macros and methods for dealing with boolean attributes.
8
+ # Adds macros for dealing with boolean attributes.
7
9
  module Attribool
8
10
  ##
9
- # Creates a method that returns a boolean for attributes that may or may
10
- # not be booleans themselves.
11
+ # Creates methods that return a boolean for attributes that may or may not be
12
+ # booleans themselves. Multiple readers can be created at once
11
13
  #
12
- # @param [Symbol, String] attribute
14
+ # @param [Symbol, String] *attributes
13
15
  #
14
- # @kwarg [Boolean] allow_nil:
16
+ # @kwarg [Boolean] allow_nil
15
17
  #
16
- # @kwarg [Lambda] condition:
18
+ # @kwarg [Proc] condition
17
19
  #
18
- # @kwarg [Symbol, String] method:
19
- #
20
- # @kwarg [Symbol, String] prefix:
20
+ # @kwarg [Symbol, String, Proc] method_name
21
21
  def bool_reader(
22
- attribute,
22
+ *attributes,
23
23
  allow_nil: true,
24
24
  condition: nil,
25
- prefix: nil,
26
- method: nil
25
+ method_name: nil
27
26
  )
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
27
+ MethodName.validate(method_name, attributes.size)
28
+ attributes.map { |a| Attribute.new(a) }.each do |attribute|
29
+ define_method(MethodName.new(attribute.name, method_name).name) do
30
+ value = instance_variable_get(attribute.ivar)
31
+ raise TypeError, "#{attribute.ivar} is nil" if value.nil? && !allow_nil
35
32
 
36
- condition ? condition.call(ivar) : !!(ivar)
33
+ condition ? condition.call(value) : !!(value)
34
+ end
37
35
  end
38
36
  end
39
37
 
@@ -41,18 +39,18 @@ module Attribool
41
39
  # Creates a writer for boolean attributes. Always coerces to boolean based
42
40
  # on truthiness.
43
41
  #
44
- # @param [Symbol, String] attribute
42
+ # @param [Symbol, String] *attributes
45
43
  #
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'
53
- end
44
+ # @kwarg [Boolean] strict
45
+ def bool_writer(*attributes, strict: false)
46
+ attributes.map { |a| Attribute.new(a) }.each do |attribute|
47
+ define_method("#{attribute.name}=") do |v|
48
+ if strict && ![TrueClass, FalseClass].include?(v.class)
49
+ raise ArgumentError, 'Argument must be a boolean'
50
+ end
54
51
 
55
- instance_variable_set(attribute, !!v)
52
+ instance_variable_set(attribute.ivar, !!v)
53
+ end
56
54
  end
57
55
  end
58
56
 
@@ -60,9 +58,9 @@ module Attribool
60
58
  # Creates a simple reader and writer for booleans. This should only be used
61
59
  # when the attribute should only ever be +true+ or +false+.
62
60
  #
63
- # @param [Symbol, String] attribute
64
- def bool_accessor(attribute)
65
- bool_reader(attribute)
66
- bool_writer(attribute)
61
+ # @param [Symbol, String] *attributes
62
+ def bool_accessor(*attributes)
63
+ bool_reader(*attributes)
64
+ bool_writer(*attributes)
67
65
  end
68
66
  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.1.0
4
+ version: 1.0.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-12-21 00:00:00.000000000 Z
11
+ date: 2022-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -79,6 +79,8 @@ files:
79
79
  - Rakefile
80
80
  - attribool.gemspec
81
81
  - lib/attribool.rb
82
+ - lib/attribool/attribute.rb
83
+ - lib/attribool/method_name.rb
82
84
  - lib/attribool/version.rb
83
85
  homepage: https://github.com/evanthegrayt/attribool
84
86
  licenses: