-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add NestedError and ability to import
Fix #6
- Loading branch information
Showing
6 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module AdequateErrors | ||
# Represents one single error | ||
# @!attribute [r] base | ||
# @return [ActiveModel::Base] the object which the error belongs to | ||
# @!attribute [r] attribute | ||
# @return [Symbol] attribute of the object which the error belongs to | ||
# @!attribute [r] type | ||
# @return [Symbol] error's type | ||
# @!attribute [r] options | ||
# @return [Hash] additional options | ||
# @!attribute [r] inner_error | ||
# @return [Error] inner error | ||
class NestedError < Error | ||
def initialize(base, inner_error, override_options = {}) | ||
@base = base | ||
@inner_error = inner_error | ||
@attribute = override_options.fetch(:attribute) { inner_error.attribute } | ||
@type = override_options.fetch(:type) { inner_error.type } | ||
@options = inner_error.options | ||
end | ||
|
||
attr_reader :inner_error | ||
|
||
# Full message of the error. | ||
# Sourced from inner error. | ||
def message | ||
@inner_error.message | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module AdequateErrors | ||
VERSION = "0.1.0" | ||
VERSION = "0.1.1" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require "cases/helper" | ||
require "minitest/autorun" | ||
require 'rspec/mocks/minitest_integration' | ||
require 'adequate_errors' | ||
require "models/topic" | ||
require "models/reply" | ||
|
||
describe AdequateErrors::NestedError do | ||
let(:topic) { Topic.new } | ||
let(:inner_error) { AdequateErrors::Error.new(topic, :mineral, :not_enough, count: 2) } | ||
|
||
let(:reply) { Reply.new } | ||
subject { AdequateErrors::NestedError.new(reply, inner_error) } | ||
|
||
describe '#initialize' do | ||
it 'assigns attributes' do | ||
assert_equal reply, subject.base | ||
assert_equal inner_error.attribute, subject.attribute | ||
assert_equal inner_error.type, subject.type | ||
assert_equal(inner_error.options, subject.options) | ||
end | ||
|
||
describe 'overriding attribute and type' do | ||
subject { AdequateErrors::NestedError.new(reply, inner_error, attribute: :parent, type: :foo) } | ||
|
||
it 'assigns attributes' do | ||
assert_equal reply, subject.base | ||
assert_equal :parent, subject.attribute | ||
assert_equal :foo, subject.type | ||
assert_equal(inner_error.options, subject.options) | ||
end | ||
end | ||
end | ||
|
||
describe '#message' do | ||
it "return inner error's message" do | ||
expect(inner_error).to receive(:message) | ||
subject.message | ||
end | ||
end | ||
end |