handlebars_exec 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 60e887a03f7cdda968d7abee96dcc17abc299aa8
4
+ data.tar.gz: 6dcc67d0a537f4cd33ef5168fc0a317fdbf9c525
5
+ SHA512:
6
+ metadata.gz: 3d9a4d42dac28b9b371b7186ad58de118a2ccea08a0ba3f9bfee3dc6cbf22f204940a0605633a5ab24f235efacff1d740dcf19af6aece671c713d8648f4c92ad
7
+ data.tar.gz: 7d868fb7d3432ba8ebe25c3cc003a17b8b2729184c0a69ce0672bd492eefd3512f920209c134cfc288286dfbff48605d70cf30f23e4d9ebc7df90312bdaabb6e
@@ -0,0 +1,5 @@
1
+ Gemfile.lock
2
+ .DS_Store
3
+ .ruby-version
4
+ .*.swp
5
+ gemfiles/*
@@ -0,0 +1,9 @@
1
+ appraise "mini_racer-0.1" do
2
+ gem "mini_racer", "0.1.3"
3
+ end
4
+ appraise "therubyracer-0.12" do
5
+ gem "therubyracer", "0.12.0"
6
+ end
7
+ appraise "external_js" do
8
+ # assumption made that node.js or other external runtime is installed
9
+ end
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2016 Anthony Fojas
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,54 @@
1
+ ## HandlebarsExec
2
+
3
+ Bindings for [Handlebars.js][1] using [execjs][2]. Tested using [mini_racer][3], [therubyracer][4], and native [nodejs][5].
4
+
5
+ ## Usage
6
+
7
+ ### Simple Templates
8
+
9
+ handlebars = HandlebarsExec::Context.new
10
+ template = handlebars.compile("My Name is {{name}}")
11
+ template.template(name: "Slim Shady") #=> "My Name is Slim Shady"
12
+
13
+ ### Partials
14
+
15
+ Partials can be added by either adding a string with the partial or by adding a file with the partial registration in it. Referencing a file allows you to use the same files for handlebars on the backend and frontend.
16
+
17
+ # passed as a string
18
+ handlebars.register_partial("my_partial", "{{foo}} {{bar}}")
19
+
20
+ # passed as a file location
21
+ handlebars.register_partial_file(partial_file_path)
22
+
23
+ ### Helpers
24
+
25
+ Helpers can be added by either adding a string with the helper or by adding a file with the helper registration in it. Referencing a file allows you to use the same files for handlebars on the backend and frontend.
26
+
27
+ # passed as a string
28
+ handlebars.register_helper("formatPhoneNumber", <<-HELP)
29
+ function(phoneNumber) {
30
+ phoneNumber = phoneNumber.toString();
31
+ return "(" + phoneNumber.substr(0,3) + ") " + phoneNumber.substr(3,3) + "-" + phoneNumber.substr(6,4);
32
+ }
33
+ HELP
34
+
35
+ # passed as a file location
36
+ handlebars.register_partial_file(partial_file_path)
37
+
38
+ ## Test
39
+
40
+ Testing in different JS runtimes are handled using [Appraisal][6]. Once installed, the following will setup the different js runtime test setups
41
+
42
+ appraisal install
43
+
44
+ Tests can then be run like so:
45
+
46
+ appraisal rake
47
+
48
+
49
+ [1]: http://github.com/wycats/handlebars.js "Handlebars JavaScript templating library"
50
+ [2]: https://github.com/rails/execjs "execjs"
51
+ [3]: https://github.com/discourse/mini_racer "mini_racer"
52
+ [4]: http://github.com/cowboyd/therubyracer "The Ruby Racer"
53
+ [5]: https://nodejs.org/ "Node.js"
54
+ [6]: https://github.com/thoughtbot/appraisal "Appraisal"
@@ -0,0 +1,24 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'rake'
5
+ require 'appraisal'
6
+ require 'pry'
7
+
8
+ require 'rake/testtask'
9
+
10
+ Rake::TestTask.new do |t|
11
+ t.libs.push 'spec'
12
+ t.pattern = 'spec/**/*_spec.rb'
13
+ t.warning = true
14
+ t.verbose = true
15
+ end
16
+
17
+ task :default => :test
18
+
19
+ desc 'Generates a coverage report'
20
+ task :coverage do
21
+ ENV['COVERAGE'] = 'true'
22
+ Rake::Task['test'].execute
23
+ end
24
+
@@ -0,0 +1,21 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "handlebars_exec"
3
+ s.version = '0.1.0'
4
+ s.authors = ["Anthony Fojas"]
5
+ s.email = "anthony.fojas@vibes.com"
6
+ s.summary = "Ruby bindings for Handlebars.js"
7
+ s.description = "Bindings for handlebars using execjs. Tested using mini_racer, therubyracer, and native nodejs"
8
+ s.required_rubygems_version = ">= 1.3.6"
9
+
10
+ s.files = `git ls-files`.split("\n")
11
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
12
+
13
+ s.add_development_dependency "rake"
14
+ s.add_development_dependency "minitest"
15
+ s.add_development_dependency "appraisal"
16
+ s.add_development_dependency "pry"
17
+
18
+ s.add_runtime_dependency 'execjs', '>= 2.7.0'
19
+ s.add_runtime_dependency 'handlebars-source'
20
+ s.license = 'MIT'
21
+ end
@@ -0,0 +1,5 @@
1
+ module HandlebarsExec
2
+ end
3
+
4
+ require 'handlebars_exec/context'
5
+ require 'handlebars_exec/template'
@@ -0,0 +1,59 @@
1
+ require "execjs"
2
+ require "handlebars/source"
3
+
4
+ module HandlebarsExec
5
+ class Context
6
+
7
+ attr_reader :context
8
+ def initialize
9
+ @sources= []
10
+ @_handlebars_source = File.read Handlebars::Source.bundled_path
11
+ @context = generate_context
12
+ end
13
+
14
+ def generate_context
15
+ sources = ([@_handlebars_source] + @sources).join("\n")
16
+ ExecJS.compile(sources)
17
+ end
18
+
19
+ def compile template
20
+ HandlebarsExec::Template.new self.context,template
21
+ end
22
+
23
+ def add_file_to_context file
24
+ add_to_context File.read(file)
25
+ end
26
+ alias :register_helper_file :add_file_to_context
27
+ alias :register_partial_file :add_file_to_context
28
+
29
+ def register_partial_file name, file
30
+ add_to_context "Handlebars.registerPartial('#{name}', #{File.read(file).strip.inspect});"
31
+ end
32
+
33
+ def register_helper name, function
34
+ add_to_context "Handlebars.registerHelper('#{name}', #{function});"
35
+ end
36
+ def register_partial name, partial
37
+ add_to_context "Handlebars.registerPartial('#{name}', #{partial.inspect});"
38
+ end
39
+
40
+ def add_to_context str
41
+ @sources.push (str)
42
+ if context_preserved_between_calls?
43
+ context.eval("(function(){ #{str}})()")
44
+ else
45
+ # ExecJS::ExternalRuntime::Context does not have a writable context
46
+ # the only way to attach new variables and functions is to destroy the old context
47
+ @context = generate_context
48
+ end
49
+ end
50
+
51
+ private
52
+ def context_preserved_between_calls?
53
+ [
54
+ "ExecJS::RubyRacerRuntime::Context",
55
+ "ExecJS::MiniRacerRuntime::Context"
56
+ ].include? context.class.to_s
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,12 @@
1
+ module HandlebarsExec
2
+ class Template
3
+ def initialize context, template
4
+ @context = context
5
+ @template_string= template
6
+ end
7
+
8
+ def template variables
9
+ @context.eval("Handlebars.compile(#{@template_string.inspect})(#{variables.to_json})")
10
+ end
11
+ end
12
+ end
@@ -0,0 +1 @@
1
+ {{name}}
@@ -0,0 +1,4 @@
1
+ Handlebars.registerHelper("formatPhoneNumber", function(phoneNumber) {
2
+ phoneNumber = phoneNumber.toString();
3
+ return "(" + phoneNumber.substr(0,3) + ") " + phoneNumber.substr(3,3) + "-" + phoneNumber.substr(6,4);
4
+ })
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+ describe HandlebarsExec::Context do
4
+
5
+ let(:subject) { HandlebarsExec::Context.new }
6
+
7
+ describe "a simple template" do
8
+
9
+ describe "compiled" do
10
+ let(:compiled_template) { subject.compile("Hello {{name}}") }
11
+ it "compiles a template" do
12
+ compiled_template.must_be_instance_of HandlebarsExec::Template
13
+ end
14
+ end
15
+ end
16
+
17
+ describe "#register_helper_file" do
18
+ let(:helper_file_path) { 'spec/fixtures/phone_helpers.js' }
19
+ before(:context) do
20
+ subject.register_helper_file(helper_file_path)
21
+ end
22
+ it "has a helper" do
23
+ subject.context.eval('typeof Handlebars.helpers["formatPhoneNumber"]').must_equal "function"
24
+ end
25
+ it "can use a helper" do
26
+ subject.compile("call {{formatPhoneNumber telephone}}").template(telephone: "2813308004").must_equal "call (281) 330-8004"
27
+ end
28
+ end
29
+
30
+ describe "#register_helper" do
31
+ let(:helper) { File.read 'spec/fixtures/phone_helpers.js' }
32
+ before(:context) do
33
+ subject.register_helper("formatPhoneNumber", <<-HELP)
34
+ function(phoneNumber) {
35
+ phoneNumber = phoneNumber.toString();
36
+ return "(" + phoneNumber.substr(0,3) + ") " + phoneNumber.substr(3,3) + "-" + phoneNumber.substr(6,4);
37
+ }
38
+ HELP
39
+ end
40
+ it "has a helper" do
41
+ subject.context.eval('typeof Handlebars.helpers["formatPhoneNumber"]').must_equal "function"
42
+ end
43
+ it "can use a helper" do
44
+ subject.compile("call {{formatPhoneNumber telephone}}").template(telephone: "2813308004").must_equal "call (281) 330-8004"
45
+ end
46
+ end
47
+
48
+ describe "#register_partial_file" do
49
+ let(:partial_file_path) { 'spec/fixtures/partial.hbs' }
50
+ before(:context) do
51
+ subject.register_partial_file('myPartial', partial_file_path)
52
+ end
53
+ it "has a partial" do
54
+ subject.context.eval('typeof Handlebars.partials["myPartial"]').must_equal "string"
55
+ end
56
+ it "can parse the partial" do
57
+ subject.compile("Hello {{>myPartial}}").template(name: 'World').must_equal "Hello World"
58
+ end
59
+ end
60
+
61
+ describe "#register_partial" do
62
+ let(:partial) { File.read 'spec/fixtures/partial.hbs' }
63
+ before(:context) do
64
+ subject.register_partial('myPartial', '{{name}}')
65
+ end
66
+ it "has a partial" do
67
+ subject.context.eval('typeof Handlebars.partials["myPartial"]').must_equal "string"
68
+ end
69
+ it "can parse the partial" do
70
+ subject.compile("Hello {{>myPartial}}").template(name: 'World').must_equal "Hello World"
71
+ end
72
+ end
73
+ end
74
+
@@ -0,0 +1,66 @@
1
+ require "spec_helper"
2
+
3
+ describe HandlebarsExec::Template do
4
+ let(:context) { HandlebarsExec::Context.new }
5
+ let(:subject) { HandlebarsExec::Template.new context.context, template_string }
6
+
7
+ describe "a simple template" do
8
+ let(:template_string) { "Hello {{name}}" }
9
+
10
+ it "allows simple subsitution" do
11
+ subject.template(name: "World").must_equal "Hello World"
12
+ end
13
+ end
14
+
15
+ describe "a template with multiple lines" do
16
+ let(:template_string) do
17
+ <<-TEMPLATE
18
+ Hello {{name.last}}
19
+ TEMPLATE
20
+ end
21
+
22
+ it "allows simple subsitution" do
23
+ subject.template(name: { last: "World"}).must_equal <<-TEMPLATE
24
+ Hello World
25
+ TEMPLATE
26
+ end
27
+ end
28
+
29
+ describe "a template with hash attributes" do
30
+ let(:template_string) { "Hello {{name.last}}" }
31
+
32
+ it "allows simple subsitution" do
33
+ subject.template(name: { last: "World"}).must_equal "Hello World"
34
+ end
35
+ end
36
+
37
+ describe "a template with an array " do
38
+ let(:template_string) { "Hello {{#each name}}{{this}}{{/each}}" }
39
+
40
+ it "allows simple subsitution" do
41
+ subject.template(name: %w(W o r l d)).must_equal "Hello World"
42
+ end
43
+ end
44
+
45
+ describe "with a helper" do
46
+ let(:helper) { 'spec/fixtures/phone_helpers.js' }
47
+ let(:template_string) { "call {{formatPhoneNumber telephone}}" }
48
+ before(:context) do
49
+ context.register_helper_file(helper)
50
+ end
51
+ it "can use a helper" do
52
+ subject.template(telephone: "2813308004").must_equal "call (281) 330-8004"
53
+ end
54
+ end
55
+
56
+ describe "with a partial" do
57
+ let(:partial) { 'spec/fixtures/partial.hbs' }
58
+ let(:template_string) { "Hello {{> myPartial}}" }
59
+ before(:context) do
60
+ context.register_partial_file("myPartial", partial)
61
+ end
62
+ it "can use a partial" do
63
+ subject.template(name: "World").must_equal "Hello World"
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+
3
+ describe HandlebarsExec do
4
+ end
@@ -0,0 +1,3 @@
1
+ require "minitest/autorun"
2
+ require 'handlebars_exec'
3
+ require "pry"
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: handlebars_exec
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Anthony Fojas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-06-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: appraisal
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: execjs
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 2.7.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 2.7.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: handlebars-source
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Bindings for handlebars using execjs. Tested using mini_racer, therubyracer,
98
+ and native nodejs
99
+ email: anthony.fojas@vibes.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Appraisals
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - handlebars_exec.gemspec
111
+ - lib/handlebars_exec.rb
112
+ - lib/handlebars_exec/context.rb
113
+ - lib/handlebars_exec/template.rb
114
+ - spec/fixtures/partial.hbs
115
+ - spec/fixtures/phone_helpers.js
116
+ - spec/handlebars_exec/context_spec.rb
117
+ - spec/handlebars_exec/template_spec.rb
118
+ - spec/handlebars_exec_spec.rb
119
+ - spec/spec_helper.rb
120
+ homepage:
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: 1.3.6
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.5.1
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Ruby bindings for Handlebars.js
144
+ test_files:
145
+ - spec/fixtures/partial.hbs
146
+ - spec/fixtures/phone_helpers.js
147
+ - spec/handlebars_exec/context_spec.rb
148
+ - spec/handlebars_exec/template_spec.rb
149
+ - spec/handlebars_exec_spec.rb
150
+ - spec/spec_helper.rb