Class: SatanicPages::PageList

Inherits:
Object
  • Object
show all
Defined in:
lib/satanic_pages/page_list.rb

Defined Under Namespace

Classes: PageNotFound

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller_name) ⇒ PageList

Returns a new instance of PageList.



8
9
10
11
12
13
14
15
16
17
# File 'lib/satanic_pages/page_list.rb', line 8

def initialize(controller_name)
  @base_path = Rails.root.join("app/views/", controller_name)

  @pages = Dir[base_path + "**/*.html*"]
    .reject { |path| path.match?(/(^_|\/_)/) }
    .each_with_object({}) do |full_path, hash|
      page = Page.new(full_path, base_path)
      hash[page.path] = page
    end
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



19
20
21
# File 'lib/satanic_pages/page_list.rb', line 19

def base_path
  @base_path
end

#pagesObject (readonly)

Returns the value of attribute pages.



19
20
21
# File 'lib/satanic_pages/page_list.rb', line 19

def pages
  @pages
end

Instance Method Details

#find(path) ⇒ Object



27
28
29
30
31
# File 'lib/satanic_pages/page_list.rb', line 27

def find(path)
  find!(path)
rescue PageNotFound
  nil
end

#find!(path) ⇒ Object



21
22
23
24
25
# File 'lib/satanic_pages/page_list.rb', line 21

def find!(path)
  @pages.fetch(path)
rescue KeyError
  raise PageNotFound, "No page `#{path}' found at #{base_path}"
end

#match?(req_path) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/satanic_pages/page_list.rb', line 33

def match?(req_path)
  path = req_path.gsub(/^\//, "")
  pages.keys.include?(path)
end