Class: Aef::Init

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

Overview

Clean and simple *nix init scripts with Ruby

Constant Summary collapse

VERSION =
'1.0.0'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_command(command) ⇒ Object

Set a default command to be called if no command is specified on the commandline.



65
66
67
# File 'lib/init/init.rb', line 65

def self.default_command(command)
  @@default_command = command
end

.parseObject

Call this to begin commandline parsing

If an invalid command is specified on the commandline, a usage example is displayed. If no command is specified, the default command is started



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/init/init.rb', line 28

def self.parse
  command = ARGV.shift || :default

  valid_commands = []

  self.ancestors.each do |klass|
    valid_commands += klass.public_instance_methods(false)
    break if klass == Aef::Init
  end

  valid_commands.uniq!

  @@default_command ||= 'restart'
  @@stop_start_delay ||= 0

  # This is neccessary because since ruby 1.9, the instance_methods method
  # returns an array of symbols instead of an array of strings which it did
  # in 1.8
  command = command.to_sym if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('1.9')

  if command == :default
    new.send(@@default_command)
  elsif valid_commands.include?(command)
    new.send(command)
  else
    puts "Usage: #$PROGRAM_NAME {#{valid_commands.sort.join('|')}}"; exit false
  end
end

.stop_start_delay(seconds) ⇒ Object

Set a delay in seconds between the call of the stop and the start method in the predefined restart method



59
60
61
# File 'lib/init/init.rb', line 59

def self.stop_start_delay(seconds)
  @@stop_start_delay = seconds
end

Instance Method Details

#restartObject

By default restart simply calls stop and then start



80
81
82
83
84
# File 'lib/init/init.rb', line 80

def restart
  stop
  sleep @@stop_start_delay
  start
end

#startObject

The start method needs to be implemented in a subclass



70
71
72
# File 'lib/init/init.rb', line 70

def start
  warn 'start method needs to be implemented'; exit false
end

#stopObject

The stop method needs to be implemented in a subclass



75
76
77
# File 'lib/init/init.rb', line 75

def stop
  warn 'stop method needs to be implemented'; exit false
end