Module: StoreModel::Enum

Defined in:
lib/store_model/enum.rb

Overview

Allows defining Rails-like enums

Instance Method Summary collapse

Instance Method Details

#enum(name, values = nil, **kwargs) ⇒ Object

Defines new enum

Parameters:

  • name (String)

    name of the enum to define

  • values (Object) (defaults to: nil)
  • kwargs (Object)


11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/store_model/enum.rb', line 11

def enum(name, values = nil, **kwargs)
  values ||= kwargs[:in] || kwargs
  options = retrieve_options(kwargs)

  ensure_hash(values).tap do |mapping|
    define_attribute(name, mapping, options)
    define_reader(name, mapping)
    define_writer(name, mapping, options[:raise_on_invalid_values])
    define_method("#{name}_value") { attributes[name.to_s] }
    define_map_readers(name, mapping)
    define_predicate_methods(name, mapping, options)
  end
end