Skip to content

Commit 2f2b3fc

Browse files
committed
♻️ Simplify Config::AttrAccessors a little
1 parent 69bd2e4 commit 2f2b3fc

3 files changed

Lines changed: 10 additions & 10 deletions

File tree

lib/net/imap/config.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@ def defaults_hash
631631
to_h.reject {|k,v| DEFAULT_TO_INHERIT.include?(k) }
632632
end
633633

634+
Struct = AttrAccessors.struct
634635
@default = AttrVersionDefaults.compile_default!
635636
@global = default.new
636637
AttrVersionDefaults.compile_version_defaults!

lib/net/imap/config/attr_accessors.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,23 @@ def self.included(mod)
2727

2828
def self.attr_accessor(name) # :nodoc: internal API
2929
name = name.to_sym
30+
raise ArgumentError, "already defined #{name}" if attributes.include?(name)
31+
attributes << name
3032
def_delegators :data, name, :"#{name}="
3133
end
3234

33-
def self.attributes
34-
instance_methods.grep(/=\z/).map { _1.to_s.delete_suffix("=").to_sym }
35-
end
36-
private_class_method :attributes
35+
# An array of Config attribute names
36+
singleton_class.attr_reader :attributes
37+
@attributes = []
3738

3839
def self.struct # :nodoc: internal API
39-
unless defined?(self::Struct)
40-
const_set :Struct, Struct.new(*attributes)
41-
end
42-
self::Struct
40+
attributes.freeze
41+
Struct.new(*attributes)
4342
end
4443

4544
def initialize # :notnew:
4645
super()
47-
@data = AttrAccessors.struct.new
46+
@data = Config::Struct.new
4847
end
4948

5049
# Freezes the internal attributes struct, in addition to +self+.

test/net/imap/test_config.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ def duck.to_r = 1/11111
363363
expected = {
364364
debug: false, open_timeout: 30, idle_response_timeout: 5, sasl_ir: true,
365365
}
366-
attributes = Config::AttrAccessors::Struct.members
366+
attributes = Config::Struct.members
367367
default_hash = Config.default.to_h
368368
assert_equal expected, default_hash.slice(*expected.keys)
369369
assert_equal attributes, default_hash.keys

0 commit comments

Comments
 (0)