Class: Dotenv::Diff
- Inherits:
-
Object
- Object
- Dotenv::Diff
- Defined in:
- lib/dotenv/diff.rb
Overview
A diff between multiple states of ENV.
Instance Attribute Summary collapse
-
#a ⇒ Object
readonly
The initial state.
-
#b ⇒ Object
readonly
The final or current state.
Instance Method Summary collapse
-
#added ⇒ Object
Return a Hash of keys added with their new values.
-
#any? ⇒ Boolean
Returns true if any keys were added, removed, or changed.
-
#changed ⇒ Object
Returns of Hash of keys changed with an array of their previous and new values.
-
#env ⇒ Object
Returns a Hash of all added, changed, and removed keys and their new values.
-
#initialize(a: snapshot, b: ENV) {|diff| ... } ⇒ Diff
constructor
Create a new diff.
-
#removed ⇒ Object
Returns a Hash of keys removed with their previous values.
Constructor Details
#initialize(a: snapshot, b: ENV) {|diff| ... } ⇒ Diff
Create a new diff. If given a block, the state of ENV after the block will be preserved as the final state for comparison. Otherwise, the current ENV will be the final state.
16 17 18 19 20 21 |
# File 'lib/dotenv/diff.rb', line 16 def initialize(a: snapshot, b: ENV, &block) @a, @b = a, b block&.call self ensure @b = snapshot if block end |
Instance Attribute Details
#a ⇒ Object (readonly)
The initial state
5 6 7 |
# File 'lib/dotenv/diff.rb', line 5 def a @a end |
#b ⇒ Object (readonly)
The final or current state
8 9 10 |
# File 'lib/dotenv/diff.rb', line 8 def b @b end |
Instance Method Details
#added ⇒ Object
Return a Hash of keys added with their new values
24 25 26 |
# File 'lib/dotenv/diff.rb', line 24 def added b.slice(*(b.keys - a.keys)) end |
#any? ⇒ Boolean
Returns true if any keys were added, removed, or changed
46 47 48 |
# File 'lib/dotenv/diff.rb', line 46 def any? [added, removed, changed].any?(&:any?) end |
#changed ⇒ Object
Returns of Hash of keys changed with an array of their previous and new values
34 35 36 37 38 |
# File 'lib/dotenv/diff.rb', line 34 def changed (b.slice(*a.keys).to_a - a.to_a).map do |(k, v)| [k, [a[k], v]] end.to_h end |
#env ⇒ Object
Returns a Hash of all added, changed, and removed keys and their new values
41 42 43 |
# File 'lib/dotenv/diff.rb', line 41 def env b.slice(*(added.keys + changed.keys)).merge(removed.transform_values { |v| nil }) end |
#removed ⇒ Object
Returns a Hash of keys removed with their previous values
29 30 31 |
# File 'lib/dotenv/diff.rb', line 29 def removed a.slice(*(a.keys - b.keys)) end |