Module: ReeEnum::Enumerable::ClassMethods
- Includes:
- Contractable
- Defined in:
- lib/ree_lib/packages/ree_enum/package/ree_enum/enumerable.rb
Constant Summary collapse
- RESTRICTED_METHODS =
[ :setup_enum, :get_values, :get_enum_name, :val, :__ENCODING__, :__LINE__, :__FILE__, :BEGIN, :END, :alias, :and, :begin, :break, :case, :class, :def, :defined?, :do, :else, :elsif, :end, :ensure, :false, :for, :if, :in, :module, :next, :nil, :not, :or, :redo, :rescue, :retry, :return, :self, :super, :then, :true, :undef, :unless, :until, :when, :while, :yield ].freeze
- ALLOWED_VALUE_TO_METHOD_REGEXP =
/^[a-z_]\w*[?!]?$/
Instance Method Summary collapse
- #get_enum_name ⇒ Object
- #get_values ⇒ Object
- #setup_enum(enum_name) ⇒ Object
- #val(value, mapped_value = nil, method: nil) ⇒ Object
Methods included from Contractable
Instance Method Details
#get_enum_name ⇒ Object
34 35 36 |
# File 'lib/ree_lib/packages/ree_enum/package/ree_enum/enumerable.rb', line 34 def get_enum_name @values&.enum_name end |
#get_values ⇒ Object
30 31 32 |
# File 'lib/ree_lib/packages/ree_enum/package/ree_enum/enumerable.rb', line 30 def get_values @values end |
#setup_enum(enum_name) ⇒ Object
26 27 28 |
# File 'lib/ree_lib/packages/ree_enum/package/ree_enum/enumerable.rb', line 26 def setup_enum(enum_name) @values ||= ReeEnum::Values.new(self, enum_name) end |
#val(value, mapped_value = nil, method: nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ree_lib/packages/ree_enum/package/ree_enum/enumerable.rb', line 38 def val(value, mapped_value = nil, method: nil) value = value.to_s if value.is_a?(Symbol) mapped_value ||= value if method.nil? && value.is_a?(String) && value.match?(ALLOWED_VALUE_TO_METHOD_REGEXP) method = value.to_sym end if RESTRICTED_METHODS.include?(method) raise ArgumentError.new("#{method.inspect} is not allowed as enum method") end enum_value = @values.add(value, mapped_value, method) if !method.nil? class_eval(<<~RUBY, __FILE__, __LINE__ + 1) def #{method} get_values.by_value(#{value.inspect}.freeze) end def self.#{method} get_values.by_value(#{value.inspect}.freeze) end RUBY end enum_value end |