Class: Usher::Interface::Rails22

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

Direct Known Subclasses

Rails20

Defined Under Namespace

Classes: Mapper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRails22

Returns a new instance of Rails22.



10
11
12
# File 'lib/usher/interface/rails22.rb', line 10

def initialize
  reset!
end

Instance Attribute Details

#configuration_fileObject

Returns the value of attribute configuration_file.



8
9
10
# File 'lib/usher/interface/rails22.rb', line 8

def configuration_file
  @configuration_file
end

#usherObject (readonly)

Returns the value of attribute usher.



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

def usher
  @usher
end

Instance Method Details

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



56
57
58
# File 'lib/usher/interface/rails22.rb', line 56

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

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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/usher/interface/rails22.rb', line 28

def add_route(path, options = {})
  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 = @usher.add_route(path, 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

#draw(options = {}) {|Mapper.new(self)| ... } ⇒ Object

Yields:



115
116
117
118
119
# File 'lib/usher/interface/rails22.rb', line 115

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

#empty?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/usher/interface/rails22.rb', line 64

def empty?
  @usher.route_count.zero?
end

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/usher/interface/rails22.rb', line 68

def generate(options, recall = {}, method = :generate, route_name = nil)
  route = if(route_name)
    @usher.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



90
91
92
# File 'lib/usher/interface/rails22.rb', line 90

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

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



121
122
123
124
125
126
# File 'lib/usher/interface/rails22.rb', line 121

def install_helpers(destinations = [ActionController::Base, ActionView::Base], regenerate_code = false)
  Array(destinations).each do |destination|
    destination.module_eval { include Helpers }
    destination.__send__(:include, @usher.generator.generation_module)
  end
end

#load_routes!Object



111
112
113
# File 'lib/usher/interface/rails22.rb', line 111

def load_routes!
  reload
end

#named_routesObject



98
99
100
# File 'lib/usher/interface/rails22.rb', line 98

def named_routes
  @usher.named_routes
end

#path_for_options(options) ⇒ Object



94
95
96
# File 'lib/usher/interface/rails22.rb', line 94

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

#recognize(request) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/usher/interface/rails22.rb', line 47

def recognize(request)
  node = @usher.recognize(request)
  params = node.params_as_hash
  request.path_parameters = (node.params.empty? ? node.path.route.destination : node.path.route.destination.merge(params)).with_indifferent_access
  "#{request.path_parameters[:controller].camelize}Controller".constantize
rescue
  raise ActionController::RoutingError, "No route matches #{request.path.inspect} with #{request.inspect}"
end

#reloadObject



102
103
104
105
106
107
108
109
# File 'lib/usher/interface/rails22.rb', line 102

def reload
  @usher.reset!
  if @configuration_file
    Kernel.load(@configuration_file)
  else
    @usher.add_route ":controller/:action/:id"
  end
end

#reset!(options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/usher/interface/rails22.rb', line 14

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]

  @usher = Usher.new(options)
  @module ||= Module.new
  @module.instance_methods.each do |selector|
    @module.class_eval { remove_method selector }
  end
  @controller_action_route_added = false
  @controller_route_added = false
  @usher.reset!
end

#route_countObject



60
61
62
# File 'lib/usher/interface/rails22.rb', line 60

def route_count
  @usher.route_count
end

#routesObject



128
129
130
# File 'lib/usher/interface/rails22.rb', line 128

def routes
  @usher.routes
end