Class: Usher::Interface::Rails23
- Inherits:
-
Object
- Object
- Usher::Interface::Rails23
- Defined in:
- lib/usher/interface/rails23.rb,
lib/usher/interface/rails23/mapper.rb
Defined Under Namespace
Classes: Mapper
Instance Attribute Summary collapse
-
#configuration_files ⇒ Object
readonly
Returns the value of attribute configuration_files.
Instance Method Summary collapse
- #add_configuration_file(file) ⇒ Object
- #add_named_route(name, route, options = {}) ⇒ Object
- #add_route(path, options = {}) ⇒ Object
- #call(env) ⇒ Object
- #draw(options = {}) {|ActionController::Routing::RouteSet::Mapper.new(self)| ... } ⇒ Object
- #generate(options, recall = {}, method = :generate, route_name = nil) ⇒ Object
- #generate_url(route, params) ⇒ Object
-
#initialize ⇒ Rails23
constructor
A new instance of Rails23.
- #install_helpers(destinations = [ActionController::Base, ActionView::Base], regenerate_code = false) ⇒ Object
- #named_routes ⇒ Object
- #path_for_options(options) ⇒ Object
- #recognize(request) ⇒ Object
- #reload! ⇒ Object (also: #reload)
- #reset!(options = {}) ⇒ Object
- #route_count ⇒ Object
- #routes ⇒ Object
Constructor Details
#initialize ⇒ Rails23
Returns a new instance of Rails23.
35 36 37 |
# File 'lib/usher/interface/rails23.rb', line 35 def initialize reset! end |
Instance Attribute Details
#configuration_files ⇒ Object (readonly)
Returns the value of attribute configuration_files.
5 6 7 |
# File 'lib/usher/interface/rails23.rb', line 5 def configuration_files @configuration_files end |
Instance Method Details
#add_configuration_file(file) ⇒ Object
39 40 41 |
# File 'lib/usher/interface/rails23.rb', line 39 def add_configuration_file(file) @configuration_files << file end |
#add_named_route(name, route, options = {}) ⇒ Object
11 12 13 |
# File 'lib/usher/interface/rails23.rb', line 11 def add_named_route(name, route, = {}) @router.add_route(route, ).name(name) end |
#add_route(path, options = {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/usher/interface/rails23.rb', line 15 def add_route(path, = {}) if !@controller_action_route_added && path =~ %r{^/?:controller/:action/:id$} add_route('/:controller/:action', .dup) @controller_action_route_added = true end if !@controller_route_added && path =~ %r{^/?:controller/:action$} add_route('/:controller', .merge({:action => 'index'})) @controller_route_added = true end [:action] = 'index' unless [:action] path[0, 0] = '/' unless path[0] == ?/ route = @router.add_route(path, ).to() raise "your route must include a controller" unless (route.paths.first.dynamic_keys && route.paths.first.dynamic_keys.include?(:controller)) || route.destination.include?(:controller) route end |
#call(env) ⇒ Object
61 62 63 64 65 |
# File 'lib/usher/interface/rails23.rb', line 61 def call(env) request = ActionController::Request.new(env) app = recognize(request) app.call(env).to_a end |
#draw(options = {}) {|ActionController::Routing::RouteSet::Mapper.new(self)| ... } ⇒ Object
85 86 87 88 89 |
# File 'lib/usher/interface/rails23.rb', line 85 def draw(={}) reset!() yield ActionController::Routing::RouteSet::Mapper.new(self) install_helpers end |
#generate(options, recall = {}, method = :generate, route_name = nil) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/usher/interface/rails23.rb', line 105 def generate(, recall = {}, method = :generate, route_name = nil) route = if(route_name) @router.named_routes[route_name] else = [:controller] = recall[:controller] unless .key?(:controller) unless .key?(:action) [:action] = '' end () end case method when :generate ||= recall.merge() url = generate_url(route, ) url.slice!(-1) if url[-1] == ?/ url else raise "method #{method} not recognized" end end |
#generate_url(route, params) ⇒ Object
127 128 129 |
# File 'lib/usher/interface/rails23.rb', line 127 def generate_url(route, params) @router.generator.generate(route, params) end |
#install_helpers(destinations = [ActionController::Base, ActionView::Base], regenerate_code = false) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/usher/interface/rails23.rb', line 91 def install_helpers(destinations = [ActionController::Base, ActionView::Base], regenerate_code = false) #*_url and hash_for_*_url Array(destinations).each do |d| d.module_eval { include Helpers } @router.named_routes.keys.each do |name| @module.module_eval <<-end_eval # We use module_eval to avoid leaks def #{name}_url(options = {}) ActionController::Routing::UsherRoutes.generate(options, {}, :generate, :#{name}) end end_eval end d.__send__(:include, @module) end end |
#named_routes ⇒ Object
7 8 9 |
# File 'lib/usher/interface/rails23.rb', line 7 def named_routes @router.named_routes end |
#path_for_options(options) ⇒ Object
131 132 133 |
# File 'lib/usher/interface/rails23.rb', line 131 def () @router.() end |
#recognize(request) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/usher/interface/rails23.rb', line 67 def recognize(request) response = @router.recognize(request) request.path_parameters = (response.params.empty? ? response.path.route.destination : response.path.route.destination.merge(response.params.inject({}){|h,(k,v)| h[k]=v; h })).with_indifferent_access response.params.each { |pair| request.path_parameters[pair.first] = pair.last } "#{request.path_parameters[:controller].camelize}Controller".constantize end |
#reload! ⇒ Object Also known as: reload
43 44 45 46 47 48 49 50 |
# File 'lib/usher/interface/rails23.rb', line 43 def reload! if configuration_files.any? configuration_files.each { |config| load(config) } else add_route ":controller/:action/:id" end end |
#reset!(options = {}) ⇒ Object
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/usher/interface/rails23.rb', line 74 def reset!(={}) [:generator] = [:generator] || Usher::Util::Generators::URL.new [:request_methods] = [:request_methods] || [:protocol, :domain, :port, :query_string, :remote_ip, :user_agent, :referer, :method, :subdomains] @router = Usher.new() @configuration_files = [] @module ||= Module.new @controller_route_added = false @controller_action_route_added = false end |
#route_count ⇒ Object
53 54 55 |
# File 'lib/usher/interface/rails23.rb', line 53 def route_count routes.size end |
#routes ⇒ Object
57 58 59 |
# File 'lib/usher/interface/rails23.rb', line 57 def routes @router.routes end |