attribool 1.0.0 → 1.0.1

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: dc735929535da353af1b584656aafb3ba6ba46b7996af32ae37dedc30bcfb0a2
4
- data.tar.gz: 116a6a7fd8f9803773d10a6fff0b294f73bb9f5eb872db573f11273f8fc1ddcb
3
+ metadata.gz: e18815dc8107a42c84084e2c35614d8111f7ae827224b4b78cbb3e920ff9c970
4
+ data.tar.gz: 84642d3baca03e19bf84794ed88f2696d1f639801b79f55cd9fc2636309f8061
5
5
  SHA512:
6
- metadata.gz: 73fc2ab7f625fdfbb7577cda39c60eb86f6f53511fa685b2c0b86a79c90d22876123fcd34db74a76deb25eabc223cf891964fac36f962732af87a0c0c9aabcb0
7
- data.tar.gz: ec6dc2496637c4c21c3c252a4c02fa1d574b4e4e033b74bd9019d5a8c286febba0990cd6d5659cc15be20b6a6c6dc3428f23a8bcb11ded8ed6a884403f78f682
6
+ metadata.gz: 3984a29d0285e92995b01d36ba9fbe0fe4919b9800116bff642ff5e7f0ee8e3fe1c4444a5bb1f3494f87e41228c31b4f0a7eec086ae3f3e5f07f255a5943ba41
7
+ data.tar.gz: 68246fd9004eb745078a8399ee4dc160ba2b06535a12fad81173958053c738c523fe5df152e266e2103e74f84715f9ca0bb309726eb30159e313f45efd9a8761
@@ -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
- def initialize(attribute)
6
- attribute = attribute.to_s
7
- @ivar = attribute.start_with?('@') ? attribute : "@#{attribute}"
8
- @name = @ivar.delete_prefix('@')
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
@@ -21,7 +21,7 @@ module Attribool
21
21
  # Patch version.
22
22
  #
23
23
  # @return [Integer]
24
- PATCH = 0
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
- 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
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("#{attribute.name}=") do |v|
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.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-06 00:00:00.000000000 Z
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