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(args, 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.
36 37 38 |
# File 'lib/usher/interface/rails23.rb', line 36 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
40 41 42 |
# File 'lib/usher/interface/rails23.rb', line 40 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, = {}) 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 34 |
# File 'lib/usher/interface/rails23.rb', line 15 def add_route(path, = {}) path.gsub!(/(\..*?(?!\)))$/, '(\1)') 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
62 63 64 65 66 |
# File 'lib/usher/interface/rails23.rb', line 62 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(args, recall = {}, method = :generate, route_name = nil) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/usher/interface/rails23.rb', line 118 def generate(args, recall = {}, method = :generate, route_name = nil) if args.is_a?(Hash) = args args = nil else args = Array(args) = args.last.is_a?(Hash) ? args.pop : {} end 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, args ? args << : ) url.slice!(-1) if url[-1] == ?/ url else raise "method #{method} not recognized" end end |
#generate_url(route, params) ⇒ Object
148 149 150 |
# File 'lib/usher/interface/rails23.rb', line 148 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 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# 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(*args) UsherRailsRouter.generate(args, {}, :generate, :#{name}) end def #{name}_path(*args) UsherRailsRouter.generate(args, {}, :generate, :#{name}) end end_eval end d.__send__(:include, @module) @router.named_routes.instance_eval " def helpers { } end " unless @module.const_defined?(:UsherRailsRouter) @module.const_set(:UsherRailsRouter, self) end @router.named_routes.helpers.__send__(:extend, @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
152 153 154 |
# File 'lib/usher/interface/rails23.rb', line 152 def () @router.() end |
#recognize(request) ⇒ Object
68 69 70 71 72 73 |
# File 'lib/usher/interface/rails23.rb', line 68 def recognize(request) response = @router.recognize(request) request.path_parameters.merge!(response.destination) request.path_parameters.merge!(response.params_as_hash) "#{request.path_parameters[:controller].camelize}Controller".constantize end |
#reload! ⇒ Object Also known as: reload
44 45 46 47 48 49 50 51 |
# File 'lib/usher/interface/rails23.rb', line 44 def reload! if configuration_files.any? configuration_files.each { |config| load(config) } else add_route ":controller/:action/:id" end end |
#reset!(options = {}) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/usher/interface/rails23.rb', line 75 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
54 55 56 |
# File 'lib/usher/interface/rails23.rb', line 54 def route_count routes.size end |
#routes ⇒ Object
58 59 60 |
# File 'lib/usher/interface/rails23.rb', line 58 def routes @router.routes end |