Method: Constancy::Diff#initialize

Defined in:
lib/constancy/diff.rb

#initialize(target:, local:, remote:, mode:) ⇒ Diff

Returns a new instance of Diff.



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
# File 'lib/constancy/diff.rb', line 5

def initialize(target:, local:, remote:, mode:)
  @target = target
  @local = local
  @remote = remote
  @mode = mode

  @all_keys = (@local.keys + @remote.keys).sort.uniq

  @diff =
    @all_keys.collect do |key|
      excluded = false
      op = :noop
      if @remote.has_key?(key) and not @local.has_key?(key)
        case @mode
        when :push
          op = @target.delete? ? :delete : :ignore
        when :pull
          op = :create
        end
      elsif @local.has_key?(key) and not @remote.has_key?(key)
        case @mode
        when :push
          op = :create
        when :pull
          op = @target.delete? ? :delete : :ignore
        end
      else
        if @remote[key] == @local[key]
          op = :noop
        else
          op = :update
        end
      end

      consul_key = [@target.prefix, key].compact.join("/").squeeze("/")

      if @target.exclude.include?(key) or @target.exclude.include?(consul_key)
        op = :ignore
        excluded = true
      end

      filename =
        case @target.type
        when :dir then File.join(@target.base_path, key)
        when :file then @target.base_path
        end

      display_filename =
        case @target.type
        when :dir then File.join(@target.base_path, key).trim_path
        when :file then "#{@target.base_path.trim_path}#{':'.gray}#{key.cyan}"
        end

      OpenStruct.new(
        op: op,
        excluded: excluded,
        relative_path: key,
        filename: filename,
        display_filename: display_filename,
        consul_key: consul_key,
        local_content: @local[key],
        remote_content: @remote[key],
      )
    end
end