Class: MultiCase::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/multi_case/dsl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lhs, rhs) ⇒ DSL

Returns a new instance of DSL.

Parameters:

  • lhs (#===)
  • rhs (#===)


6
7
8
9
10
11
# File 'lib/multi_case/dsl.rb', line 6

def initialize(lhs, rhs)
  @lhs = lhs
  @rhs = rhs

  @result = nil
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



13
14
15
# File 'lib/multi_case/dsl.rb', line 13

def result
  @result
end

Instance Method Details

#multi(from_to, &block) ⇒ Object

A single case to which @lhs & @rhs are matched against

Parameters:

  • from_to (Hash)

    @lhs is matched against keys, @rhs against values, they both can be arrays

  • &block (Object)

    User-specified result if both of parameters match

Returns:

  • (Object)

    Return-value of user-specified function



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/multi_case/dsl.rb', line 22

def multi(from_to, &block)
  from = from_to.keys.flatten(1)
  from = [@lhs]  if from.empty?  # catch-all on empty array

  to = from_to.values.flatten(1)
  to = [@rhs]  if to.empty?

  if from.product(to).map(&method(:param_equal)).any?
    @result ||= block.call
  end
end