Sha256: d9a28b83659f438e5caf5a9d05093062a0515f8d127ed4c010a76e14c909f471

Contents?: true

Size: 1.77 KB

Versions: 18

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

module Net
  class IMAP
    class Config
      # >>>
      #   *NOTE:* This module is an internal implementation detail, with no
      #   guarantee of backward compatibility.
      #
      # Adds a +type+ keyword parameter to +attr_accessor+, to enforce that
      # config attributes have valid types, for example: boolean, numeric,
      # enumeration, non-nullable, etc.
      module AttrTypeCoercion
        # :stopdoc: internal APIs only

        module Macros # :nodoc: internal API
          def attr_accessor(attr, type: nil)
            super(attr)
            AttrTypeCoercion.attr_accessor(attr, type: type)
          end
        end
        private_constant :Macros

        def self.included(mod)
          mod.extend Macros
        end
        private_class_method :included

        def self.attr_accessor(attr, type: nil)
          return unless type
          if    :boolean == type then boolean attr
          elsif Integer  == type then integer attr
          elsif Array   === type then enum    attr, type
          else raise ArgumentError, "unknown type coercion %p" % [type]
          end
        end

        def self.boolean(attr)
          define_method :"#{attr}=" do |val| super !!val end
          define_method :"#{attr}?" do send attr end
        end

        def self.integer(attr)
          define_method :"#{attr}=" do |val| super Integer val end
        end

        def self.enum(attr, enum)
          enum = enum.dup.freeze
          expected = -"one of #{enum.map(&:inspect).join(", ")}"
          define_method :"#{attr}=" do |val|
            unless enum.include?(val)
              raise ArgumentError, "expected %s, got %p" % [expected, val]
            end
            super val
          end
        end

      end
    end
  end
end

Version data entries

18 entries across 17 versions & 4 rubygems

Version Path
net-imap-0.5.6 lib/net/imap/config/attr_type_coercion.rb
net-imap-0.4.19 lib/net/imap/config/attr_type_coercion.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.1.0/gems/net-imap-0.5.1/lib/net/imap/config/attr_type_coercion.rb
trusty-cms-7.0.9.1 vendor/bundle/ruby/3.3.0/gems/net-imap-0.5.1/lib/net/imap/config/attr_type_coercion.rb
net-imap-0.5.5 lib/net/imap/config/attr_type_coercion.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/net-imap-0.4.14/lib/net/imap/config/attr_type_coercion.rb
net-imap-0.5.4 lib/net/imap/config/attr_type_coercion.rb
net-imap-0.5.3 lib/net/imap/config/attr_type_coercion.rb
net-imap-0.5.2 lib/net/imap/config/attr_type_coercion.rb
net-imap-0.4.18 lib/net/imap/config/attr_type_coercion.rb
net-imap-0.5.1 lib/net/imap/config/attr_type_coercion.rb
net-imap-0.5.0 lib/net/imap/config/attr_type_coercion.rb
net-imap-0.4.17 lib/net/imap/config/attr_type_coercion.rb
net-imap-0.4.16 lib/net/imap/config/attr_type_coercion.rb
net-imap-0.4.15 lib/net/imap/config/attr_type_coercion.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/net-imap-0.4.14/lib/net/imap/config/attr_type_coercion.rb
net-imap-0.4.14 lib/net/imap/config/attr_type_coercion.rb
net-imap-0.4.13 lib/net/imap/config/attr_type_coercion.rb