Class: PageSet

Inherits:
Array
  • Object
show all
Defined in:
app/models/page_set.rb

Overview

Container for a set of pages with methods for manipulation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(web, pages = nil, condition = nil) ⇒ PageSet

Returns a new instance of PageSet.



6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/models/page_set.rb', line 6

def initialize(web, pages = nil, condition = nil)
  @web = web
  # if pages is not specified, make a list of all pages in the web
  if pages.nil?
    super(web.pages.values)
  # otherwise use specified pages and condition to produce a set of pages
  elsif condition.nil?
    super(pages)
  else
    super(pages.select { |page| condition[page] })
  end
end

Instance Attribute Details

#webObject (readonly)

Returns the value of attribute web.



4
5
6
# File 'app/models/page_set.rb', line 4

def web
  @web
end

Instance Method Details

#authorsObject



85
86
87
# File 'app/models/page_set.rb', line 85

def authors
  self.inject([]) { |authors, page| authors << page.authors }.flatten.uniq.sort
end

#by_nameObject Also known as: sort



24
25
26
# File 'app/models/page_set.rb', line 24

def by_name
  PageSet.new(@web, sort_by { |page| page.name })
end

#by_revisionObject



30
31
32
# File 'app/models/page_set.rb', line 30

def by_revision
  PageSet.new(@web, sort_by { |page| page.created_at }).reverse 
end

#charactersObject



50
51
52
# File 'app/models/page_set.rb', line 50

def characters
  self.inject(0) { |chars,page| chars += page.content.size }
end

#most_recent_revisionObject



19
20
21
# File 'app/models/page_set.rb', line 19

def most_recent_revision
  self.map { |page| page.created_at }.max || Time.at(0)
end

#namesObject



77
78
79
# File 'app/models/page_set.rb', line 77

def names
  self.map { |page| page.name }
end

#orphaned_pagesObject

Returns all the orphaned pages in this page set. That is, pages in this set for which there is no reference in the web. The HomePage and author pages are always assumed to have references and so cannot be orphans Pages that refer to themselves and have no links from outside are oprphans.



59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/page_set.rb', line 59

def orphaned_pages
  never_orphans = web.select.authors + ['HomePage']
  self.select { |page|
    if never_orphans.include? page.name
      false
    else
      references = pages_that_reference(page.name)
      references.empty? or references == [page]
    end
  }
end

#pages_authored_by(author) ⇒ Object



46
47
48
# File 'app/models/page_set.rb', line 46

def pages_authored_by(author)
  self.select { |page| page.authors.include?(author) }
end

#pages_that_include(page_name) ⇒ Object



42
43
44
# File 'app/models/page_set.rb', line 42

def pages_that_include(page_name)
  self.select { |page| page.wiki_includes.include?(page_name) }
end


38
39
40
# File 'app/models/page_set.rb', line 38

def pages_that_link_to(page_name)
  self.select { |page| page.wiki_words.include?(page_name) }
end

#pages_that_reference(page_name) ⇒ Object



34
35
36
# File 'app/models/page_set.rb', line 34

def pages_that_reference(page_name)
  self.select { |page| page.wiki_references.include?(page_name) }
end

#wanted_pagesObject

Returns all the wiki words in this page set for which there are no pages in this page set’s web



73
74
75
# File 'app/models/page_set.rb', line 73

def wanted_pages
  wiki_words - web.select.names
end

#wiki_wordsObject



81
82
83
# File 'app/models/page_set.rb', line 81

def wiki_words
  self.inject([]) { |wiki_words, page| wiki_words << page.wiki_words }.flatten.uniq
end