Class: RoadForest::Dispatcher

Inherits:
Webmachine::Dispatcher
  • Object
show all
Defined in:
lib/roadforest/application/dispatcher.rb

Defined Under Namespace

Classes: RouteMap

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(services) ⇒ Dispatcher

Returns a new instance of Dispatcher.



8
9
10
11
12
13
14
# File 'lib/roadforest/application/dispatcher.rb', line 8

def initialize(services)
  super(method(:create_resource))
  @services = services
  @route_names = {}
  @route_mappings = []
  @trace_by_default = false
end

Instance Attribute Details

#servicesObject

Returns the value of attribute services.



15
16
17
# File 'lib/roadforest/application/dispatcher.rb', line 15

def services
  @services
end

#trace_by_defaultObject

Returns the value of attribute trace_by_default.



15
16
17
# File 'lib/roadforest/application/dispatcher.rb', line 15

def trace_by_default
  @trace_by_default
end

Instance Method Details

#add_route(name = nil, path_spec = nil, resource_type = nil, interface_class = nil) {|binder| ... } ⇒ Object Also known as: add

Add a named route to the dispatcher - the 90% case is handled by passing arguments in, but more control is available but manipulating the RouteBinding object used to create the Route and ResourceAdapter

Yields:

  • (binder)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/roadforest/application/dispatcher.rb', line 62

def add_route(name=nil, path_spec=nil, resource_type=nil, interface_class=nil)
  binder = Application::RouteBinding.new(self)

  binder.route_name = name
  binder.path_spec = path_spec
  binder.resource_type = resource_type
  binder.interface_class = interface_class

  yield binder if block_given?

  binder.validate!

  @route_names[name] = binder.route
  @routes << binder.route
  binder.route
end

#add_route_map(route_map) ⇒ Object



98
99
100
# File 'lib/roadforest/application/dispatcher.rb', line 98

def add_route_map(route_map)
  @route_mappings << route_map
end

#add_traced_route(name, path_spec, resource_type, interface_class, bindings = nil, &block) ⇒ Object Also known as: add_traced

Deprecated.

Just use add_route



90
91
92
93
94
95
# File 'lib/roadforest/application/dispatcher.rb', line 90

def add_traced_route(name, path_spec, resource_type, interface_class, bindings = nil, &block)
  add_route(name, path_spec, resource_type, interface_class) do |binder|
    binder.trace = true
    yield binder if block_given?
  end
end

#add_untraced_route(name = nil, path_spec = nil, resource_type = nil, interface_class = nil) ⇒ Object Also known as: add_untraced

Deprecated.

Just use add_route



81
82
83
84
85
86
# File 'lib/roadforest/application/dispatcher.rb', line 81

def add_untraced_route(name = nil, path_spec = nil, resource_type = nil, interface_class = nil)
  add_route(name, path_spec, resource_type, interface_class) do |binder|
    binder.trace = false
    yield binder if block_given?
  end
end

#default_content_engineObject



49
50
51
# File 'lib/roadforest/application/dispatcher.rb', line 49

def default_content_engine
  @services.default_content_engine
end

#each_name_and_route(&block) ⇒ Object



45
46
47
# File 'lib/roadforest/application/dispatcher.rb', line 45

def each_name_and_route(&block)
  @route_names.each_pair(&block)
end

#each_route(&block) ⇒ Object



41
42
43
# File 'lib/roadforest/application/dispatcher.rb', line 41

def each_route(&block)
  @routes.each(&block)
end

#find_route(*args) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/roadforest/application/dispatcher.rb', line 33

def find_route(*args)
  if block_given?
    @routes.find{|route| yield(route)}
  else
    super
  end
end

#map_in(route_name) ⇒ Object



135
136
137
# File 'lib/roadforest/application/dispatcher.rb', line 135

def map_in(route_name)
  RouteMap::Configurator.new(route_name, self)
end

#mapped_route_for_name(from, name, params) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/roadforest/application/dispatcher.rb', line 21

def mapped_route_for_name(from, name, params)
  mapping = @route_mappings.find do |mapping|
    mapping.matches?(from, name, params)
  end

  unless mapping.nil?
    name = mapping.to_name
  end

  return route_for_name(name)
end

#path_provider(route_name) ⇒ Object



53
54
55
# File 'lib/roadforest/application/dispatcher.rb', line 53

def path_provider(route_name)
  PathProvider.new(route_name, self)
end

#route_for_name(name) ⇒ Object



17
18
19
# File 'lib/roadforest/application/dispatcher.rb', line 17

def route_for_name(name)
  @route_names.fetch(name)
end