mustache-sinatra 1.0.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ff55e20ce7e3821875601fb0b2c8cfb81103e7d8
4
+ data.tar.gz: 8b7b161f5110d393ca53c1f8eb86b4de32372144
5
+ SHA512:
6
+ metadata.gz: 75f801d22be8f58faaeff17d749ccd4921bfe3a0d5cffc2997eebe6f4262f9f100dee5d156a73fb72b39064f558a0a0cf030549b82da4cab240f6d530efc5c88
7
+ data.tar.gz: fb6e7613d8042bcdc139b20ef03ede699e72c3829042b29ed34c3a7a3c35184c9b6a707502bfeb3a4d07e93d3ab182ed677e931ec7a7173cc4168c8f952664cb
data/.gitignore ADDED
@@ -0,0 +1,34 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ # Gemfile.lock
30
+ # .ruby-version
31
+ # .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mustache-sinatra.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Mustache
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ricardo Mendes
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # Mustache::Sinatra
2
+
3
+ Support for Mustache in your Sinatra app.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'mustache-sinatra'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install mustache-sinatra
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'mustache/sinatra'
25
+
26
+ class Hurl < Sinatra::Base
27
+ register Mustache::Sinatra
28
+
29
+ set :mustache, {
30
+ # Should be the path to your .mustache template files.
31
+ :templates => "path/to/mustache/templates",
32
+
33
+ # Should be the path to your .rb Mustache view files.
34
+ :views => "path/to/mustache/views",
35
+
36
+ # This tells Mustache where to look for the Views module,
37
+ # under which your View classes should live. By default it's
38
+ # the class of your app - in this case `Hurl`. That is, for an :index
39
+ # view Mustache will expect Hurl::Views::Index by default.
40
+ # If our Sinatra::Base subclass was instead Hurl::App,
41
+ # we'd want to do `set :namespace, Hurl::App`
42
+ :namespace => Hurl
43
+ }
44
+
45
+ get '/stats' do
46
+ mustache :stats
47
+ end
48
+ end
49
+ ```
50
+
51
+ As noted above, Mustache will look for `Hurl::Views::Index` when
52
+ `mustache :index` is called.
53
+
54
+ If no `Views::Stats` class exists Mustache will render the template
55
+ file directly.
56
+
57
+ You can indeed use layouts with this library. Where you'd normally
58
+ <%= yield %> you instead {{{yield}}} - the body of the subview is
59
+ set to the `yield` variable and made available to you.
60
+
61
+ If you don't want the Sinatra extension to look up your view class,
62
+ maybe because you've already loaded it or you're pulling it in from
63
+ a gem, you can hand the `mustache` helper a Mustache subclass directly:
64
+
65
+ ```ruby
66
+ # Assuming `class Omnigollum::Login < Mustache`
67
+ get '/login' do
68
+ @title = "Log In"
69
+ require 'lib/omnigollum/views/login'
70
+ mustache Omnigollum::Login
71
+ end
72
+ ```
73
+
74
+ ## Contributing
75
+
76
+ 1. Fork it ( https://github.com/[my-github-username]/mustache-sinatra/fork )
77
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
78
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
79
+ 4. Push to the branch (`git push origin my-new-feature`)
80
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,155 @@
1
+ require "mustache/sinatra/version"
2
+
3
+ class Mustache
4
+ module Sinatra
5
+ module Helpers
6
+ # Call this in your Sinatra routes.
7
+ def mustache(template, options={}, locals={})
8
+ # Locals can be passed as options under the :locals key.
9
+ locals.update(options.delete(:locals) || {})
10
+
11
+ # Grab any user-defined settings.
12
+ if settings.respond_to?(:mustache)
13
+ options = settings.send(:mustache).merge(options)
14
+ end
15
+
16
+ # If they aren't explicitly disabling layouts, try to find
17
+ # one.
18
+ if options[:layout] != false
19
+ # Let the user pass in a layout name.
20
+ layout_name = options[:layout]
21
+
22
+ # If all they said was `true` (or nothing), default to :layout.
23
+ layout_name = :layout if layout_name == true || !layout_name
24
+
25
+ # If they passed a layout name use that.
26
+ layout = mustache_class(layout_name, options)
27
+
28
+ # If it's just an anonymous subclass then don't bother, otherwise
29
+ # give us a layout instance.
30
+ if layout.name && layout.name.empty?
31
+ layout = nil
32
+ else
33
+ layout = layout.new
34
+ end
35
+ end
36
+
37
+ # If instead of a symbol they gave us a Mustache class,
38
+ # use that for rendering.
39
+ klass = template if template.is_a?(Class) && template < Mustache
40
+
41
+ # Find and cache the view class we want if we don't have
42
+ # one yet. This ensures the compiled template is cached,
43
+ # too - no looking up and compiling templates on each page
44
+ # load.
45
+ if klass.nil?
46
+ klass = mustache_class(template, options)
47
+ end
48
+
49
+ # Does the view subclass the layout? If so we'll use the
50
+ # view to render the layout so you can override layout
51
+ # methods in your view - tricky.
52
+ view_subclasses_layout = klass < layout.class if layout
53
+
54
+ # Create a new instance for playing with.
55
+ instance = klass.new
56
+
57
+ # Copy instance variables set in Sinatra to the view
58
+ instance_variables.each do |name|
59
+ instance.instance_variable_set(name, instance_variable_get(name))
60
+ end
61
+
62
+ # Render with locals.
63
+ rendered = instance.render(instance.template, locals)
64
+
65
+ # Now render the layout with the view we just rendered, if we
66
+ # need to.
67
+ if layout && view_subclasses_layout
68
+ rendered = instance.render(layout.template, :yield => rendered)
69
+ elsif layout
70
+ rendered = layout.render(layout.template, :yield => rendered)
71
+ end
72
+
73
+ # That's it.
74
+ rendered
75
+ end
76
+
77
+ # Returns a View class for a given template name.
78
+ def mustache_class(template, options = {})
79
+ @template_cache.fetch(:mustache, template) do
80
+ compile_mustache(template, options)
81
+ end
82
+ end
83
+
84
+ # Given a view name and settings, finds and prepares an
85
+ # appropriate view class for this view.
86
+ def compile_mustache(view, options = {})
87
+ options[:templates] ||= settings.views if settings.respond_to?(:views)
88
+ options[:namespace] ||= self.class
89
+
90
+ unless options[:namespace].to_s.include? 'Views'
91
+ options[:namespace] = options[:namespace].const_get(:Views) rescue Object
92
+ end
93
+
94
+ factory = Class.new(Mustache) do
95
+ self.view_namespace = options[:namespace]
96
+ self.view_path = options[:views]
97
+ end
98
+
99
+ # If we were handed :"positions.atom" or some such as the
100
+ # template name, we need to remember the extension.
101
+ if view.to_s.include?('.')
102
+ view, ext = view.to_s.split('.')
103
+ end
104
+
105
+ # Try to find the view class for a given view, e.g.
106
+ # :view => Hurl::Views::Index.
107
+ klass = factory.view_class(view)
108
+ klass.view_namespace = options[:namespace]
109
+ klass.view_path = options[:views]
110
+
111
+ # If there is no view class, issue a warning and use the one
112
+ # we just generated to cache the compiled template.
113
+ if klass == Mustache
114
+ warn "No view class found for #{view} in #{factory.view_path}"
115
+ klass = factory
116
+
117
+ # If this is a generic view class make sure we set the
118
+ # template name as it was given. That is, an anonymous
119
+ # subclass of Mustache won't know how to find the
120
+ # "index.mustache" template unless we tell it to.
121
+ klass.template_name = view.to_s
122
+ elsif ext
123
+ # We got an ext (like "atom"), so look for an "Atom" class
124
+ # under the current View's namespace.
125
+ #
126
+ # So if our template was "positions.atom", try to find
127
+ # Positions::Atom.
128
+ if klass.const_defined?(ext_class = ext.capitalize)
129
+ # Found Positions::Atom - set it
130
+ klass = klass.const_get(ext_class)
131
+ else
132
+ # Didn't find Positions::Atom - create it by creating an
133
+ # anonymous subclass of Positions and setting that to
134
+ # Positions::Atom.
135
+ new_class = Class.new(klass)
136
+ new_class.template_name = "#{view}.#{ext}"
137
+ klass.const_set(ext_class, new_class)
138
+ klass = new_class
139
+ end
140
+ end
141
+
142
+ # Set the template path and return our class.
143
+ klass.template_path = options[:templates] if options[:templates]
144
+ klass
145
+ end
146
+ end
147
+
148
+ # Called when you `register Mustache::Sinatra` in your Sinatra app.
149
+ def self.registered(app)
150
+ app.helpers Mustache::Sinatra::Helpers
151
+ end
152
+ end
153
+ end
154
+
155
+ Sinatra.register Mustache::Sinatra
@@ -0,0 +1,5 @@
1
+ class Mustache
2
+ module Sinatra
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mustache/sinatra/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mustache-sinatra"
8
+ spec.version = Mustache::Sinatra::VERSION
9
+ spec.authors = ["Ricardo Mendes"]
10
+ spec.email = ["rokusu@gmail.com"]
11
+ spec.summary = %q{Mustache support for Sinatra applications}
12
+ spec.description = %q{Mustache support for Sinatra applications}
13
+ spec.homepage = "https://github.com/mustache/mustache-sinatra"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mustache-sinatra
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ricardo Mendes
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Mustache support for Sinatra applications
42
+ email:
43
+ - rokusu@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - lib/mustache/sinatra.rb
55
+ - lib/mustache/sinatra/version.rb
56
+ - mustache-sinatra.gemspec
57
+ homepage: https://github.com/mustache/mustache-sinatra
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.2.2
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Mustache support for Sinatra applications
81
+ test_files: []
82
+ has_rdoc: