Changeset 680
- Timestamp:
- 09/24/07 07:03:31 (1 year ago)
- Files:
-
- branches/ry_routes/lib/merb.rb (modified) (1 diff)
- branches/ry_routes/lib/merb/action.rb (modified) (5 diffs)
- branches/ry_routes/lib/merb/constants.rb (modified) (1 diff)
- branches/ry_routes/lib/merb/core_ext/module.rb (modified) (2 diffs)
- branches/ry_routes/lib/merb/router.rb (modified) (3 diffs)
- branches/ry_routes/specs/merb/merb_action_spec.rb (added)
- branches/ry_routes/specs/merb/merb_simple_router.rb (added)
- branches/ry_routes/specs/spec_helper.rb (modified) (1 diff)
- branches/ry_routes/target_api.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/ry_routes/lib/merb.rb
r632 r680 30 30 autoload :AbstractController, 'merb/abstract_controller' 31 31 autoload :Const, 'merb/constants' 32 autoload :Action, 'merb/action' 32 33 autoload :Controller, 'merb/controller' 33 34 autoload :Dispatcher, 'merb/dispatcher' branches/ry_routes/lib/merb/action.rb
r678 r680 1 1 module Merb 2 2 class Action 3 include Merb::ControllerExceptions 4 class_inheritable_accessor :acceptable_methods 5 self.acceptable_methods = [:get, :head] # default acceptable methods 3 6 class << self 4 def base_url 5 name.split('::').join('/') 7 8 def method(*m) 9 self.acceptable_methods = m 6 10 end 7 11 8 def dispatch(request, status = 200) 9 unless @@acceptable_methods.include? request.method 10 raise NotAcceptable, "#{name} does not respond to #{request.method}" 12 def dispatch(request) 13 unless acceptable_methods.include?(request.method) 14 raise(ControllerExceptions::NotAcceptable, 15 "Action #{name} does not respond to HTTP method #{request.method}") 11 16 end 12 17 13 18 action = new(request) 14 action. build19 action.setup 15 20 16 21 # negotiate which content type to use 17 extension = request.accept _extensions.find do |ext|22 extension = request.acceptable_extensions.find do |ext| 18 23 responds_to.include?(ext) 24 end 25 26 unless extension 27 raise(ControllerExceptions::NotAcceptable, 28 "Action #{name} does not respond the requested content types") 19 29 end 20 30 … … 24 34 # render the action 25 35 unless request.method == :head 26 action.body = action.send(" to_" + extension)36 action.body = action.send("respond_" + extension.to_s) 27 37 end 28 38 … … 35 45 # the list is based on instance methods defined as "to_extension" 36 46 def responds_to 37 @@responds_to ||= instance_methods.grep(/^to_(.+)/).map { |x| x[3..-1] } 38 end 39 private :responds_to 40 41 def method(*m) 42 @@acceptable_methods = [:get, :head].include?(m) ? [:get, :head] : [m] 47 @@responds_to ||= begin 48 Const::TYPES.keys.find_all { |ext| method_defined?("respond_" + ext) } 49 end 43 50 end 44 51 end … … 48 55 end 49 56 50 def build57 def setup 51 58 end 52 59 … … 71 78 end 72 79 80 def status 81 @_status ||= 200 82 end 83 84 def status=(s) 85 @_status = s 86 end 87 88 def body=(b) 89 @_body = b 90 end 91 73 92 def body 74 @_body 93 @_body 75 94 end 76 95 end branches/ry_routes/lib/merb/constants.rb
r678 r680 13 13 :yaml => %w[application/x-yaml text/yaml], 14 14 :text => %w[text/plain], 15 :json => %w[application/json], 15 16 :html => %w[text/html application/xhtml+xml application/html], 16 17 :xml => %w[application/xml text/xml application/x-xml], branches/ry_routes/lib/merb/core_ext/module.rb
r678 r680 1 1 class Module 2 3 def tree 4 submodules.inject({}) do |hash, sub| 5 hash.update sub => sub.tree 6 end 7 end 2 8 3 9 def submodules … … 6 12 c if c.kind_of?(Module) 7 13 end.compact 14 end 15 16 def nest(m) 17 const_set(m.name.split('::').last, m) 8 18 end 9 19 branches/ry_routes/lib/merb/router.rb
r678 r680 1 module Actions1 module Root 2 2 end 3 3 4 4 module Merb 5 class SimpleRouter 6 5 class Router 7 6 class << self 8 7 9 8 def table 10 @@table ||= module_route_table( Actions)9 @@table ||= module_route_table(Root) 11 10 end 12 11 … … 14 13 hash = {} 15 14 mod.submodules.each do |sub| 16 hash[sub.base_url] = sub if sub.respond_to?(:base_url) 17 hash.update module_route_table(sub) 15 hash[sub.name.split('::').last] = 16 if sub.respond_to?(:superclass) and sub.superclass == Merb::Action 17 sub 18 else 19 module_route_table(sub) 20 end 18 21 end 19 22 hash … … 21 24 22 25 def match(path) 23 # this can be optimized a lot.26 # TODO: normalize path 24 27 sections = path.split('/') 25 sections.length.times do 26 j = sections.join('/') 27 return table[j] if table.has_key?(j) 28 sections.pops 28 sections.shift # remove initial "" 29 h = sections.inject(table) do |h, sec| 30 return nil unless h.respond_to?(:[]) 31 h[sec] 32 end 33 if h.respond_to?(:superclass) and h.superclass == Merb::Action 34 h 35 else 36 nil 29 37 end 30 38 end branches/ry_routes/specs/spec_helper.rb
r627 r680 13 13 14 14 FIXTURES = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures')) 15 16 15 17 16 # Creates a new controller, e.g. branches/ry_routes/target_api.rb
r678 r680 1 2 1 module Actions::Products 3 2 before Index do |req| 4 session[:user].admin?3 req.session[:user].admin? 5 4 end 6 5 7 6 class Index < Action 8 def build7 def setup 9 8 @products = Product.paginate(params[:page] || 1) 10 9 end 11 10 12 def to_json11 def respond_json 13 12 @products.to_json 14 13 end 15 14 16 def to_html 17 render 18 end 15 # By default Action implements this behavior 16 # so no need to state it explicitly 17 # 18 # def respond_html 19 # render 20 # end 19 21 end 20 22 21 23 class Update < Action 22 24 method :post 23 extra_route':id'24 def build25 url_params ':id' 26 def setup 25 27 @product = Product.find(req.params[:id]) 26 28 end
