Class: Finitio::DressHelper

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

Instance Method Summary collapse

Constructor Details

#initializeDressHelper

Returns a new instance of DressHelper.



4
5
6
# File 'lib/finitio/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/finitio/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



46
47
48
49
# File 'lib/finitio/support/dress_helper.rb', line 46

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

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

Raises:



42
43
44
# File 'lib/finitio/support/dress_helper.rb', line 42

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

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

Raises:



37
38
39
40
# File 'lib/finitio/support/dress_helper.rb', line 37

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/finitio/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
29
# File 'lib/finitio/support/dress_helper.rb', line 24

def just_try(rescue_on = TypeError)
  [ true, yield ]
rescue rescue_on => cause
  #STDERR.puts cause.inspect
  [ false, cause ]
end

#locationObject



51
52
53
# File 'lib/finitio/support/dress_helper.rb', line 51

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

#try(type, value) ⇒ Object



31
32
33
34
35
# File 'lib/finitio/support/dress_helper.rb', line 31

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