Class: Philiprehberger::EnvDiff::Diff
- Inherits:
-
Object
- Object
- Philiprehberger::EnvDiff::Diff
- Defined in:
- lib/philiprehberger/env_diff/diff.rb
Overview
Represents the result of comparing two sets of environment variables.
Instance Attribute Summary collapse
-
#added ⇒ Array<String>
readonly
Keys present in target but not source.
-
#changed ⇒ Hash{String => Hash}
readonly
Keys with different values ({ key => { source:, target: } }).
-
#removed ⇒ Array<String>
readonly
Keys present in source but not target.
-
#unchanged ⇒ Array<String>
readonly
Keys with identical values in both sets.
Instance Method Summary collapse
-
#changed? ⇒ Boolean
Whether there are any differences between source and target.
-
#filter(pattern:) ⇒ Diff
Return a new Diff containing only keys matching the given pattern.
-
#initialize(source, target) ⇒ Diff
constructor
Build a diff from two environment hashes.
-
#stats ⇒ Hash
Statistics about the diff.
-
#summary(mask: []) ⇒ String
Human-readable multiline summary of all differences.
-
#to_h ⇒ Hash
Structured hash representation of the diff.
-
#to_json(*_args) ⇒ String
JSON serialization of the structured hash.
Constructor Details
#initialize(source, target) ⇒ Diff
Build a diff from two environment hashes.
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
#added ⇒ Array<String> (readonly)
Returns keys present in target but not source.
10 11 12 |
# File 'lib/philiprehberger/env_diff/diff.rb', line 10 def added @added end |
#changed ⇒ Hash{String => Hash} (readonly)
Returns keys with different values ({ key => { source:, target: } }).
16 17 18 |
# File 'lib/philiprehberger/env_diff/diff.rb', line 16 def changed @changed end |
#removed ⇒ Array<String> (readonly)
Returns keys present in source but not target.
13 14 15 |
# File 'lib/philiprehberger/env_diff/diff.rb', line 13 def removed @removed end |
#unchanged ⇒ Array<String> (readonly)
Returns 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.
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.
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 |
#stats ⇒ Hash
Statistics about the diff.
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.
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_h ⇒ Hash
Structured hash representation of the diff.
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.
68 69 70 |
# File 'lib/philiprehberger/env_diff/diff.rb', line 68 def to_json(*_args) JSON.generate(to_h) end |