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
15
# 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] || {}
  @template_options = options[:template_options] || {}
  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_optionsObject (readonly)

Returns the value of attribute template_options.



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

def template_options
  @template_options
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



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

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



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

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

#context_for(template, env) ⇒ Object



100
101
102
# File 'lib/brochure/application.rb', line 100

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

#default_not_foundObject



124
125
126
127
128
129
130
# File 'lib/brochure/application.rb', line 124

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



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

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



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

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

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



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

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



74
75
76
77
78
79
80
# File 'lib/brochure/application.rb', line 74

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

#forbiddenObject



132
133
134
# File 'lib/brochure/application.rb', line 132

def forbidden
  respond_with 403, "Forbidden"
end

#forbidden?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#helpersObject



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

def helpers
  @helpers ||= []
end

#initialize_pluginsObject



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

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

#not_found(env) ⇒ Object



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

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

#pluginsObject



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

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



104
105
106
107
108
109
110
# File 'lib/brochure/application.rb', line 104

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



112
113
114
# File 'lib/brochure/application.rb', line 112

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

#template_for(template_path) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/brochure/application.rb', line 92

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



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

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



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

def templates
  @templates ||= {}
end