Module: Minfraud::Enum::ClassMethods
- Defined in:
- lib/minfraud/enum.rb
Overview
ClassMethods provides helpers for working with attributes with enumerated types.
Instance Method Summary collapse
-
#enum_accessor(attribute, assignable_values) ⇒ nil
Create a set of methods for enum-like behavior of the attribute.
-
#mapping ⇒ Hash
Returns a hash in the following format: attribute_name => permitted_values.
Instance Method Details
#enum_accessor(attribute, assignable_values) ⇒ nil
Create a set of methods for enum-like behavior of the attribute.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/minfraud/enum.rb', line 37 def enum_accessor(attribute, assignable_values) mapping[attribute] = assignable_values.map(&:intern) self.class.instance_eval do define_method("#{attribute}_values") { mapping[attribute] } end class_eval do define_method(attribute.to_s) { 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 ? value.intern : nil) end end nil end |
#mapping ⇒ Hash
Returns a hash in the following format: attribute_name => permitted_values
23 24 25 26 27 28 |
# File 'lib/minfraud/enum.rb', line 23 def mapping # rubocop:disable ThreadSafety/ClassInstanceVariable # This is a false positive - this is set during class definition and then only read @mapping ||= {} # rubocop:enable ThreadSafety/ClassInstanceVariable end |