Class: Canon::TreeDiff::Operations::Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/canon/tree_diff/operations/operation.rb

Overview

Base class for all tree diff operations

Represents a high-level semantic operation detected from tree matching. Each operation has a type, affected nodes, and metadata.

Examples:

operation = Operation.new(
  type: :insert,
  node: new_node,
  parent: parent_node,
  position: 2
)

Constant Summary collapse

TYPES =

Operation types based on XDiff and JATS-diff research

%i[
  insert
  delete
  update
  move
  merge
  split
  upgrade
  downgrade
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, **metadata) ⇒ Operation

Initialize a new operation

Parameters:

  • type (Symbol)

    Operation type (must be in TYPES)

  • metadata (Hash)

    Operation-specific metadata



38
39
40
41
42
43
44
45
# File 'lib/canon/tree_diff/operations/operation.rb', line 38

def initialize(type:, **)
  unless TYPES.include?(type)
    raise ArgumentError, "Invalid operation type: #{type}"
  end

  @type = type
  @metadata = 
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



32
33
34
# File 'lib/canon/tree_diff/operations/operation.rb', line 32

def 
  @metadata
end

#typeObject (readonly)

Returns the value of attribute type.



32
33
34
# File 'lib/canon/tree_diff/operations/operation.rb', line 32

def type
  @type
end

Instance Method Details

#==(other) ⇒ Boolean

Check if two operations are equal

Parameters:

Returns:

  • (Boolean)


67
68
69
70
71
# File 'lib/canon/tree_diff/operations/operation.rb', line 67

def ==(other)
  return false unless other.is_a?(Operation)

  type == other.type &&  == other.
end

#[](key) ⇒ Object?

Get a metadata value

Parameters:

  • key (Symbol)

    Metadata key

Returns:

  • (Object, nil)

    Metadata value



59
60
61
# File 'lib/canon/tree_diff/operations/operation.rb', line 59

def [](key)
  @metadata[key]
end

#inspectString

Detailed string representation

Returns:

  • (String)


83
84
85
86
87
88
# File 'lib/canon/tree_diff/operations/operation.rb', line 83

def inspect
   = @metadata.map do |k, v|
    "#{k}: #{v.inspect}"
  end.join(", ")
  "#<#{self.class.name} type=#{type} #{}>"
end

#to_sString

String representation

Returns:

  • (String)


76
77
78
# File 'lib/canon/tree_diff/operations/operation.rb', line 76

def to_s
  "Operation(#{type})"
end

#type?(type) ⇒ Boolean

Check if operation is a specific type

Parameters:

  • type (Symbol)

    Type to check

Returns:

  • (Boolean)


51
52
53
# File 'lib/canon/tree_diff/operations/operation.rb', line 51

def type?(type)
  @type == type
end