hbs_plus 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/handlebars.rb ADDED
@@ -0,0 +1,27 @@
1
+
2
+ require 'handlebars/loader'
3
+
4
+ module Handlebars
5
+
6
+ @loader = Loader.new
7
+
8
+ module_function
9
+
10
+ def compile(*args)
11
+ handlebars.compile(*args)
12
+ end
13
+
14
+ def register_helper(name, &fn)
15
+ handlebars.registerHelper(name, fn)
16
+ end
17
+
18
+ def register_partial(name, str)
19
+ handlebars.registerPartial(name, str)
20
+ end
21
+
22
+ def handlebars
23
+ Handlebars.module_eval do
24
+ @loader.require('handlebars')
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,29 @@
1
+
2
+ require 'v8'
3
+ require 'pathname'
4
+
5
+ module Handlebars
6
+ class Loader
7
+
8
+ def initialize
9
+ @cxt = V8::Context.new
10
+ @path = Pathname(__FILE__).dirname.join('..','..','js','lib')
11
+ @modules = {}
12
+ end
13
+
14
+ def require(modname)
15
+ unless mod = @modules[modname]
16
+ filename = modname =~ /\.js$/ ? modname : "#{modname}.js"
17
+ filepath = @path.join(filename)
18
+ fail LoadError, "no such file: #{filename}" unless filepath.exist?
19
+ load = @cxt.eval("(function(require, module, exports) {#{File.read(filepath)}})", filepath.expand_path)
20
+ object = @cxt['Object']
21
+ mod = object.new
22
+ mod['exports'] = object.new
23
+ @modules[modname] = mod
24
+ load.call(method(:require), mod, mod.exports)
25
+ end
26
+ return mod.exports
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module Handlebars
2
+ VERSION = "0.1.3"
3
+ end
@@ -0,0 +1,55 @@
1
+
2
+ require 'handlebars'
3
+ describe(Handlebars) do
4
+
5
+ before {extend Handlebars}
6
+
7
+ describe "a simple template" do
8
+ let(:t) {compile("Hello {{name}}")}
9
+ it "allows simple subsitution" do
10
+ t.call(:name => 'World').should eql "Hello World"
11
+ end
12
+
13
+ it "allows Ruby blocks as a property" do
14
+ t.call(:name => lambda {|context| ;"Mate"}).should eql "Hello Mate"
15
+ end
16
+
17
+ it "can use any Ruby object as a context" do
18
+ t.call(mock(:Object, :name => "Flipper")).should eql "Hello Flipper"
19
+ end
20
+ end
21
+
22
+ describe "registering Helpers" do
23
+ Handlebars.register_helper('alsowith') do |context, block|
24
+ block.call(context)
25
+ end
26
+ Handlebars.register_helper(:twice) do |block|
27
+ "#{block.call}#{block.call}"
28
+ end
29
+
30
+ it "correctly passes context and implementation" do
31
+ t = compile("it's so {{#alsowith weather}}*{{summary}}*{{/alsowith}}!")
32
+ t.call(:weather => {:summary => "sunny"}).should eql "it's so *sunny*!"
33
+ end
34
+
35
+ it "doesn't nee a context or arguments to the call" do
36
+ t = compile("{{#twice}}Hurray!{{/twice}}")
37
+ t.call.should eql "Hurray!Hurray!"
38
+ end
39
+ end
40
+
41
+ describe "registering Partials" do
42
+ Handlebars.register_partial('futher_info', 'futher information about {{name}}')
43
+ Handlebars.register_partial(:extra_info, 'more info')
44
+
45
+ it "render a simple content" do
46
+ t = compile("I have {{> extra_info}}")
47
+ t.call.should eql "I have more info"
48
+ end
49
+
50
+ it "render the content using data in context" do
51
+ t = compile("I also know {{> futher_info}}")
52
+ t.call(:name => "John").should eql "I also know futher information about John"
53
+ end
54
+ end
55
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hbs_plus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Charles Lowell
9
+ - German DZ
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-01-26 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: therubyracer
17
+ requirement: &70341393771060 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 0.9.4
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *70341393771060
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ requirement: &70341393770560 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *70341393770560
37
+ description: Uses the actual JavaScript implementation of Handlebars, but supports
38
+ using Ruby objects as template contexts and Ruby procs as view functions and named
39
+ helpers
40
+ email:
41
+ - cowboyd@thefrontside.net
42
+ - germ@ndz.com.ar
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - .gitignore
48
+ - .gitmodules
49
+ - .rspec
50
+ - Gemfile
51
+ - README.mdown
52
+ - Rakefile
53
+ - hbs.gemspec
54
+ - lib/handlebars.rb
55
+ - lib/handlebars/loader.rb
56
+ - lib/handlebars/version.rb
57
+ - spec/handlebars_spec.rb
58
+ - js/.gitignore
59
+ - js/Gemfile
60
+ - js/Gemfile.lock
61
+ - js/LICENSE
62
+ - js/README.markdown
63
+ - js/Rakefile
64
+ - js/bench/benchwarmer.js
65
+ - js/bench/handlebars.js
66
+ - js/lib/handlebars.js
67
+ - js/lib/handlebars/ast.js
68
+ - js/lib/handlebars/base.js
69
+ - js/lib/handlebars/compiler.js
70
+ - js/lib/handlebars/debug.js
71
+ - js/lib/handlebars/printer.js
72
+ - js/lib/handlebars/utils.js
73
+ - js/lib/handlebars/visitor.js
74
+ - js/spec/acceptance_spec.rb
75
+ - js/spec/parser_spec.rb
76
+ - js/spec/qunit_spec.js
77
+ - js/spec/spec_helper.rb
78
+ - js/spec/tokenizer_spec.rb
79
+ - js/src/handlebars.l
80
+ - js/src/handlebars.yy
81
+ - js/lib/handlebars/parser.js
82
+ homepage: http://github.com/germandz/handlebars.rb
83
+ licenses: []
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project: handlebars
102
+ rubygems_version: 1.8.10
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Ruby bindings for the handlebars.js templating library, based on 'hbs' gem
106
+ test_files:
107
+ - spec/handlebars_spec.rb