Module: Net::IMAP::Config::AttrTypeCoercion

Included in:
Net::IMAP::Config
Defined in:
lib/net/imap/config/attr_type_coercion.rb

Overview

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.

Constant Summary collapse

Types =
Hash.new do |h, type| type => Proc | nil; safe{type} end
NilOrInteger =
safe{->val { Integer val unless val.nil? }}
Enum =
->(*enum) {
  safe_enum = safe{enum}
  expected = -"one of #{safe_enum.map(&:inspect).join(", ")}"
  safe{->val {
    return val if safe_enum.include?(val)
    raise ArgumentError, "expected %s, got %p" % [expected, val]
  }}
}

Class Method Summary collapse

Class Method Details

.attr_accessor(attr, type: nil) ⇒ Object



54
55
56
57
58
# File 'lib/net/imap/config/attr_type_coercion.rb', line 54

def self.attr_accessor(attr, type: nil)
  type = Types[type] or return
  define_method :"#{attr}=" do |val| super type[val] end
  define_method :"#{attr}?" do send attr end if type == Boolean
end