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/static.rb,
lib/usher/route/variable.rb,
lib/usher/interface/rack/route.rb,
lib/usher/route/request_method.rb

Defined Under Namespace

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Route.



18
19
20
21
22
23
# File 'lib/usher/route.rb', line 18

def initialize(parsed_paths, router, conditions, requirements, default_values, generate_with, match_partially, priority)
  @router, @requirements, @conditions, @default_values, @match_partially, @priority = router, requirements, conditions, default_values, match_partially, priority
  @recognizable = true
  @paths = parsed_paths.collect {|path| Path.new(self, path)}
  @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.



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

def conditions
  @conditions
end

#default_valuesObject (readonly)

Returns the value of attribute default_values.



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

def default_values
  @default_values
end

#destinationObject (readonly)

Returns the value of attribute destination.



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

def destination
  @destination
end

#generate_withObject (readonly)

Returns the value of attribute generate_with.



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

def generate_with
  @generate_with
end

#grapherObject



36
37
38
39
40
41
42
# File 'lib/usher/route.rb', line 36

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

#match_partiallyObject (readonly)

Returns the value of attribute match_partially.



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

def match_partially
  @match_partially
end

#namedObject (readonly)

Returns the value of attribute named.



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

def named
  @named
end

#parent_routeObject

Returns the value of attribute parent_route.



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

def parent_route
  @parent_route
end

#pathsObject (readonly)

Returns the value of attribute paths.



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

def paths
  @paths
end

#priorityObject (readonly)

Returns the value of attribute priority.



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

def priority
  @priority
end

#recognizableObject

Returns the value of attribute recognizable.



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

def recognizable
  @recognizable
end

#requirementsObject (readonly)

Returns the value of attribute requirements.



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

def requirements
  @requirements
end

#routerObject

Returns the value of attribute router.



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

def router
  @router
end

Instance Method Details

#destination_keysObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/usher/route.rb', line 25

def destination_keys
  @destination_keys ||= case
  when Hash
    destination.keys
  when CompoundDestination
    destination.options.keys
  else
    nil
  end
end

#dupObject



58
59
60
61
62
63
64
# File 'lib/usher/route.rb', line 58

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

#find_matching_path(params) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/usher/route.rb', line 66

def find_matching_path(params)
  #if router.find_matching_paths_based_on_destination_keys?
  matching_path = if params.nil? || params.empty?
    @paths.first
  else
    @paths.size == 1 ? @paths.first : grapher.find_matching_path(params)
  end
  
  if matching_path.nil? and router.find_matching_paths_based_on_destination_keys?
    # do something
  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



132
133
134
135
# File 'lib/usher/route.rb', line 132

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'


126
127
128
129
130
# File 'lib/usher/route.rb', line 126

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

#partial_match?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/usher/route.rb', line 137

def partial_match?
  @match_partially
end

#recognizable!Object



49
50
51
52
# File 'lib/usher/route.rb', line 49

def recognizable!
  self.recognizable = true
  self
end

#recognizable?Boolean

Returns:

  • (Boolean)


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

def recognizable?
  self.recognizable
end

#redirect(path, status = 302) ⇒ Object

add(“/index.html”).redirect(“/”)



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/usher/interface/rack/route.rb', line 4

def redirect(path, status = 302)
  unless (300..399).include?(status)
    raise ArgumentError, "Status has to be an integer between 300 and 399"
  end
  @destination = lambda do |env|
    response = Rack::Response.new
    response.redirect(path, status)
    response.finish
  end
  return self
end

#to(*args, &block) ⇒ Object

Sets destination on a route. Returns self.

This method acceps varargs. If you pass in more than one variable, it will be returned to you wrapped in a CompoundDestination. If you send it varargs and the last member is a Hash, it will pop off the hash, and will be stored under #options. Otherwise, if you use send a single variable, or call it with a block, these will be returned to you by #destination.

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'}


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/usher/route.rb', line 102

def to(*args, &block)
  if !args.empty? && block
    @destination = CompoundDestination.new(args, block, args.last.is_a?(Hash) ? args.pop : {})
  elsif block.nil?
    case args.size
    when 0 
      raise "destination should be set as something"
    when 1
      @destination = args.first
    else
      @destination = CompoundDestination.new(args, nil, args.last.is_a?(Hash) ? args.pop : {})
    end
  else
    @destination = block
  end
  args.first.parent_route = self if args.first.respond_to?(:parent_route=)
  self
end

#unrecognizable!Object



44
45
46
47
# File 'lib/usher/route.rb', line 44

def unrecognizable!
  self.recognizable = false
  self
end