steering 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +7 -0
- data/LICENSE +22 -0
- data/README.md +41 -0
- data/Rakefile +12 -0
- data/lib/steering.rb +63 -0
- data/lib/steering/version.rb +3 -0
- data/steering.gemspec +23 -0
- data/test/steering_test.rb +46 -0
- metadata +106 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Andrew White
|
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,41 @@
|
|
1
|
+
# Steering
|
2
|
+
|
3
|
+
Steering is a bridge to the [Handlebars.js][1] template precompiler. By precompiling your templates you can speed up page loading in two ways - firstly the final compilation step is much quicker as the template source code does not have to be parsed and the size of the library can be reduced by using the smaller runtime library if all your templates are precompiled.
|
4
|
+
|
5
|
+
require "steering"
|
6
|
+
|
7
|
+
Steering.compile(File.read("template.hb"))
|
8
|
+
# => "function(...) {...}"
|
9
|
+
|
10
|
+
context = Steering.context_for("Hello {{ name }}")
|
11
|
+
context.call("template", :name => "Andrew")
|
12
|
+
# => "Hello Andrew"
|
13
|
+
|
14
|
+
Steering.render("Hello {{ name }}", :name => "world")
|
15
|
+
# => "Hello world"
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
$ gem install steering
|
20
|
+
|
21
|
+
## Dependencies
|
22
|
+
|
23
|
+
This library depends on the `steering-source` gem which is updated any time a new version of Handlebars.js is released (The `steering-source` gem's version number is synced with each official Handlebars.js release). This way you can build against different versions of Handlebars.js by requiring the correct version of the `steering-source` gem.
|
24
|
+
|
25
|
+
In addition, you can use this library with unreleased versions of Handlebars.js by setting the `HANDLEBARS_SOURCE_PATH` and `HANDLEBARS_RUNTIME_PATH` environment variable:
|
26
|
+
|
27
|
+
export HANDLEBARS_SOURCE_PATH=/path/to/handlebars.js
|
28
|
+
export HANDLEBARS_RUNTIME_PATH=/path/to/handlebars.runtime.js
|
29
|
+
|
30
|
+
### ExecJS
|
31
|
+
|
32
|
+
The [ExecJS][2] library is used to automatically choose the best JavaScript engine for your platform. Check out its [README][3] for a complete list of supported engines.
|
33
|
+
|
34
|
+
## Acknowledgements
|
35
|
+
|
36
|
+
The structure and code patterns for this gem were derived from the [Ruby Eco][4] gem
|
37
|
+
|
38
|
+
[1]: https://github.com/wycats/handlebars.js
|
39
|
+
[2]: https://github.com/sstephenson/execjs
|
40
|
+
[3]: https://github.com/sstephenson/execjs/blob/master/README.md
|
41
|
+
[4]: https://github.com/sstephenson/ruby-eco
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "rake/testtask"
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
|
5
|
+
desc "Default: run handlebars unit tests."
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
Rake::TestTask.new do |t|
|
9
|
+
t.libs += %w[lib test]
|
10
|
+
t.pattern = "test/**/*_test.rb"
|
11
|
+
t.warning = true
|
12
|
+
end
|
data/lib/steering.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require "execjs"
|
2
|
+
require "steering/source"
|
3
|
+
|
4
|
+
module Steering
|
5
|
+
module Source
|
6
|
+
def self.path
|
7
|
+
@path ||= ENV["HANDLEBARS_SOURCE_PATH"] || bundled_path
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.path=(path)
|
11
|
+
@contents = @version = @context = nil
|
12
|
+
@path = path
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.contents
|
16
|
+
@contents ||= File.read(path)
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.context
|
20
|
+
@context ||= ExecJS.compile(contents)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.runtime_path
|
24
|
+
@runtime_path ||= ENV["HANDLEBARS_RUNTIME_PATH"] || bundled_runtime_path
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.runtime_path=(runtime_path)
|
28
|
+
@runtime = nil
|
29
|
+
@runtime_path = runtime_path
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.runtime
|
33
|
+
@runtime_contents ||= File.read(runtime_path)
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.version
|
37
|
+
@version ||= context.eval("Handlebars.VERSION")
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class << self
|
42
|
+
def version
|
43
|
+
Source.version
|
44
|
+
end
|
45
|
+
|
46
|
+
def compile(template)
|
47
|
+
template = template.read if template.respond_to?(:read)
|
48
|
+
Source.context.call("Handlebars.precompile", template, { :knownHelpers => known_helpers })
|
49
|
+
end
|
50
|
+
|
51
|
+
def context_for(template, extra = "")
|
52
|
+
ExecJS.compile("#{Source.runtime}; #{extra}; var template = Handlebars.template(#{compile(template)})")
|
53
|
+
end
|
54
|
+
|
55
|
+
def known_helpers
|
56
|
+
Source.known_helpers
|
57
|
+
end
|
58
|
+
|
59
|
+
def render(template, locals = {})
|
60
|
+
context_for(template).call("template", locals)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/steering.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "steering/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.name = "steering"
|
7
|
+
gem.version = Steering::VERSION
|
8
|
+
gem.platform = Gem::Platform::RUBY
|
9
|
+
gem.authors = ["Andrew White"]
|
10
|
+
gem.email = ["andyw@pixeltrix.co.uk"]
|
11
|
+
gem.homepage = "https://github.com/pixeltrix/steering"
|
12
|
+
|
13
|
+
gem.summary = %q{Ruby Handlebars.js Compiler}
|
14
|
+
gem.description = %q{Steering is a bridge to the official JavaScript Handlebars.js compiler.}
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split("\n")
|
17
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
|
21
|
+
gem.add_runtime_dependency 'execjs', '>= 1.3.0'
|
22
|
+
gem.add_runtime_dependency 'steering-source', '>= 1.0.beta.6'
|
23
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require "steering"
|
2
|
+
require "stringio"
|
3
|
+
require "test/unit"
|
4
|
+
|
5
|
+
class SteeringTest < Test::Unit::TestCase
|
6
|
+
JS_FUNCTION_PATTERN = /^function\s*\(.*?\)\s*\{.*\}$/m
|
7
|
+
|
8
|
+
def test_version
|
9
|
+
assert_equal Steering::Source::VERSION, Steering.version
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_compile
|
13
|
+
assert_match JS_FUNCTION_PATTERN, Steering.compile("Hello {{ name }}")
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_compile_with_io
|
17
|
+
io = StringIO.new("Hello {{ name }}")
|
18
|
+
assert_equal Steering.compile("Hello {{ name }}"), Steering.compile(io)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_compilation_error
|
22
|
+
assert_raise ExecJS::ProgramError do
|
23
|
+
Steering.compile("{{ name")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_context_for
|
28
|
+
context = Steering.context_for("Hello {{ name }}")
|
29
|
+
assert_equal "Hello Andrew", context.call("template", :name => "Andrew")
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_render
|
33
|
+
assert_equal "Hello Andrew", Steering.render("Hello {{ name }}", :name => "Andrew")
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_runtime_error
|
37
|
+
helper = "Handlebars.registerHelper('throw', function(arg) { throw arg; })"
|
38
|
+
context = Steering.context_for("Hello {{ throw foo }}", helper)
|
39
|
+
|
40
|
+
begin
|
41
|
+
context.call("template", :foo => "bar")
|
42
|
+
rescue ExecJS::ProgramError => e
|
43
|
+
assert_equal "bar", e.message
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: steering
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Andrew White
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-03-21 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: execjs
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 27
|
29
|
+
segments:
|
30
|
+
- 1
|
31
|
+
- 3
|
32
|
+
- 0
|
33
|
+
version: 1.3.0
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: steering-source
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 2469281371
|
45
|
+
segments:
|
46
|
+
- 1
|
47
|
+
- 0
|
48
|
+
- beta
|
49
|
+
- 6
|
50
|
+
version: 1.0.beta.6
|
51
|
+
type: :runtime
|
52
|
+
version_requirements: *id002
|
53
|
+
description: Steering is a bridge to the official JavaScript Handlebars.js compiler.
|
54
|
+
email:
|
55
|
+
- andyw@pixeltrix.co.uk
|
56
|
+
executables: []
|
57
|
+
|
58
|
+
extensions: []
|
59
|
+
|
60
|
+
extra_rdoc_files: []
|
61
|
+
|
62
|
+
files:
|
63
|
+
- .gitignore
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- lib/steering.rb
|
69
|
+
- lib/steering/version.rb
|
70
|
+
- steering.gemspec
|
71
|
+
- test/steering_test.rb
|
72
|
+
homepage: https://github.com/pixeltrix/steering
|
73
|
+
licenses: []
|
74
|
+
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
hash: 3
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
version: "0"
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
hash: 3
|
95
|
+
segments:
|
96
|
+
- 0
|
97
|
+
version: "0"
|
98
|
+
requirements: []
|
99
|
+
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 1.8.14
|
102
|
+
signing_key:
|
103
|
+
specification_version: 3
|
104
|
+
summary: Ruby Handlebars.js Compiler
|
105
|
+
test_files:
|
106
|
+
- test/steering_test.rb
|