Class: Snaptoken::DiffLine

Inherits:
Object
  • Object
show all
Defined in:
lib/snaptoken/diff_line.rb

Constant Summary collapse

TYPES =
[:added, :removed, :unchanged, :folded]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, source, line_numbers) ⇒ DiffLine

Returns a new instance of DiffLine.



7
8
9
10
11
12
13
14
# File 'lib/snaptoken/diff_line.rb', line 7

def initialize(type, source, line_numbers)
  unless TYPES.include? type
    raise ArgumentError, "type must be one of: #{TYPES.inspect}"
  end
  @type = type
  @source = source.chomp
  @line_numbers = line_numbers
end

Instance Attribute Details

#line_numbersObject

Returns the value of attribute line_numbers.



4
5
6
# File 'lib/snaptoken/diff_line.rb', line 4

def line_numbers
  @line_numbers
end

#sourceObject

Returns the value of attribute source.



4
5
6
# File 'lib/snaptoken/diff_line.rb', line 4

def source
  @source
end

#typeObject

Returns the value of attribute type.



4
5
6
# File 'lib/snaptoken/diff_line.rb', line 4

def type
  @type
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/snaptoken/diff_line.rb', line 27

def blank?
  @source.strip.empty?
end

#cloneObject



16
17
18
# File 'lib/snaptoken/diff_line.rb', line 16

def clone
  Snaptoken::DiffLine.new(@type, @source.dup, @line_numbers.dup)
end

#line_numberObject



31
32
33
34
35
36
37
38
# File 'lib/snaptoken/diff_line.rb', line 31

def line_number
  case @type
  when :removed, :folded
    @line_numbers[0]
  when :added, :unchanged
    @line_numbers[1]
  end
end

#to_patch(options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/snaptoken/diff_line.rb', line 40

def to_patch(options = {})
  options[:unchanged_char] ||= " "

  case @type
  when :added
    "+#{@source}\n"
  when :removed
    "-#{@source}\n"
  when :unchanged
    "#{options[:unchanged_char]}#{@source}\n"
  when :folded
    raise "can't convert folded line to patch"
  end
end