mustache 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -304,6 +304,15 @@ Now:
304
304
  Convoluted but you get the idea.
305
305
 
306
306
 
307
+ Sinatra
308
+ -------
309
+
310
+ Mustache ships with Sinatra integration. Please see
311
+ `lib/mustache/sinatra.rb` or
312
+ <http://defunkt.github.com/mustache/classes/Mustache/Sinatra.html> for
313
+ complete documentation.
314
+
315
+
307
316
  Installation
308
317
  ------------
309
318
 
data/Rakefile CHANGED
@@ -34,5 +34,6 @@ end
34
34
 
35
35
  desc "Push a new version to Gemcutter"
36
36
  task :publish => [ :gemspec, :build ] do
37
- exec "gem push pkg/mustache-#{Mustache::Version}.gem"
37
+ system "gem push pkg/mustache-#{Mustache::Version}.gem"
38
+ exec "git clean -fd"
38
39
  end
@@ -6,7 +6,9 @@ class Mustache
6
6
  #
7
7
  # require 'mustache/sinatra'
8
8
  #
9
- # class App < Sinatra::Base
9
+ # class Hurl < Sinatra::Base
10
+ # register Mustache::Sinatra
11
+ #
10
12
  # # Should be the path to your .mustache template files.
11
13
  # set :views, "path/to/mustache/templates"
12
14
  #
@@ -14,19 +16,21 @@ class Mustache
14
16
  # # Only needed if different from the `views` setting
15
17
  # set :mustaches, "path/to/mustache/views"
16
18
  #
17
- # # This tells Mustache where to look for the Views modules,
19
+ # # This tells Mustache where to look for the Views module,
18
20
  # # under which your View classes should live. By default it's
19
- # # Object. That is, for an :index view Mustache will expect
20
- # # Views::Index. In this example, we're telling Mustache to look
21
- # # for index at App::Views::Index.
22
- # set :namespace, App
21
+ # # the class of your app - in this case `Hurl`. That is, for an :index
22
+ # # view Mustache will expect Hurl::Views::Index by default.
23
+ #
24
+ # # If our Sinatra::Base subclass was instead Hurl::App,
25
+ # # we'd want to do `set :namespace, Hurl::App`
26
+ # set :namespace, Hurl
23
27
  #
24
28
  # get '/stats' do
25
29
  # mustache :stats
26
30
  # end
27
31
  # end
28
32
  #
29
- # As noted above, Mustache will look for `App::Views::Index` when
33
+ # As noted above, Mustache will look for `Hurl::Views::Index` when
30
34
  # `mustache :index` is called.
31
35
  #
32
36
  # If no `Views::Stats` class exists Mustache will render the template
@@ -36,61 +40,67 @@ class Mustache
36
40
  # <%= yield %> you instead {{{yield}}} - the body of the subview is
37
41
  # set to the `yield` variable and made available to you.
38
42
  module Sinatra
39
- # Call this in your Sinatra routes.
40
- def mustache(template, options={}, locals={})
41
- render :mustache, template, options, locals
42
- end
43
+ module Helpers
44
+ # Call this in your Sinatra routes.
45
+ def mustache(template, options={}, locals={})
46
+ render :mustache, template, options, locals
47
+ end
43
48
 
44
- # This is called by Sinatra's `render` with the proper paths
45
- # and, potentially, a block containing a sub-view
46
- def render_mustache(template, data, options, locals, &block)
47
- name = Mustache.classify(template.to_s)
49
+ # This is called by Sinatra's `render` with the proper paths
50
+ # and, potentially, a block containing a sub-view
51
+ def render_mustache(template, data, opts, locals, &block)
52
+ name = Mustache.classify(template.to_s)
48
53
 
49
- # This is a horrible hack but we need it to know under which namespace
50
- # Views is located. If you have Haystack::Views, namespace should be
51
- # set to Haystack.
52
- namespace = self.class.namespace
54
+ # This is a horrible hack but we need it to know under which namespace
55
+ # Views is located. If you have Hurl::App::Views, namespace should be
56
+ # set to Hurl:App.
57
+ namespace = options.namespace
53
58
 
54
- if namespace.const_defined?(:Views) && namespace::Views.const_defined?(name)
55
- # First try to find the existing view,
56
- # e.g. Haystack::Views::Index
57
- klass = namespace::Views.const_get(name)
59
+ if namespace.const_defined?(:Views) && namespace::Views.const_defined?(name)
60
+ # First try to find the existing view,
61
+ # e.g. Hurl::Views::Index
62
+ klass = namespace::Views.const_get(name)
58
63
 
59
- elsif File.exists?(file = "#{self.class.mustaches}/#{template}.rb")
60
- # Couldn't find it - try to require the file if it exists, then
61
- # load in the view.
62
- require "#{file}".chomp('.rb')
63
- klass = namespace::Views.const_get(name)
64
+ elsif File.exists?(file = "#{options.mustaches}/#{template}.rb")
65
+ # Couldn't find it - try to require the file if it exists, then
66
+ # load in the view.
67
+ require "#{file}".chomp('.rb')
68
+ klass = namespace::Views.const_get(name)
64
69
 
65
- else
66
- # Still nothing. Use the stache.
67
- klass = Mustache
70
+ else
71
+ # Still nothing. Use the stache.
72
+ klass = Mustache
68
73
 
69
- end
74
+ end
70
75
 
71
- # Create a new instance for playing with
72
- instance = klass.new
76
+ # Create a new instance for playing with
77
+ instance = klass.new
73
78
 
74
- # Copy instance variables set in Sinatra to the view
75
- instance_variables.each do |name|
76
- instance.instance_variable_set(name, instance_variable_get(name))
77
- end
79
+ # Copy instance variables set in Sinatra to the view
80
+ instance_variables.each do |name|
81
+ instance.instance_variable_set(name, instance_variable_get(name))
82
+ end
78
83
 
79
- # Locals get added to the view's context
80
- locals.each do |local, value|
81
- instance[local] = value
82
- end
84
+ # Locals get added to the view's context
85
+ locals.each do |local, value|
86
+ instance[local] = value
87
+ end
88
+
89
+ # If we're paseed a block it's a subview. Sticking it in yield
90
+ # lets us use {{yield}} in layout.html to render the actual page.
91
+ instance[:yield] = block.call if block
83
92
 
84
- # If we're paseed a block it's a subview. Sticking it in yield
85
- # lets us use {{yield}} in layout.html to render the actual page.
86
- instance[:yield] = block.call if block
93
+ instance.template = data
94
+ instance.to_html
95
+ end
96
+ end
87
97
 
88
- instance.template = data
89
- instance.to_html
98
+ def self.registered(app)
99
+ app.helpers Mustache::Sinatra::Helpers
100
+ app.set :mustaches, ::Sinatra::Base.views
101
+ app.set :namespace, app
90
102
  end
91
103
  end
92
104
  end
93
105
 
94
- Sinatra::Base.helpers Mustache::Sinatra
95
- Sinatra::Base.set :mustaches, Sinatra::Base.views
96
- Sinatra::Base.set :namespace, Object
106
+ Sinatra.register Mustache::Sinatra
@@ -1,3 +1,3 @@
1
1
  class Mustache
2
- Version = '0.1.3'
2
+ Version = '0.1.4'
3
3
  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.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Wanstrath