Class: Usher::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/usher/route.rb,
lib/usher/route/path.rb,
lib/usher/route/util.rb,
lib/usher/route/variable.rb,
lib/usher/route/request_method.rb

Defined Under Namespace

Modules: Util Classes: GenerateWith, Path, RequestMethod, Variable

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parsed_paths, router, conditions, requirements, default_values, generate_with, match_partially) ⇒ Route

Returns a new instance of Route.



15
16
17
18
19
# File 'lib/usher/route.rb', line 15

def initialize(parsed_paths, router, conditions, requirements, default_values, generate_with, match_partially)
  @paths = parsed_paths.collect {|path| Path.new(self, path)}
  @router, @requirements, @conditions, @default_values, @match_partially = router, requirements, conditions, default_values, match_partially
  @generate_with = GenerateWith.new(generate_with[:scheme], generate_with[:port], generate_with[:host]) if generate_with
end

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



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

def conditions
  @conditions
end

#default_valuesObject (readonly)

Returns the value of attribute default_values.



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

def default_values
  @default_values
end

#destinationObject (readonly)

Returns the value of attribute destination.



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

def destination
  @destination
end

#generate_withObject (readonly)

Returns the value of attribute generate_with.



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

def generate_with
  @generate_with
end

#grapherObject



21
22
23
24
25
26
27
# File 'lib/usher/route.rb', line 21

def grapher
  unless @grapher
    @grapher = Grapher.new
    @grapher.add_route(self)
  end
  @grapher
end

#match_partiallyObject (readonly)

Returns the value of attribute match_partially.



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

def match_partially
  @match_partially
end

#namedObject (readonly)

Returns the value of attribute named.



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

def named
  @named
end

#parent_routeObject

Returns the value of attribute parent_route.



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

def parent_route
  @parent_route
end

#pathsObject (readonly)

Returns the value of attribute paths.



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

def paths
  @paths
end

#requirementsObject (readonly)

Returns the value of attribute requirements.



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

def requirements
  @requirements
end

#routerObject

Returns the value of attribute router.



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

def router
  @router
end

Instance Method Details

#dupObject



29
30
31
32
33
34
35
# File 'lib/usher/route.rb', line 29

def dup
  result = super
  result.instance_eval do
    @grapher = nil
  end
  result
end

#find_matching_path(params) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/usher/route.rb', line 37

def find_matching_path(params)
  if params.nil? || params.empty?
    matching_path = @paths.first
  else
    matching_path = @paths.size == 1 ? @paths.first : grapher.find_matching_path(params)
  end

  if parent_route
    matching_path = parent_route.find_matching_path(params).merge(matching_path)
    matching_path.route = self
  end

  matching_path
end

#match_partially!Object



81
82
83
84
# File 'lib/usher/route.rb', line 81

def match_partially!
  @match_partially = true
  self
end

#name(name) ⇒ Object

Sets route as referenceable from name. Returns self.

set = Usher.new
route = set.add_route('/test').name(:route)
set.generate_url(:route) => '/test'


75
76
77
78
79
# File 'lib/usher/route.rb', line 75

def name(name)
  @named = name
  @router.name(name, self)
  self
end

#partial_match?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/usher/route.rb', line 86

def partial_match?
  @match_partially
end

#to(options = nil, &block) ⇒ Object

Sets options on a route. Returns self.

Request = Struct.new(:path)
set = Usher.new
route = set.add_route('/test')
route.to(:controller => 'testing', :action => 'index')
set.recognize(Request.new('/test')).first.params => {:controller => 'testing', :action => 'index'}


59
60
61
62
63
64
65
66
67
68
# File 'lib/usher/route.rb', line 59

def to(options = nil, &block)
  raise "cannot set destination as block and argument" if block_given? && options
  @destination = if block_given?
    block
  else
    options.parent_route = self if options.respond_to?(:parent_route=)
    options
  end
  self
end