Skip to content

Commit 0a0fc18

Browse files
committed
♻️ Refactor Config attr type coercion
Switching to a "Types" registry, which holds a proc for each coercion.
1 parent 27d0cb6 commit 0a0fc18

File tree

2 files changed

+16
-26
lines changed

2 files changed

+16
-26
lines changed

lib/net/imap/config.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ def self.[](config)
245245
# present. When capabilities are unknown, Net::IMAP will automatically
246246
# send a +CAPABILITY+ command first before sending +LOGIN+.
247247
#
248-
attr_accessor :enforce_logindisabled, type: [
248+
attr_accessor :enforce_logindisabled, type: Enum[
249249
false, :when_capabilities_cached, true
250250
]
251251

@@ -275,7 +275,7 @@ def self.[](config)
275275
# Raise an ArgumentError with the deprecation warning.
276276
#
277277
# Note: #responses_without_args is an alias for #responses_without_block.
278-
attr_accessor :responses_without_block, type: [
278+
attr_accessor :responses_without_block, type: Enum[
279279
:silence_deprecation_warning, :warn, :frozen_dup, :raise,
280280
]
281281

@@ -320,7 +320,7 @@ def self.[](config)
320320
#
321321
# [+false+ <em>(planned default for +v0.6+)</em>]
322322
# ResponseParser _only_ uses AppendUIDData and CopyUIDData.
323-
attr_accessor :parser_use_deprecated_uidplus_data, type: [
323+
attr_accessor :parser_use_deprecated_uidplus_data, type: Enum[
324324
true, :up_to_max_size, false
325325
]
326326

lib/net/imap/config/attr_type_coercion.rb

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,24 @@ def self.included(mod)
2626
end
2727
private_class_method :included
2828

29-
def self.attr_accessor(attr, type: nil)
30-
return unless type
31-
if :boolean == type then boolean attr
32-
elsif Integer == type then integer attr
33-
elsif Array === type then enum attr, type
34-
else raise ArgumentError, "unknown type coercion %p" % [type]
35-
end
36-
end
37-
38-
def self.boolean(attr)
39-
define_method :"#{attr}=" do |val| super !!val end
40-
define_method :"#{attr}?" do send attr end
41-
end
29+
Types = Hash.new do |h, type| type => Proc | nil; type end
30+
Types[:boolean] = Boolean = -> {!!_1}
31+
Types[Integer] = ->{Integer(_1)}
4232

43-
def self.integer(attr)
44-
define_method :"#{attr}=" do |val| super Integer val end
33+
def self.attr_accessor(attr, type: nil)
34+
type = Types[type] or return
35+
define_method :"#{attr}=" do |val| super type[val] end
36+
define_method :"#{attr}?" do send attr end if type == Boolean
4537
end
4638

47-
def self.enum(attr, enum)
39+
Enum = ->(*enum) {
4840
enum = enum.dup.freeze
4941
expected = -"one of #{enum.map(&:inspect).join(", ")}"
50-
define_method :"#{attr}=" do |val|
51-
unless enum.include?(val)
52-
raise ArgumentError, "expected %s, got %p" % [expected, val]
53-
end
54-
super val
55-
end
56-
end
42+
->val {
43+
return val if enum.include?(val)
44+
raise ArgumentError, "expected %s, got %p" % [expected, val]
45+
}
46+
}
5747

5848
end
5949
end

0 commit comments

Comments
 (0)