aboutsummaryrefslogtreecommitdiff
path: root/spec/support/augeas.rb
blob: 75486844d75ade8b8b0736556eea189d4cb0c674 (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
68
69
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 ==(other)
      other.is_a?(self.class) && [other.target, other.name] == [target, name]
    end
    alias_method :eql?, :==

    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)
      index = @set.index(change) || @set.length
      @set[index] = change
    end

    def to_a
      changes
    end

    def changes
      @set.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