From 7c23b27007ca62ff545411aa3d1200fce4eec8c9 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Fri, 6 Dec 2024 23:52:43 +0200 Subject: refactor --- lib/dslkeywords/only_when.rb | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 lib/dslkeywords/only_when.rb (limited to 'lib/dslkeywords/only_when.rb') diff --git a/lib/dslkeywords/only_when.rb b/lib/dslkeywords/only_when.rb new file mode 100644 index 0000000..2700bee --- /dev/null +++ b/lib/dslkeywords/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 -- cgit v1.2.3