Class: Diff::Display::Line

Inherits:
String
  • Object
show all
Defined in:
lib/diff/display/data_structure.rb

Overview

Every line from the passed in diff gets transformed into an instance of one of line Line class’s subclasses. One subclass exists for each line type in a diff. As such there is an AddLine class for added lines, a RemLine class for removed lines, an UnModLine class for lines which remain unchanged and a SepLine class which represents all the lines that aren’t part of the diff.

Direct Known Subclasses

AddLine, HeaderLine, NonewlineLine, RemLine, SepLine, UnModLine

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(line, old_number = nil, new_number = nil) ⇒ Line

Returns a new instance of Line.



77
78
79
80
81
# File 'lib/diff/display/data_structure.rb', line 77

def initialize(line, old_number = nil, new_number = nil)
  super(line)
  @old_number, @new_number = old_number, new_number
  @inline = false
end

Instance Attribute Details

#new_numberObject (readonly)

Returns the value of attribute new_number.



82
83
84
# File 'lib/diff/display/data_structure.rb', line 82

def new_number
  @new_number
end

#old_numberObject (readonly)

Returns the value of attribute old_number.



82
83
84
# File 'lib/diff/display/data_structure.rb', line 82

def old_number
  @old_number
end

Class Method Details

.add(line, line_number, inline = false) ⇒ Object



56
57
58
# File 'lib/diff/display/data_structure.rb', line 56

def add(line, line_number, inline = false)
  AddLine.new(line, line_number, inline)
end

.header(line) ⇒ Object



72
73
74
# File 'lib/diff/display/data_structure.rb', line 72

def header(line)
  HeaderLine.new(line)
end

.nonewline(line) ⇒ Object



68
69
70
# File 'lib/diff/display/data_structure.rb', line 68

def nonewline(line)
  NonewlineLine.new(line)
end

.rem(line, line_number, inline = false) ⇒ Object



60
61
62
# File 'lib/diff/display/data_structure.rb', line 60

def rem(line, line_number, inline = false)
  RemLine.new(line, line_number, inline)
end

.unmod(line, old_number, new_number) ⇒ Object



64
65
66
# File 'lib/diff/display/data_structure.rb', line 64

def unmod(line, old_number, new_number)
  UnModLine.new(line, old_number, new_number)
end

Instance Method Details

#expand_inline_changes_with(prefix, postfix) ⇒ Object

Expand any inline changes with prefix and postfix



102
103
104
105
106
107
108
# File 'lib/diff/display/data_structure.rb', line 102

def expand_inline_changes_with(prefix, postfix)
  return self.dup unless inline_changes?
  str = self.dup
  str.sub!('\\0', prefix.to_s)
  str.sub!('\\1', postfix.to_s)
  str
end

#identifierObject



84
85
86
# File 'lib/diff/display/data_structure.rb', line 84

def identifier
  self.class.name[/\w+$/].gsub(/Line$/, "").downcase.to_sym
end

#inline_changes?Boolean

Returns:

  • (Boolean)


88
89
90
91
# File 'lib/diff/display/data_structure.rb', line 88

def inline_changes?
  # Is set in the AddLine+RemLine subclasses
  @inline
end

#inspectObject



110
111
112
# File 'lib/diff/display/data_structure.rb', line 110

def inspect
  %Q{#<#{self.class.name} [#{old_number.inspect}-#{new_number.inspect}] "#{self}">}
end

#segmentsObject

returns the prefix, middle and postfix parts of a line with inline changes



94
95
96
97
98
99
# File 'lib/diff/display/data_structure.rb', line 94

def segments
  return self.dup unless inline_changes?
  prefix, changed = self.dup.split('\\0')
  changed, postfix = changed.split('\\1')
  [prefix, changed, postfix]
end