Class: Signpost::Route::Simple

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#constraintsObject (readonly)

Environment constraints

Returns: Array



30
31
32
# File 'lib/signpost/route/simple.rb', line 30

def constraints
  @constraints
end

#endpointObject (readonly)

Rack-compatible endpoint

Returns: Proc|Object



16
17
18
# File 'lib/signpost/route/simple.rb', line 16

def endpoint
  @endpoint
end

#matcherObject (readonly)

Route matcher/expander

Returns: Mustermann



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

def matcher
  @matcher
end

#nameObject (readonly)

Route name

Returns: Symbol



37
38
39
# File 'lib/signpost/route/simple.rb', line 37

def name
  @name
end

#paramsObject (readonly)

Specific route params

Returns: Hash



23
24
25
# File 'lib/signpost/route/simple.rb', line 23

def params
  @params
end

Instance Method Details

#expand(data = {}) ⇒ Object

Generate a string from path pattern

Params:

  • data Hash key-values for expanding

Example:

route.expand({ id: 42 }) #=> '/users/42'

Returns: String



72
73
74
# File 'lib/signpost/route/simple.rb', line 72

def expand(data={})
  matcher.expand(:append, data)
end

#match(path, env) ⇒ Object

Match path and return matched data or nil if path doesn’t match

Params:

  • path String uri path

Example:

route.match('/users/42') #=> { 'controller' => 'Users', 'action' => 'Show', 'id' => '42' }
route.match('/foo/bar')  #=> nil

Returns: Hash|NilClass



52
53
54
55
56
57
58
# File 'lib/signpost/route/simple.rb', line 52

def match(path, env)
  return unless data = matcher.match(path)

  return unless constraints.all? { |c| c.call(env) }

  params.merge(Hash[data.names.zip(data.captures)])
end