class HighLevelBrowse::CallNumberRange

A callnumber-range keeps track of the original begin/end strings as well as the normalized versions, and can be serialized to JSON

Constants

DIGIT_TO_LETTER
SPACE_OR_PUNCT

Attributes

firstletter[R]
max[R]
max_raw[R]
min[R]
min_raw[R]
redundant[RW]
topic_array[RW]

Public Class Methods

force_break_between_digit_and_letter(str) click to toggle source

@nodoc Force a space between any digit->letter transition

# File lib/high_level_browse/call_number_range.rb, line 42
def self.force_break_between_digit_and_letter(str)
  str.gsub(DIGIT_TO_LETTER, '\1 \2')
end
json_create(h) click to toggle source

@nodoc

# File lib/high_level_browse/call_number_range.rb, line 106
def self.json_create(h)
  cnr = self.allocate
  cnr.reconstitute(*(h['data']))
  cnr
end
new(min:, max:, topic_array:) click to toggle source
# File lib/high_level_browse/call_number_range.rb, line 56
def initialize(min:, max:, topic_array:)
  @illegal     = false
  @redundant   = false
  self.min     = self.class.preprocess(min)
  self.max     = self.class.preprocess(max)
  @topic_array = topic_array
  @firstletter = self.min[0] unless @illegal
end
preprocess(str) click to toggle source

@nodoc Preprocess the string, removing spaces/punctuation off the end and forcing a space where there's a digit->letter transition

# File lib/high_level_browse/call_number_range.rb, line 48
def self.preprocess(str)
  str ||= ''
  force_break_between_digit_and_letter(
      strip_spaces_and_punct(str)
  )
end
strip_spaces_and_punct(str) click to toggle source

@nodoc Remove spaces/punctuation from the ends of the string

# File lib/high_level_browse/call_number_range.rb, line 36
def self.strip_spaces_and_punct(str)
  str.gsub(SPACE_OR_PUNCT, '\1')
end

Public Instance Methods

<=>(o) click to toggle source

Compare based on @min, then end @param [CallNumberRange] o the range to compare to

# File lib/high_level_browse/call_number_range.rb, line 68
def <=>(o)
  [self.min, self.max] <=> [o.min, o.max]
end
==(other) click to toggle source

Two ranges are equal if their @min, @max, and topic array are all the same @param [CallNumberRange] o the range to compare to

# File lib/high_level_browse/call_number_range.rb, line 89
def ==(other)
  @min == other.min and
      @max == other.max and
      @topic_array == other.topic_array
end
contains(x) click to toggle source
# File lib/high_level_browse/call_number_range.rb, line 147
def contains(x)
  @min <= x and @max >= x
end
Also aliased as: cover?, member?
cover?(x)
Alias for: contains
illegal?() click to toggle source
# File lib/high_level_browse/call_number_range.rb, line 138
def illegal?
  @illegal
end
max=(x) click to toggle source

Same as start. Set the illegal flag if we get an error

# File lib/high_level_browse/call_number_range.rb, line 127
def max=(x)
  @max_raw     = x
  possible_max = Lcsort.normalize(x)
  if possible_max.nil? # didn't normalize
    @illegal = true
    nil
  else
    @max = possible_max + '~' # add a tilde to make it a true endpoint
  end
end
member?(x)
Alias for: contains
min=(x) click to toggle source

In both @min= and end=, we also rescue any parsing errors and simply set the @illegal flag so we can use it later on.

# File lib/high_level_browse/call_number_range.rb, line 115
def min=(x)
  @min_raw     = x
  possible_min = Lcsort.normalize(x)
  if possible_min.nil? # didn't normalize
    @illegal = true
    nil
  else
    @min = possible_min
  end
end
reconstitute(min, max, min_raw, max_raw, firstletter, topic_array) click to toggle source
# File lib/high_level_browse/call_number_range.rb, line 76
def reconstitute(min, max, min_raw, max_raw, firstletter, topic_array)
  @min         = min
  @max         = max
  @min_raw     = min_raw
  @max_raw     = max_raw
  @firstletter = firstletter
  @topic_array = topic_array
end
surrounds(other) click to toggle source
# File lib/high_level_browse/call_number_range.rb, line 143
def surrounds(other)
  @min <= other.min and @max >= other.max
end
to_json(*a) click to toggle source

@nodoc JSON roundtrip

# File lib/high_level_browse/call_number_range.rb, line 98
def to_json(*a)
  {
      'json_class' => self.class.name,
      'data'       => [@min, @max, @min_raw, @max_raw, @firstletter, @topic_array]
  }.to_json(*a)
end
to_s() click to toggle source
# File lib/high_level_browse/call_number_range.rb, line 72
def to_s
  "[#{self.min_raw} - #{self.max_raw}]"
end