Class: Hashira::Duplication::Fragment

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

Constant Summary collapse

DIGEST_LENGTH =
12
SCHEMA =
%i[
  class_node module_node statements_node arguments_node assoc_node
  array_node hash_node keyword_hash_node constant_read_node constant_path_node
  symbol_node string_node integer_node float_node true_node false_node nil_node
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, roots) ⇒ Fragment

Returns a new instance of Fragment.



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

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

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



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

def file
  @file
end

Instance Method Details

#digestObject



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

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

#finishObject



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

def finish = @roots.last.location.end_line

#lineObject



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

def line = @roots.first.location.start_line

#locationObject



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

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

#massObject



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

def mass = types.size

#nodesObject



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

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

#overlaps?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#rangeObject



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

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

#rankObject



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

def rank = [file, line]

#schema?Boolean

Returns:

  • (Boolean)


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

def schema? = nodes.all? { directive?(it) }

#shapeObject



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

def shape = types.join(",")

#touches?(others) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#typesObject



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

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