Skip to content

Instantly share code, notes, and snippets.

/services.rb Secret

Created January 23, 2017 12:42
Show Gist options
  • Save anonymous/5e7d9fd9dd9bdd2f7494996b13d983c4 to your computer and use it in GitHub Desktop.
Save anonymous/5e7d9fd9dd9bdd2f7494996b13d983c4 to your computer and use it in GitHub Desktop.
class CollectionProcessor
attr_reader :collection, :service
def initialize(collection: collection, service: service)
@collection = collection
@service = service
end
def call
collection.map do |item|
service.new(item: item).call
end
end
end
class BitOfLoveGiver
attr_reader :item
def initialize(item: item)
@item = item
end
def call
give_a_bit_of_love(item)
end
private
def give_a_bit_of_love
{ **item, ♥: true }
end
end
users = [
{ name: 'Ivan' },
{ name: 'Joe' },
{ name: 'Jose' }
]
CollectionProcessor.new(
collection: users,
service: BitOfLoveGiver
).call
#### Spec ####
describe CollectionProcessor do
let(:collection) { [
{ name: 'Ivan' },
{ name: 'Joe' },
{ name: 'Jose' }
] }
subject { described_class.new(collection: collection, service: service) }
context 'success' do
let(:service) do
double(new: -> { { ♥: true } })
end
it 'calls service' do
result = subject.call
expect(result.first).to have_key(:♥)
end
end
context 'fail' do
let(:service) do
double(new: -> { raise 'Not today!' })
end
it 'raises error' do
expect { subject.call }.to raise_error
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment