Class: ReeEnum::Values
- Inherits:
-
Object
- Object
- ReeEnum::Values
- Defined in:
- lib/ree_lib/packages/ree_enum/package/ree_enum/values.rb
Instance Attribute Summary collapse
-
#enum_name ⇒ Object
readonly
Returns the value of attribute enum_name.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Instance Method Summary collapse
- #add(value, mapped_value, method) ⇒ Object
- #by_mapped_value(mapped_value) ⇒ Object
- #by_mapped_value!(mapped_value) ⇒ Object
- #by_value(value) ⇒ Object
- #by_value!(value) ⇒ Object
- #each ⇒ Object
-
#initialize(klass, enum_name) ⇒ Values
constructor
A new instance of Values.
- #inspect ⇒ Object
- #to_a ⇒ Object
- #value_type ⇒ Object
Constructor Details
#initialize(klass, enum_name) ⇒ Values
Returns a new instance of Values.
6 7 8 9 10 11 12 |
# File 'lib/ree_lib/packages/ree_enum/package/ree_enum/values.rb', line 6 def initialize(klass, enum_name) @klass = klass @enum_name = enum_name @collection = [] @collection_by_value = {} @collection_by_mapped_value = {} end |
Instance Attribute Details
#enum_name ⇒ Object (readonly)
Returns the value of attribute enum_name.
4 5 6 |
# File 'lib/ree_lib/packages/ree_enum/package/ree_enum/values.rb', line 4 def enum_name @enum_name end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
4 5 6 |
# File 'lib/ree_lib/packages/ree_enum/package/ree_enum/values.rb', line 4 def klass @klass end |
Instance Method Details
#add(value, mapped_value, method) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ree_lib/packages/ree_enum/package/ree_enum/values.rb', line 55 def add(value, mapped_value, method) if @value_type.nil? @value_type = value.class elsif @value_type != value.class raise ArgumentError, "#{@klass}: value types should be the same for all enum values" end if !method.nil? && @collection.any? { _1.method == method } raise ArgumentError, "#{@klass}: method #{method.inspect} was already added" end if @collection_by_value.key?(value) raise ArgumentError, "#{@klass}: value #{value.inspect} was already added" end if @collection_by_mapped_value.key?(mapped_value) raise ArgumentError, "#{@klass}: mapped_value(#{mapped_value.inspect}) for #{value.inspect} was already added" end enum_value = ReeEnum::Value.new( @klass, @enum_name, value, mapped_value, method ) @collection << enum_value @collection_by_value[value] = enum_value @collection_by_mapped_value[mapped_value] = enum_value enum_value end |
#by_mapped_value(mapped_value) ⇒ Object
40 41 42 |
# File 'lib/ree_lib/packages/ree_enum/package/ree_enum/values.rb', line 40 def by_mapped_value(mapped_value) @collection_by_mapped_value[mapped_value] end |
#by_mapped_value!(mapped_value) ⇒ Object
45 46 47 48 |
# File 'lib/ree_lib/packages/ree_enum/package/ree_enum/values.rb', line 45 def by_mapped_value!(mapped_value) by_mapped_value(mapped_value) || (raise ArgumentError.new("constant for value #{mapped_value.inspect} is not found in #{self.inspect}")) end |
#by_value(value) ⇒ Object
28 29 30 31 |
# File 'lib/ree_lib/packages/ree_enum/package/ree_enum/values.rb', line 28 def by_value(value) value = value.to_s if value.is_a?(Symbol) @collection_by_value[value] end |
#by_value!(value) ⇒ Object
34 35 36 37 |
# File 'lib/ree_lib/packages/ree_enum/package/ree_enum/values.rb', line 34 def by_value!(value) by_value(value) || (raise ArgumentError.new("constant for value #{value.inspect} is not found in #{self.inspect}")) end |
#each ⇒ Object
23 24 25 |
# File 'lib/ree_lib/packages/ree_enum/package/ree_enum/values.rb', line 23 def each(&) @collection.each(&) end |
#inspect ⇒ Object
50 51 52 |
# File 'lib/ree_lib/packages/ree_enum/package/ree_enum/values.rb', line 50 def inspect @collection.map(&:inspect).inspect end |
#to_a ⇒ Object
19 20 21 |
# File 'lib/ree_lib/packages/ree_enum/package/ree_enum/values.rb', line 19 def to_a @collection end |
#value_type ⇒ Object
14 15 16 17 |
# File 'lib/ree_lib/packages/ree_enum/package/ree_enum/values.rb', line 14 def value_type raise ArgumentError, "value_type is not defined" unless defined?(@value_type) @value_type end |