Class: Pendragon::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/pendragon/route.rb

Overview

A class for defining the route

Examples:

route = Pendragon::Route.new("/:id", "GET", capture: id: /\d+/){|params| params[:id].to_s  }
route.match("/1234") #=> #<MatchData "/category/1234" id:"1234">
route.arity

Direct Known Subclasses

Padrino::Route

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Constructs a new instance of Pendragon::Route

Parameters:

  • path (String, Regexp)

    The path of route

  • verb (String, Symbol)

    The verb of route

  • options (Hash) (defaults to: {})

    The options hash



26
27
28
29
30
31
32
# File 'lib/pendragon/route.rb', line 26

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

Instance Attribute Details

#blockObject (readonly)

The verb should be read from Pendragon::Router



17
18
19
# File 'lib/pendragon/route.rb', line 17

def block
  @block
end

#captureObject

The accessors are useful to access from Pendragon::Router



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

def capture
  @capture
end

#indexObject

For compile option



14
15
16
# File 'lib/pendragon/route.rb', line 14

def index
  @index
end

#nameObject

The accessors are useful to access from Pendragon::Router



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

def name
  @name
end

#optionsObject

The accessors are useful to access from Pendragon::Router



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

def options
  @options
end

#orderObject

The accessors are useful to access from Pendragon::Router



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

def order
  @order
end

#router=(value) ⇒ Object (writeonly)

The router will be treated in this class.



20
21
22
# File 'lib/pendragon/route.rb', line 20

def router=(value)
  @router = value
end

#verbObject (readonly)

The verb should be read from Pendragon::Router



17
18
19
# File 'lib/pendragon/route.rb', line 17

def verb
  @verb
end

Instance Method Details

#arityFixnum

Returns arity of route block

Returns:

  • (Fixnum)


43
44
45
# File 'lib/pendragon/route.rb', line 43

def arity
  @block.arity
end

#call(*args) ⇒ Object

Calls the route block with arguments

Parameters:

  • args (Array)

    The arguments are passed to the route block



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

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

#match(pattern) ⇒ MatchData, Nil

Matches a pattern with the route matcher

Parameters:

  • pattern (String)

    The pattern will be matched with route matcher

Returns:

  • (MatchData)

    If the pattern matched this route, return a MatchData.

  • (Nil)

    If the pattern doesn’t matched this route, return a nil.



56
57
58
# File 'lib/pendragon/route.rb', line 56

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

#matcherPendragon::Matcher

Returns an instance of Pendragon::Matcherthat is associated with the route

Returns:

  • (Pendragon::Matcher)


36
37
38
39
# File 'lib/pendragon/route.rb', line 36

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

#params(pattern, parameters = {}) ⇒ Hash

Matches a pattern with the route matcher, and then returns the route params

Examples:

pendragon = Pendragon.new
route = pendragon.get("/category/:name"){}
route.params("/category/Doraemon") #=> {:name=>"Doraemon"}
route.params("/category/Doraemon", hey: "Hey") #=> {:name=>"Doraemon", :hey=>"Hey"}

Parameters:

  • pattern (String)

    The pattern will be matched with the matcher

  • parameters (Hash) (defaults to: {})

    The parameters are base of the route params

Returns:

  • (Hash)

    The params for use in routing engines



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/pendragon/route.rb', line 92

def params(pattern, parameters = {})
  match_data, params = match(pattern), indifferent_hash
  if match_data.names.empty?
    params.merge!(:captures => match_data.captures) unless match_data.captures.empty?
    params
  else
    params_from_matcher = matcher.handler.params(pattern, :captures => match_data)
    params.merge!(params_from_matcher) if params_from_matcher
    params.merge(parameters){|key, old, new| old || new }
  end
end

#path(*args) ⇒ String

Expands a path using parameters

Examples:

pendragon = Pendragon.new
route = pendragon.get("/category/:name"){}
route.path(name: "Doraemon") #=> "/category/Doraemon"
route.path(name: "Doraemon", hey: "Hey") #=> "/category/Doraemon?hey=Hey"

Parameters:

  • args (Array)

Returns:

  • (String)

    The expanded path



76
77
78
79
80
81
# File 'lib/pendragon/route.rb', line 76

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

#to { ... } ⇒ Object

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

Yields:

  • The route block



62
63
64
65
66
# File 'lib/pendragon/route.rb', line 62

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