Class: Dcc::Diff::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/dcc/diff/result.rb

Overview

Result of comparing two DCC object trees.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(changes:) ⇒ Result

Returns a new instance of Result.



9
10
11
# File 'lib/dcc/diff/result.rb', line 9

def initialize(changes:)
  @changes = changes
end

Instance Attribute Details

#changesObject (readonly)

Returns the value of attribute changes.



7
8
9
# File 'lib/dcc/diff/result.rb', line 7

def changes
  @changes
end

Instance Method Details

#additionsObject



13
14
15
# File 'lib/dcc/diff/result.rb', line 13

def additions
  changes.select(&:added?)
end

#empty?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/dcc/diff/result.rb', line 25

def empty?
  changes.empty?
end

#modificationsObject



21
22
23
# File 'lib/dcc/diff/result.rb', line 21

def modifications
  changes.select(&:changed?)
end

#removalsObject



17
18
19
# File 'lib/dcc/diff/result.rb', line 17

def removals
  changes.select(&:removed?)
end

#to_json(*_args) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/dcc/diff/result.rb', line 39

def to_json(*_args)
  require "json"
  ::JSON.pretty_generate(
    additions: additions.map { |c| { path: c.path, value: c.after } },
    removals: removals.map { |c| { path: c.path, value: c.before } },
    modifications: modifications.map { |c| { path: c.path, before: c.before, after: c.after } },
  )
end

#to_sObject



29
30
31
32
33
34
35
36
37
# File 'lib/dcc/diff/result.rb', line 29

def to_s
  if empty?
    "(no structural changes)"
  else
    header = "#{changes.size} change(s): " \
            "#{additions.size} added, #{removals.size} removed, #{modifications.size} changed"
    ([header] + changes.map(&:to_s)).join("\n")
  end
end

#to_yaml(*_args) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/dcc/diff/result.rb', line 48

def to_yaml(*_args)
  require "yaml"
  {
    additions: additions.map { |c| { path: c.path, value: c.after } },
    removals: removals.map { |c| { path: c.path, value: c.before } },
    modifications: modifications.map { |c| { path: c.path, before: c.before, after: c.after } },
  }.to_yaml
end