Class: Brochure::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/brochure/application.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, options = {}) ⇒ Application

Returns a new instance of Application.



5
6
7
8
9
10
11
12
13
14
# File 'lib/brochure/application.rb', line 5

def initialize(root, options = {})
  @app_root      = File.expand_path(root)
  @template_root = File.join(@app_root, "templates")
  @asset_root    = File.join(@app_root, "public")
  @plugin_root   = File.join(@app_root, "vendor", "plugins")

  @assigns = options[:assigns] || {}
  helpers.push(*options[:helpers]) if options[:helpers]
  initialize_plugins
end

Instance Attribute Details

#app_rootObject (readonly)

Returns the value of attribute app_root.



3
4
5
# File 'lib/brochure/application.rb', line 3

def app_root
  @app_root
end

#asset_rootObject (readonly)

Returns the value of attribute asset_root.



3
4
5
# File 'lib/brochure/application.rb', line 3

def asset_root
  @asset_root
end

#assignsObject (readonly)

Returns the value of attribute assigns.



3
4
5
# File 'lib/brochure/application.rb', line 3

def assigns
  @assigns
end

#plugin_rootObject (readonly)

Returns the value of attribute plugin_root.



3
4
5
# File 'lib/brochure/application.rb', line 3

def plugin_root
  @plugin_root
end

#template_rootObject (readonly)

Returns the value of attribute template_root.



3
4
5
# File 'lib/brochure/application.rb', line 3

def template_root
  @template_root
end

Instance Method Details

#call(env) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/brochure/application.rb', line 47

def call(env)
  if forbidden?(env["PATH_INFO"])
    forbidden
  elsif template = find_template(env["PATH_INFO"])
    success template.render(env), template.content_type
  else
    not_found(env)
  end
end

#context_classObject



29
30
31
# File 'lib/brochure/application.rb', line 29

def context_class
  @context_class ||= Context.for(helpers)
end

#context_for(template, env) ⇒ Object



91
92
93
# File 'lib/brochure/application.rb', line 91

def context_for(template, env)
  context_class.new(self, template, env, assigns)
end

#default_not_foundObject



115
116
117
118
119
120
121
# File 'lib/brochure/application.rb', line 115

def default_not_found
  respond_with 404, <<-HTML
    <!DOCTYPE html>
    <html><head><title>Not Found</title></head>
    <body><h1>404 Not Found</h1></body></html>
  HTML
end

#find_partial(logical_path, format_extension = ".html") ⇒ Object



67
68
69
70
71
# File 'lib/brochure/application.rb', line 67

def find_partial(logical_path, format_extension = ".html")
  if template_path = find_partial_path(logical_path, format_extension)
    template_for(template_path)
  end
end

#find_partial_path(logical_path, format_extension) ⇒ Object



77
78
79
80
81
# File 'lib/brochure/application.rb', line 77

def find_partial_path(logical_path, format_extension)
  dirname, basename = File.split(logical_path)
  partial_path = File.join(dirname, "_" + basename)
  template_trail.find(partial_path, partial_path + format_extension)
end

#find_template(logical_path, format_extension = ".html") ⇒ Object



61
62
63
64
65
# File 'lib/brochure/application.rb', line 61

def find_template(logical_path, format_extension = ".html")
  if template_path = find_template_path(logical_path, format_extension)
    template_for(template_path)
  end
end

#find_template_path(logical_path, format_extension) ⇒ Object



73
74
75
# File 'lib/brochure/application.rb', line 73

def find_template_path(logical_path, format_extension)
  template_trail.find(logical_path, logical_path + format_extension, logical_path + "/index" + format_extension)
end

#forbiddenObject



123
124
125
# File 'lib/brochure/application.rb', line 123

def forbidden
  respond_with 403, "Forbidden"
end

#forbidden?(path) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/brochure/application.rb', line 57

def forbidden?(path)
  path[".."] || File.basename(path)[/^_/]
end

#helpersObject



37
38
39
# File 'lib/brochure/application.rb', line 37

def helpers
  @helpers ||= []
end

#initialize_pluginsObject



16
17
18
19
20
# File 'lib/brochure/application.rb', line 16

def initialize_plugins
  plugins.each do |plugin_root|
    template_trail.paths.push(File.join(plugin_root, "templates"))
  end
end

#not_found(env) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/brochure/application.rb', line 107

def not_found(env)
  if template = find_template("404")
    respond_with 404, template.render(env)
  else
    default_not_found
  end
end

#pluginsObject



41
42
43
44
45
# File 'lib/brochure/application.rb', line 41

def plugins
  @plugins ||= Dir[File.join(plugin_root, "*")].select do |entry|
    File.directory?(entry)
  end
end

#respond_with(status, body, content_type = "text/html; charset=utf-8") ⇒ Object



95
96
97
98
99
100
101
# File 'lib/brochure/application.rb', line 95

def respond_with(status, body, content_type = "text/html; charset=utf-8")
  headers = {
    "Content-Type"   => content_type,
    "Content-Length" => Rack::Utils.bytesize(body).to_s
  }
  [status, headers, [body]]
end

#success(body, content_type) ⇒ Object



103
104
105
# File 'lib/brochure/application.rb', line 103

def success(body, content_type)
  respond_with 200, body, content_type
end

#template_for(template_path) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/brochure/application.rb', line 83

def template_for(template_path)
  if Brochure.development?
    Template.new(self, template_path)
  else
    templates[template_path] ||= Template.new(self, template_path)
  end
end

#template_trailObject



22
23
24
25
26
27
# File 'lib/brochure/application.rb', line 22

def template_trail
  @template_trail ||= Hike::Trail.new(app_root).tap do |trail|
    trail.extensions.replace(Tilt.mappings.keys.sort)
    trail.paths.push(template_root)
  end
end

#templatesObject



33
34
35
# File 'lib/brochure/application.rb', line 33

def templates
  @templates ||= {}
end