Class: Usher::Interface::Rails2_2Interface
- Inherits:
-
Object
- Object
- Usher::Interface::Rails2_2Interface
- Defined in:
- lib/usher/interface/rails2_2_interface.rb,
lib/usher/interface/rails2_2_interface/mapper.rb
Defined Under Namespace
Classes: Mapper
Instance Attribute Summary collapse
-
#configuration_file ⇒ Object
Returns the value of attribute configuration_file.
-
#usher ⇒ Object
readonly
Returns the value of attribute usher.
Instance Method Summary collapse
- #add_named_route(name, route, options = {}) ⇒ Object
- #add_route(path, options = {}) ⇒ Object
- #draw {|Mapper.new(self)| ... } ⇒ Object
- #empty? ⇒ Boolean
- #generate(options, recall = {}, method = :generate, route_name = nil) ⇒ Object
- #generate_url(route, params) ⇒ Object
-
#initialize ⇒ Rails2_2Interface
constructor
A new instance of Rails2_2Interface.
- #install_helpers(destinations = [ActionController::Base, ActionView::Base], regenerate_code = false) ⇒ Object
- #load_routes! ⇒ Object
- #named_routes ⇒ Object
- #path_for_options(options) ⇒ Object
- #recognize(request) ⇒ Object
- #reload ⇒ Object
- #reset! ⇒ Object
- #route_count ⇒ Object
Constructor Details
#initialize ⇒ Rails2_2Interface
Returns a new instance of Rails2_2Interface.
10 11 12 |
# File 'lib/usher/interface/rails2_2_interface.rb', line 10 def initialize reset! end |
Instance Attribute Details
#configuration_file ⇒ Object
Returns the value of attribute configuration_file.
8 9 10 |
# File 'lib/usher/interface/rails2_2_interface.rb', line 8 def configuration_file @configuration_file end |
#usher ⇒ Object (readonly)
Returns the value of attribute usher.
7 8 9 |
# File 'lib/usher/interface/rails2_2_interface.rb', line 7 def usher @usher end |
Instance Method Details
#add_named_route(name, route, options = {}) ⇒ Object
53 54 55 |
# File 'lib/usher/interface/rails2_2_interface.rb', line 53 def add_named_route(name, route, = {}) @usher.add_route(route, ).name(name) end |
#add_route(path, options = {}) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/usher/interface/rails2_2_interface.rb', line 25 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 = @usher.add_route(path, ) 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 |
#draw {|Mapper.new(self)| ... } ⇒ Object
112 113 114 115 116 |
# File 'lib/usher/interface/rails2_2_interface.rb', line 112 def draw reset! yield Mapper.new(self) install_helpers end |
#empty? ⇒ Boolean
61 62 63 |
# File 'lib/usher/interface/rails2_2_interface.rb', line 61 def empty? @usher.route_count.zero? end |
#generate(options, recall = {}, method = :generate, route_name = nil) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/usher/interface/rails2_2_interface.rb', line 65 def generate(, recall = {}, method = :generate, route_name = nil) route = if(route_name) @usher.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
87 88 89 |
# File 'lib/usher/interface/rails2_2_interface.rb', line 87 def generate_url(route, params) @usher.generator.generate(route, params) end |
#install_helpers(destinations = [ActionController::Base, ActionView::Base], regenerate_code = false) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/usher/interface/rails2_2_interface.rb', line 118 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 } @usher.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 |
#load_routes! ⇒ Object
108 109 110 |
# File 'lib/usher/interface/rails2_2_interface.rb', line 108 def load_routes! reload end |
#named_routes ⇒ Object
95 96 97 |
# File 'lib/usher/interface/rails2_2_interface.rb', line 95 def named_routes @usher.named_routes end |
#path_for_options(options) ⇒ Object
91 92 93 |
# File 'lib/usher/interface/rails2_2_interface.rb', line 91 def () @usher.() end |
#recognize(request) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/usher/interface/rails2_2_interface.rb', line 44 def recognize(request) node = @usher.recognize(request) params = node.params.inject({}){|h,(k,v)| h[k]=v; h } request.path_parameters = (node.params.empty? ? node.path.route.destination : node.path.route.destination.merge(params)).with_indifferent_access "#{request.path_parameters[:controller].camelize}Controller".constantize rescue raise ActionController::RoutingError, "No route matches #{request.path.inspect} with #{request.inspect}" end |
#reload ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/usher/interface/rails2_2_interface.rb', line 99 def reload @usher.reset! if @configuration_file Kernel.load(@configuration_file) else @usher.add_route ":controller/:action/:id" end end |
#reset! ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/usher/interface/rails2_2_interface.rb', line 14 def reset! @usher ||= Usher.new(:generator => Usher::Util::Generators::URL.new) @module ||= Module.new @module.instance_methods.each do |selector| @module.class_eval { remove_method selector } end @controller_action_route_added = false @controller_route_added = false @usher.reset! end |
#route_count ⇒ Object
57 58 59 |
# File 'lib/usher/interface/rails2_2_interface.rb', line 57 def route_count @usher.route_count end |