Class: Remap::Rule::Enum

Inherits:
Object
  • Object
show all
Extended by:
Dry::Initializer
Includes:
Dry::Core::Constants
Defined in:
lib/remap/rule/support/enum.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(&block) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/remap/rule/support/enum.rb', line 16

def self.call(&block)
  unless block
    raise ArgumentError, "no block given"
  end

  new.tap { _1.execute(&block) }
end

Instance Method Details

#[](key) ⇒ Object



24
25
26
# File 'lib/remap/rule/support/enum.rb', line 24

def [](key)
  mappings[key]
end

#from(*keys, to:) ⇒ VOID

Map all keys in keys to to

Returns:

  • (VOID)


42
43
44
45
46
# File 'lib/remap/rule/support/enum.rb', line 42

def from(*keys, to:)
  keys.each do |key|
    mappings[key] = Some(to)
  end
end

#get(key, &error) ⇒ Object Also known as: call



28
29
30
31
32
33
34
35
36
# File 'lib/remap/rule/support/enum.rb', line 28

def get(key, &error)
  unless error
    return get(key) { raise Error, _1 }
  end

  mappings[key].bind { return _1 }.or do
    error["Enum key [#{key}] not found among [#{mappings.keys.inspect}]"]
  end
end

#otherwise(value) ⇒ Void

Fallback value when #call fails

Returns:



58
59
60
# File 'lib/remap/rule/support/enum.rb', line 58

def otherwise(value)
  mappings.default = Some(value)
end

#value(id) ⇒ VOID

Maps var to var

Returns:

  • (VOID)


51
52
53
# File 'lib/remap/rule/support/enum.rb', line 51

def value(id)
  from(id, to: id)
end