Class: Comatose::PageWrapper

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

private :page



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/comatose/page_wrapper.rb', line 12

def initialize(page, locals={}, system={})
  @current_user = system[:current_user]
  @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
rescue => e
  Comatose.logger.debug e.message
  Comatose.logger.debug e.backtrace.join("\n")
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/comatose/page_wrapper.rb', line 132

def method_missing(method_id, *args)
  #Comatose.logger.debug "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
    #Comatose.logger.debug "sending method_id to page"
    page_value = page.send(method_id, *args)
    #Comatose.logger.debug "page returned: #{page_value}"
    return page_value
  else
    # Access nazi says: NO ACCESS FOR YOU!!
    # but he says it silently...
    Comatose.logger.debug("PageWrapper#method_missing: could not find #{method_id}")
  end
end

Instance Attribute Details

#current_userObject

Returns the value of attribute current_user.



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

def current_user
  @current_user
end

#pageObject

Returns the value of attribute page.



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

def page
  @page
end

Instance Method Details

#[](key) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/comatose/page_wrapper.rb', line 107

def [](key)
  Comatose.logger.debug "using [] to find key: #{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



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

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



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

def content
  @page.to_html( @locals )
end

#def(last_child) ⇒ Object



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

def def last_child
  children.last
end

#first_childObject



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

def first_child
  children.first
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/comatose/page_wrapper.rb', line 117

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

#has_keywordObject



34
35
36
# File 'lib/comatose/page_wrapper.rb', line 34

def has_keyword
  @keyword_hsh
end

#has_keyword?(keyword) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/comatose/page_wrapper.rb', line 29

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.



73
74
75
76
# File 'lib/comatose/page_wrapper.rb', line 73

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

#nextObject



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

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

#parentObject



84
85
86
87
88
89
90
91
92
# File 'lib/comatose/page_wrapper.rb', line 84

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

#previousObject



50
51
52
53
54
55
56
57
58
# File 'lib/comatose/page_wrapper.rb', line 50

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



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

def rchildren
  children.reverse
end

#to_liquidObject



127
128
129
# File 'lib/comatose/page_wrapper.rb', line 127

def to_liquid
  self
end

#to_sObject



122
123
124
# File 'lib/comatose/page_wrapper.rb', line 122

def to_s
  @page.title
end