Last active
August 29, 2015 13:59
-
-
Save iamvery/10908221 to your computer and use it in GitHub Desktop.
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
require 'forwardable' | |
module Api | |
class Errors | |
extend Forwardable | |
def_delegators :errors, :<<, :to_ary, :empty?, :count | |
def initialize(errors=[]) | |
@errors = Array(errors) | |
end | |
def +(other) | |
self.class.new(errors + other.to_errors.to_ary) | |
end | |
def as_json | |
{ errors: errors } | |
end | |
private | |
attr_reader :errors | |
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
require 'spec_helper' | |
describe Api::Errors do | |
subject(:errors){ described_class.new } | |
describe '+' do | |
it 'returns errors when errors are added together' do | |
expect(errors + errors).to be_a(described_class) | |
end | |
it 'returns errors when an array is added to it' do | |
expect(errors + []).to be_a(described_class) | |
end | |
end | |
describe '#as_json' do | |
it 'wraps the output in a top-level "errors" node' do | |
expect(errors.as_json).to include(:errors) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment