Class: PageMagic::Transitions

Inherits:
Hash show all
Defined in:
lib/page_magic/transitions.rb

Overview

class Transitions - used for registering the page classes that should be used against particular paths

Constant Summary collapse

REGEXP_MAPPING_MSG =
'URL could not be derived because mapping contains Regexps'

Instance Method Summary collapse

Methods inherited from Hash

#to_query

Constructor Details

#initialize(transitions) ⇒ Transitions

Create a new transitions object.

Examples:

Transitions.new('/path1' => Page1, Matcher.new('/another_*') => AnotherPageClass )

Parameters:



15
16
17
18
19
20
21
# File 'lib/page_magic/transitions.rb', line 15

def initialize(transitions)
  super
  transitions.each do |key, value|
    key = key.is_a?(Mapping) ? key : Mapping.new(key)
    self[key] = value
  end
end

Instance Method Details

#mapped_page(url) ⇒ PageMagic

get the page class mapped to the given url

Parameters:

  • url (String)
    • the url to search against

Returns:



39
40
41
# File 'lib/page_magic/transitions.rb', line 39

def mapped_page(url)
  matches(url).first
end

#url_for(page, base_url:) ⇒ Object

get the url to be used when visiting the path mapped against the given page

Parameters:

  • page (PageMagic)
    • the page class to get the mapped path from
  • base_url (String)
    • the base url of the site to be joined to the mapped path

Returns:

  • String

Raises:

  • InvalidURLException - Raised if it is not possible to generate the url for the mapped page i.e. if the mapping is a regular expression.



29
30
31
32
33
34
# File 'lib/page_magic/transitions.rb', line 29

def url_for(page, base_url:)
  return unless (mapping = key(page))
  raise InvalidURLException, REGEXP_MAPPING_MSG unless mapping.can_compute_uri?

  PageMagic::Utils::URL.concat(base_url, mapping.compute_uri)
end