Class: RustyKey::Case

Inherits:
Object
  • Object
show all
Defined in:
lib/rusty_key/case.rb

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Case

Returns a new instance of Case.



11
12
13
14
15
# File 'lib/rusty_key/case.rb', line 11

def initialize(value)
  @value = value
  @found = false
  @result = -> {}
end

Instance Method Details

#else(&block) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/rusty_key/case.rb', line 25

def else(&block)
  if @found
    @result&.call(@value)
  else
    block&.call(@value)
  end
end

#when(condition, &block) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/rusty_key/case.rb', line 17

def when(condition, &block)
  if !@found && condition === @value
    @found = true
    @result = block
  end
  self
end