Class: Padrino::PathRouter::Route

Inherits:
Object
  • Object
show all
Defined in:
padrino-core/lib/padrino-core/path_router/route.rb

Constant Summary

SIGNIFICANT_VARIABLES_REGEX =
/(^|[^\\])[:\*]([a-zA-Z0-9_]+)/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, verb, options = {}, &block) ⇒ Route

Constructs an instance of PathRouter::Route.



28
29
30
31
32
33
34
35
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 28

def initialize(path, verb, options = {}, &block)
  @path = path
  @verb = verb
  @capture = {}
  @order   = 0
  @block   = block if block_given?
  merge_with_options!(options)
end

Instance Attribute Details

#actionObject

The accessors will be used in other classes



22
23
24
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 22

def action
  @action
end

#blockObject (readonly)

A reader for compile option



12
13
14
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 12

def block
  @block
end

#cacheObject

The accessors will be used in other classes



22
23
24
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 22

def cache
  @cache
end

#cache_expiresObject

The accessors will be used in other classes



22
23
24
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 22

def cache_expires
  @cache_expires
end

#cache_keyObject

The accessors will be used in other classes



22
23
24
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 22

def cache_key
  @cache_key
end

#captureObject

The accessors are useful to access from PathRouter::Router



7
8
9
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 7

def capture
  @capture
end

#controllerObject

The accessors will be used in other classes



22
23
24
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 22

def controller
  @controller
end

#default_valuesObject

The accessors will be used in other classes



22
23
24
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 22

def default_values
  @default_values
end

#indexObject

The accessors are useful to access from PathRouter::Router



7
8
9
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 7

def index
  @index
end

#nameObject

The accessors are useful to access from PathRouter::Router



7
8
9
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 7

def name
  @name
end

#optionsObject

The accessors are useful to access from PathRouter::Router



7
8
9
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 7

def options
  @options
end

#orderObject

The accessors are useful to access from PathRouter::Router



7
8
9
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 7

def order
  @order
end

#parentObject

The accessors will be used in other classes



22
23
24
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 22

def parent
  @parent
end

#path_for_generationObject

The accessors will be used in other classes



22
23
24
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 22

def path_for_generation
  @path_for_generation
end

#regexpObject

The accessors are useful to access from PathRouter::Router



7
8
9
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 7

def regexp
  @regexp
end

#router=(value) ⇒ Object (writeonly)

The router will be treated in this class



17
18
19
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 17

def router=(value)
  @router = value
end

#use_layoutObject

The accessors will be used in other classes



22
23
24
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 22

def use_layout
  @use_layout
end

#user_agentObject

The accessors will be used in other classes



22
23
24
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 22

def user_agent
  @user_agent
end

#verbObject (readonly)

A reader for compile option



12
13
14
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 12

def verb
  @verb
end

Instance Method Details

#after_filters(&block) ⇒ Object

Returns after_filters as an array.



136
137
138
139
140
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 136

def after_filters(&block)
  @_after_filters ||= []
  @_after_filters << block if block_given?
  @_after_filters
end

#before_filters(&block) ⇒ Object

Returns before_filters as an array.



127
128
129
130
131
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 127

def before_filters(&block)
  @_before_filters ||= []
  @_before_filters << block if block_given?
  @_before_filters
end

#block_parameter_lengthObject

Returns block parameter length.



154
155
156
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 154

def block_parameter_length
  matcher.capture_length
end

#call(app, *args) ⇒ Object

Calls the route block with arguments.



40
41
42
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 40

def call(app, *args)
  @block.call(app, *args)
end

#custom_conditions(&block) ⇒ Object

Returns custom_conditions as an array.



145
146
147
148
149
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 145

def custom_conditions(&block)
  @_custom_conditions ||= []
  @_custom_conditions << block if block_given?
  @_custom_conditions
end

#match(pattern) ⇒ Object

See Also:



84
85
86
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 84

def match(pattern)
  matcher.match(pattern)
end

#matcherObject

Returns an instance of PathRouter::Matcher that is associated with the route.



77
78
79
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 77

def matcher
  @matcher ||= Matcher.new(@path, :capture => @capture, :default_values => default_values)
end

#original_pathObject

Returns the original path.



54
55
56
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 54

def original_path
  @path
end

#params_for(pattern, others = {}) ⇒ Object

Returns parameters which is created by the matcher.



120
121
122
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 120

def params_for(pattern, others = {})
  matcher.params_for(pattern, others)
end

#path(*args) ⇒ Object

Expands the path by using parameters.

See Also:



101
102
103
104
105
106
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 101

def path(*args)
  return @path if args.empty?
  params = args[0].dup
  params.delete(:captures)
  matcher.expand(params) if matcher.mustermann?
end

#path=(pattern) ⇒ Object

Overwrites path value by passing new path string.



111
112
113
114
115
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 111

def path=(pattern)
  @path = pattern
  @matcher = nil
  @significant_variable_names = nil
end

#request_methodsObject

Returns the route's verb as an array.



47
48
49
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 47

def request_methods
  [verb.to_s.upcase]
end

#significant_variable_namesObject

Returns signficant variable names.



63
64
65
66
67
68
69
70
71
72
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 63

def significant_variable_names
  @significant_variable_names ||=
    if @path.is_a?(String)
      @path.scan(SIGNIFICANT_VARIABLES_REGEX).map{ |p| p.last.to_sym }
    elsif @path.is_a?(Regexp) and @path.respond_to?(:named_captures)
      @path.named_captures.keys.map(&:to_sym)
    else
      []
    end
end

#to(&block) ⇒ Object

Associates a block with the route, and increments current order of the router.



91
92
93
94
95
# File 'padrino-core/lib/padrino-core/path_router/route.rb', line 91

def to(&block)
  @block = block if block_given?
  @order = @router.current_order
  @router.increment_order
end