Class: Diff::LCS::Block

Inherits:
Data
  • Object
show all
Defined in:
lib/diff/lcs/block.rb,
lib/diff/lcs/block.rb

Overview

A block is an operation removing, adding, or changing a group of items, a list of changes, where each change adds or deletes a single item.

Used by bin/ldiff.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#changesObject (readonly)

Returns the value of attribute changes

Returns:

  • (Object)

    the current value of changes



3
4
5
# File 'lib/diff/lcs/block.rb', line 3

def changes
  @changes
end

#insertObject (readonly)

Returns the value of attribute insert

Returns:

  • (Object)

    the current value of insert



3
4
5
# File 'lib/diff/lcs/block.rb', line 3

def insert
  @insert
end

#removeObject (readonly)

Returns the value of attribute remove

Returns:

  • (Object)

    the current value of remove



3
4
5
# File 'lib/diff/lcs/block.rb', line 3

def remove
  @remove
end

Class Method Details

.from_chunk(chunk) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/diff/lcs/block.rb', line 10

def self.from_chunk(chunk)
  changes, insert, remove = [], [], []

  chunk.each do
    changes << _1
    remove << _1 if _1.deleting?
    insert << _1 if _1.adding?
  end

  new(changes: changes.freeze, remove: remove.freeze, insert: insert.freeze)
end

Instance Method Details

#diff_sizeObject



28
# File 'lib/diff/lcs/block.rb', line 28

def diff_size = insert.size - remove.size

#opObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/diff/lcs/block.rb', line 30

def op
  case [remove, insert]
  # Unchanged
  in [[], []] then "^"
  # Delete
  in [_, []] then "-"
  # Insert
  in [[], _] then "+"
  # Conflict
  in [_, _] then "!"
  end
end