Skip to content

Instantly share code, notes, and snippets.

@iamvery
Last active August 29, 2015 13:59
Show Gist options
  • Save iamvery/10908221 to your computer and use it in GitHub Desktop.
Save iamvery/10908221 to your computer and use it in GitHub Desktop.
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
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