Class: Yae::Enum
Class Method Summary collapse
-
.define(key, value) ⇒ Object
Define an entry in the enum.
-
.get(key, raises: true) ⇒ Object
Get the enum value.
-
.get_multiple(*keys, raises: true) ⇒ Object
Get multiple keys at once.
-
.include?(value) ⇒ Boolean
Check if value is used in enum.
- .inherited(subclass) ⇒ Object
-
.keyify(name) ⇒ Object
Convert a string to a enum key.
Class Method Details
.define(key, value) ⇒ Object
Define an entry in the enum
17 18 19 20 |
# File 'lib/yae/enum.rb', line 17 def define(key, value) @enum_store = {} unless enum_store enum_store[key] = value end |
.get(key, raises: true) ⇒ Object
Get the enum value
23 24 25 26 |
# File 'lib/yae/enum.rb', line 23 def get(key, raises: true) return unless enum_store raises ? enum_store.fetch(key) : enum_store.fetch(key, nil) end |
.get_multiple(*keys, raises: true) ⇒ Object
Get multiple keys at once
29 30 31 |
# File 'lib/yae/enum.rb', line 29 def get_multiple(*keys, raises: true) keys.map { |key| get(key, raises: raises) } end |
.include?(value) ⇒ Boolean
Check if value is used in enum
34 35 36 |
# File 'lib/yae/enum.rb', line 34 def include?(value) !!enum_store&.has_value?(value) end |
.inherited(subclass) ⇒ Object
11 12 13 14 |
# File 'lib/yae/enum.rb', line 11 def inherited(subclass) subclass.instance_variable_set(:@enum_store, @enum_store.clone) super end |
.keyify(name) ⇒ Object
Convert a string to a enum key
52 53 54 |
# File 'lib/yae/enum.rb', line 52 def keyify(name) name.gsub(%r{[\-\s/\\]}, "_").downcase.to_sym end |