Class: Aef::Init
- Inherits:
-
Object
- Object
- Aef::Init
- 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
'2.1.0'.freeze
Class Attribute Summary collapse
-
.default_command(command = false) ⇒ Object
The default command to be called if no command is specified on the commandline.
-
.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.
Class Method Summary collapse
-
.find_variable_recursive(name) ⇒ Class, false
protected
Finds out which class in the class hierarchy defined a given variable, if at all.
-
.get(name) ⇒ Object
Evaluates a lazy-evaluated class variable.
-
.method_missing(name, *arguments) ⇒ Object
protected
Makes lazy-evaluated class variables available in public interface.
-
.parse ⇒ Object
Call this to begin commandline parse.
-
.set(name, &block) ⇒ Proc
Defines a lazy-evaluated class variable.
-
.set?(name) ⇒ true, false
Checks if a lazy-evaluated class variable is defined.
Instance Method Summary collapse
-
#method_missing(name, *arguments) ⇒ Object
protected
Makes lazy-evaluated class variables available in the instance.
-
#restart ⇒ Object
By default restart simply calls stop and then start.
-
#start ⇒ Object
The start method needs to be implemented in a subclass.
-
#stop ⇒ Object
The stop method needs to be implemented in a subclass.
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
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.
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.
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.
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 |
.parse ⇒ Object
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.
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.
126 127 128 |
# File 'lib/aef/init.rb', line 126 def set?(name) !!find_variable_recursive(name) end |
Instance Method Details
#restart ⇒ Object
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 |
#start ⇒ Object
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 |
#stop ⇒ Object
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 |