class Vidibus::Permalink::Dispatcher

Public Class Methods

new(path, options = {}) click to toggle source

Initialize a new Dispatcher instance. Provide an absolute path to be dispatched.

# File lib/vidibus/permalink/dispatcher.rb, line 9
def initialize(path, options = {})
  self.path = path
  @scope = options[:scope]
end

Public Instance Methods

found?() click to toggle source

Returns true if all parts could be resolved.

# File lib/vidibus/permalink/dispatcher.rb, line 37
def found?
  @is_found ||= begin
    !objects.include?(nil)
  end
end
objects() click to toggle source

Returns permalink objects matching the parts.

# File lib/vidibus/permalink/dispatcher.rb, line 32
def objects
  @objects ||= resolve_path
end
parts() click to toggle source

Returns parts of the path. Ignores file extension and request params

# File lib/vidibus/permalink/dispatcher.rb, line 27
def parts
  @parts ||= path.gsub(/(\.[^\/]+)?(\?.*)?$/, "").scan(/[^?\/]+/)
end
path() click to toggle source

Returns the path to dispatch.

# File lib/vidibus/permalink/dispatcher.rb, line 15
def path
  @path
end
path=(value) click to toggle source

Sets path to dispatch

# File lib/vidibus/permalink/dispatcher.rb, line 20
def path=(value)
  raise PathError.new("Path must be absolute.") unless value.match(/^\//)
  @path = value
end
redirect?() click to toggle source

Returns true if any part does not reflect the current permalink of the underlying linkable.

# File lib/vidibus/permalink/dispatcher.rb, line 45
def redirect?
  @is_redirect ||= begin
    return unless found?
    redirectables.any?
  end
end
redirect_path() click to toggle source

Returns the path to redirect to, if any.

# File lib/vidibus/permalink/dispatcher.rb, line 53
def redirect_path
  @redirect_path ||= begin
    return unless redirect?
    "/" << current_parts.join("/")
  end
end

Private Instance Methods

current_parts() click to toggle source

Returns an array containing the current permalinks of all objects.

# File lib/vidibus/permalink/dispatcher.rb, line 78
def current_parts
  objects.map { |o| o.current.value }
end
redirectables() click to toggle source
# File lib/vidibus/permalink/dispatcher.rb, line 82
def redirectables
  objects.select { |o| !o.current? }
end
resolve_path() click to toggle source
# File lib/vidibus/permalink/dispatcher.rb, line 62
def resolve_path
  results = ::Permalink.for_scope(@scope).any_in(value: parts)
  links = Array.new(parts.length)
  done = {}
  for result in results
    if i = parts.index(result.value)
      key = "#{result.linkable_type}##{result.linkable_id}"
      next if done[key]
      links[i] = result
      done[key] = true
    end
  end
  links
end