Skip to content

Commit

Permalink
gem scaffold + code extracted from main mustache repo
Browse files Browse the repository at this point in the history
  • Loading branch information
locks committed Dec 2, 2014
1 parent 2e5e902 commit 3705ae2
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in mustache-sinatra.gemspec
gemspec
22 changes: 22 additions & 0 deletions LICENSE.txt
@@ -0,0 +1,22 @@
Copyright (c) 2014 Ricardo Mendes

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 changes: 30 additions & 3 deletions README.md
@@ -1,4 +1,31 @@
mustache-sinatra
================
# Mustache::Sinatra

Mustache support for Sinatra applications
TODO: Write a gem description

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'mustache-sinatra'
```

And then execute:

$ bundle

Or install it yourself as:

$ gem install mustache-sinatra

## Usage

TODO: Write usage instructions here

## Contributing

1. Fork it ( https://github.com/[my-github-username]/mustache-sinatra/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
2 changes: 2 additions & 0 deletions Rakefile
@@ -0,0 +1,2 @@
require "bundler/gem_tasks"

204 changes: 204 additions & 0 deletions lib/mustache/sinatra.rb
@@ -0,0 +1,204 @@
require "mustache/sinatra/version"

class Mustache
# Support for Mustache in your Sinatra app.
#
# require 'mustache/sinatra'
#
# class Hurl < Sinatra::Base
# register Mustache::Sinatra
#
# set :mustache, {
# # Should be the path to your .mustache template files.
# :templates => "path/to/mustache/templates",
#
# # Should be the path to your .rb Mustache view files.
# :views => "path/to/mustache/views",
#
# # This tells Mustache where to look for the Views module,
# # under which your View classes should live. By default it's
# # the class of your app - in this case `Hurl`. That is, for an :index
# # view Mustache will expect Hurl::Views::Index by default.
# # If our Sinatra::Base subclass was instead Hurl::App,
# # we'd want to do `set :namespace, Hurl::App`
# :namespace => Hurl
# }
#
# get '/stats' do
# mustache :stats
# end
# end
#
# As noted above, Mustache will look for `Hurl::Views::Index` when
# `mustache :index` is called.
#
# If no `Views::Stats` class exists Mustache will render the template
# file directly.
#
# You can indeed use layouts with this library. Where you'd normally
# <%= yield %> you instead {{{yield}}} - the body of the subview is
# set to the `yield` variable and made available to you.
#
# If you don't want the Sinatra extension to look up your view class,
# maybe because you've already loaded it or you're pulling it in from
# a gem, you can hand the `mustache` helper a Mustache subclass directly:
#
# # Assuming `class Omnigollum::Login < Mustache`
# get '/login' do
# @title = "Log In"
# require 'lib/omnigollum/views/login'
# mustache Omnigollum::Login
# end
#
module Sinatra
module Helpers
# Call this in your Sinatra routes.
def mustache(template, options={}, locals={})
# Locals can be passed as options under the :locals key.
locals.update(options.delete(:locals) || {})

# Grab any user-defined settings.
if settings.respond_to?(:mustache)
options = settings.send(:mustache).merge(options)
end

# If they aren't explicitly disabling layouts, try to find
# one.
if options[:layout] != false
# Let the user pass in a layout name.
layout_name = options[:layout]

# If all they said was `true` (or nothing), default to :layout.
layout_name = :layout if layout_name == true || !layout_name

# If they passed a layout name use that.
layout = mustache_class(layout_name, options)

# If it's just an anonymous subclass then don't bother, otherwise
# give us a layout instance.
if layout.name && layout.name.empty?
layout = nil
else
layout = layout.new
end
end

# If instead of a symbol they gave us a Mustache class,
# use that for rendering.
klass = template if template.is_a?(Class) && template < Mustache

# Find and cache the view class we want if we don't have
# one yet. This ensures the compiled template is cached,
# too - no looking up and compiling templates on each page
# load.
if klass.nil?
klass = mustache_class(template, options)
end

# Does the view subclass the layout? If so we'll use the
# view to render the layout so you can override layout
# methods in your view - tricky.
view_subclasses_layout = klass < layout.class if layout

# Create a new instance for playing with.
instance = klass.new

# Copy instance variables set in Sinatra to the view
instance_variables.each do |name|
instance.instance_variable_set(name, instance_variable_get(name))
end

# Render with locals.
rendered = instance.render(instance.template, locals)

# Now render the layout with the view we just rendered, if we
# need to.
if layout && view_subclasses_layout
rendered = instance.render(layout.template, :yield => rendered)
elsif layout
rendered = layout.render(layout.template, :yield => rendered)
end

# That's it.
rendered
end

# Returns a View class for a given template name.
def mustache_class(template, options = {})
@template_cache.fetch(:mustache, template) do
compile_mustache(template, options)
end
end

# Given a view name and settings, finds and prepares an
# appropriate view class for this view.
def compile_mustache(view, options = {})
options[:templates] ||= settings.views if settings.respond_to?(:views)
options[:namespace] ||= self.class

unless options[:namespace].to_s.include? 'Views'
options[:namespace] = options[:namespace].const_get(:Views) rescue Object
end

factory = Class.new(Mustache) do
self.view_namespace = options[:namespace]
self.view_path = options[:views]
end

# If we were handed :"positions.atom" or some such as the
# template name, we need to remember the extension.
if view.to_s.include?('.')
view, ext = view.to_s.split('.')
end

# Try to find the view class for a given view, e.g.
# :view => Hurl::Views::Index.
klass = factory.view_class(view)
klass.view_namespace = options[:namespace]
klass.view_path = options[:views]

# If there is no view class, issue a warning and use the one
# we just generated to cache the compiled template.
if klass == Mustache
warn "No view class found for #{view} in #{factory.view_path}"
klass = factory

# If this is a generic view class make sure we set the
# template name as it was given. That is, an anonymous
# subclass of Mustache won't know how to find the
# "index.mustache" template unless we tell it to.
klass.template_name = view.to_s
elsif ext
# We got an ext (like "atom"), so look for an "Atom" class
# under the current View's namespace.
#
# So if our template was "positions.atom", try to find
# Positions::Atom.
if klass.const_defined?(ext_class = ext.capitalize)
# Found Positions::Atom - set it
klass = klass.const_get(ext_class)
else
# Didn't find Positions::Atom - create it by creating an
# anonymous subclass of Positions and setting that to
# Positions::Atom.
new_class = Class.new(klass)
new_class.template_name = "#{view}.#{ext}"
klass.const_set(ext_class, new_class)
klass = new_class
end
end

# Set the template path and return our class.
klass.template_path = options[:templates] if options[:templates]
klass
end
end

# Called when you `register Mustache::Sinatra` in your Sinatra app.
def self.registered(app)
app.helpers Mustache::Sinatra::Helpers
end
end
end

Sinatra.register Mustache::Sinatra
5 changes: 5 additions & 0 deletions lib/mustache/sinatra/version.rb
@@ -0,0 +1,5 @@
class Mustache
module Sinatra
VERSION = "0.1.0"
end
end
23 changes: 23 additions & 0 deletions mustache-sinatra.gemspec
@@ -0,0 +1,23 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'mustache/sinatra/version'

Gem::Specification.new do |spec|
spec.name = "mustache-sinatra"
spec.version = Mustache::Sinatra::VERSION
spec.authors = ["Ricardo Mendes"]
spec.email = ["rokusu@gmail.com"]
spec.summary = %q{TODO: Write a short summary. Required.}
spec.description = %q{TODO: Write a longer description. Optional.}
spec.homepage = ""
spec.license = "MIT"

spec.files = `git ls-files -z`.split("\x0")
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.7"
spec.add_development_dependency "rake", "~> 10.0"
end

0 comments on commit 3705ae2

Please sign in to comment.