Class: Contrast::Utils::TagUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/contrast/utils/tag_util.rb

Overview

Utility methods for working with tag ranges

Class Method Summary collapse

Class Method Details

.covered?(remaining_ranges, ranges) ⇒ Boolean

Determine if the given array of tags is covered by the other remaining_ranges: the tags left that haven’t been covered by those given ranges: the tags that are covering the first

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/contrast/utils/tag_util.rb', line 12

def covered? remaining_ranges, ranges
  return true unless remaining_ranges&.any?

  tag = remaining_ranges[0]
  ranges.each do |range|
    # If we covered the tag before using all the ranges, break
    break if tag.length <= 0

    relationship = tag.compare_range(range.start_idx, range.end_idx)
    case relationship
    when Contrast::Agent::Assess::Tag::BELOW
      # since the tags are ordered, if we're below, nope out
      return false
    when Contrast::Agent::Assess::Tag::LOW_SPAN
      # if we ever get a low span, that means a low part
      # won't be covered. there's no need to continue
      return false
    when Contrast::Agent::Assess::Tag::WITHOUT
      # if we ever get a without, that means a low part won't
      # be covered. there's no need to continue
      return false
    when Contrast::Agent::Assess::Tag::WITHIN
      # if we're within, then 0 out this tag since it is
      # completely covered
      tag.update_start(0)
      tag.update_end(0)
    when Contrast::Agent::Assess::Tag::HIGH_SPAN
      tag.update_start(range.end_idx)
    when Contrast::Agent::Assess::Tag::ABOVE
      # The tag's above where we are, it doesn't cover us but a later
      # one may
    end
  end
  return false unless tag.length <= 0

  remaining_ranges.shift
  covered?(remaining_ranges, ranges)
end

.merge_tags(tags) ⇒ Object

Given a collection of tags, merge any tags that are continuous

If tags is a hash, it should be in the format label => [tags] The array of tags will each be merged

If tags is an array in the format [tags], the array will be merged

The original object is returned, although setters should not be necessary since tags is a collection in either case



86
87
88
89
90
91
92
# File 'lib/contrast/utils/tag_util.rb', line 86

def merge_tags tags
  if tags.is_a?(Hash)
    tags.each_value { |value| smallerize(value) }
  else
    smallerize(tags)
  end
end

.ordered_merge(arr, new_arr) ⇒ Object

Given an array of tags, add all new tags to that array

The addition is done such that the new entry(ies) are inserted so that the range they cover is in order Any overlapping ranges are merged before returning

arr: the array of tags to which we are adding new_arr: misnomer. either an array of or a single Tag to be added



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/contrast/utils/tag_util.rb', line 59

def ordered_merge arr, new_arr
  # [Contrast::Agent::Assess::Tag, ...]
  if new_arr.is_a?(Array)
    return arr unless new_arr.any?
    return new_arr unless arr&.any?

    new_arr.each { |new_element| single_ordered_merge(arr, new_element) }
  # Contrast::Agent::Assess::Tag
  else
    return arr unless new_arr
    return [new_arr] unless arr

    single_ordered_merge(arr, new_arr)
  end
  smallerize(arr)
end

.size_aware_merge(target_object, tags) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/contrast/utils/tag_util.rb', line 94

def size_aware_merge target_object, tags
  max_size = target_object.to_s.length
  tags = merge_tags(tags)
  tags.each do |tag|
    tag.update_end(max_size) if tag.extends_beyond_string_size?(max_size)
  end
end