Debian Codegraph @

lib/mustache/settings.rb

raw symbols SCIP
1 # Settings which can be configured for all view classes, a single
2 # view class, or a single Mustache instance.
3 class Mustache
4
5 def initialize_settings
6 @template = nil
7 @template_path = nil
8 @template_extension = nil
9 @template_name = nil
10 @template_file = nil
11 @raise_on_context_miss = nil
12 end
13
14 def self.initialize_settings
15 @template = nil
16 @template_path = nil
17 @template_extension = nil
18 @template_name = nil
19 @template_file = nil
20 @raise_on_context_miss = nil
21 end
22
23 initialize_settings
24
25 def self.inherited(subclass)
26 subclass.initialize_settings
27 end
28
29 #
30 # Template Path
31 #
32
33 # The template path informs your Mustache view where to look for its
34 # corresponding template. By default it's the current directory (".")
35 #
36 # A class named Stat with a template_path of "app/templates" will look
37 # for "app/templates/stat.mustache"
38
39 def self.template_path
40 @template_path ||= inheritable_config_for :template_path, '.'
41 end
42
43 def self.template_path=(path)
44 @template_path = File.expand_path(path)
45 @template = nil
46 end
47
48 def template_path
49 @template_path ||= self.class.template_path
50 end
51 alias_method :path, :template_path
52
53 def template_path=(path)
54 @template_path = File.expand_path(path)
55 @template = nil
56 end
57
58 # Alias for `template_path`
59 def self.path
60 template_path
61 end
62
63 # Alias for `template_path`
64 def self.path=(path)
65 self.template_path = path
66 end
67
68
69 #
70 # Template Extension
71 #
72
73 # A Mustache template's default extension is 'mustache', but this can be changed.
74
75 def self.template_extension
76 @template_extension ||= inheritable_config_for :template_extension, 'mustache'
77 end
78
79 def self.template_extension=(template_extension)
80 @template_extension = template_extension
81 @template = nil
82 end
83
84 def template_extension
85 @template_extension ||= self.class.template_extension
86 end
87
88 def template_extension=(template_extension)
89 @template_extension = template_extension
90 @template = nil
91 end
92
93
94 #
95 # Template Name
96 #
97
98 # The template name is the Mustache template file without any
99 # extension or other information. Defaults to `class_name`.
100 #
101 # You may want to change this if your class is named Stat but you want
102 # to re-use another template.
103 #
104 # class Stat
105 # self.template_name = "graphs" # use graphs.mustache
106 # end
107
108 def self.template_name
109 @template_name || underscore
110 end
111
112 def self.template_name=(template_name)
113 @template_name = template_name
114 @template = nil
115 end
116
117 def template_name
118 @template_name ||= self.class.template_name
119 end
120
121 def template_name=(template_name)
122 @template_name = template_name
123 @template = nil
124 end
125
126
127 #
128 # Template File
129 #
130
131 # The template file is the absolute path of the file Mustache will
132 # use as its template. By default it's ./class_name.mustache
133
134 def self.template_file
135 @template_file || "#{path}/#{template_name}.#{template_extension}"
136 end
137
138 def self.template_file=(template_file)
139 @template_file = template_file
140 @template = nil
141 end
142
143 # The template file is the absolute path of the file Mustache will
144 # use as its template. By default it's ./class_name.mustache
145 def template_file
146 @template_file || "#{path}/#{template_name}.#{template_extension}"
147 end
148
149 def template_file=(template_file)
150 @template_file = template_file
151 @template = nil
152 end
153
154
155 #
156 # Template
157 #
158
159 # The template is the actual string Mustache uses as its template.
160 # There is a bit of magic here: what we get back is actually a
161 # Mustache::Template object, but you can still safely use `template=`
162 # with a string.
163
164 def self.template
165 @template ||= templateify(File.read(template_file))
166 end
167
168 def self.template=(template)
169 @template = templateify(template)
170 end
171
172 # The template can be set at the instance level.
173 def template
174 return @template if @template
175
176 # If they sent any instance-level options use that instead of the class's.
177 if @template_path || @template_extension || @template_name || @template_file
178 @template = templateify(File.read(template_file))
179 else
180 @template = self.class.template
181 end
182 end
183
184 def template=(template)
185 @template = templateify(template)
186 end
187
188
189 #
190 # Raise on context miss
191 #
192
193 # Should an exception be raised when we cannot find a corresponding method
194 # or key in the current context? By default this is false to emulate ctemplate's
195 # behavior, but it may be useful to enable when debugging or developing.
196 #
197 # If set to true and there is a context miss, `Mustache::ContextMiss` will
198 # be raised.
199
200 def self.raise_on_context_miss?
201 @raise_on_context_miss
202 end
203
204 def self.raise_on_context_miss=(boolean)
205 @raise_on_context_miss = boolean
206 end
207
208 # Instance level version of `Mustache.raise_on_context_miss?`
209 def raise_on_context_miss?
210 self.class.raise_on_context_miss? || @raise_on_context_miss
211 end
212
213 def raise_on_context_miss=(boolean)
214 @raise_on_context_miss = boolean
215 end
216
217
218 #
219 # View Namespace
220 #
221
222 # The constant under which Mustache will look for views when autoloading.
223 # By default the view namespace is `Object`, but it might be nice to set
224 # it to something like `Hurl::Views` if your app's main namespace is `Hurl`.
225
226 def self.view_namespace
227 @view_namespace ||= inheritable_config_for(:view_namespace, Object)
228 end
229
230 def self.view_namespace=(namespace)
231 @view_namespace = namespace
232 end
233
234
235 #
236 # View Path
237 #
238
239 # Mustache searches the view path for .rb files to require when asked to find a
240 # view class. Defaults to "."
241
242 def self.view_path
243 @view_path ||= inheritable_config_for(:view_path, '.')
244 end
245
246 def self.view_path=(path)
247 @view_path = path
248 end
249 end