Class: Page

Inherits:
Object
  • Object
show all
Defined in:
lib/wiki/page.rb

Overview

Page Class Handles writing and reading JSON data to and from files.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_directoryObject

Directory where default (pre-existing) pages are stored.



14
15
16
# File 'lib/wiki/page.rb', line 14

def default_directory
  @default_directory
end

#directoryObject

Directory where pages are to be stored.



12
13
14
# File 'lib/wiki/page.rb', line 12

def directory
  @directory
end

#plugins_directoryObject

Directory where plugins that may have pages are stored.



16
17
18
# File 'lib/wiki/page.rb', line 16

def plugins_directory
  @plugins_directory
end

Instance Method Details

#exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
# File 'lib/wiki/page.rb', line 46

def exists?(name)
  Store.exists?(File.join(directory, name)) or
  File.exist?(File.join(default_directory, name)) or
  !plugin_page_path(name).nil?
end

#get(name) ⇒ Hash

Get a page

Parameters:

  • name (String)
    • The name of the file to retrieve, relative to Page.directory.

Returns:

  • (Hash)

    The contents of the retrieved page (parsed JSON).



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/wiki/page.rb', line 28

def get(name)
  assert_attributes_set
  path = File.join(directory, name)
  default_path = File.join(default_directory, name)
  page = Store.get_page(path)
  if page
    page
  elsif File.exist?(default_path)
    FileStore.get_page(default_path)
  elsif (path = plugin_page_path name)
    page = FileStore.get_page(path)
    page['plugin'] = path.match(/#{self.plugins_directory}\/wiki-plugin-(.*?)\/pages/)[1]
    page
  else
    halt 404
  end
end

#plugin_page_path(name) ⇒ Object



18
19
20
21
22
# File 'lib/wiki/page.rb', line 18

def plugin_page_path name
  if pages = Dir.glob(File.join(plugins_directory, 'wiki-plugin-*', 'pages', name))
    return pages.first
  end
end

#put(name, page) ⇒ Hash

Create or update a page

Parameters:

  • name (String)
    • The name of the file to create/update, relative to Page.directory.

  • page (Hash)
    • The page data to be written to the file (it will be converted to JSON).

Returns:

  • (Hash)

    The contents of the retrieved page (parsed JSON).



57
58
59
60
61
62
# File 'lib/wiki/page.rb', line 57

def put(name, page)
  assert_attributes_set
  path = File.join directory, name
  page.delete 'plugin'
  Store.put_page(path, page, :name => name, :directory => directory)
end