Class: Qrb::DressHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/qrb/support/dress_helper.rb

Instance Method Summary collapse

Constructor Details

#initializeDressHelper

Returns a new instance of DressHelper.



4
5
6
# File 'lib/qrb/support/dress_helper.rb', line 4

def initialize
  @stack = []
end

Instance Method Details

#deeper(location) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/qrb/support/dress_helper.rb', line 16

def deeper(location)
  @stack.push(location.to_s)
  res = yield
ensure
  @stack.pop
  res
end

#default_error_message(type, value) ⇒ Object



45
46
47
48
# File 'lib/qrb/support/dress_helper.rb', line 45

def default_error_message(type, value)
  value_s, type_s = value_to_s(value), type_to_s(type)
  "Invalid value `#{value_s}` for #{type_s}"
end

#fail!(msg, cause = nil) ⇒ Object

Raises:



41
42
43
# File 'lib/qrb/support/dress_helper.rb', line 41

def fail!(msg, cause = nil)
  raise TypeError.new(msg, cause, location)
end

#failed!(type, value, cause = nil) ⇒ Object

Raises:



36
37
38
39
# File 'lib/qrb/support/dress_helper.rb', line 36

def failed!(type, value, cause = nil)
  msg = default_error_message(type, value)
  raise TypeError.new(msg, cause, location)
end

#iterate(value) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/qrb/support/dress_helper.rb', line 8

def iterate(value)
  value.each.each_with_index do |elm, index|
    deeper(index) do
      yield(elm, index)
    end
  end
end

#just_try(rescue_on = TypeError) ⇒ Object



24
25
26
27
28
# File 'lib/qrb/support/dress_helper.rb', line 24

def just_try(rescue_on = TypeError)
  [ true, yield ]
rescue rescue_on => cause
  [ false, nil ]
end

#locationObject



50
51
52
# File 'lib/qrb/support/dress_helper.rb', line 50

def location
  @stack.join('/')
end

#try(type, value) ⇒ Object



30
31
32
33
34
# File 'lib/qrb/support/dress_helper.rb', line 30

def try(type, value)
  yield
rescue TypeError => cause
  failed!(type, value, cause)
end