Class: Usher::Interface::Rails23

Inherits:
Object
  • Object
show all
Defined in:
lib/usher/interface/rails23.rb,
lib/usher/interface/rails23/mapper.rb

Defined Under Namespace

Classes: Mapper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRails23

Returns a new instance of Rails23.



36
37
38
# File 'lib/usher/interface/rails23.rb', line 36

def initialize
  reset!
end

Instance Attribute Details

#configuration_filesObject (readonly)

Returns the value of attribute configuration_files.



5
6
7
# File 'lib/usher/interface/rails23.rb', line 5

def configuration_files
  @configuration_files
end

Instance Method Details

#add_configuration_file(file) ⇒ Object



40
41
42
# File 'lib/usher/interface/rails23.rb', line 40

def add_configuration_file(file)
  @configuration_files << file
end

#add_named_route(name, route, options = {}) ⇒ Object



11
12
13
# File 'lib/usher/interface/rails23.rb', line 11

def add_named_route(name, route, options = {})
  add_route(route, options).name(name)
end

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/usher/interface/rails23.rb', line 15

def add_route(path, options = {})
  path.gsub!(/(\..*?(?!\)))$/, '(\1)')
  if !@controller_action_route_added && path =~ %r{^/?:controller/:action/:id$}
    add_route('/:controller/:action', options.dup)
    @controller_action_route_added = true
  end

  if !@controller_route_added && path =~ %r{^/?:controller/:action$}
    add_route('/:controller', options.merge({:action => 'index'}))
    @controller_route_added = true
  end

  options[:action] = 'index' unless options[:action]

  path[0, 0] = '/' unless path[0] == ?/
  route = @router.add_route(path, options).to(options)

  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

#call(env) ⇒ Object



62
63
64
65
66
# File 'lib/usher/interface/rails23.rb', line 62

def call(env)
  request = ActionController::Request.new(env)
  app = recognize(request)
  app.call(env).to_a
end

#draw(options = {}) {|ActionController::Routing::RouteSet::Mapper.new(self)| ... } ⇒ Object

Yields:

  • (ActionController::Routing::RouteSet::Mapper.new(self))


85
86
87
88
89
# File 'lib/usher/interface/rails23.rb', line 85

def draw(options={})
  reset!(options)
  yield ActionController::Routing::RouteSet::Mapper.new(self)
  install_helpers
end

#generate(options, recall = {}, method = :generate, route_name = nil) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/usher/interface/rails23.rb', line 114

def generate(options, recall = {}, method = :generate, route_name = nil)
  route = if(route_name)
    @router.named_routes[route_name]
  else
    merged_options = options
    merged_options[:controller] = recall[:controller] unless options.key?(:controller)
    unless options.key?(:action)
      options[:action] = ''
    end
    path_for_options(merged_options)
  end
  case method
    when :generate
      merged_options ||= recall.merge(options)
      url = generate_url(route, merged_options)
      url.slice!(-1) if url[-1] == ?/
      url
    else
      raise "method #{method} not recognized"
  end
end

#generate_url(route, params) ⇒ Object



136
137
138
# File 'lib/usher/interface/rails23.rb', line 136

def generate_url(route, params)
  @router.generator.generate(route, params)
end

#install_helpers(destinations = [ActionController::Base, ActionView::Base], regenerate_code = false) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/usher/interface/rails23.rb', line 91

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 }
    @router.named_routes.keys.each do |name|
      @module.module_eval <<-end_eval # We use module_eval to avoid leaks
        def #{name}_url(options = {})
          ActionController::Routing::Routes.generate(options, {}, :generate, :#{name})
        end
        def #{name}_path(options = {})
          ActionController::Routing::Routes.generate(options, {}, :generate, :#{name})
        end
      end_eval
    end
    d.__send__(:include, @module)
    @router.named_routes.instance_eval "
      def helpers
        { }
      end
    "
    @router.named_routes.helpers.__send__(:extend, @module)
  end
end

#named_routesObject



7
8
9
# File 'lib/usher/interface/rails23.rb', line 7

def named_routes
  @router.named_routes
end

#path_for_options(options) ⇒ Object



140
141
142
# File 'lib/usher/interface/rails23.rb', line 140

def path_for_options(options)
  @router.path_for_options(options)
end

#recognize(request) ⇒ Object



68
69
70
71
72
73
# File 'lib/usher/interface/rails23.rb', line 68

def recognize(request)
  response = @router.recognize(request)
  request.path_parameters.merge!(response.destination)
  request.path_parameters.merge!(response.params_as_hash)
  "#{request.path_parameters[:controller].camelize}Controller".constantize
end

#reload!Object Also known as: reload



44
45
46
47
48
49
50
51
# File 'lib/usher/interface/rails23.rb', line 44

def reload!
  if configuration_files.any?
    configuration_files.each { |config| load(config) }
  else
    add_route ":controller/:action/:id"
  end

end

#reset!(options = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/usher/interface/rails23.rb', line 75

def reset!(options={})
  options[:generator] = options[:generator] || Usher::Util::Generators::URL.new
  options[:request_methods] = options[:request_methods] || [:protocol, :domain, :port, :query_string, :remote_ip, :user_agent, :referer, :method, :subdomains]
  @router = Usher.new(options)
  @configuration_files = []
  @module ||= Module.new
  @controller_route_added = false
  @controller_action_route_added = false
end

#route_countObject



54
55
56
# File 'lib/usher/interface/rails23.rb', line 54

def route_count
  routes.size
end

#routesObject



58
59
60
# File 'lib/usher/interface/rails23.rb', line 58

def routes
  @router.routes
end