123456789_123456789_123456789_123456789_123456789_

Class: ActionDispatch::Journey::Format

Do not use. This class is for internal use only.
Relationships & Source Files
Namespace Children
Classes:
Inherits: Object
Defined in: actionpack/lib/action_dispatch/journey/visitors.rb

Constant Summary

Class Method Summary

Instance Method Summary

  • #evaluate(hash)
  • #add_to(buffer, hash) protected

    Appends this format's expansion to buffer - or nothing at all when a parameter is missing, which is how an optional group whose parameter is absent drops out of the generated path.

  • #join_adjacent_literals(parts) private

    Joins adjacent literals, so "/", "posts", "/" is appended as one "/posts/".

Constructor Details

.new(parts) ⇒ Format

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/journey/visitors.rb', line 22

def initialize(parts)
  @parts      = join_adjacent_literals(parts)
  @parameters = @parts.grep(Parameter)
end

Class Method Details

.required_path(symbol)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/journey/visitors.rb', line 14

def self.required_path(symbol)
  Parameter.new symbol, ESCAPE_PATH
end

.required_segment(symbol)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/journey/visitors.rb', line 18

def self.required_segment(symbol)
  Parameter.new symbol, ESCAPE_SEGMENT
end

Instance Method Details

#add_to(buffer, hash) (protected)

Appends this format's expansion to buffer - or nothing at all when a parameter is missing, which is how an optional group whose parameter is absent drops out of the generated path. Building into a shared buffer avoids the intermediate arrays and strings a join-based evaluation allocates on every generated URL.

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/journey/visitors.rb', line 38

protected def add_to(buffer, hash)
  return if @parameters.any? { |parameter| hash[parameter.name].nil? }

  @parts.each do |part|
    case part
    when String
      buffer << part
    when Journey::Format
      part.add_to(buffer, hash)
    else
      buffer << part.escape(hash[part.name])
    end
  end
end

#evaluate(hash)

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/journey/visitors.rb', line 27

def evaluate(hash)
  buffer = +""
  add_to(buffer, hash)
  buffer
end

#join_adjacent_literals(parts) (private)

Joins adjacent literals, so "/", "posts", "/" is appended as one "/posts/".

[ GitHub ]

  
# File 'actionpack/lib/action_dispatch/journey/visitors.rb', line 55

def join_adjacent_literals(parts)
  parts.each_with_object([]) do |part, joined|
    if String === part && String === joined.last
      joined[-1] = -(joined.last + part)
    else
      joined << part
    end
  end
end