Class: Rack::Mount::Mappers::RailsDraft
- Inherits:
-
Object
- Object
- Rack::Mount::Mappers::RailsDraft
- Defined in:
- lib/rack/mount/mappers/rails_draft.rb
Defined Under Namespace
Classes: RoutingError
Constant Summary collapse
- NotFound =
lambda { |env| raise RoutingError, "No route matches #{env[Const::PATH_INFO].inspect} with #{env.inspect}" }
- DynamicController =
lambda { |env| app = "#{env[Const::RACK_ROUTING_ARGS][:controller].camelize}Controller" app = ActiveSupport::Inflector.constantize(app) app.call(env) }
Instance Method Summary collapse
- #controller(controller, &block) ⇒ Object
- #delete(path, options = {}) ⇒ Object
- #get(path, options = {}) ⇒ Object
-
#initialize(set) ⇒ RailsDraft
constructor
A new instance of RailsDraft.
- #match(path, options = {}, &block) ⇒ Object
- #namespace(namespace, &block) ⇒ Object
- #post(path, options = {}) ⇒ Object
- #put(path, options = {}) ⇒ Object
- #resources(*entities, &block) ⇒ Object
Constructor Details
#initialize(set) ⇒ RailsDraft
Returns a new instance of RailsDraft.
28 29 30 31 32 |
# File 'lib/rack/mount/mappers/rails_draft.rb', line 28 def initialize(set) require 'action_controller' @set = set @scope_stack = [] end |
Instance Method Details
#controller(controller, &block) ⇒ Object
111 112 113 114 115 116 117 118 |
# File 'lib/rack/mount/mappers/rails_draft.rb', line 111 def controller(controller, &block) @scope_stack.push(:controller => controller) begin instance_eval(&block) ensure @scope_stack.pop end end |
#delete(path, options = {}) ⇒ Object
46 47 48 |
# File 'lib/rack/mount/mappers/rails_draft.rb', line 46 def delete(path, = {}) match(path, .merge(:via => :delete)) end |
#get(path, options = {}) ⇒ Object
34 35 36 |
# File 'lib/rack/mount/mappers/rails_draft.rb', line 34 def get(path, = {}) match(path, .merge(:via => :get)) end |
#match(path, options = {}, &block) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/rack/mount/mappers/rails_draft.rb', line 50 def match(path, = {}, &block) if block @scope_stack.push(.merge({:path => path})) begin instance_eval(&block) ensure @scope_stack.pop end return end = {} method = .delete(:via) requirements = .delete(:constraints) || {} defaults = {} if path.is_a?(Symbol) && .has_key?(:path) defaults[:action] = path.to_s path = [:path] elsif path.is_a?(Regexp) else scoped_path = @scope_stack.map { |scope| scope[:path] }.compact scoped_path << path if path.is_a?(String) scoped_path.map! { |path| path =~ /^\// ? path : "/#{path}" } path = scoped_path.join end if path.is_a?(String) path = optionalize_trailing_dynamic_segments(path) end if controller = [:controller] defaults[:controller] = controller.to_s end if to = .delete(:to) controller, action = to.to_s.split('#') if controller && action && defaults[:controller] defaults[:controller] = "#{defaults[:controller]}#{controller}" defaults[:action] = action elsif !action && defaults[:controller] defaults[:action] = controller if controller else defaults[:controller] = controller if controller defaults[:action] = action if action end end app = defaults.has_key?(:controller) ? ActiveSupport::Inflector.constantize("#{defaults[:controller].camelize}Controller") : DynamicController if path.is_a?(String) path = Utils.convert_segment_string_to_regexp(path, requirements, %w( / . ? )) end conditions = { :method => method, :path => path } @set.add_route(app, conditions, defaults) end |
#namespace(namespace, &block) ⇒ Object
120 121 122 123 124 125 126 127 |
# File 'lib/rack/mount/mappers/rails_draft.rb', line 120 def namespace(namespace, &block) @scope_stack.push(:path => namespace.to_s, :controller => "#{namespace}/") begin instance_eval(&block) ensure @scope_stack.pop end end |
#post(path, options = {}) ⇒ Object
38 39 40 |
# File 'lib/rack/mount/mappers/rails_draft.rb', line 38 def post(path, = {}) match(path, .merge(:via => :post)) end |
#put(path, options = {}) ⇒ Object
42 43 44 |
# File 'lib/rack/mount/mappers/rails_draft.rb', line 42 def put(path, = {}) match(path, .merge(:via => :put)) end |
#resources(*entities, &block) ⇒ Object
129 130 131 132 |
# File 'lib/rack/mount/mappers/rails_draft.rb', line 129 def resources(*entities, &block) = entities. entities.each { |entity| map_resource(entity, .dup, &block) } end |