Skip to content

Instantly share code, notes, and snippets.

@gumayunov
Created September 5, 2012 19:55
Show Gist options
  • Save gumayunov/3643523 to your computer and use it in GitHub Desktop.
Save gumayunov/3643523 to your computer and use it in GitHub Desktop.
Configus.build Rails.env do
env :production do
services do
stripe "StripeService"
stripe_gateway "StripeGateway"
end
end
env :development, parent: :production do
end
env :test, parent: :production do
services do
stripe_gateway "StripeGatewayStub"
end
end
end
class CreateStripeInvoceItemJob < Struct.new(:invoce_id)
include BaseJob
def perform
ServiceLocator.service(:stripe).create_invoice_item(invoce_id)
end
end
describe CreateStripeInvoceItemJob, "#perform" do
it "calls StripeService.create_invoce_item" do
invoce_id = 123
service_mock = mock("StripeService")
service_mock.should_receive(:create_invoice_item).with(invoce_id)
ServiceLocator.set_service(stripe: service_mock) do
CreateStripeInvoceItemJob.new(invoce_id).perform
end
end
end
class ServiceLocator
class << self
def set_service(hash, &block)
prev = {}
hash.symbolize_keys.each do |name, srv|
prev[name] = services[name]
services[name]= srv
end
if block_given?
begin
block.call
ensure
set_service(prev)
end
end
end
def service(name)
sname = name.to_sym
services[sname] ||= configus.services.send(sname).constantize
end
private
def services
@services ||= {}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment