Method: SmartEnum::Attributes::ClassMethods#attribute

Defined in:
lib/smart_enum/attributes.rb

#attribute(name, types, coercer: nil, reader_method: nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/smart_enum/attributes.rb', line 49

def attribute(name, types, coercer: nil, reader_method: nil)
  name = name.to_sym
  # ensure `types` is an array.  From activesupport's Array#wrap.
  types = if types.nil?
    []
  elsif types.respond_to?(:to_ary)
    types.to_ary || [types]
  else
    [types]
  end
  attribute_set[name] = Attribute.new(name, types, coercer)
  define_method(reader_method || name) do
    attributes[name]
  end
  if types == Boolean
    alias_method "#{name}?".to_sym, name
  end
end