class Journey::Formatter - journey-1.0.4 Documentation

# class Journey::Formatter

The Formatter class is used for formatting URLs. For example, parameters passed to `url_for` in rails will eventually call #generate

### Attributes

routes[R]

### Public Class Methods

new(routes) click to toggle source

    # File lib/journey/formatter.rb, line 8
    def initialize routes
      @routes = routes
      @cache  = nil
    end

### Public Instance Methods

clear() click to toggle source

    # File lib/journey/formatter.rb, line 49
    def clear
      @cache = nil
    end

generate(key, name, options, recall = {}) click to toggle source

    # File lib/journey/formatter.rb, line 13
    def generate key, name, options, recall = {}, parameterize = nil
      constraints = recall.merge options

      match_route(name, constraints) do |route|
        data = constraints.dup

        keys_to_keep = route.parts.reverse.drop_while { |part|
          !options.key?(part) || (options[part] || recall[part]).nil?
        } | route.required_parts

        (data.keys - keys_to_keep).each do |bad_key|
          data.delete bad_key
        end

        parameterized_parts = data.dup

        if parameterize
          parameterized_parts.each do |k,v|
            parameterized_parts[k] = parameterize.call(k, v)
          end
        end

        parameterized_parts.keep_if { |_,v| v  }

        next if !name && route.requirements.empty? && route.parts.empty?

        next unless verify_required_parts!(route, parameterized_parts)

        z = Hash[options.to_a - data.to_a - route.defaults.to_a]

        return [route.format(parameterized_parts), z]
      end

      raise Router::RoutingError
    end