Class: Rack::Mount::Mappers::Merb

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/mount/mappers/merb.rb

Defined Under Namespace

Classes: DeferredProc, RequestConditions

Constant Summary collapse

DynamicController =
lambda { |env|
  app = ActiveSupport::Inflector.camelize("#{env[Const::RACK_ROUTING_ARGS][:controller]}Controller")
  app = ActiveSupport::Inflector.constantize(app)
  app.call(env)
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(set) ⇒ Merb

Returns a new instance of Merb.



92
93
94
95
# File 'lib/rack/mount/mappers/merb.rb', line 92

def initialize(set)
  @set = set
  @root_behavior = ::Merb::Router::Behavior.new.defaults(:action => 'index')
end

Instance Attribute Details

#root_behaviorObject

Returns the value of attribute root_behavior.



90
91
92
# File 'lib/rack/mount/mappers/merb.rb', line 90

def root_behavior
  @root_behavior
end

Instance Method Details

#add_route(conditions, params, deferred_procs, options = {}) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rack/mount/mappers/merb.rb', line 109

def add_route(conditions, params, deferred_procs, options = {})
  new_conditions = {}
  new_conditions[:path] = conditions.delete(:path)[0]
  new_conditions[:method] = conditions.delete(:method)

  requirements = {}
  conditions.each do |k, v|
    if v.is_a?(Regexp)
      requirements[k.to_sym] = conditions.delete(k)
    end
  end

  if new_conditions[:path].is_a?(String)
    new_conditions[:path] = Utils.convert_segment_string_to_regexp(
      new_conditions[:path], requirements, %w( / . ? ))
  end

  app = params.has_key?(:controller) ?
    ActiveSupport::Inflector.constantize(ActiveSupport::Inflector.camelize("#{params[:controller]}Controller")) :
    DynamicController

  if deferred_procs.any?
    app = DeferredProc.new(app, deferred_procs.first)
  end

  if conditions.any?
    app = RequestConditions.new(app, conditions)
  end

  @set.add_route(app, new_conditions, params)
end

#prepare(first = [], last = [], &block) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rack/mount/mappers/merb.rb', line 97

def prepare(first = [], last = [], &block)
  Thread.current[:merb_routes] = []
  begin
    root_behavior._with_proxy(&block)
    routes = Thread.current[:merb_routes]
    routes.each { |route| add_route(*route) }
    self
  ensure
    Thread.current[:merb_routes] = nil
  end
end