Skip to content

Commit

Permalink
Merge pull request #138 from collectionspace/bugfix-unknown-term-from…
Browse files Browse the repository at this point in the history
…-nil-str

Bugfix unknown term from nil str
  • Loading branch information
kspurgin authored May 23, 2022
2 parents 31f4cf6 + 156bbe2 commit a79c700
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 2 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ This project bumps the version number for any changes (including documentation u
## [Unreleased] - i.e. pushed to main branch but not yet tagged as a release
- none

## [4.0.1] - 2022-05-23

See [PR 138](https://github.com/collectionspace/collectionspace-mapper/pull/138) for more details on changes.

### Changed
- BUGFIX: Fixes error that could occur if an unknown term using different cases (i.e. U.S. Equestian Team vs. U.S. Equestrian team). TermHandler would check whether the second value was cached as unknown with case-swapping, and would return true. However, when actually fetching the cached value, case-swapping wasn't used, so a nil value would be returned and passed on, later causing a MethodMissing on nil error.

## [4.0.0] - 2022-05-13

See [PR 137](https://github.com/collectionspace/collectionspace-mapper/pull/137) for more details on changes.
Expand Down
9 changes: 9 additions & 0 deletions lib/collectionspace/mapper/error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

module CollectionSpace
module Mapper
# Base class for application-specific errors
class Error < StandardError
end
end
end
6 changes: 5 additions & 1 deletion lib/collectionspace/mapper/term_searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ def cached_as_unknown?(val)
end

def cached_unknown(type_subtype, val)
@cache.get('unknownvalue', type_subtype, val)
returned = @cache.get('unknownvalue', type_subtype, val)
return returned if returned

returned = @cache.get('unknownvalue', type_subtype, case_swap(val))
return returned if returned
end

private def type_subtype
Expand Down
9 changes: 9 additions & 0 deletions lib/collectionspace/mapper/unknown_term.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@ module CollectionSpace
module Mapper
class UnknownTerm

class ReconstituteNilError < CS::Mapper::Error
def initialize
msg = "Cannot reconstitute from NilValue"
super(msg)
end
end

# Reconstitute UnknownTerm object from cached string
# @param str [String] of form: type|||subtype|||term
def self.from_string(str)
fail(ReconstituteNilError.new) if str.nil?

parts = str.split('|||')
self.new(type: parts[0], subtype: parts[1], term: parts[2])
end
Expand Down
2 changes: 1 addition & 1 deletion lib/collectionspace/mapper/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module CollectionSpace
module Mapper
VERSION = '4.0.0'
VERSION = '4.0.1'
end
end
29 changes: 29 additions & 0 deletions spec/collectionspace/mapper/unknown_term_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe CollectionSpace::Mapper::UnknownTerm do
describe '.from_string' do
let(:result){ described_class.from_string(str) }

context 'with valid string (3 parts, separated by `|||`)' do
let(:str){ 'a|||b|||c' }

it 'creates new as expected', :aggregate_failures do
expect(result).to be_a(described_class)
expect(result.type).to eq('a')
expect(result.subtype).to eq('b')
expect(result.display_name).to eq('c')
expect(result.urn).to eq(str)
end
end

context 'with nil' do
let(:str){ nil }

it 'fails', :aggregate_failures do
expect{ result }.to raise_error(CS::Mapper::UnknownTerm::ReconstituteNilError)
end
end
end
end

0 comments on commit a79c700

Please sign in to comment.