Class: Canon::Diff::DiffBlock

Inherits:
Object
  • Object
show all
Defined in:
lib/canon/diff/diff_block.rb

Overview

Represents a contiguous block of changes in a diff A diff block is a run of consecutive change lines (-, +, !)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_idx:, end_idx:, types: [], diff_lines: [], diff_node: nil) ⇒ DiffBlock

Returns a new instance of DiffBlock.



11
12
13
14
15
16
17
18
19
# File 'lib/canon/diff/diff_block.rb', line 11

def initialize(start_idx:, end_idx:, types: [], diff_lines: [],
diff_node: nil)
  @start_idx = start_idx
  @end_idx = end_idx
  @types = types
  @diff_lines = diff_lines
  @diff_node = diff_node
  @normative = nil
end

Instance Attribute Details

#diff_linesObject (readonly)

Returns the value of attribute diff_lines.



8
9
10
# File 'lib/canon/diff/diff_block.rb', line 8

def diff_lines
  @diff_lines
end

#diff_nodeObject (readonly)

Returns the value of attribute diff_node.



8
9
10
# File 'lib/canon/diff/diff_block.rb', line 8

def diff_node
  @diff_node
end

#end_idxObject (readonly)

Returns the value of attribute end_idx.



8
9
10
# File 'lib/canon/diff/diff_block.rb', line 8

def end_idx
  @end_idx
end

#normativeObject

Returns the value of attribute normative.



9
10
11
# File 'lib/canon/diff/diff_block.rb', line 9

def normative
  @normative
end

#start_idxObject (readonly)

Returns the value of attribute start_idx.



8
9
10
# File 'lib/canon/diff/diff_block.rb', line 8

def start_idx
  @start_idx
end

#typesObject (readonly)

Returns the value of attribute types.



8
9
10
# File 'lib/canon/diff/diff_block.rb', line 8

def types
  @types
end

Instance Method Details

#==(other) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/canon/diff/diff_block.rb', line 68

def ==(other)
  other.is_a?(DiffBlock) &&
    start_idx == other.start_idx &&
    end_idx == other.end_idx &&
    types == other.types &&
    diff_lines == other.diff_lines &&
    diff_node == other.diff_node
end

#formatting?Boolean

Returns true if all lines in this block are formatting-only.

Returns:

  • (Boolean)

    true if all lines in this block are formatting-only



46
47
48
49
50
# File 'lib/canon/diff/diff_block.rb', line 46

def formatting?
  return false if diff_lines.empty?

  diff_lines.all?(&:formatting?)
end

#includes_type?(type) ⇒ Boolean

Check if this block contains a specific type of change

Returns:

  • (Boolean)


53
54
55
# File 'lib/canon/diff/diff_block.rb', line 53

def includes_type?(type)
  types.include?(type)
end

#informative?Boolean

Returns true if this block represents an informative-only difference.

Returns:

  • (Boolean)

    true if this block represents an informative-only difference



41
42
43
# File 'lib/canon/diff/diff_block.rb', line 41

def informative?
  !normative?
end

#normative?Boolean

Returns true if this block represents a normative difference.

Returns:

  • (Boolean)

    true if this block represents a normative difference



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/canon/diff/diff_block.rb', line 27

def normative?
  return @normative unless @normative.nil?

  # If we have a diff_node, use its normative status
  return diff_node.normative? if diff_node

  # If we have diff_lines, check if any are normative
  return diff_lines.any?(&:normative?) if diff_lines&.any?

  # Default to true (treat as normative if we can't determine)
  true
end

#sizeObject

Number of lines in this block



22
23
24
# File 'lib/canon/diff/diff_block.rb', line 22

def size
  end_idx - start_idx + 1
end

#to_hObject



57
58
59
60
61
62
63
64
65
66
# File 'lib/canon/diff/diff_block.rb', line 57

def to_h
  {
    start_idx: start_idx,
    end_idx: end_idx,
    types: types,
    diff_lines: diff_lines.map(&:to_h),
    diff_node: diff_node&.to_h,
    normative: normative?,
  }
end