Skip to content

Commit

Permalink
Remove deprecated AbstractFindError syntax.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepingkingstudios committed Dec 15, 2023
1 parent 1e80028 commit d4440d3
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 143 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Remove deprecations from previous versions:

- Removed `Cuprum::Collections::Relation::Disambiguation`.
- Removed initializing an `AbstractFindError` subclass with `primary_key_name` and `primary_key_values` keywords.

## 0.4.0

Expand Down
24 changes: 1 addition & 23 deletions lib/cuprum/collections/errors/abstract_find_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,7 @@ def resolve_query_options(**options)
end

def resolve_options(**options) # rubocop:disable Metrics/MethodLength
if options[:primary_key_name] && options[:primary_key_values]
resolve_primary_key_options(**options)
elsif options[:attribute_name] && options.key?(:attribute_value)
if options[:attribute_name] && options.key?(:attribute_value)
resolve_attribute_options(**options)
elsif options[:attributes]
resolve_attributes_options(**options)
Expand All @@ -163,26 +161,6 @@ def resolve_options(**options) # rubocop:disable Metrics/MethodLength
end
end

def resolve_primary_key_options(**options) # rubocop:disable Metrics/MethodLength
values = Array(options[:primary_key_values])

unless values.size == 1
raise ArgumentError,
'deprecated mode does not support empty or multiple attribute values'
end

SleepingKingStudios::Tools::CoreTools
.instance
.deprecate(
'NotFound.new(primary_key_name:, primary_key_values:)',
message: 'use NotFound.new(attribute_name:, attribute_value:)'
)

@attribute_name = options[:primary_key_name]
@attribute_value = values.first
@primary_key = true
end

def titleize(string)
tools.str.underscore(string).split('_').map(&:capitalize).join(' ')
end
Expand Down
120 changes: 0 additions & 120 deletions spec/support/examples/find_error_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,6 @@ module FindErrorExamples
end
end

shared_context 'when initialized with primary key name and values' do
let(:primary_key_name) { 'id' }
let(:primary_key_value) { 0 }
let(:constructor_options) do
{
collection_name: collection_name,
primary_key_name: primary_key_name,
primary_key_values: [primary_key_value]
}
end

before(:example) do
allow(SleepingKingStudios::Tools::CoreTools.instance)
.to receive(:deprecate)
end
end

shared_examples 'should implement the FindError methods' \
do |message_fragment|
describe '.new' do
Expand Down Expand Up @@ -244,56 +227,6 @@ module FindErrorExamples
end
end
end

describe 'with primary_key_name:, primary_key_values:' do
let(:keywords) do
{
collection_name: collection_name,
primary_key_name: 'name',
primary_key_values: ['Alan Bradley']
}
end
let(:error_message) do
'deprecated mode does not support empty or multiple attribute ' \
'values'
end

before(:example) do
allow(SleepingKingStudios::Tools::CoreTools.instance)
.to receive(:deprecate)
end

it 'should print a deprecation warning' do # rubocop:disable RSpec/ExampleLength
described_class.new(**keywords)

expect(SleepingKingStudios::Tools::CoreTools.instance)
.to have_received(:deprecate)
.with(
'NotFound.new(primary_key_name:, primary_key_values:)',
message: 'use NotFound.new(attribute_name:, attribute_value:)'
)
end

context 'when the values Array is empty' do
let(:keywords) { super().merge(primary_key_values: []) }

it 'should raise an exception' do
expect { described_class.new(**keywords) }
.to raise_error ArgumentError, error_message
end
end

context 'when the values Array has multiple items' do
let(:keywords) do
super().merge(primary_key_values: ['Alan Bradley', 'Kevin Flynn'])
end

it 'should raise an exception' do
expect { described_class.new(**keywords) }
.to raise_error ArgumentError, error_message
end
end
end
end

describe '#as_json' do
Expand Down Expand Up @@ -370,20 +303,6 @@ def tools

it { expect(error.as_json).to be == expected }
end

wrap_context 'when initialized with primary key name and values' do
let(:expected_data) do
{
'attribute_name' => primary_key_name,
'collection_name' => collection_name,
'attribute_value' => primary_key_value,
'details' => error.details,
'primary_key' => true
}
end

it { expect(error.as_json).to be == expected }
end
end

describe '#attribute_name' do
Expand All @@ -400,10 +319,6 @@ def tools
it { expect(error.attribute_name).to be nil }
end
# rubocop:enable RSpec/RepeatedExampleGroupBody

wrap_context 'when initialized with primary key name and values' do
it { expect(error.attribute_name).to be == primary_key_name }
end
end

describe '#attribute_value' do
Expand All @@ -426,10 +341,6 @@ def tools
it { expect(error.attribute_value).to be nil }
end
# rubocop:enable RSpec/RepeatedExampleGroupBody

wrap_context 'when initialized with primary key name and values' do
it { expect(error.attribute_value).to be == primary_key_value }
end
end

describe '#attributes' do
Expand All @@ -439,15 +350,9 @@ def tools
it { expect(error.attributes).to be == attributes }
end

# rubocop:disable RSpec/RepeatedExampleGroupBody
wrap_context 'when initialized with query: value' do
it { expect(error.attributes).to be nil }
end

wrap_context 'when initialized with primary key name and values' do
it { expect(error.attributes).to be nil }
end
# rubocop:enable RSpec/RepeatedExampleGroupBody
end

describe '#collection_name' do
Expand Down Expand Up @@ -476,12 +381,6 @@ def tools

it { expect(error.details).to be == expected }
end

wrap_context 'when initialized with primary key name and values' do
let(:expected) { [[primary_key_name, :equal, primary_key_value]] }

it { expect(error.details).to be == expected }
end
end

describe '#message' do
Expand All @@ -502,15 +401,6 @@ def tools

it { expect(error.message).to be == expected }
end

wrap_context 'when initialized with primary key name and values' do
let(:expected) do
"Book #{message_fragment} with #{primary_key_name.inspect} " \
"#{primary_key_value.inspect} (primary key)"
end

it { expect(error.message).to be == expected }
end
end

describe '#primary_key?' do
Expand All @@ -528,29 +418,19 @@ def tools
wrap_context 'when initialized with primary_key: true' do
it { expect(error.primary_key?).to be true }
end

wrap_context 'when initialized with primary key name and values' do
it { expect(error.primary_key?).to be true }
end
# rubocop:enable RSpec/RepeatedExampleGroupBody
end

describe '#query' do
include_examples 'should define reader', :query, nil

# rubocop:disable RSpec/RepeatedExampleGroupBody
wrap_context 'when initialized with attributes: value' do
it { expect(error.query).to be nil }
end

wrap_context 'when initialized with query: value' do
it { expect(error.query).to be query }
end

wrap_context 'when initialized with primary key name and values' do
it { expect(error.query).to be nil }
end
# rubocop:enable RSpec/RepeatedExampleGroupBody
end
end
end
Expand Down

0 comments on commit d4440d3

Please sign in to comment.