Module: PriceHubble::Concern::Attributes::Enum
- Extended by:
- ActiveSupport::Concern
- Included in:
- PriceHubble::Concern::Attributes
- Defined in:
- lib/price_hubble/entity/concern/attributes/enum.rb
Overview
A separated enum typed attribute helper.
Class Method Summary collapse
-
.typed_attr_enum(name, values:, **_args) ⇒ Object
Register a fixed enum attribute.
Class Method Details
.typed_attr_enum(name, values:, **_args) ⇒ Object
Register a fixed enum attribute.
rubocop:disable Metrics/MethodLength – because of the inline meta
method definitions
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/price_hubble/entity/concern/attributes/enum.rb', line 19 def typed_attr_enum(name, values:, **_args) values = values.map(&:to_sym) const_values = "ATTR_#{name.to_s.upcase}" class_eval " # Define the constant for all valid values\n \#{const_values} = \#{values}.freeze\n\n def \#{name}=(value)\n \#{name}_will_change!\n value = value.to_sym\n\n unless \#{const_values}.include? value\n raise ArgumentError, \"'\\\#{value}' is not a valid \#{name} \" \\\n \"(values: \\\#{\#{const_values}})\"\n end\n\n @\#{name} = value\n end\n RUBY\n\n values.each do |value|\n class_eval <<-RUBY, __FILE__, __LINE__ + 1\n def \#{value}!\n self.\#{name} = :\#{value}\n end\n\n def \#{value}?\n self.\#{name} == :\#{value}\n end\n RUBY\n end\nend\n", __FILE__, __LINE__ + 1 |