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
Public Class Methods
@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
@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
# 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
@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
@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
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
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
# File lib/high_level_browse/call_number_range.rb, line 147 def contains(x) @min <= x and @max >= x end
# File lib/high_level_browse/call_number_range.rb, line 138 def illegal? @illegal end
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
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
# 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
# File lib/high_level_browse/call_number_range.rb, line 143 def surrounds(other) @min <= other.min and @max >= other.max end
@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
# File lib/high_level_browse/call_number_range.rb, line 72 def to_s "[#{self.min_raw} - #{self.max_raw}]" end