diff --git a/lib/net/imap/config.rb b/lib/net/imap/config.rb index 29c01bd4..642c88e7 100644 --- a/lib/net/imap/config.rb +++ b/lib/net/imap/config.rb @@ -631,6 +631,7 @@ def defaults_hash to_h.reject {|k,v| DEFAULT_TO_INHERIT.include?(k) } end + Struct = AttrAccessors.struct @default = AttrVersionDefaults.compile_default! @global = default.new AttrVersionDefaults.compile_version_defaults! diff --git a/lib/net/imap/config/attr_accessors.rb b/lib/net/imap/config/attr_accessors.rb index 8725428c..cef757e0 100644 --- a/lib/net/imap/config/attr_accessors.rb +++ b/lib/net/imap/config/attr_accessors.rb @@ -27,24 +27,23 @@ def self.included(mod) def self.attr_accessor(name) # :nodoc: internal API name = name.to_sym + raise ArgumentError, "already defined #{name}" if attributes.include?(name) + attributes << name def_delegators :data, name, :"#{name}=" end - def self.attributes - instance_methods.grep(/=\z/).map { _1.to_s.delete_suffix("=").to_sym } - end - private_class_method :attributes + # An array of Config attribute names + singleton_class.attr_reader :attributes + @attributes = [] def self.struct # :nodoc: internal API - unless defined?(self::Struct) - const_set :Struct, Struct.new(*attributes) - end - self::Struct + attributes.freeze + Struct.new(*attributes) end def initialize # :notnew: super() - @data = AttrAccessors.struct.new + @data = Config::Struct.new end # Freezes the internal attributes struct, in addition to +self+. diff --git a/test/net/imap/test_config.rb b/test/net/imap/test_config.rb index 6a35bff9..8e1ac569 100644 --- a/test/net/imap/test_config.rb +++ b/test/net/imap/test_config.rb @@ -363,7 +363,7 @@ def duck.to_r = 1/11111 expected = { debug: false, open_timeout: 30, idle_response_timeout: 5, sasl_ir: true, } - attributes = Config::AttrAccessors::Struct.members + attributes = Config::Struct.members default_hash = Config.default.to_h assert_equal expected, default_hash.slice(*expected.keys) assert_equal attributes, default_hash.keys