Class: Yadtfp::Outputters::Pretty

Inherits:
Object
  • Object
show all
Includes:
Diffable
Defined in:
lib/yadtfp/outputters/pretty.rb

Constant Summary collapse

TYPES =
[ 'c', 'a', 'd' ]
DiffValue =
->(value) do
  value.is_a?(::Array) ? value.to_enum.with_index(1).map { |v, i| "\xA#{"\x20" * 5}#{i}.\x20#{v}" }.join : value
end

Instance Attribute Summary

Attributes included from Diffable

#diff

Instance Method Summary collapse

Methods included from Diffable

#appends, #by_type, #changes, #deletes, #initialize

Instance Method Details

#body(type) ⇒ Object

Returns body for given type

Type can be one of either Change c, Append a or Delete d:

Returns empty string if type is not one of `TYPES ('c', 'a' or 'd')

Raises TypeValuesMismatchError if type is 'd' but diff contains nil left Raises TypeValuesMismatchError if type is 'a' but diff contains nil right



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/yadtfp/outputters/pretty.rb', line 60

def body(type)
  return '' if !TYPES.include?(type)

  diffs = case type
          when 'c'
            changes
          when 'a'
            appends
          when 'd'
            deletes
          end


  if diffs.any? { |d| d[:lvalue].nil? } && type == 'd'
    raise Yadtfp::Outputters::TypeValuesMismatchError, "Unexpected Left value"
  end


  if diffs.any? { |d| d[:rvalue].nil? } && type == 'a'
    raise Yadtfp::Outputters::TypeValuesMismatchError, "Unexpected Right value"
  end


  body = diffs.to_enum.with_index(1).inject("") do |body, (diff, index)|
    body << "\xA#{index}.\x20Path:\x20#{diff[:path]}"
    body << "\xA#{repeatc("\x20", 3)}Left:\x20#{DiffValue.call(diff[:lvalue])}" if diff.key?(:lvalue)
    body << "\xA#{repeatc("\x20", 3)}Right:\x20#{DiffValue.call(diff[:rvalue])}" if diff.key?(:rvalue)
    body << "\xA"
  end

  body << "\xA"
end

#header(type) ⇒ Object

Returns header information for given type

Type can be one of either Change c, Append a or Delete d:

Returns empty string if type is not one of TYPES ('c', 'a' or 'd')



37
38
39
40
41
42
43
44
45
46
# File 'lib/yadtfp/outputters/pretty.rb', line 37

def header(type)
  return '' if !TYPES.include?(type)

  header = "Changes (Replace left value with right value)"  if type === 'c'
  header = "Appends (Add values to left)"                   if type === 'a'
  header = "Deletes (Remove values from left)"              if type === 'd'

  header << "\xA#{ "-" * header.length }\xA"
  header
end


22
23
24
25
26
# File 'lib/yadtfp/outputters/pretty.rb', line 22

def print
  content = TYPES.map { |type| header(type) + body(type) }.join ''

  $stdout.print "#{content}#{repeatc("\xA", 1)}#{summary}"
end

#repeatc(char, length) ⇒ Object (private)

Repeats character char for length number of times



122
123
124
# File 'lib/yadtfp/outputters/pretty.rb', line 122

def repeatc(char, length)
  char * length
end

#summaryObject

Returns summary of diff

Prints total number of differences altogether including changes, appends and deletes.



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/yadtfp/outputters/pretty.rb', line 100

def summary
  summary = "Summary of differences"
  summary << "\xA#{ "-" * summary.length }\xA"

  summary << "Number of differences: #{@diff.length}"
  summary << "\xA Changes 'c': #{changes.length}" if changes.length > 0
  summary << "\xA Appends 'a': #{appends.length}" if appends.length > 0
  summary << "\xA Deletes 'd': #{deletes.length}" if deletes.length > 0
  summary << "\xA"

  summary
end