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

Instance Method Summary collapse

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

Returns:

  • (#get, #set, #clear)


29
30
31
# File 'lib/jadof/page.rb', line 29

def cache
  @cache
end

#dirString

Returns The root directory that pages are loaded from. Defaults to “./pages/”.

Returns:

  • (String)

    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

#formattersHash{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.

Returns:

  • (Hash{String => #call})


47
48
49
# File 'lib/jadof/page.rb', line 47

def formatters
  @formatters
end

Instance Method Details

#[](name) ⇒ Page

Returns Alias for Page.get.

Returns:

  • (Page)

    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.

Returns:

  • (Array(Page))

    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

#countFixnum

Returns the count of all Pages

Returns:

  • (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.

Returns:

  • (Page, nil)

    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.

Returns:

  • (Page)

    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.expand_path 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.

Returns:

  • (Page)

    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

#lastPage?

Returns the last Page

Returns:

  • (Page, nil)

    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.

Returns:

  • (true, false)

    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.

Returns:

  • (String)

    Using the #filename of the page given and available



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.message == '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).

Returns:

  • (Array(Page))

    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