summaryrefslogtreecommitdiff
path: root/lib/dslkeywords/only_when.rb
blob: 82dbc1fa5dd6a28ccef041d45a28b3da73455bea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
module RCM
  # OnlyWhen (e.g. run on host foo)
  class OnlyWhen
    require 'socket'

    def initialize = @conds = {}
    def is(arg) = arg
    def method_missing(method_name, *args, &block) = @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
      conds.instance_eval(&block)
      @conds_met = conds.met?
    end
  end
end