Class: Hashira::Duplication::Delta

Inherits:
Object
  • Object
show all
Defined in:
lib/hashira/duplication/delta.rb

Constant Summary collapse

ADVICE =
{
  identical: "byte-for-byte identical — extract a shared method and call it from each site.",
  literal: "differs only in literal values — extract a method, pass them as arguments.",
  message: "differs only in the receiver or message — extract a method taking the receiver.",
  constant: "differs only in a constant — extract a method and parameterize it.",
  structure: "the control flow differs — extract the common core, but verify by hand (lower confidence).",
  mixed: "extract the shared shape and pass what differs as parameters."
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(cluster) ⇒ Delta

Returns a new instance of Delta.



15
16
17
# File 'lib/hashira/duplication/delta.rb', line 15

def initialize(cluster)
  @cluster = cluster
end

Instance Method Details

#kindObject



21
22
23
24
25
26
27
# File 'lib/hashira/duplication/delta.rb', line 21

def kind
  tags = kinds
  return :identical if tags.empty?
  return :structure if tags.include?(:structure)

  tags.size == 1 ? tags.first : :mixed
end

#summaryObject



19
# File 'lib/hashira/duplication/delta.rb', line 19

def summary = ADVICE.fetch(kind)

#to_hObject



29
30
31
32
# File 'lib/hashira/duplication/delta.rb', line 29

def to_h
  { mass: @cluster.mass, sites: @cluster.size, kind:,
    locations: @cluster.sites.sort_by(&:sort_key).map(&:range) }
end