Class: ActionSite::PageContext

Inherits:
Context
  • Object
show all
Includes:
Helpers::FormHelper, Helpers::MarkabyHelper, Helpers::UrlHelper
Defined in:
lib/action_site/page_context.rb

Overview

an instance of this class is created for each page this is how pages can access magical things like link_to, etc

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::MarkabyHelper

#markaby

Methods included from Helpers::UrlHelper

#image_tag, #link_to, #link_to_function, #stylesheet_link_tag

Constructor Details

#initialize(html_generator, global_context, file_name) ⇒ PageContext

Returns a new instance of PageContext.



16
17
18
19
20
21
# File 'lib/action_site/page_context.rb', line 16

def initialize(html_generator, global_context, file_name)
  @html_generator, @global_context, @file_name = 
    html_generator, global_context, file_name
  layout(:application) rescue nil
  helper(:application, false)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/action_site/page_context.rb', line 59

def method_missing(sym, *args)
  return @global_context.send(sym, *args) if @global_context.respond_to?(sym)

  name = sym.to_s
  if name.starts_with?("content_for_") && name.ends_with?("?")
    return !!instance_variable_get("@#{name[0..-2]}")
  end

  super
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



14
15
16
# File 'lib/action_site/page_context.rb', line 14

def content
  @content
end

#file_nameObject

Returns the value of attribute file_name.



14
15
16
# File 'lib/action_site/page_context.rb', line 14

def file_name
  @file_name
end

#global_contextObject

Returns the value of attribute global_context.



14
15
16
# File 'lib/action_site/page_context.rb', line 14

def global_context
  @global_context
end

#layout_templateObject

Returns the value of attribute layout_template.



14
15
16
# File 'lib/action_site/page_context.rb', line 14

def layout_template
  @layout_template
end

#patternObject

Returns the value of attribute pattern.



14
15
16
# File 'lib/action_site/page_context.rb', line 14

def pattern
  @pattern
end

Instance Method Details

#content_for(name) ⇒ Object



51
52
53
# File 'lib/action_site/page_context.rb', line 51

def content_for(name)
  self.send("content_for_#{name}=".to_sym, yield)
end

#get_bindingObject



55
56
57
# File 'lib/action_site/page_context.rb', line 55

def get_binding
  binding
end

#helper(name, assert_loaded = true) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/action_site/page_context.rb', line 37

def helper(name, assert_loaded = true)
  file_name = "#{name}_helper"
  fully_qualified_file = File.join(@html_generator.template_directory, "helpers", file_name + ".rb")
  return unless File.exist?(fully_qualified_file) || assert_loaded
  
  class_name = file_name.classify
  if Object.const_defined?(class_name)
    Object.send(:remove_const, class_name)
  end

  load fully_qualified_file
  metaclass.send(:include, class_name.constantize)
end

#layout(name) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/action_site/page_context.rb', line 23

def layout(name)
  if name
    files = Dir[File.join(@html_generator.template_directory, 'layouts', "#{name}.*")]
    raise "couldn't find layout #{name}" if files.empty?
    @layout_template = files.first
  else
    @layout_template = nil
  end
end

#process_file(*args) ⇒ Object



33
34
35
# File 'lib/action_site/page_context.rb', line 33

def process_file(*args)
  @html_generator.process_file(*args)
end