Just so we can play w/ ruby.
Created
January 31, 2012 04:03
-
-
Save davidjbeveridge/1708699 to your computer and use it in GitHub Desktop.
Routing
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 AssetController | |
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 Decision | |
def self.find_controller(controller_name) | |
# require "#{controller_name}_controller" | |
controller_whole_name = controller_name.split('_').map {|w| w.capitalize}.join | |
controller_const_name = "#{controller_whole_name}Controller" | |
raise NameError.new "Constant not found: " unless Kernel.const_defined?(controller_const_name) | |
controller = Kernel.const_get(controller_const_name).new | |
puts "Controller Name: #{controller_const_name}" | |
puts "Instance: #{controller.inspect}" | |
controller | |
end | |
def initialize(controller_name, action) | |
@controller = controller_name | |
@action = action.to_sym | |
end | |
def controller | |
find_controller(@controller) | |
end | |
def find_controller(controller_name) | |
self.class.find_controller("#{controller_name}") | |
end | |
def action | |
@action | |
end | |
def run | |
{} | |
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
# 1. I want to instantiate the Controller | |
# - Where Controller is a named controller specified by Query | |
# 2. I want to instantiate Controllers w/ an access type, as specified by the Query object | |
# 3. I want to call a method of the controller, where the method is the named action specified by the Query object | |
# - should return a Hash | |
# 4. I want pass the hash returned by the named action to a Presenter object | |
require "spec_helper" | |
require 'decision' | |
require 'user_controller' | |
require 'asset_controller' | |
describe Decision do | |
it "returns a named controller" do | |
puts Decision.find_controller('user') | |
Decision.find_controller('user').should be_a(UserController) | |
Decision.find_controller('asset').should be_a(AssetController) | |
end | |
it "instantiates a controller, given a query" do | |
query = Query.new "/json/user/create" | |
UserController.should_receive(:new) | |
decision = Decision.new(query.controller, query.action) | |
decision.controller.should be_a(UserController) | |
decision.action.should == :create | |
end | |
# it "runs the action and recieves a hash" do | |
# query = Query.new "/json/user/create" | |
# decision = Decision.new(query.controller, query.action) | |
# decision.run.should be_a(Hash) | |
# end | |
# | |
# it "runs the action with parameters" do | |
# query = Query.new "/json/user/create" | |
# decision = Decision.new(query.controller, query.action) | |
# decision.run.should be_a(Hash) | |
# 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
# A sample Gemfile | |
source "http://rubygems.org" | |
gem 'rspec' |
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
GEM | |
remote: http://rubygems.org/ | |
specs: | |
diff-lcs (1.1.3) | |
rspec (2.8.0) | |
rspec-core (~> 2.8.0) | |
rspec-expectations (~> 2.8.0) | |
rspec-mocks (~> 2.8.0) | |
rspec-core (2.8.0) | |
rspec-expectations (2.8.0) | |
diff-lcs (~> 1.1.2) | |
rspec-mocks (2.8.0) | |
PLATFORMS | |
ruby | |
DEPENDENCIES | |
rspec |
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 Query | |
attr_accessor :format | |
attr_accessor :controller | |
attr_accessor :action | |
def initialize(route) | |
route_pieces = route.downcase.split('/') | |
@format = route_pieces[1] | |
@controller = route_pieces[2] | |
@action = route_pieces[3] | |
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
require 'spec_helper' | |
describe Query do | |
it "should return a Query object, given a slash-delimited call" do | |
{ | |
'/json/asset/create' => {:format => 'json', :controller => 'asset', :action => 'create'}, | |
'/html/person/delete' => {:format => 'html', :controller => 'person', :action => 'delete'}, | |
}.each do |route, query_properties| | |
query = Query.new(route) | |
query.should be_a(Query) | |
query.format.should == query_properties[:format] | |
query.controller.should == query_properties[:controller] | |
query.action.should == query_properties[:action] | |
end | |
end | |
it "should return the same object, regardless or the case of the specified route" do | |
query1 = Query.new('/json/asset/create') | |
query2 = Query.new('/JSON/ASSET/CREATE') | |
query1.format.should == query2.format | |
query1.controller.should == query2.controller | |
query1.action.should == query2.action | |
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
$: << File.join(File.dirname(__FILE__), '/../lib') | |
$: << File.join(File.dirname(__FILE__), '/examples') | |
require 'rspec' | |
require 'query' |
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 UserController | |
def initialize | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment