Module: Fear::Utils

Included in:
RightBiased::Left
Defined in:
lib/fear/utils.rb

Constant Summary collapse

UNDEFINED =
Object.new.freeze

Class Method Summary collapse

Class Method Details

.assert_arg_or_block!(method_name, *args) ⇒ Object


9
10
11
12
13
# File 'lib/fear/utils.rb', line 9

def assert_arg_or_block!(method_name, *args)
  unless block_given? ^ !args.empty?
    raise ArgumentError, "##{method_name} accepts either one argument or block"
  end
end

.assert_type!(value, *types) ⇒ Object


23
24
25
26
27
# File 'lib/fear/utils.rb', line 23

def assert_type!(value, *types)
  if types.none? { |type| value.is_a?(type) }
    raise TypeError, "expected `#{value.inspect}` to be of #{types.join(", ")} class"
  end
end

.return_or_call_proc(value) ⇒ Object


29
30
31
32
33
34
35
# File 'lib/fear/utils.rb', line 29

def return_or_call_proc(value)
  if value.respond_to?(:call)
    value.()
  else
    value
  end
end

.with_block_or_argument(method_name, arg = UNDEFINED, block = nil) ⇒ Object


15
16
17
18
19
20
21
# File 'lib/fear/utils.rb', line 15

def with_block_or_argument(method_name, arg = UNDEFINED, block = nil)
  if block.nil? ^ arg.equal?(UNDEFINED)
    yield(block || arg)
  else
    raise ArgumentError, "#{method_name} accepts either block or partial function"
  end
end