Created
July 15, 2011 18:25
-
-
Save timuruski/1085227 to your computer and use it in GitHub Desktop.
A modular way to write helpers
This file contains hidden or 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
| module Authorization | |
| module Service | |
| class << self | |
| def authorize(name) | |
| case name | |
| when 'timuruski', 'stevejobs' then "authorized" | |
| else "unauthorized" | |
| end | |
| end | |
| end | |
| end | |
| module Helpers | |
| def authorize | |
| Service.authorize(params[:name]) | |
| end | |
| end | |
| end | |
| require 'rubygems' | |
| require 'sinatra' | |
| class MyApp < Sinatra::Base | |
| helpers Authorization::Helpers | |
| before do | |
| @authorized = authorize | |
| end | |
| get "/" do | |
| @authorized | |
| end | |
| run! if app_file == $0 | |
| end |
This file contains hidden or 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 'helpers' | |
| require 'rack/test' | |
| def app | |
| MyApp | |
| end | |
| describe Authorization::Helpers do | |
| include Rack::Test::Methods | |
| it "can be stubbed" do | |
| Authorization::Service.stub(:authorize).and_return { 'walrus' } | |
| get "/", {:name => 'timuruski' } | |
| last_response.body.should == 'walrus' | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment