Class: Mustermann::Mapper

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

Overview

A mapper allows mapping one string to another based on pattern parsing and expanding.

Examples:

require 'mustermann/mapper'
mapper = Mustermann::Mapper.new("/:foo" => "/:foo.html")
mapper['/example'] # => "/example.html"

Instance Method Summary collapse

Constructor Details

#initialize(**options) { ... } ⇒ Mapper #initialize(**options) {|mapper| ... } ⇒ Mapper #initialize(map = {}, **options) ⇒ Mapper

Creates a new mapper.

Overloads:

  • #initialize(**options) { ... } ⇒ Mapper

    Examples:

    require 'mustermann/mapper'
    Mustermann::Mapper.new(type: :rails) {{
      "/:foo" => ["/:foo.html", "/:foo.:format"]
    }}
    

    Yields:

    • block for generating mappings as a hash

    Yield Returns:

  • #initialize(**options) {|mapper| ... } ⇒ Mapper

    Examples:

    require 'mustermann/mapper'
    Mustermann::Mapper.new(type: :rails) do |mapper|
      mapper["/:foo"] = ["/:foo.html", "/:foo.:format"]
    end
    

    Yields:

    • block for generating mappings as a hash

    Yield Parameters:

  • #initialize(map = {}, **options) ⇒ Mapper

    Examples:

    map before options

    require 'mustermann/mapper'
    Mustermann::Mapper.new("/:foo" => "/:foo.html", type: :rails)
    

    map after options

    require 'mustermann/mapper'
    Mustermann::Mapper.new(type: :rails, "/:foo" => "/:foo.html")
    

47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mustermann/mapper.rb', line 47

def initialize(options = {}, &block)
  @map               = []
  @additional_values = options.delete(:additional_values) || :ignore
  @options           = options
  map = @options.inject({}) do |result, entry|
    result[entry[0]] = @options.delete(entry[0]) if entry[0].is_a?(String)
    result
  end
  block.arity == 0 ? update(yield) : yield(self) if block
  update(map) if map
end

Instance Method Details

#[]=(key, value) ⇒ Object

Add a single mapping.


92
93
94
# File 'lib/mustermann/mapper.rb', line 92

def []=(key, value)
  update key => value
end

#convert(input, values = {}) ⇒ Object Also known as: []

Convert a string according to mappings. You can pass in additional params.

Examples:

mapping with and without additional parameters

mapper = Mustermann::Mapper.new("/:example" => "(/:prefix)?/:example.html")

80
81
82
83
84
85
86
# File 'lib/mustermann/mapper.rb', line 80

def convert(input, values = {})
  @map.inject(input) do |current, (pattern, expander)|
    params = pattern.params(current)
    params &&= Hash[values.merge(params).map { |k,v| [k.to_s, v] }]
    expander.expandable?(params) ? expander.expand(params) : current
  end
end

#to_hHash{Patttern: Expander}


71
72
73
# File 'lib/mustermann/mapper.rb', line 71

def to_h
  Hash[@map]
end

#update(map) ⇒ Object

Add multiple mappings.


62
63
64
65
66
67
68
# File 'lib/mustermann/mapper.rb', line 62

def update(map)
  map.to_hash.each_pair do |input, output|
    input  = Mustermann.new(input, @options.dup)
    output = Expander.new(*output, @options.merge(additional_values: @additional_values)) unless output.is_a? Expander
    @map << [input, output]
  end
end