From e37f1d7dfeb3ff40ee80c6e097cae30b9ec512c4 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Wed, 19 Feb 2025 01:29:01 +0200 Subject: more on this --- lib/dsl.rb | 4 +++- lib/dslkeywords/file.rb | 3 +++ lib/dslkeywords/given.rb | 32 ++++++++++++++++++++++++++++++++ lib/dslkeywords/keyword.rb | 6 ------ lib/dslkeywords/notify.rb | 3 +++ lib/dslkeywords/only_when.rb | 32 -------------------------------- lib/dslkeywords/resource.rb | 8 +++++++- 7 files changed, 48 insertions(+), 40 deletions(-) create mode 100644 lib/dslkeywords/given.rb delete mode 100644 lib/dslkeywords/only_when.rb (limited to 'lib') diff --git a/lib/dsl.rb b/lib/dsl.rb index 353bb15..4a2c4ad 100644 --- a/lib/dsl.rb +++ b/lib/dsl.rb @@ -1,9 +1,10 @@ require_relative 'config' require_relative 'options' require_relative 'log' +require_relative 'chained' require_relative 'dslkeywords/file' -require_relative 'dslkeywords/only_when' +require_relative 'dslkeywords/given' require_relative 'dslkeywords/notify' # Ruby Configiration Management system @@ -22,6 +23,7 @@ module RCM include Config include Options include Log + include Chained class DuplicateResource < StandardError; end diff --git a/lib/dslkeywords/file.rb b/lib/dslkeywords/file.rb index eeff82f..40daecb 100644 --- a/lib/dslkeywords/file.rb +++ b/lib/dslkeywords/file.rb @@ -3,6 +3,7 @@ require 'erb' require 'fileutils' require_relative 'resource' +require_relative '../chained' module RCM # Backup the file on change @@ -23,6 +24,7 @@ module RCM # Managing files class File < Resource include FileBackup + include Chained class UnsupportedOperation < StandardError; end @@ -46,6 +48,7 @@ module RCM def without(what) = @without_backup = validate_op(__method__, what, backup) == backup def from(what) = @from = validate_op(__method__, what, sourcefile, template) + # TODO: Delete this, as should not be required anymore due to Chained module def method_missing(method_name, *args) if %i[present absent directory backup sourcefile template].include?(method_name) method_name diff --git a/lib/dslkeywords/given.rb b/lib/dslkeywords/given.rb new file mode 100644 index 0000000..fa14e68 --- /dev/null +++ b/lib/dslkeywords/given.rb @@ -0,0 +1,32 @@ +require 'socket' + +require_relative 'keyword' + +module RCM + # Given (e.g. run on host foo) + class Given < Keyword + def initialize(dsl_id) + super(dsl_id) + @conds = {} + end + + def is(arg) = arg + def method_missing(method_name, *args) = @conds[method_name] = args.first + def respond_to_missing? = true + + def met? + return false if @conds.key?(:hostname) && Socket.gethostname != @conds[:hostname].to_s + + true + end + end + + # Add 'only_when' to DSL + class DSL + def given(&block) + conds = Given.new(id) + conds.instance_eval(&block) + @conds_met = conds.met? + end + end +end diff --git a/lib/dslkeywords/keyword.rb b/lib/dslkeywords/keyword.rb index ee3cfba..1cbda9f 100644 --- a/lib/dslkeywords/keyword.rb +++ b/lib/dslkeywords/keyword.rb @@ -15,11 +15,5 @@ module RCM def to_s = @id class KeywordError < StandardError; end - - def method_missing(method_name, *args) - raise KeywordError, "No such method: #{method_name}" - end - - def respond_to_missing? = true end end diff --git a/lib/dslkeywords/notify.rb b/lib/dslkeywords/notify.rb index e1d8a3d..6616cb0 100644 --- a/lib/dslkeywords/notify.rb +++ b/lib/dslkeywords/notify.rb @@ -2,10 +2,13 @@ require 'erb' require 'fileutils' require_relative 'resource' +require_relative '../chained' module RCM # Only to print out something class Notify < Resource + include Chained + def initialize(message) super(message) @message = message diff --git a/lib/dslkeywords/only_when.rb b/lib/dslkeywords/only_when.rb deleted file mode 100644 index f23c33e..0000000 --- a/lib/dslkeywords/only_when.rb +++ /dev/null @@ -1,32 +0,0 @@ -require 'socket' - -require_relative 'keyword' - -module RCM - # OnlyWhen (e.g. run on host foo) - class OnlyWhen < Keyword - def initialize(dsl_id) - super(dsl_id) - @conds = {} - end - - def is(arg) = arg - def method_missing(method_name, *args) = @conds[method_name] = args.first - def respond_to_missing? = true - - def met? - return false if @conds.key?(:hostname) && Socket.gethostname != @conds[:hostname].to_s - - true - end - end - - # Add 'only_when' to DSL - class DSL - def only_when(&block) - conds = OnlyWhen.new(id) - conds.instance_eval(&block) - @conds_met = conds.met? - end - end -end diff --git a/lib/dslkeywords/resource.rb b/lib/dslkeywords/resource.rb index 3aff069..2358ff5 100644 --- a/lib/dslkeywords/resource.rb +++ b/lib/dslkeywords/resource.rb @@ -26,6 +26,12 @@ module RCM return @requires if others.empty? others.flatten.each do |other| + unless other.include?('(') + # Convert "notify foo" to "notify('foo')" + resource, rest = other.split(' ', 2) + other = "#{resource}('#{rest}')" + end + info "Registered dependency on #{other}" @requires << other end @@ -74,7 +80,7 @@ module RCM def self.find(id) return @@resource_find_cache[id] if @@resource_find_cache.key?(id) - klass = Object.const_get("RCM::#{id.split('(').first.capitalize}") + klass = Object.const_get("RCM::#{id.split(/[( ]/).first.capitalize}") resource = ObjectSpace.each_object(klass).find { _1.id == id } raise NoSuchResourceObject, "Unable to find resource #{id}" if resource.nil? -- cgit v1.2.3