Class: SmartCase

Inherits:
Object
  • Object
show all
Defined in:
lib/version.rb,
lib/smart_case.rb

Constant Summary collapse

VERSION =
'0.0.1'

Instance Method Summary collapse

Constructor Details

#initialize(o = nil) ⇒ SmartCase

Returns a new instance of SmartCase.



2
3
4
5
6
# File 'lib/smart_case.rb', line 2

def initialize(o = nil)
  @o, @when, @then = o, [], []
  instance_eval(&Proc.new) if block_given?
  self
end

Instance Method Details

#call(o = nil, multi: nil) ⇒ Object

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/smart_case.rb', line 11

def call(o = nil, multi: nil)
  raise ArgumentError unless @when.size == @then.size

  if multi
    result = @when.zip(@then).map do |w, t|
      w.call(o || @o) ? t.call(o || @o) : nil
    end
    return result
  else
    @when.zip(@then).each do |w, t|
      return t.call(o || @o) if w.call(o || @o)
    end
  end

  return nil
end

#t(proc = nil) ⇒ Object



9
# File 'lib/smart_case.rb', line 9

def t(proc = nil) @then << (proc || Proc.new) end

#w(proc = nil) ⇒ Object



8
# File 'lib/smart_case.rb', line 8

def w(proc = nil) @when << (proc || Proc.new) end