Module: AttrTypecastable::ClassMethods

Defined in:
lib/attr_typecastable.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  allow_nil: true,
  cast_method: true,
  reset_method: true,
}.freeze

Instance Method Summary collapse

Instance Method Details

#typed_attr_accessor(attribute_name, typecaster_name = Object, **options) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/attr_typecastable.rb', line 26

def typed_attr_accessor(attribute_name, typecaster_name = Object, **options)
  attribute_name = attribute_name.to_sym
  options = DEFAULT_OPTIONS.merge(options)
  must_have_default_when_disallow_nil(options)

  typecaster = Types.typecaster_for(typecaster_name || Object).new(options)
  typed_attr_reflections[attribute_name] = Reflection.new(attribute_name, typecaster, options)

  attr_reader attribute_name

  define_typed_attr_writer(attribute_name)
  define_cast_attribute(attribute_name) if options[:cast_method]
  define_reset_attribute(attribute_name) if options[:reset_method]
end