summaryrefslogtreecommitdiff
path: root/lib/autorequire/only_when.rb
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2024-12-06 22:33:07 +0200
committerPaul Buetow <paul@buetow.org>2024-12-06 22:33:07 +0200
commitbfdcad7efca071374de0be3124dcc92deeab681e (patch)
tree6215f6fcf5287f69840d06ad8c009dab0945ef6d /lib/autorequire/only_when.rb
parent4a3629e42cb3e1fbca4356cf789a8e79043cdc51 (diff)
auto require
Diffstat (limited to 'lib/autorequire/only_when.rb')
-rw-r--r--lib/autorequire/only_when.rb37
1 files changed, 37 insertions, 0 deletions
diff --git a/lib/autorequire/only_when.rb b/lib/autorequire/only_when.rb
new file mode 100644
index 0000000..2700bee
--- /dev/null
+++ b/lib/autorequire/only_when.rb
@@ -0,0 +1,37 @@
+module RCM
+ # OnlyWhen (e.g. run on host foo)
+ class OnlyWhen
+ require 'socket'
+
+ def initialize
+ @conds = {}
+ end
+
+ def is(arg)
+ arg
+ end
+
+ def method_missing(method_name, *args, &block)
+ @conds[method_name] = args.first
+ end
+
+ def respond_to_missing?
+ true
+ end
+
+ def met?
+ return false if @conds.key?(:hostname) && Socket.gethostname != @conds[:hostname].to_s
+
+ true
+ end
+ end
+
+ # Add 'only_when' to DSL
+ class RCM
+ def only_when(&block)
+ conds = OnlyWhen.new
+ conds.instance_eval(&block)
+ @conds_met = conds.met?
+ end
+ end
+end