Module: JADOF::PageAPI
- Included in:
- Page
- Defined in:
- lib/jadof/page.rb
Overview
This is the class interface for Page.
This is split out into a separate module so we can extend other classes (that inherit from Page) with this.
Actually, if you inherit from Page, you automatically get this module extended into your class.
Instance Attribute Summary collapse
-
#cache ⇒ #get, ...
This can be set to a standard cache object and, if it is set, all pages will be cached so they don’t have to be re-opened and we don’t have to look for the files.
-
#dir ⇒ String
The root directory that pages are loaded from.
-
#formatters ⇒ Hash{String => #call}
A Hash of available formatters.
Instance Method Summary collapse
-
#[](name) ⇒ Page
Alias for Page.get.
-
#all(conditions = nil) ⇒ Array(Page)
Get all Pages in Page.dir.
-
#cache_for(key, &block) ⇒ Object
Helper for caching.
-
#count ⇒ Fixnum
Returns the count of all Pages.
-
#first(conditions = nil) ⇒ Page?
Gets a page given some simple conditions.
-
#from_path(path) ⇒ Page
Loads a Page from a given path to a file.
-
#get(name) ⇒ Page
Get a Page by name.
-
#inherited(base) ⇒ Object
When a class inheritcs from Page (or from any class that inherits page), we extend that class with PageAPI so it will get methods like ‘Page.all`.
-
#last ⇒ Page?
Returns the last Page.
-
#matches_conditions?(page, conditions) ⇒ true, false
Helper for #where and #first.
-
#render(page) ⇒ String
(also: #to_html)
Page.formatters, we render and return the page #body.
-
#where(conditions) ⇒ Array(Page)
Gets pages given some simple conditions (only == equality is supported).
Instance Attribute Details
#cache ⇒ #get, ...
This can be set to a standard cache object and, if it is set, all pages will be cached so they don’t have to be re-opened and we don’t have to look for the files.
Any cache object that supports these standard methods is supported:
get(key)
set(key, value)
clear
29 30 31 |
# File 'lib/jadof/page.rb', line 29 def cache @cache end |
#dir ⇒ String
Returns The root directory that pages are loaded from. Defaults to “./pages/”.
14 15 16 |
# File 'lib/jadof/page.rb', line 14 def dir @dir end |
#formatters ⇒ Hash{String => #call}
A Hash of available formatters. The key is used to match a given file extension and the value should be something that you can #call (like a lambda) with text which returns the formatted text.
47 48 49 |
# File 'lib/jadof/page.rb', line 47 def formatters @formatters end |
Instance Method Details
#[](name) ⇒ Page
Returns Alias for Page.get.
57 58 59 |
# File 'lib/jadof/page.rb', line 57 def [] name get name end |
#all(conditions = nil) ⇒ Array(Page)
Returns Get all Pages in Page.dir.
62 63 64 65 66 67 |
# File 'lib/jadof/page.rb', line 62 def all conditions = nil pages = cache_for 'all' do Dir[ File.join(dir, "**/*") ].reject {|path| File.directory?(path) }.map {|path| from_path(path) } end conditions.nil? ? pages : where(conditions) end |
#cache_for(key, &block) ⇒ Object
Helper for caching. Will check to see if the #cache contains the given key and, if not, it will set the cache by calling the block given
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/jadof/page.rb', line 142 def cache_for key, &block return block.call unless cache from_cache = cache.get(key) unless from_cache from_cache = block.call cache.set(key, from_cache) end from_cache end |
#count ⇒ Fixnum
Returns the count of all Pages
70 71 72 |
# File 'lib/jadof/page.rb', line 70 def count all.length end |
#first(conditions = nil) ⇒ Page?
Returns Gets a page given some simple conditions.
85 86 87 88 89 90 91 |
# File 'lib/jadof/page.rb', line 85 def first conditions = nil if conditions all.find { |page| matches_conditions? page, conditions } else all.first end end |
#from_path(path) ⇒ Page
Returns Loads a Page from a given path to a file.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/jadof/page.rb', line 94 def from_path path path = File. path filename = File.basename path name = filename[/^[^\.]+/] # get everything before a . body = File.read path # Remove YAML from top of body and get the YAML variables from it. # Then we can merge in the name, path, etc, and use it to inialize a page body.sub! /^---(.*)\n---\n/m, '' variables = $1 ? YAML.load($1) : {} variables.merge! :name => name, :path => path, :filename => filename, :body => body # If the file is in a subdirectory, get the name of the subdirectory[ies] # and set it as :parent, so it's easily accessible from the Page. # Also, we strip the first and last '/' characters off of it. variables[:parent] = File.dirname(path).sub(dir, '').sub(/^\//, '') new variables end |
#get(name) ⇒ Page
Returns Get a Page by name.
52 53 54 |
# File 'lib/jadof/page.rb', line 52 def get name first :full_name => name.to_s end |
#inherited(base) ⇒ Object
When a class inheritcs from Page (or from any class that inherits page), we extend that class with JADOF::PageAPI so it will get methods like ‘Page.all`.
164 165 166 |
# File 'lib/jadof/page.rb', line 164 def inherited base base.extend PageAPI end |
#last ⇒ Page?
Returns the last Page
75 76 77 |
# File 'lib/jadof/page.rb', line 75 def last all.last end |
#matches_conditions?(page, conditions) ⇒ true, false
Returns Helper for #where and #first.
155 156 157 158 159 |
# File 'lib/jadof/page.rb', line 155 def matches_conditions? page, conditions matches = true conditions.each {|k,v| matches = false unless page.send(k) == v } matches end |
#render(page) ⇒ String Also known as: to_html
Page.formatters, we render and return the page #body.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/jadof/page.rb', line 116 def render page html = page.body page.extensions.reverse.each do |extension| # ["markdown", "erb"] if formatter = formatters[extension] begin html = formatter.call(html) rescue ArgumentError => ex if ex. == 'wrong number of arguments (1 for 2)' html = formatter.call(html, page) else raise end end end end html end |
#where(conditions) ⇒ Array(Page)
Returns Gets pages given some simple conditions (only == equality is supported).
80 81 82 |
# File 'lib/jadof/page.rb', line 80 def where conditions all.select { |page| matches_conditions? page, conditions } end |