mustache 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY.md ADDED
@@ -0,0 +1,5 @@
1
+ ## 0.2.1 (2009-10-11)
2
+
3
+ * Mustache.underscore can now be called without an argument
4
+ * Settings now mostly live at the class level, excepting `template`
5
+ * Any setting changes causes the template to be recompiled
data/benchmarks/speed.rb CHANGED
@@ -43,9 +43,9 @@ content = File.read(ComplexView.template_file)
43
43
 
44
44
  unless ENV['CACHED']
45
45
  bench '{ w/o caching' do
46
- tpl = ComplexView.new
47
- tpl.template = content
48
- tpl[:item] = items
49
- tpl.to_html
46
+ ctpl = ComplexView.new
47
+ ctpl.template = content
48
+ ctpl[:item] = items
49
+ ctpl.to_html
50
50
  end
51
51
  end
data/lib/mustache.rb CHANGED
@@ -16,10 +16,11 @@ require 'mustache/context'
16
16
  # While Mustache will do its best to load and render a template for
17
17
  # you, this process is completely customizable using a few options.
18
18
  #
19
- # All settings can be overriden at either the class or instance
20
- # level. For example, going with the above example, we can do either
21
- # `Stats.template_path = "/usr/local/templates"` or
22
- # `view.template_path = "/usr/local/templates"`
19
+ # All settings can be overriden at the class level.
20
+ #
21
+ # For example, going with the above example, we can use
22
+ # `Stats.template_path = "/usr/local/templates"` to specify the path
23
+ # Mustache uses to find templates.
23
24
  #
24
25
  # Here are the available options:
25
26
  #
@@ -49,6 +50,12 @@ require 'mustache/context'
49
50
  # >> Mustache.render("Hello {{planet}}", :planet => "World!")
50
51
  # => "Hello World!"
51
52
  #
53
+ # The `template` setting is also available on instances.
54
+ #
55
+ # view = Mustache.new
56
+ # view.template = "Hi, {{person}}!"
57
+ # view[:person] = 'Mom'
58
+ # view.render # => Hi, mom!
52
59
  class Mustache
53
60
  # Helper method for quickly instantiating and rendering a view.
54
61
  def self.render(*args)
@@ -68,11 +75,12 @@ class Mustache
68
75
  # The template path informs your Mustache subclass where to look for its
69
76
  # corresponding template. By default it's the current directory (".")
70
77
  def self.template_path
71
- @template_path || '.'
78
+ @template_path ||= '.'
72
79
  end
73
80
 
74
81
  def self.template_path=(path)
75
82
  @template_path = File.expand_path(path)
83
+ @template = nil
76
84
  end
77
85
 
78
86
  # Alias for `template_path`
@@ -87,21 +95,23 @@ class Mustache
87
95
 
88
96
  # A Mustache template's default extension is 'html'
89
97
  def self.template_extension
90
- @template_extension || 'html'
98
+ @template_extension ||= 'html'
91
99
  end
92
100
 
93
101
  def self.template_extension=(template_extension)
94
102
  @template_extension = template_extension
103
+ @template = nil
95
104
  end
96
105
 
97
106
  # The template file is the absolute path of the file Mustache will
98
107
  # use as its template. By default it's ./class_name.html
99
108
  def self.template_file
100
- @template_file || "#{template_path}/#{underscore(to_s)}.#{template_extension}"
109
+ @template_file || "#{path}/#{underscore}.#{template_extension}"
101
110
  end
102
111
 
103
112
  def self.template_file=(template_file)
104
113
  @template_file = template_file
114
+ @template = nil
105
115
  end
106
116
 
107
117
  # The template is the actual string Mustache uses as its template.
@@ -109,11 +119,11 @@ class Mustache
109
119
  # Mustache::Template object here, but you can still safely use
110
120
  # `template=` with a string.
111
121
  def self.template
112
- @template || templateify(File.read(template_file))
122
+ @template ||= templateify(File.read(template_file))
113
123
  end
114
124
 
115
125
  def self.template=(template)
116
- @template = templateify(template)
126
+ @template = template
117
127
  end
118
128
 
119
129
  # template_partial => TemplatePartial
@@ -124,7 +134,11 @@ class Mustache
124
134
  end
125
135
 
126
136
  # TemplatePartial => template_partial
127
- def self.underscore(classified)
137
+ # Takes a string but defaults to using the current class' name.
138
+ def self.underscore(classified = name)
139
+ classified = name if classified.to_s.empty?
140
+ classified = superclass.name if classified.to_s.empty?
141
+
128
142
  string = classified.dup.split('::').last
129
143
  string[0] = string[0].chr.downcase
130
144
  string.gsub(/[A-Z]/) { |s| "_#{s.downcase}"}
@@ -140,46 +154,13 @@ class Mustache
140
154
  end
141
155
  end
142
156
 
143
- # Instance version.Turns a string into a Mustache::Template. If passed a Template,
144
157
  def templateify(obj)
145
- if obj.is_a?(Template)
146
- obj
147
- else
148
- Template.new(obj.to_s, template_path, template_extension)
149
- end
150
- end
151
-
152
- #
153
- # Instance level settings
154
- #
155
-
156
- def template_path
157
- @template_path || self.class.template_path
158
- end
159
-
160
- def template_path=(template_path)
161
- @template_path = template_path
162
- end
163
-
164
- def template_extension
165
- @template_extension || self.class.template_extension
166
- end
167
-
168
- def template_extension=(template_extension)
169
- @template_extension = template_extension
170
- end
171
-
172
- def template_file
173
- return @template_file if @template_file
174
- "#{template_path}/#{Mustache.underscore(self.class.name)}.#{template_extension}"
175
- end
176
-
177
- def template_file=(template_file)
178
- @template_file = template_file
158
+ self.class.templateify(obj)
179
159
  end
180
160
 
161
+ # The template can be set at the instance level.
181
162
  def template
182
- @template || templateify(File.read(template_file))
163
+ @template ||= self.class.template
183
164
  end
184
165
 
185
166
  def template=(template)
@@ -1,3 +1,3 @@
1
1
  class Mustache
2
- Version = '0.2.0'
2
+ Version = '0.2.1'
3
3
  end
@@ -98,8 +98,9 @@ end_partial
98
98
  end
99
99
 
100
100
  def test_template_partial_with_custom_extension
101
- partial = TemplatePartial.new
101
+ partial = Class.new(TemplatePartial)
102
102
  partial.template_extension = 'txt'
103
+ partial.template_path = File.dirname(__FILE__) + '/../examples'
103
104
 
104
105
  assert_equal <<-end_partial.strip, partial.render.strip
105
106
  Welcome
@@ -129,6 +130,11 @@ end_partial
129
130
  assert_equal 'template_partial', Mustache.underscore('TemplatePartial')
130
131
  end
131
132
 
133
+ def test_anon_subclass_underscore
134
+ klass = Class.new(TemplatePartial)
135
+ assert_equal 'template_partial', klass.underscore
136
+ end
137
+
132
138
  def test_namespaced_underscore
133
139
  assert_equal 'stat_stuff', Mustache.underscore('Views::StatStuff')
134
140
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mustache
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Wanstrath
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-10 00:00:00 -07:00
12
+ date: 2009-10-11 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -26,6 +26,7 @@ files:
26
26
  - .gitignore
27
27
  - .kick
28
28
  - CONTRIBUTORS
29
+ - HISTORY.md
29
30
  - LICENSE
30
31
  - README.md
31
32
  - Rakefile