Class: Remap::Rule::Enum
- Defined in:
- lib/remap/rule/support/enum.rb
Class Method Summary collapse
-
.call(&block) ⇒ Any
Builds an enumeration using the block as context.
Instance Method Summary collapse
- #[](key) ⇒ Maybe
- #default ⇒ Maybe
- #from(*keys, to:) ⇒ void
-
#get(key) {|String| ... } ⇒ Any
(also: #call)
Translates key into a value using predefined mappings.
- #mappings ⇒ Hash
- #otherwise(value) ⇒ void
- #value(*ids) ⇒ void
Methods inherited from Proxy
Class Method Details
.call(&block) ⇒ Any
Builds an enumeration using the block as context
31 32 33 34 35 36 37 |
# File 'lib/remap/rule/support/enum.rb', line 31 def self.call(&block) unless block raise ArgumentError, "no block given" end new.tap { _1.execute(&block) } end |
Instance Method Details
#[](key) ⇒ Maybe
59 60 61 |
# File 'lib/remap/rule/support/enum.rb', line 59 def [](key) mappings[key] end |
#default ⇒ Maybe
12 |
# File 'lib/remap/rule/support/enum.rb', line 12 option :default, default: -> { None() } |
#from(*keys, to:) ⇒ void
This method returns an undefined value.
64 65 66 67 68 69 70 71 |
# File 'lib/remap/rule/support/enum.rb', line 64 def from(*keys, to:) value = Some(to) keys.each do |key| mappings[key] = value mappings[to] = value end end |
#get(key) {|String| ... } ⇒ Any Also known as: call
Translates key into a value using predefined mappings
47 48 49 50 51 52 53 54 55 |
# File 'lib/remap/rule/support/enum.rb', line 47 def get(key, &error) unless error return get(key) { raise Error, _1 } end self[key].bind { return _1 }.or do error["Enum key [#{key}] not found among [#{mappings.keys.inspect}]"] end end |
#mappings ⇒ Hash
9 |
# File 'lib/remap/rule/support/enum.rb', line 9 option :mappings, default: -> { Hash.new { default } } |
#otherwise(value) ⇒ void
This method returns an undefined value.
81 82 83 |
# File 'lib/remap/rule/support/enum.rb', line 81 def otherwise(value) mappings.default = Some(value) end |
#value(*ids) ⇒ void
This method returns an undefined value.
74 75 76 77 78 |
# File 'lib/remap/rule/support/enum.rb', line 74 def value(*ids) ids.each do |id| from(id, to: id) end end |