sinatra-ember 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.md +45 -0
- data/lib/sinatra/ember.rb +83 -0
- data/sinatra-ember.gemspec +14 -0
- metadata +60 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
.*.swp
|
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# Sinatra Ember
|
2
|
+
|
3
|
+
Helpers for serving an [Ember.js][ember] app from [Sinatra][sinatra].
|
4
|
+
|
5
|
+
- Pre-compile handlebars templates from separate view files rather than littering them throughout your html.
|
6
|
+
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
You can install Sinatra Ember as a Ruby gem. You can install it via `gem install`.
|
11
|
+
|
12
|
+
``` console
|
13
|
+
$ gem install sinatra-ember
|
14
|
+
```
|
15
|
+
|
16
|
+
### Bundler users
|
17
|
+
|
18
|
+
If you use Bundler, add it to your *Gemfile.*
|
19
|
+
|
20
|
+
``` ruby
|
21
|
+
gem 'sinatra-ember', :require => 'sinatra/ember'
|
22
|
+
```
|
23
|
+
|
24
|
+
|
25
|
+
## Setup
|
26
|
+
|
27
|
+
Install the plugin and add some options.
|
28
|
+
|
29
|
+
``` ruby
|
30
|
+
require 'sinatra/ember'
|
31
|
+
|
32
|
+
class App < Sinatra::Base
|
33
|
+
register Sinatra::Ember
|
34
|
+
serve_templates '/js/templates.js'
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
## Sinatra AssetPack
|
39
|
+
|
40
|
+
If you're using the [sinatra-assetpack][assetpack] gem, add your served templates to a package.
|
41
|
+
|
42
|
+
|
43
|
+
[assetpack]: https://github.com/rstacruz/sinatra-assetpack
|
44
|
+
[ember]: http://emberjs.com
|
45
|
+
[sinatra]: http://sinatrarb.com
|
@@ -0,0 +1,83 @@
|
|
1
|
+
module Sinatra
|
2
|
+
module Ember
|
3
|
+
def self.version
|
4
|
+
"0.0.1"
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.registered(app)
|
8
|
+
app.extend ClassMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
# set ember options
|
13
|
+
def ember(&block)
|
14
|
+
@ember_options ||= Options.new(self, &block)
|
15
|
+
self.ember_init! if block_given?
|
16
|
+
@ember_options
|
17
|
+
end
|
18
|
+
|
19
|
+
def ember_init!
|
20
|
+
ember.template_packages.each do |route, globs|
|
21
|
+
get route do
|
22
|
+
mtime, output = @template_cache.fetch(route) do
|
23
|
+
# find all the template files
|
24
|
+
paths = globs.map do |glob|
|
25
|
+
glob = File.expand_path(File.join(settings.root, glob))
|
26
|
+
Dir[glob].map { |x| x.squeeze('/') }
|
27
|
+
end
|
28
|
+
paths = paths.flatten.uniq
|
29
|
+
|
30
|
+
# build up template hash
|
31
|
+
template_paths = {}
|
32
|
+
paths.each do |path|
|
33
|
+
template_paths[File.basename(path, '.hbs')] = path
|
34
|
+
end
|
35
|
+
|
36
|
+
# build up the javascript
|
37
|
+
templates = template_paths.map do |(name, path)|
|
38
|
+
content = File.read(path)
|
39
|
+
"Ember.TEMPLATES[#{name.inspect}] = Ember.Handlebars.compile(#{content.inspect});"
|
40
|
+
end
|
41
|
+
|
42
|
+
# wrap it up in a closure
|
43
|
+
output = %{
|
44
|
+
(function() {
|
45
|
+
#{templates.join("\n")}
|
46
|
+
})();
|
47
|
+
}
|
48
|
+
output = output.strip.gsub(/^ {16}/, '')
|
49
|
+
|
50
|
+
# compute the maximum mtime for all paths
|
51
|
+
mtime = paths.map do |path|
|
52
|
+
if File.file?(path)
|
53
|
+
File.mtime(path).to_i
|
54
|
+
end
|
55
|
+
end.compact.max
|
56
|
+
|
57
|
+
[mtime, output]
|
58
|
+
end
|
59
|
+
|
60
|
+
content_type :js
|
61
|
+
last_modified mtime
|
62
|
+
output
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
class Options
|
69
|
+
attr_reader :template_packages
|
70
|
+
|
71
|
+
def initialize(app, &block)
|
72
|
+
@app = app
|
73
|
+
@template_packages = {}
|
74
|
+
|
75
|
+
instance_eval(&block)
|
76
|
+
end
|
77
|
+
|
78
|
+
def templates(route, files=[])
|
79
|
+
@template_packages[route] = files
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require './lib/sinatra/ember'
|
2
|
+
Gem::Specification.new do |s|
|
3
|
+
s.name = 'sinatra-ember'
|
4
|
+
s.version = Sinatra::Ember.version
|
5
|
+
s.summary = 'Helpers for serving an Ember.js app from Sinatra.'
|
6
|
+
s.description = 'Serves Ember Handlebars pages.'
|
7
|
+
s.authors = ['Ben Scott']
|
8
|
+
s.email = ['gamepoet@gmail.com']
|
9
|
+
s.homepage = 'http://github.com/gamepoet/sinatra-ember'
|
10
|
+
s.files = `git ls-files`.strip.split("\n")
|
11
|
+
s.executables = Dir['bin/*'].map { |f| File.basename(f) }
|
12
|
+
|
13
|
+
s.add_dependency 'sinatra'
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-ember
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ben Scott
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: sinatra
|
16
|
+
requirement: &70219783856880 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70219783856880
|
25
|
+
description: Serves Ember Handlebars pages.
|
26
|
+
email:
|
27
|
+
- gamepoet@gmail.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- README.md
|
34
|
+
- lib/sinatra/ember.rb
|
35
|
+
- sinatra-ember.gemspec
|
36
|
+
homepage: http://github.com/gamepoet/sinatra-ember
|
37
|
+
licenses: []
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.8.17
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: Helpers for serving an Ember.js app from Sinatra.
|
60
|
+
test_files: []
|