Class: Comatose::PageWrapper
- Inherits:
-
Object
- Object
- Comatose::PageWrapper
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)
if @@allowed_methods.include? method_id.to_s or page.attributes.has_key? method_id.to_s
page.send( method_id, *args)
else
end
end
|
Instance Attribute Details
#page=(value) ⇒ Object
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 page.send( key )
elsif @@custom_methods.include? key
self.send(key.to_sym)
end
end
|
#children ⇒ Object
79
80
81
82
|
# File 'lib/comatose/page_wrapper.rb', line 79
def children
@children ||= @page.children.to_a.collect {|child| Comatose::PageWrapper.new( child, @locals )}
end
|
#content ⇒ Object
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_child ⇒ Object
50
51
52
|
# File 'lib/comatose/page_wrapper.rb', line 50
def first_child
children.first
end
|
#has_key?(key) ⇒ 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_keyword ⇒ Object
26
27
28
|
# File 'lib/comatose/page_wrapper.rb', line 26
def has_keyword
@keyword_hsh
end
|
#has_keyword?(keyword) ⇒ Boolean
22
23
24
|
# File 'lib/comatose/page_wrapper.rb', line 22
def has_keyword?(keyword)
@page.has_keyword?(keyword)
end
|
#link(title = nil) ⇒ Object
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
|
#next ⇒ Object
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
|
#parent ⇒ Object
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
|
#previous ⇒ Object
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
|
#rchildren ⇒ Object
Children, in reverse order
85
86
87
|
# File 'lib/comatose/page_wrapper.rb', line 85
def rchildren
children.reverse
end
|
#to_liquid ⇒ Object
105
106
107
|
# File 'lib/comatose/page_wrapper.rb', line 105
def to_liquid
self
end
|
#to_s ⇒ Object
101
102
103
|
# File 'lib/comatose/page_wrapper.rb', line 101
def to_s
@page.title
end
|