Class: Philiprehberger::EnvDiff::Diff

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/env_diff/diff.rb

Overview

Represents the result of comparing two sets of environment variables.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, target) ⇒ Diff

Build a diff from two environment hashes.

Parameters:

  • source (Hash)

    the baseline environment hash

  • target (Hash)

    the environment hash to compare against



25
26
27
28
29
30
31
32
# File 'lib/philiprehberger/env_diff/diff.rb', line 25

def initialize(source, target)
  @source = source
  @target = target
  @added = (target.keys - source.keys).sort
  @removed = (source.keys - target.keys).sort
  @changed = build_changed(source, target)
  @unchanged = build_unchanged(source, target)
end

Instance Attribute Details

#addedArray<String> (readonly)

Returns keys present in target but not source.

Returns:

  • (Array<String>)

    keys present in target but not source



10
11
12
# File 'lib/philiprehberger/env_diff/diff.rb', line 10

def added
  @added
end

#changedHash{String => Hash} (readonly)

Returns keys with different values ({ key => { source:, target: } }).

Returns:

  • (Hash{String => Hash})

    keys with different values ({ key => { source:, target: } })



16
17
18
# File 'lib/philiprehberger/env_diff/diff.rb', line 16

def changed
  @changed
end

#removedArray<String> (readonly)

Returns keys present in source but not target.

Returns:

  • (Array<String>)

    keys present in source but not target



13
14
15
# File 'lib/philiprehberger/env_diff/diff.rb', line 13

def removed
  @removed
end

#unchangedArray<String> (readonly)

Returns keys with identical values in both sets.

Returns:

  • (Array<String>)

    keys with identical values in both sets



19
20
21
# File 'lib/philiprehberger/env_diff/diff.rb', line 19

def unchanged
  @unchanged
end

Instance Method Details

#changed?Boolean

Whether there are any differences between source and target.

Returns:

  • (Boolean)

    true if added, removed, or changed are non-empty



37
38
39
# File 'lib/philiprehberger/env_diff/diff.rb', line 37

def changed?
  !@added.empty? || !@removed.empty? || !@changed.empty?
end

#filter(pattern:) ⇒ Diff

Return a new Diff containing only keys matching the given pattern.

Parameters:

  • pattern (Regexp)

    regex pattern to match keys against

Returns:

  • (Diff)

    filtered diff



76
77
78
79
80
# File 'lib/philiprehberger/env_diff/diff.rb', line 76

def filter(pattern:)
  filtered_source = @source.select { |k, _| k.match?(pattern) }
  filtered_target = @target.select { |k, _| k.match?(pattern) }
  Diff.new(filtered_source, filtered_target)
end

#statsHash

Statistics about the diff.

Returns:

  • (Hash)

    counts of added, removed, changed, unchanged, and total keys



85
86
87
88
89
90
91
92
93
# File 'lib/philiprehberger/env_diff/diff.rb', line 85

def stats
  {
    added: @added.length,
    removed: @removed.length,
    changed: @changed.length,
    unchanged: @unchanged.length,
    total: @added.length + @removed.length + @changed.length + @unchanged.length
  }
end

#summary(mask: []) ⇒ String

Human-readable multiline summary of all differences.

Parameters:

  • mask (Array<String, Regexp>) (defaults to: [])

    patterns for keys whose values should be masked

Returns:

  • (String)

    formatted summary



45
46
47
48
49
50
51
# File 'lib/philiprehberger/env_diff/diff.rb', line 45

def summary(mask: [])
  lines = []
  append_added(lines, mask)
  append_removed(lines, mask)
  append_changed(lines, mask)
  lines.empty? ? 'No differences found.' : lines.join("\n")
end

#to_hHash

Structured hash representation of the diff.

Returns:

  • (Hash)

    with :added, :removed, :changed, :unchanged keys



56
57
58
59
60
61
62
63
# File 'lib/philiprehberger/env_diff/diff.rb', line 56

def to_h
  {
    added: @added.to_h { |k| [k, @target[k]] },
    removed: @removed.to_h { |k| [k, @source[k]] },
    changed: @changed.transform_values { |v| { source: v[:source], target: v[:target] } },
    unchanged: @unchanged.to_h { |k| [k, @source[k]] }
  }
end

#to_json(*_args) ⇒ String

JSON serialization of the structured hash.

Returns:

  • (String)

    JSON string



68
69
70
# File 'lib/philiprehberger/env_diff/diff.rb', line 68

def to_json(*_args)
  JSON.generate(to_h)
end