Class: Mustermann::Router::Simple

Inherits:
Object
  • Object
show all
Defined in:
lib/mustermann/router/simple.rb

Overview

Simple pattern based router that allows matching a string to a given callback.

Examples:

require 'mustermann/router/simple'

router = Mustermann::Router::Simple.new do
  on ':name/:sub' do |string, params|
    params['sub']
  end

  on 'foo' do
    "bar"
  end
end

router.call("foo") # => "bar"
router.call("a/b") # => "b"
router.call("bar") # => nil

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default: nil, **options, &block) ⇒ Mustermann::Router::Simple

Returns new router instance.

Examples:

with a default value

require 'mustermann/router/simple'

router = Mustermann::Router::Simple.new(default: 42)
router.on(':name', capture: :digit) { |string| string.to_i }
router.call("23")      # => 23
router.call("example") # => 42

block with implicit receiver

require 'mustermann/router/simple'

router = Mustermann::Router::Simple.new do
  on('/foo') { 'foo' }
  on('/bar') { 'bar' }
end

block with explicit receiver

require 'mustermann/router/simple'

router = Mustermann::Router::Simple.new(type: :rails) do |r|
  r.on('/foo') { 'foo' }
  r.on('/bar') { 'bar' }
end

Parameters:

  • default (defaults to: nil)

    value to be returned if nothing matches

  • options (Hash)

    pattern options



54
55
56
57
58
59
60
# File 'lib/mustermann/router/simple.rb', line 54

def initialize(default: nil, **options, &block)
  @options = options
  @map     = []
  @default = default

  block.arity == 0 ? instance_eval(&block) : yield(self) if block
end

Instance Attribute Details

#defaultObject

Default value for when no pattern matches



25
26
27
# File 'lib/mustermann/router/simple.rb', line 25

def default
  @default
end

Instance Method Details

#[](string) ⇒ #call?

Returns callback for given string, if a pattern matches.

Examples:

require 'mustermann/router/simple'

router = Mustermann::Router::Simple.new
router.on(':a/:b') { 42 }
router['foo/bar'] # => <#Proc:...>
router['foo_bar'] # => nil

Returns:

  • (#call, nil)

    callback for given string, if a pattern matches



71
72
73
74
# File 'lib/mustermann/router/simple.rb', line 71

def [](string)
  string = string_for(string) unless string.is_a? String
  @map.detect { |p,v| p === string }[1]
end

#[]=(pattern, callback) ⇒ Object

Examples:

require 'mustermann/router/simple'

router = Mustermann::Router::Simple.new
router['/:name'] = proc { |string, params| params['name'] }
router.call('/foo') # => "foo"

Parameters:

See Also:



86
87
88
# File 'lib/mustermann/router/simple.rb', line 86

def []=(pattern, callback)
  on(pattern, call: callback)
end

#call(input, &fallback) ⇒ Object

Finds the matching callback and calls ‘call` on it with the given input and the params.

Returns:

  • the callback’s return value



121
122
123
124
125
126
127
128
129
# File 'lib/mustermann/router/simple.rb', line 121

def call(input, &fallback)
  @map.each do |pattern, callback|
    catch(:pass) do
      next unless params = pattern.params(string_for(input))
      return invoke(callback, input, params, pattern)
    end
  end
  @default
end

#on(*patterns, call: Proc.new, **options) ⇒ Object

Examples:

with block

require 'mustermann/router/simple'

router = Mustermann::Router::Simple.new

router.on(':a/:b') { 42 }
router.call('foo/bar') # => 42
router.call('foo_bar') # => nil

with callback option

require 'mustermann/router/simple'

callback = proc { 42 }
router   = Mustermann::Router::Simple.new

router.on(':a/:b', call: callback)
router.call('foo/bar') # => 42
router.call('foo_bar') # => nil

Parameters:

  • patterns (Array<String, Pattern>)
  • call (#call) (defaults to: Proc.new)

    callback object, need to hand in block if missing

  • options (Hash)

    pattern options



112
113
114
115
116
117
# File 'lib/mustermann/router/simple.rb', line 112

def on(*patterns, call: Proc.new, **options)
  patterns.each do |pattern|
    pattern = Mustermann.new(pattern.to_str, **options, **@options) if pattern.respond_to? :to_str
    @map << [pattern, call]
  end
end