Created
September 5, 2012 19:55
-
-
Save gumayunov/3643523 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
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 |
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
class CreateStripeInvoceItemJob < Struct.new(:invoce_id) | |
include BaseJob | |
def perform | |
ServiceLocator.service(:stripe).create_invoice_item(invoce_id) | |
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
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 |
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
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