Class: Aef::Init

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

Overview

Clean and simple *nix init scripts with Ruby

Constant Summary collapse

VARIABLE_PREFIX =
'aef_init_variable'.freeze
VERSION =

The currently loaded library version

Using Semantic Versioning (2.0.0-rc.1) rules

See Also:

'2.1.0'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *arguments) ⇒ Object (protected)

Makes lazy-evaluated class variables available in the instance



192
193
194
195
196
197
198
# File 'lib/aef/init.rb', line 192

def method_missing(name, *arguments)
  if arguments.empty? && self.class.set?(name)
    self.class.get(name)
  else
    super
  end
end

Class Attribute Details

.default_command(command = false) ⇒ Object

Note:

This used to be implicitly set to “restart” but in practice caused far too much unwanted service restarts, so it is now not enabled by default.

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



58
59
60
61
62
63
64
# File 'lib/aef/init.rb', line 58

def default_command(command = false)
  if command.equal?(false)
    @default_command
  else
    @default_command = command.to_sym
  end
end

.stop_start_delay(seconds = false) ⇒ Float

The delay in seconds between the call of the stop and the start method in the predefined restart method.

Parameters:

  • seconds (Numeric, false) (defaults to: false)

    time in seconds, if false is given, the value will be returned and not modified.

Returns:

  • (Float)

    time in seconds



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

def stop_start_delay(seconds = false)
  if seconds.equal?(false)
    @stop_start_delay ||= 0.0
  else
    @stop_start_delay = seconds.to_f
  end
end

Class Method Details

.find_variable_recursive(name) ⇒ Class, false (protected)

Finds out which class in the class hierarchy defined a given variable, if at all.

Returns:

  • (Class, false)

    if self contains the given variable self is returned, otherwise false



149
150
151
152
153
154
155
156
157
# File 'lib/aef/init.rb', line 149

def find_variable_recursive(name)
  if instance_variable_defined?("@#{VARIABLE_PREFIX}_#{name}")
    self
  elsif superclass.ancestors.include?(Init)
    superclass.find_variable_recursive(name)
  else
    false
  end
end

.get(name) ⇒ Object

Evaluates a lazy-evaluated class variable.

Returns:

  • (Object)

    the result of the evaluated block



133
134
135
136
137
138
139
140
# File 'lib/aef/init.rb', line 133

def get(name)
  if klass = find_variable_recursive(name)
    block = klass.instance_variable_get("@#{VARIABLE_PREFIX}_#{name}")
    instance_eval &block
  else
    nil
  end
end

.method_missing(name, *arguments) ⇒ Object (protected)

Makes lazy-evaluated class variables available in public interface



160
161
162
163
164
165
166
# File 'lib/aef/init.rb', line 160

def method_missing(name, *arguments)
  if arguments.empty? && set?(name)
    get(name)
  else
    super
  end
end

.parseObject

Call this to begin commandline parse.

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/aef/init.rb', line 91

def parse
  command = ARGV.shift || :default
    
  valid_commands = []
    
  ancestors.each do |klass|
    valid_commands += klass.public_instance_methods(false)
    break if klass == Aef::Init
  end

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

.set(name, &block) ⇒ Proc

Defines a lazy-evaluated class variable.

Returns:

  • (Proc)

    the given block



119
120
121
# File 'lib/aef/init.rb', line 119

def set(name, &block)
  instance_variable_set("@#{VARIABLE_PREFIX}_#{name}", block)
end

.set?(name) ⇒ true, false

Checks if a lazy-evaluated class variable is defined.

Returns:

  • (true, false)

    true if the variable is defined. false otherwise.



126
127
128
# File 'lib/aef/init.rb', line 126

def set?(name)
  !!find_variable_recursive(name)
end

Instance Method Details

#restartObject

By default restart simply calls stop and then start



183
184
185
186
187
# File 'lib/aef/init.rb', line 183

def restart
  stop
  sleep self.class.stop_start_delay
  start
end

#startObject

The start method needs to be implemented in a subclass



171
172
173
174
# File 'lib/aef/init.rb', line 171

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

#stopObject

The stop method needs to be implemented in a subclass



177
178
179
180
# File 'lib/aef/init.rb', line 177

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