Class: Rack::Mount::Mappers::RailsDraft

Inherits:
Object
  • Object
show all
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

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, options = {})
  match(path, options.merge(:via => :delete))
end

#get(path, options = {}) ⇒ Object



34
35
36
# File 'lib/rack/mount/mappers/rails_draft.rb', line 34

def get(path, options = {})
  match(path, options.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, options = {}, &block)
  if block
    @scope_stack.push(options.merge({:path => path}))
    begin
      instance_eval(&block)
    ensure
      @scope_stack.pop
    end

    return
  end

  new_options = {}
  method = options.delete(:via)
  requirements = options.delete(:constraints) || {}
  defaults = {}

  if path.is_a?(Symbol) && scope_options.has_key?(:path)
    defaults[:action] = path.to_s
    path = scope_options[: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 = scope_options[:controller]
    defaults[:controller] = controller.to_s
  end

  if to = options.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, options = {})
  match(path, options.merge(:via => :post))
end

#put(path, options = {}) ⇒ Object



42
43
44
# File 'lib/rack/mount/mappers/rails_draft.rb', line 42

def put(path, options = {})
  match(path, options.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)
  options = entities.extract_options!
  entities.each { |entity| map_resource(entity, options.dup, &block) }
end