summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2025-02-15 09:25:57 +0200
committerPaul Buetow <paul@buetow.org>2025-02-15 09:25:57 +0200
commit6879b03b2735b082b913ab17e63857f464f53c93 (patch)
tree525b1b78983ba3b3d6f4e218daf0055edf7728a9 /lib
parent463f429166c5ace855998b433e865140f9b737ca (diff)
dsl keyword base class added
Diffstat (limited to 'lib')
-rw-r--r--lib/dsl.rb7
-rw-r--r--lib/dslkeywords/file.rb18
-rw-r--r--lib/dslkeywords/keyword.rb15
-rw-r--r--lib/dslkeywords/only_when.rb14
4 files changed, 34 insertions, 20 deletions
diff --git a/lib/dsl.rb b/lib/dsl.rb
index 413a34c..f3ccfc3 100644
--- a/lib/dsl.rb
+++ b/lib/dsl.rb
@@ -2,7 +2,8 @@ require_relative 'config'
require_relative 'options'
require_relative 'log'
-Dir["#{Dir.pwd}/lib/dslkeywords/*.rb"].each { |m| require m }
+require_relative 'dslkeywords/file'
+require_relative 'dslkeywords/only_when'
# Ruby Configiration Management system
module RCM
@@ -23,13 +24,13 @@ module RCM
def initialize(reset)
DSL.reset! if reset
- @id = "#{self.class}(#{@@rcm_counter += 1})"
+ @id = "DSL[#{@@rcm_counter += 1}]"
@conds_met = true
@scheduled = []
yield self if block_given?
end
- def to_s = "RCM #{@number}"
+ def to_s = @id
def evaluate! = @scheduled.each(&:evaluate!)
def <<(obj)
diff --git a/lib/dslkeywords/file.rb b/lib/dslkeywords/file.rb
index 1b99a91..50f753e 100644
--- a/lib/dslkeywords/file.rb
+++ b/lib/dslkeywords/file.rb
@@ -1,24 +1,18 @@
require 'erb'
require 'fileutils'
-require_relative '../options'
-require_relative '../log'
+require_relative 'keyword'
module RCM
# Managing files
- class File
- attr_reader :id, :path
-
- include Options
- include Log
+ class File < Keyword
+ attr_reader :path
def initialize(path)
- @id = "#{self.class}(#{path})"
+ super(path)
@path = path
end
- def to_s = id
-
def content(text = nil)
return @content if text.nil?
@@ -40,9 +34,7 @@ module RCM
def evaluate_ensure_line!
return write_content!(@ensure_line) unless ::File.file?(@path)
-
- lines = ::File.readlines(@path, chomp: true)
- return if lines.include?(@ensure_line)
+ return if ::File.readlines(@path, chomp: true).include?(@ensure_line)
::File.open(@path, 'a') do |fd|
fd.puts(@ensure_line)
diff --git a/lib/dslkeywords/keyword.rb b/lib/dslkeywords/keyword.rb
new file mode 100644
index 0000000..33d03db
--- /dev/null
+++ b/lib/dslkeywords/keyword.rb
@@ -0,0 +1,15 @@
+require_relative '../options'
+require_relative '../log'
+
+module RCM
+ # The base class of all DSL key words
+ class Keyword
+ attr_reader :id
+
+ include Options
+ include Log
+
+ def initialize(name) = @id = "#{self.class.to_s.sub('RCM::', '')}[#{name}]"
+ def to_s = @id
+ end
+end
diff --git a/lib/dslkeywords/only_when.rb b/lib/dslkeywords/only_when.rb
index 485fd42..f23c33e 100644
--- a/lib/dslkeywords/only_when.rb
+++ b/lib/dslkeywords/only_when.rb
@@ -1,9 +1,15 @@
+require 'socket'
+
+require_relative 'keyword'
+
module RCM
# OnlyWhen (e.g. run on host foo)
- class OnlyWhen
- require 'socket'
+ class OnlyWhen < Keyword
+ def initialize(dsl_id)
+ super(dsl_id)
+ @conds = {}
+ end
- def initialize = @conds = {}
def is(arg) = arg
def method_missing(method_name, *args) = @conds[method_name] = args.first
def respond_to_missing? = true
@@ -18,7 +24,7 @@ module RCM
# Add 'only_when' to DSL
class DSL
def only_when(&block)
- conds = OnlyWhen.new
+ conds = OnlyWhen.new(id)
conds.instance_eval(&block)
@conds_met = conds.met?
end