aboutsummaryrefslogtreecommitdiff
path: root/spec/support/augeas.rb
blob: 4912e1a22e8b814ada924a9d54e7e9ad14c6b164 (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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
require "delegate"

module Augeas
  class Change
    attr_reader :target, :name, :delimiter

    def initialize(target, name, value = nil, delimiter = "\"")
      @target = target
      @name = name
      @value = value
      @delimiter = delimiter
    end

    def to_s
      "#{action} #{delimiter}target[. = '#{target}']/#{name}#{delimiter}#{value}"
    end

    def hash
      [target, name, Change].hash
    end

    def eql?(other)
      other.is_a?(self.class) && [other.target, other.name] == [target, name]
    end

    private

    def action
      return "set" unless @value.nil?
      "rm "
    end

    def value
      " #{@value}" if @value
    end
  end

  class ChangeSet
    def initialize
      @set = {}
    end

    def <<(change)
      @set[change] = change
    end

    def to_a
      changes
    end

    def changes
      @set.values.map(&:to_s)
    end
  end

  class TargetedChangeSet < DelegateClass(ChangeSet)
    def initialize(target)
      @target = target
      super(ChangeSet.new)
    end

    def with(*args)
      self << Change.new(@target, *args)
      self
    end
  end
end