Class: Handle

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

Direct Known Subclasses

H

Constant Summary collapse

NotValidError =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.it(options = {}, &block) ⇒ Object



7
8
9
# File 'lib/handle.rb', line 7

def it(options = {}, &block)
  new.it(options, &block)
end

Instance Method Details

#<=Object



26
27
28
# File 'lib/handle.rb', line 26

def <=
  result
end

#>(args = {}, &block) ⇒ Object



22
23
24
# File 'lib/handle.rb', line 22

def >(args={}, &block)
  with(args, &block)
end

#e(&block) ⇒ Object



30
31
32
# File 'lib/handle.rb', line 30

def e(&block)
  on_fail(&block)
end

#errorObject



64
65
66
# File 'lib/handle.rb', line 64

def error
  @result.error
end

#it(options) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/handle.rb', line 12

def it(options)
  validate(options)

  @result = OpenStruct.new return: yield, success: true, error: nil
  @returns_pool = []
  self
rescue StandardError => e
  error_handler(e)
end

#on_fail(&block) ⇒ Object



51
52
53
54
# File 'lib/handle.rb', line 51

def on_fail(&block)
  block.call(@result.error, **options = {}) unless success?
  self
end

#resultObject



56
57
58
# File 'lib/handle.rb', line 56

def result
  @result.return
end

#success?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/handle.rb', line 60

def success?
  @result.success == true
end

#with(args = {}, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/handle.rb', line 34

def with(args={}, &block)
  args = args.slice(:on_fail)

  if success?
    @returns_pool << block.call(@result.return, **options = {})
    @result.return = @returns_pool.last
  end
  self
rescue StandardError => e
  if args[:on_fail] == :rollback
    @result.return = @returns_pool.last
    self
  else
    error_handler(e)
  end
end