Class: MetaEnum::Type
- Inherits:
-
Object
- Object
- MetaEnum::Type
- Defined in:
- lib/meta_enum/type.rb
Instance Attribute Summary collapse
-
#values ⇒ Object
readonly
Returns the value of attribute values.
-
#values_by_name ⇒ Object
readonly
Returns the value of attribute values_by_name.
-
#values_by_number ⇒ Object
readonly
Returns the value of attribute values_by_number.
Instance Method Summary collapse
-
#[](key) ⇒ Object
-
is a “do what I mean” operator.
-
#initialize(values) ⇒ Type
constructor
Initialize takes a single hash of name to number.
- #inspect ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(values) ⇒ Type
Initialize takes a single hash of name to number.
e.g. MetaEnum::Type.new(red: 0, green: 1, blue: 2)
Additional data can also be associated with each value by passing an array of [number, extra data]. This can be used for additional description or any other reason.
e.g. MetaEnum::Type.new(small: [0, “Less than 10], large: [1, ”At least 10“]
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/meta_enum/type.rb', line 16 def initialize(values) @values_by_number = {} @values_by_name = {} @values = Set.new values.each do |name, number_and_data| number_and_data = Array(number_and_data) v = Value.new number_and_data[0], name, number_and_data[1], self raise ArgumentError, "duplicate number: #{v.number}" if @values_by_number.key? v.number raise ArgumentError, "duplicate name: #{v.name}" if @values_by_name.key? v.name @values_by_number[v.number] = v @values_by_name[v.name] = v @values.add(v) end @values_by_number.freeze @values_by_name.freeze @values.freeze freeze end |
Instance Attribute Details
#values ⇒ Object (readonly)
Returns the value of attribute values.
5 6 7 |
# File 'lib/meta_enum/type.rb', line 5 def values @values end |
#values_by_name ⇒ Object (readonly)
Returns the value of attribute values_by_name.
5 6 7 |
# File 'lib/meta_enum/type.rb', line 5 def values_by_name @values_by_name end |
#values_by_number ⇒ Object (readonly)
Returns the value of attribute values_by_number.
5 6 7 |
# File 'lib/meta_enum/type.rb', line 5 def values_by_number @values_by_number end |
Instance Method Details
#[](key) ⇒ Object
-
is a “do what I mean” operator. It returns the Value from this type depending on the key.
When key is a symbol, it is considered the name of the Value to return. Since symbols are used from number, it is considered an error if the key is not found and it raises an exception.
When key can be converted to an integer by Integer(), then it is considered the number of the Value to return. Retrieving by number is presumed to converting from external data where a missing value should not be considered fatal. In this case it returns a MissingValue is with number as the key. This allows a Type to only specify the values is needs while passing through the others unmodified.
Finally, when key is a MetaEnum::Value, it is simply returned (unless it belongs to a different Type in which case an ArgumentError is raised).
See #values_by_number and #values_by_name for non-fuzzy value selection.
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/meta_enum/type.rb', line 54 def [](key) case key when Value, MissingValue raise ArgumentError, "wrong type" unless key.type == self key when Symbol values_by_name.fetch(key) else key = Integer(key) values_by_number.fetch(key) { MissingValue.new key, self } end end |
#inspect ⇒ Object
67 68 69 |
# File 'lib/meta_enum/type.rb', line 67 def inspect sprintf('#<%s: {%s}>', self.class, values.to_a.map { |v| "#{v.name}: #{v.number}"}.join(", ")) end |
#size ⇒ Object
71 72 73 |
# File 'lib/meta_enum/type.rb', line 71 def size values.size end |