Class: AtlasEngine::AddressNumberRange

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
app/models/atlas_engine/address_number_range.rb

Defined Under Namespace

Classes: RangeError

Constant Summary collapse

RANGE_FORMAT =

ex. (A1..A9)/2

%r{^\((?<min>[^\)]+)\.\.(?<max>[^\)]+)\)/(?<step>[12])$}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(range_string:) ⇒ AddressNumberRange

Returns a new instance of AddressNumberRange.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/models/atlas_engine/address_number_range.rb', line 40

def initialize(range_string:)
  range = RANGE_FORMAT.match(range_string)
  if range
    @min = AddressNumber.new(value: T.must(range[:min]))
    @max = AddressNumber.new(value: T.must(range[:max]))

    if @min.segments.length != @max.segments.length
      raise RangeError, "min and max of range are not compatible, range_string: #{range_string}"
    end

    @step = range[:step].to_i
  else
    @min = AddressNumber.new(value: range_string)
    @max = AddressNumber.new(value: range_string)
    @step = 1
  end
end

Class Method Details

.merge_overlapping_ranges(overlapping_ranges) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'app/models/atlas_engine/address_number_range.rb', line 16

def merge_overlapping_ranges(overlapping_ranges)
  overlapping_ranges.sort_by(&:min).inject([]) do |ranges, range|
    if !ranges.empty? && (ranges.last.overlaps?(range) || consecutive_ranges?(ranges.last, range))
      ranges[0...-1] + [merge_ranges(ranges.last, range)]
    else
      ranges + [range]
    end
  end
end

Instance Method Details

#approx_numeric_rangeObject



82
83
84
85
86
87
88
89
# File 'app/models/atlas_engine/address_number_range.rb', line 82

def approx_numeric_range
  min_numeric = @min.to_i
  max_numeric = @max.to_i

  return unless min_numeric && max_numeric

  (min_numeric..max_numeric)
end

#include?(value, exact_match = true) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/atlas_engine/address_number_range.rb', line 59

def include?(value, exact_match = true)
  value_number = AddressNumber.new(value: value)
  value_segments = value_number.segments

  if value_segments.length != format.length
    return false if exact_match || format.length > 1

    numeric_only_value = value_number.to_i
    return false unless numeric_only_value

    value_segments = [numeric_only_value.to_s]
  end

  format.each_with_index do |range_segment, i|
    value_segment = value_segments[i]

    return false unless value_segment == range_segment || range_segment.include?(value_segment)
  end

  true
end