Class: VirtualMaster::Callbacks::Callback

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Callback

Returns a new instance of Callback.



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

def initialize(name, &block)
  @name = name
  @blocks = {}
  @option = nil
  
  instance_eval &block
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/vmaster/callbacks.rb', line 13

def name
  @name
end

Instance Method Details

#after(event, &block) ⇒ Object



27
28
29
# File 'lib/vmaster/callbacks.rb', line 27

def after(event, &block)
  store_callback(event, :after, &block)
end

#before(event, &block) ⇒ Object



23
24
25
# File 'lib/vmaster/callbacks.rb', line 23

def before(event, &block)
  store_callback(event, :before, &block)
end

#fire(event, phase, options, server) ⇒ Object



44
45
46
# File 'lib/vmaster/callbacks.rb', line 44

def fire(event, phase, options, server)
  @blocks[event][phase].call(options, server) if includes?(event, phase) && options[@option[:name]]
end

#has_option?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/vmaster/callbacks.rb', line 52

def has_option?
  not @option.nil?
end

#option(name, type, description) ⇒ Object

command line options provide configuration

TODO support for multiple options



36
37
38
39
40
41
42
# File 'lib/vmaster/callbacks.rb', line 36

def option(name, type, description)
  @option = {
    :name => name,
    :type => type,
    :description => description
  }
end

#to_optionObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/vmaster/callbacks.rb', line 56

def to_option
  arguments = []

  option_name = "--#{@option[:name]}"
  option_name << " VALUE" if @option[:type]

  arguments << option_name
  arguments << @option[:type] if @option[:type]
  arguments << @option[:description]
end

#to_sObject



48
49
50
# File 'lib/vmaster/callbacks.rb', line 48

def to_s
  @blocks.inspect
end