Class: Backup::Hooks

Inherits:
Object
  • Object
show all
Includes:
CLI::Helpers
Defined in:
lib/backup/hooks.rb

Constant Summary

Constants included from CLI::Helpers

CLI::Helpers::UTILITY

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, &block) ⇒ Hooks

Returns a new instance of Hooks.



20
21
22
23
24
25
# File 'lib/backup/hooks.rb', line 20

def initialize(model, &block)
  @model = model
  @before_proc = Proc.new { } # noop
  @after_proc = Proc.new { } # noop
  instance_eval(&block) if block_given?
end

Instance Attribute Details

#after_procObject

Stores a block to be run before the backup



14
15
16
# File 'lib/backup/hooks.rb', line 14

def after_proc
  @after_proc
end

#before_procObject

Stores a block to be run before the backup



10
11
12
# File 'lib/backup/hooks.rb', line 10

def before_proc
  @before_proc
end

#modelObject (readonly)

The model



18
19
20
# File 'lib/backup/hooks.rb', line 18

def model
  @model
end

Instance Method Details

#after(&code) ⇒ Object



31
32
33
# File 'lib/backup/hooks.rb', line 31

def after(&code)
  @after_proc = code
end

#after!Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/backup/hooks.rb', line 47

def after!
  begin
    Logger.message "Performing After Hook"
    @after_proc.call(model)
    Logger.message "After Hook Completed Successfully"
  rescue => err
    raise Errors::Hooks::AfterHookError.wrap(
      err, "After Hook Failed!"
    )
  end
end

#before(&code) ⇒ Object



27
28
29
# File 'lib/backup/hooks.rb', line 27

def before(&code)
  @before_proc = code
end

#before!Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/backup/hooks.rb', line 35

def before!
  begin
    Logger.message "Performing Before Hook"
    @before_proc.call(model)
    Logger.message "Before Hook Completed Successfully"
  rescue => err
    raise Errors::Hooks::BeforeHookError.wrap(
      err, "Before Hook Failed!"
    )
  end
end

#perform!(hook) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/backup/hooks.rb', line 59

def perform!(hook)
  case hook
  when :before
    before!
  when :after
    after!
  end
end