Class: Hashira::Duplication::Fragment

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

Constant Summary collapse

DIGEST_LENGTH =
12

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, roots) ⇒ Fragment

Returns a new instance of Fragment.



10
11
12
13
# File 'lib/hashira/duplication/fragment.rb', line 10

def initialize(file, roots)
  @file = file
  @roots = roots
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



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

def file
  @file
end

Instance Method Details

#digestObject



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

def digest = Digest::SHA256.hexdigest(shape).slice(0, DIGEST_LENGTH)

#finishObject



27
# File 'lib/hashira/duplication/fragment.rb', line 27

def finish = @roots.last.location.end_line

#lineObject



25
# File 'lib/hashira/duplication/fragment.rb', line 25

def line = @roots.first.location.start_line

#locationObject



29
# File 'lib/hashira/duplication/fragment.rb', line 29

def location = "#{file}:#{line}"

#massObject



23
# File 'lib/hashira/duplication/fragment.rb', line 23

def mass = types.size

#nodesObject



39
# File 'lib/hashira/duplication/fragment.rb', line 39

def nodes = @nodes ||= @roots.flat_map { Analysis::NodeWalk.collect(it) }

#overlaps?(other) ⇒ Boolean

Returns:

  • (Boolean)


35
# File 'lib/hashira/duplication/fragment.rb', line 35

def overlaps?(other) = file == other.file && line <= other.finish && other.line <= finish

#overlaps_any?(others) ⇒ Boolean

Returns:

  • (Boolean)


37
# File 'lib/hashira/duplication/fragment.rb', line 37

def overlaps_any?(others) = others.any? { overlaps?(it) }

#rangeObject



31
# File 'lib/hashira/duplication/fragment.rb', line 31

def range = "#{file}:#{line}-#{finish}"

#shapeObject



21
# File 'lib/hashira/duplication/fragment.rb', line 21

def shape = types.join(",")

#sort_keyObject



33
# File 'lib/hashira/duplication/fragment.rb', line 33

def sort_key = [file, line]

#typesObject



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

def types = @types ||= nodes.map(&:type)