class Mustermann::Flask::Converter

A class for easy creating of converters. @see Mustermann::Flask#register_converter

Attributes

constraint[RW]

Constraint on the format used for the capture. Should be a regexp (or a string corresponding to a regexp) @see Mustermann::Flask#register_converter

convert[RW]

Callback Should be a Proc. @see Mustermann::Flask#register_converter

node_type[RW]

Constraint on the format used for the capture. Should be a regexp (or a string corresponding to a regexp) @see Mustermann::Flask#register_converter @!visibility private

qualifier[RW]

Constraint on the format used for the capture. Should be a regexp (or a string corresponding to a regexp) @see Mustermann::Flask#register_converter @!visibility private

Public Class Methods

create(&block) click to toggle source

@!visibility private

# File lib/mustermann/flask.rb, line 67
def self.create(&block)
  Class.new(self) do
    define_method(:initialize) { |*a, **o| block[self, *a, **o] }
  end
end

Public Instance Methods

between(min, max) { |input| ... } click to toggle source

Makes sure a given value falls inbetween a min and a max. Uses the passed block to convert the value from a string to whatever format you'd expect.

@example

require 'mustermann/flask'

class MyPattern < Mustermann::Flask
  register_converter(:x) { between(5, 15, &:to_i) }
end

pattern = MyPattern.new('<x:id>')
pattern.params('/12') # => { 'id' => 12 }
pattern.params('/16') # => { 'id' => 15 }

@see Mustermann::Flask#register_converter

# File lib/mustermann/flask.rb, line 89
def between(min, max)
  self.convert = proc do |input|
    value = yield(input)
    value = yield(min) if min and value < yield(min)
    value = yield(max) if max and value > yield(max)
    value
  end
end