Module: Minfraud::Enum::ClassMethods
- Defined in:
- lib/minfraud/enum.rb
Instance Method Summary collapse
-
#enum_accessor(attribute, assignable_values) ⇒ Object
Creates a set of methods for enum-like behaviour of the attribute.
-
#mapping ⇒ Hash
Returns a hash with in the following format: attribute_name => permitted_values.
Instance Method Details
#enum_accessor(attribute, assignable_values) ⇒ Object
Creates a set of methods for enum-like behaviour of the attribute
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/minfraud/enum.rb', line 17 def enum_accessor(attribute, assignable_values) mapping[attribute] = assignable_values.map(&:intern) self.class.instance_eval do define_method("#{attribute}_values") { mapping[attribute] } end self.class_eval do define_method("#{attribute}") { instance_variable_get("@#{attribute}") } define_method "#{attribute}=" do |value| raise NotEnumValueError, 'Value is not permitted' if value && !self.class.mapping[attribute].include?(value.intern) instance_variable_set("@#{attribute}", value&.intern) end end end |
#mapping ⇒ Hash
Returns a hash with in the following format: attribute_name => permitted_values
10 11 12 |
# File 'lib/minfraud/enum.rb', line 10 def mapping @mapping ||= {} end |