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"]
    }}

    Parameters:

    • options (Hash)

      options The options hash

    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

    Parameters:

    • options (Hash)

      options The options hash

    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)

    Parameters:

    • map (Hash) (defaults to: {})

      see #update

    • options (Hash)

      The options hash



45
46
47
48
49
50
51
# File 'lib/mustermann/mapper.rb', line 45

def initialize(map = {}, additional_values: :ignore, **options, &block)
  @options           = options
  @additional_values = additional_values
  @set               = Set.new(use_trie: false, use_cache: false, **options)
  block.arity == 0 ? update(yield) : yield(self) if block
  update(map) if map
end

Instance Method Details

#[]=(key, value) ⇒ Object

Add a single mapping.

Parameters:

  • key (String, Pattern)

    format of the input string

  • value (String, Pattern, Arry<String, Pattern>, Expander)

    format of the output string



82
83
84
# File 'lib/mustermann/mapper.rb', line 82

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")


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

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

#to_hHash{Patttern: Expander}

Returns Hash version of the mapper.

Returns:

  • (Hash{Patttern: Expander})

    Hash version of the mapper.



64
# File 'lib/mustermann/mapper.rb', line 64

def to_h = @set.patterns.to_h { [_1, @set[_1]] }

#update(map) ⇒ Object

Add multiple mappings.

Parameters:

  • map (Hash{String, Pattern: String, Pattern, Arry<String, Pattern>, Expander})

    the mapping



56
57
58
59
60
61
# File 'lib/mustermann/mapper.rb', line 56

def update(map)
  map.to_h.each_pair do |input, output|
    output = Expander.new(*output, additional_values: @additional_values, **@options) unless output.is_a? Expander
    @set.add(input, output)
  end
end