Class: Mustermann::Flask

Inherits:
AST::Pattern show all
Defined in:
lib/mustermann/flask.rb

Overview

Flask style pattern implementation.

Examples:

Mustermann.new('/<foo>', type: :flask) === '/bar' # => true

See Also:

Defined Under Namespace

Classes: Converter

Constant Summary

Constants inherited from Pattern

Pattern::PATTERN_METHODS

Constants included from Mustermann

DEFAULT_TYPE, VERSION

Instance Attribute Summary collapse

Attributes inherited from RegexpBased

#regexp

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AST::Pattern

#expand, #to_templates

Methods inherited from RegexpBased

#peek_match, #peek_size

Methods inherited from Pattern

#&, #===, #=~, #^, #expand, #match, #named_captures, #names, new, #params, #peek, #peek_match, #peek_params, #peek_size, supported?, supported_options, #to_proc, #to_s, #to_templates, #|

Methods included from Mustermann

[], new, register

Constructor Details

#initialize(input, options = {}) ⇒ Flask

Returns a new instance of Flask.



199
200
201
202
203
204
# File 'lib/mustermann/flask.rb', line 199

def initialize(input, options = {})
  converters = options[:converters] || {}
  @converters = self.class.converters.dup
  converters.each { |k,v| @converters[k.to_s] = v } if converters
  super(input, options)
end

Instance Attribute Details

#convertersObject (readonly)

Returns the value of attribute converters.



197
198
199
# File 'lib/mustermann/flask.rb', line 197

def converters
  @converters
end

Class Method Details

.register_converter(name, converter = nil, &block) ⇒ Object

Allows you to register your own converters.

It is reommended to use this on a subclass, so to not influence other subsystems using flask templates.

The object passed in as converter can implement #convert and/or #constraint.

It can also instead implement #new, which will then return an object responding to some of these methods. Arguments from the flask pattern will be passed to #new.

If passed a block, it will be yielded to with a Converter instance and any arguments in the flask pattern.

Examples:

with simple object

require 'mustermann/flask'

MyPattern    = Class.new(Mustermann::Flask)
up_converter = Struct.new(:convert).new(:upcase.to_proc)
MyPattern.register_converter(:upper, up_converter)

MyPattern.new("/<up:name>").params('/foo') # => { "name" => "FOO" }

with block

require 'mustermann/flask'

MyPattern    = Class.new(Mustermann::Flask)
MyPattern.register_converter(:upper) { |c| c.convert = :upcase.to_proc }

MyPattern.new("/<up:name>").params('/foo') # => { "name" => "FOO" }

with converter class

require 'mustermann/flasl'

class MyPattern < Mustermann::Flask
  class Converter
    attr_reader :convert
    def initialize(send: :to_s)
      @convert = send.to_sym.to_proc
    end
  end

  register_converter(:t, Converter)
end

MyPattern.new("/<t(send=upcase):name>").params('/Foo')   # => { "name" => "FOO" }
MyPattern.new("/<t(send=downcase):name>").params('/Foo') # => { "name" => "foo" }

Parameters:

  • name (#to_s)

    converter name

  • converter (#new, #convert, #constraint, nil) (defaults to: nil)


153
154
155
156
# File 'lib/mustermann/flask.rb', line 153

def self.register_converter(name, converter = nil, &block)
  converter ||= Converter.create(&block)
  converters(false)[name.to_s] = converter
end