diff options
Diffstat (limited to 'lib/dslkeywords')
| -rw-r--r-- | lib/dslkeywords/file.rb | 18 | ||||
| -rw-r--r-- | lib/dslkeywords/keyword.rb | 15 | ||||
| -rw-r--r-- | lib/dslkeywords/only_when.rb | 14 |
3 files changed, 30 insertions, 17 deletions
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 |
