Class: Vidibus::Permalink::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/vidibus/permalink/dispatcher.rb

Defined Under Namespace

Classes: PathError

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Dispatcher

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



9
10
11
12
# File 'lib/vidibus/permalink/dispatcher.rb', line 9

def initialize(path, options = {})
  self.path = path
  @scope = options[:scope]
end

Instance Method Details

#found?Boolean

Returns true if all parts could be resolved.

Returns:

  • (Boolean)


37
38
39
40
41
# File 'lib/vidibus/permalink/dispatcher.rb', line 37

def found?
  @is_found ||= begin
    !objects.include?(nil)
  end
end

#objectsObject

Returns permalink objects matching the parts.



32
33
34
# File 'lib/vidibus/permalink/dispatcher.rb', line 32

def objects
  @objects ||= resolve_path
end

#partsObject

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



27
28
29
# File 'lib/vidibus/permalink/dispatcher.rb', line 27

def parts
  @parts ||= path.gsub(/(\.[^\/]+)?(\?.*)?$/, "").scan(/[^?\/]+/)
end

#pathObject

Returns the path to dispatch.



15
16
17
# File 'lib/vidibus/permalink/dispatcher.rb', line 15

def path
  @path
end

#path=(value) ⇒ Object

Sets path to dispatch

Raises:



20
21
22
23
# 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?Boolean

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

Returns:

  • (Boolean)


45
46
47
48
49
50
# File 'lib/vidibus/permalink/dispatcher.rb', line 45

def redirect?
  @is_redirect ||= begin
    return unless found?
    redirectables.any?
  end
end

#redirect_pathObject

Returns the path to redirect to, if any.



53
54
55
56
57
58
# File 'lib/vidibus/permalink/dispatcher.rb', line 53

def redirect_path
  @redirect_path ||= begin
    return unless redirect?
    "/" << current_parts.join("/")
  end
end