Skip to content

Commit

Permalink
Remove unnecessary backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Tabby committed Oct 14, 2024
1 parent 88e09b4 commit 9dadd1a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 118 deletions.
17 changes: 4 additions & 13 deletions lib/anony/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,25 @@ class Result

delegate :failed?, :overwritten?, :skipped?, :destroyed?, to: :status

RESULT_DEPRECATION = ActiveSupport::Deprecation.new("2.0.0", "anony")

def self.failed(error, record = nil)
def self.failed(error, record)
new(FAILED, record: record, error: error)
end

def self.overwritten(fields, record = nil)
def self.overwritten(fields, record)
new(OVERWRITTEN, record: record, fields: fields)
end

def self.skipped(record = nil)
def self.skipped(record)
new(SKIPPED, record: record)
end

def self.destroyed(record = nil)
def self.destroyed(record)
new(DESTROYED, record: record)
end

private def initialize(status, record:, fields: [], error: nil)
raise ArgumentError, "No error provided" if status == FAILED && error.nil?

if record.nil?
RESULT_DEPRECATION.warn(
"Creating a Result without a reference to the record being anonymised is deprecated " \
"and will be removed in future versions",
)
end

@status = ActiveSupport::StringInquirer.new(status)
@fields = fields
@error = error
Expand Down
151 changes: 46 additions & 105 deletions spec/anony/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@
}
end

shared_context "without model instance" do
before do
allow(described_class::RESULT_DEPRECATION).to receive(:warn)
end
end

shared_context "with model instance" do
let(:klass) do
Class.new(ActiveRecord::Base) do
Expand All @@ -34,142 +28,89 @@ def self.name
end

context "anonymised" do
shared_examples_for "anonymised result" do
it "has enumbeable state" do
expect(result.status).to eq("overwritten")
end
include_context "with model instance"
let(:result) { described_class.overwritten(field_values, model) }

it "responds to .overwritten?" do
expect(result).to be_overwritten
end
it "has enumbeable state" do
expect(result.status).to eq("overwritten")
end

context "without record" do
include_context "without model instance"
let(:result) { described_class.overwritten(field_values) }

it_behaves_like "anonymised result"
it "responds to .overwritten?" do
expect(result).to be_overwritten
end

context "with record" do
include_context "with model instance"
let(:result) { described_class.overwritten(field_values, model) }

it_behaves_like "anonymised result"

it "contains the model" do
expect(result.record).to be model
end
it "contains the model" do
expect(result.record).to be model
end
end

context "deleted" do
shared_examples_for "destroyed result" do
it "has enumbeable state" do
expect(result.status).to eq("destroyed")
end
include_context "with model instance"
let(:result) { described_class.destroyed(model) }

it "responds to .destroyed?" do
expect(result).to be_destroyed
end

it "has no fields" do
expect(result.fields).to be_empty
end
it "has enumbeable state" do
expect(result.status).to eq("destroyed")
end

context "without record" do
include_context "without model instance"
let(:result) { described_class.destroyed }

it_behaves_like "destroyed result"
it "responds to .destroyed?" do
expect(result).to be_destroyed
end

context "with record" do
include_context "with model instance"
let(:result) { described_class.destroyed(model) }

it_behaves_like "destroyed result"
it "has no fields" do
expect(result.fields).to be_empty
end

it "contains the model" do
expect(result.record).to be model
end
it "contains the model" do
expect(result.record).to be model
end
end

context "skipped" do
shared_examples_for "skipped result" do
it "has enumbeable state" do
expect(result.status).to eq("skipped")
end

it "responds to .skipped?" do
expect(result).to be_skipped
end
include_context "with model instance"
let(:result) { described_class.skipped(model) }

it "has no fields" do
expect(result.fields).to be_empty
end
it "has enumbeable state" do
expect(result.status).to eq("skipped")
end

context "without record" do
include_context "without model instance"
let(:result) { described_class.skipped }

it_behaves_like "skipped result"
it "responds to .skipped?" do
expect(result).to be_skipped
end

context "with record" do
include_context "with model instance"
let(:result) { described_class.skipped(model) }

it_behaves_like "skipped result"
it "has no fields" do
expect(result.fields).to be_empty
end

it "contains the model" do
expect(result.record).to be model
end
it "contains the model" do
expect(result.record).to be model
end
end

context "failed" do
include_context "with model instance"
let(:error) { anything }
let(:result) { described_class.failed(error, model) }

shared_examples_for "failed result" do
it "has an error" do
expect(result.error).to eq(error)
end

it "has enumbeable state" do
expect(result.status).to eq("failed")
end

it "responds to .failed?" do
expect(result).to be_failed
end

context "without an error" do
it "raises an exception" do
expect { described_class.failed(nil) }.to raise_error(ArgumentError)
end
end
it "has an error" do
expect(result.error).to eq(error)
end

context "without record" do
include_context "without model instance"
let(:result) { described_class.failed(error) }

it_behaves_like "failed result"
it "has enumbeable state" do
expect(result.status).to eq("failed")
end

context "with record" do
include_context "with model instance"
let(:result) { described_class.failed(error, model) }

it_behaves_like "failed result"
it "responds to .failed?" do
expect(result).to be_failed
end

it "contains the model" do
expect(result.record).to be model
context "without an error" do
it "raises an exception" do
expect { described_class.failed(nil) }.to raise_error(ArgumentError)
end
end

it "contains the model" do
expect(result.record).to be model
end
end
end

0 comments on commit 9dadd1a

Please sign in to comment.