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.



13
14
15
# File 'lib/hashira/duplication/delta.rb', line 13

def initialize(cluster)
  @cluster = cluster
end

Instance Method Details

#kindObject



19
20
21
22
23
24
# File 'lib/hashira/duplication/delta.rb', line 19

def kind
  tags = kinds
  return :identical if tags.empty?
  return :structure if tags.include?(:structure)
  tags.size == 1 ? tags.first : :mixed
end

#summaryObject



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

def summary = ADVICE.fetch(kind)

#to_hObject



26
27
28
29
30
31
# File 'lib/hashira/duplication/delta.rb', line 26

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