Class: LCCallNumber::CallNumber

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/lc_callnumber/lcc.rb

Direct Known Subclasses

UnparseableCallNumber

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lead, fcset = nil, ec = [], year = nil, rest = nil) ⇒ CallNumber

Returns a new instance of CallNumber.



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/lc_callnumber/lcc.rb', line 152

def initialize(lead, fcset=nil, ec=[], year=nil, rest=nil)
  @letters = lead.letters.to_s.upcase
  @digits  = lead.digits.to_num

  if fcset
    @doon1   = fcset.doon1
    @doon2   = fcset.doon2
    @firstcutter = fcset.cutter
  end

  @extra_cutters = ec
  @year = year
  @rest = rest

  @rest.strip! if @rest

  self.reassign_fields
end

Instance Attribute Details

#digitsObject

Returns the value of attribute digits.



147
148
149
# File 'lib/lc_callnumber/lcc.rb', line 147

def digits
  @digits
end

#doon1Object

Returns the value of attribute doon1.



147
148
149
# File 'lib/lc_callnumber/lcc.rb', line 147

def doon1
  @doon1
end

#doon2Object

Returns the value of attribute doon2.



147
148
149
# File 'lib/lc_callnumber/lcc.rb', line 147

def doon2
  @doon2
end

#extra_cuttersObject

Returns the value of attribute extra_cutters.



147
148
149
# File 'lib/lc_callnumber/lcc.rb', line 147

def extra_cutters
  @extra_cutters
end

#firstcutterObject

Returns the value of attribute firstcutter.



147
148
149
# File 'lib/lc_callnumber/lcc.rb', line 147

def firstcutter
  @firstcutter
end

#lettersObject

Returns the value of attribute letters.



147
148
149
# File 'lib/lc_callnumber/lcc.rb', line 147

def letters
  @letters
end

#original_stringObject

Returns the value of attribute original_string.



149
150
151
# File 'lib/lc_callnumber/lcc.rb', line 149

def original_string
  @original_string
end

#restObject

Returns the value of attribute rest.



147
148
149
# File 'lib/lc_callnumber/lcc.rb', line 147

def rest
  @rest
end

#yearObject

Returns the value of attribute year.



147
148
149
# File 'lib/lc_callnumber/lcc.rb', line 147

def year
  @year
end

Instance Method Details

#<=>(other) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/lc_callnumber/lcc.rb', line 194

def <=>(other)
  # Make call numbers sortable.
  # Work through the various parts, from left to right.
  # (They compare as strings or numbers as appropriate, thanks to the above.)

  letters_cmp = letters <=> other.letters
  return letters_cmp unless letters_cmp == 0

  digits_cmp = digits <=> other.digits
  return digits_cmp unless digits_cmp == 0

  doon1_cmp = doon1 <=> other.doon1
  return doon1_cmp unless doon1_cmp == 0

  # Cutters have letters and numbers, which we need to compare separately.
  if firstcutter
    if other.firstcutter
      # Both have firstcutters!  Hurrah!
      # Compare the letter
      firstcutter_cmp = firstcutter.letter <=> other.firstcutter.letter
      # If that didn't help, compare the digits
      if firstcutter_cmp == 0
        # Compare numbers as strings, because S43 < S298
        firstcutter_cmp = firstcutter.digits.to_s <=> other.firstcutter.digits.to_s
      end
      # If that didn't help, if other has an extra_cutter, it is last
      if firstcutter_cmp == 0
        firstcutter_cmp = -1 if (extra_cutters.empty? && ! other.extra_cutters.empty?)
      end
    else
      # self has a firstcutter but other doesn't, so other is first
      firstcutter_cmp = 1
    end
  else
    # self has no firstcutter
    if other.firstcutter
      # other does, so it comes last
      firstcutter_cmp = -1
    else
      # Neither has one, so carry on
      firstcutter_cmp = 0
    end
  end
  return firstcutter_cmp unless firstcutter_cmp == 0

  doon2_cmp = doon2 <=> other.doon2
  return doon2_cmp unless doon2_cmp == 0

  # The extra_cutters are an array, so we need to compare each.
  # Surely there is an easier way to do this?
  extra_cutters_cmp = 0
  # STDERR.puts extra_cutters
  extra_cutters.each_index do |i|
    if extra_cutters_cmp == 0 # Stop when we need go no further
      # First, compare the letter
      if other.extra_cutters[i]
        extra_cutters_cmp = extra_cutters[i].letter <=> other.extra_cutters[i].letter
        # If that didn't help, compare the digits
        if extra_cutters_cmp == 0
          # Compare numbers as strings, because S43 < S298
          extra_cutters_cmp = extra_cutters[i].digits.to_s <=> other.extra_cutters[i].digits.to_s
        end
        # If that didn't help, if self has no more extra_cutters but other does, self is first
        if extra_cutters_cmp == 0
          extra_cutters_cmp = -1 if (extra_cutters[i+1].nil? && other.extra_cutters[i+1])
        end
      else
        # other has no extra_cutter, so it is first
        extra_cutters_cmp = 1
      end
    end
  end
  return extra_cutters_cmp unless extra_cutters_cmp == 0

  year_cmp = year <=> other.year
  return year_cmp unless year_cmp == 0

  rest_cmp = rest <=> other.rest
  return rest_cmp

end

#inspectObject



181
182
183
184
185
186
187
# File 'lib/lc_callnumber/lcc.rb', line 181

def inspect
  header = %w(letters digits doon1 cut1 doon2 othercut)
  actual = [letters,digits,doon1,firstcutter,doon2,extra_cutters.join(','),year,rest].map{|x| x.to_s}
  fmt = '%-7s %-9s %-6s %-6s %-6s %-20s year=%-6s rest=%-s'
  [('%-7s %-9s %-6s %-6s %-6s %-s' % header), (fmt % actual)].join("\n")

end

#reassign_fieldsObject



171
172
173
174
175
176
177
178
179
# File 'lib/lc_callnumber/lcc.rb', line 171

def reassign_fields
  # If we've got a doon2,  but no year and no extra cutters,
  # put the doon2 in the year

  if doon2 and (doon2=~ /\A\d+\Z/) and extra_cutters.empty? and year.nil?
    @year = @doon2
    @doon2 = nil
  end
end

#valid?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/lc_callnumber/lcc.rb', line 190

def valid?
  @letters && @digits
end