Module: AttrTyped::ClassMethods

Defined in:
lib/attr_typed.rb

Overview

Class method attr_typed

Instance Method Summary collapse

Instance Method Details

#attr_typed(attrs) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/attr_typed.rb', line 109

def attr_typed(attrs)
  attrs.each do |(attribute, type)|
    raise ArgumentError, "Unsupported type #{type}" unless ALLOWED_TYPES.include?(type)

    define_method("#{attribute}_with_typing=") do |value|
      instance_variable_set("@#{attribute}", public_send("parse_typed_value", value, type))
    end

    class_eval do
      attr_accessor attribute

      alias_method "#{attribute}_without_typing=", "#{attribute}="
      alias_method "#{attribute}=", "#{attribute}_with_typing="
    end
  end
end