Class: Comatose::PageWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/comatose/page_wrapper.rb

Overview

Giving direct access to the AR model is disconcerting, so this wraps the AR Model to prevent access to certain, destructive behavior

Constant Summary collapse

@@allowed_methods =
%w(id full_path uri slug keywords title to_html filter_type author updated_on created_on)
@@custom_methods =
%w(link content parent next previous children rchildren first_child last_child has_keyword)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, locals = {}) ⇒ PageWrapper

Returns a new instance of PageWrapper.



11
12
13
14
15
16
17
18
19
20
# File 'lib/comatose/page_wrapper.rb', line 11

def initialize( page, locals={} )
  @page = page
  @keyword_lst = []
  @keyword_hsh = {}
  unless page.nil?
    @keyword_lst = (page.keywords || '').downcase.split(',').map {|k| k.strip }
    @keyword_lst.each {|kw| @keyword_hsh[kw] = true}
  end
  @locals = locals
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args) ⇒ Object



109
110
111
112
113
114
115
116
117
# File 'lib/comatose/page_wrapper.rb', line 109

def method_missing(method_id, *args)
  #STDERR.puts "Looking for method: #{method_id} (#{method_id.class.to_s}) in [#{@@allowed_methods.join(', ')}] or [#{page.attributes.keys.join(', ')}]"
  if @@allowed_methods.include? method_id.to_s or page.attributes.has_key? method_id.to_s
    page.send( method_id, *args)
  else
    # Access nazi says: NO ACCESS FOR YOU!!
    # but he says it silently...
  end
end

Instance Attribute Details

#page=(value) ⇒ Object

Sets the attribute page

Parameters:

  • value

    the value to set the attribute page to.



8
9
10
# File 'lib/comatose/page_wrapper.rb', line 8

def page=(value)
  @page = value
end

Instance Method Details

#[](key) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/comatose/page_wrapper.rb', line 89

def [](key)
  if @@allowed_methods.include? key.to_s #or page.attributes.has_key? key.to_s
    page.send( key )
  elsif @@custom_methods.include? key
    self.send(key.to_sym)
  end
end

#childrenObject



79
80
81
82
# File 'lib/comatose/page_wrapper.rb', line 79

def children
  # Cache the results so that you can have multiple calls to #children without a penalty
  @children ||= @page.children.to_a.collect {|child| Comatose::PageWrapper.new( child, @locals )}
end

#contentObject



65
66
67
# File 'lib/comatose/page_wrapper.rb', line 65

def content
  @page.to_html( @locals )
end

#def(last_child) ⇒ Object



54
55
56
# File 'lib/comatose/page_wrapper.rb', line 54

def def last_child
  children.last
end

#first_childObject



50
51
52
# File 'lib/comatose/page_wrapper.rb', line 50

def first_child
  children.first
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/comatose/page_wrapper.rb', line 97

def has_key?(key)
  @@allowed_methods.include? key or @@custom_methods.include? key
end

#has_keywordObject



26
27
28
# File 'lib/comatose/page_wrapper.rb', line 26

def has_keyword
  @keyword_hsh
end

#has_keyword?(keyword) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/comatose/page_wrapper.rb', line 22

def has_keyword?(keyword)
  @page.has_keyword?(keyword)
end

Generates a link to this page… You can pass in the link text, otherwise it will default to the page title.



60
61
62
63
# File 'lib/comatose/page_wrapper.rb', line 60

def link( title=nil )
  title = @page.title if title.nil?
  TextFilters[@page.filter_type].create_link(title, @page.uri)
end

#nextObject



30
31
32
33
34
35
36
37
38
# File 'lib/comatose/page_wrapper.rb', line 30

def next
  @next_page ||= begin
    if @page.lower_item.nil?
      nil
    else
      Comatose::PageWrapper.new( @page.lower_item )
    end
  end
end

#parentObject



69
70
71
72
73
74
75
76
77
# File 'lib/comatose/page_wrapper.rb', line 69

def parent
  @parent ||= begin
    if @page.parent.nil?
      nil
    else
      Comatose::PageWrapper.new( @page.parent )
    end
  end
end

#previousObject



40
41
42
43
44
45
46
47
48
# File 'lib/comatose/page_wrapper.rb', line 40

def previous
  @prev_page ||= begin
    if @page.higher_item.nil?
      nil
    else
      Comatose::PageWrapper.new( @page.higher_item )
    end
  end
end

#rchildrenObject

Children, in reverse order



85
86
87
# File 'lib/comatose/page_wrapper.rb', line 85

def rchildren
  children.reverse
end

#to_liquidObject



105
106
107
# File 'lib/comatose/page_wrapper.rb', line 105

def to_liquid
  self
end

#to_sObject



101
102
103
# File 'lib/comatose/page_wrapper.rb', line 101

def to_s
  @page.title
end