handlebar_wax 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,26 @@
1
+ tmp/*
2
+ *.class
3
+ *.war
4
+ *.sqlite3
5
+ *.sqlite
6
+ log/*
7
+ .DS_Store
8
+ /doc/app/*
9
+ .*.swp
10
+ *~
11
+ public/javascripts/all.js
12
+ public/uploads/*
13
+ test/coverage/*
14
+ ./devver/*
15
+ public/system/*
16
+ bin/*
17
+ db/*.sqlite3
18
+ db/*.sqlite
19
+ !vendor/gems/cache/
20
+ vendor/bundler_gems/*
21
+ !vendor/bundler_gems/cache/
22
+ *.swp
23
+ *.swo
24
+ .bundle
25
+ .#*
26
+ public/upload/*
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,95 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ handlebar_wax (0.0.1)
5
+ rails (~> 3.0)
6
+ therubyracer (~> 0.8.0)
7
+ yajl-ruby
8
+
9
+ GEM
10
+ remote: http://rubygems.org/
11
+ specs:
12
+ abstract (1.0.0)
13
+ actionmailer (3.0.4)
14
+ actionpack (= 3.0.4)
15
+ mail (~> 2.2.15)
16
+ actionpack (3.0.4)
17
+ activemodel (= 3.0.4)
18
+ activesupport (= 3.0.4)
19
+ builder (~> 2.1.2)
20
+ erubis (~> 2.6.6)
21
+ i18n (~> 0.4)
22
+ rack (~> 1.2.1)
23
+ rack-mount (~> 0.6.13)
24
+ rack-test (~> 0.5.7)
25
+ tzinfo (~> 0.3.23)
26
+ activemodel (3.0.4)
27
+ activesupport (= 3.0.4)
28
+ builder (~> 2.1.2)
29
+ i18n (~> 0.4)
30
+ activerecord (3.0.4)
31
+ activemodel (= 3.0.4)
32
+ activesupport (= 3.0.4)
33
+ arel (~> 2.0.2)
34
+ tzinfo (~> 0.3.23)
35
+ activeresource (3.0.4)
36
+ activemodel (= 3.0.4)
37
+ activesupport (= 3.0.4)
38
+ activesupport (3.0.4)
39
+ arel (2.0.9)
40
+ builder (2.1.2)
41
+ diff-lcs (1.1.2)
42
+ erubis (2.6.6)
43
+ abstract (>= 1.0.0)
44
+ fakefs (0.3.1)
45
+ i18n (0.5.0)
46
+ mail (2.2.15)
47
+ activesupport (>= 2.3.6)
48
+ i18n (>= 0.4.0)
49
+ mime-types (~> 1.16)
50
+ treetop (~> 1.4.8)
51
+ mime-types (1.16)
52
+ polyglot (0.3.1)
53
+ rack (1.2.1)
54
+ rack-mount (0.6.13)
55
+ rack (>= 1.0.0)
56
+ rack-test (0.5.7)
57
+ rack (>= 1.0)
58
+ rails (3.0.4)
59
+ actionmailer (= 3.0.4)
60
+ actionpack (= 3.0.4)
61
+ activerecord (= 3.0.4)
62
+ activeresource (= 3.0.4)
63
+ activesupport (= 3.0.4)
64
+ bundler (~> 1.0)
65
+ railties (= 3.0.4)
66
+ railties (3.0.4)
67
+ actionpack (= 3.0.4)
68
+ activesupport (= 3.0.4)
69
+ rake (>= 0.8.7)
70
+ thor (~> 0.14.4)
71
+ rake (0.8.7)
72
+ rcov (0.9.9)
73
+ rspec (2.1.0)
74
+ rspec-core (~> 2.1.0)
75
+ rspec-expectations (~> 2.1.0)
76
+ rspec-mocks (~> 2.1.0)
77
+ rspec-core (2.1.0)
78
+ rspec-expectations (2.1.0)
79
+ diff-lcs (~> 1.1.2)
80
+ rspec-mocks (2.1.0)
81
+ therubyracer (0.8.0)
82
+ thor (0.14.6)
83
+ treetop (1.4.9)
84
+ polyglot (>= 0.3.1)
85
+ tzinfo (0.3.24)
86
+ yajl-ruby (0.8.1)
87
+
88
+ PLATFORMS
89
+ ruby
90
+
91
+ DEPENDENCIES
92
+ fakefs
93
+ handlebar_wax!
94
+ rcov
95
+ rspec (~> 2.1.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Burke Libbey <burke@burkelibbey.org>
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.
data/README ADDED
@@ -0,0 +1,25 @@
1
+ = handlebar_wax
2
+
3
+ First off, credits:
4
+
5
+ * Steven Soroka (ssoroka) for pub-based discussion about this, and initial mustache_wax code
6
+ * Jamesarosen for handlebars-rails, which this project includes in its entirety, with very few modifications as of yet.
7
+ * Yehuda Katz (wycats) for the wonderful Handlebars.js.
8
+
9
+ Description goes here.
10
+
11
+ == Contributing to handlebar_wax
12
+
13
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
14
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
15
+ * Fork the project
16
+ * Start a feature/bugfix branch
17
+ * Commit and push until you are happy with your contribution
18
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
19
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
20
+
21
+ == Copyright
22
+
23
+ Copyright (c) 2011 Steven Soroka. See LICENSE.txt for
24
+ further details.
25
+
data/Rakefile ADDED
@@ -0,0 +1,33 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'rspec/core'
13
+ require 'rspec/core/rake_task'
14
+ RSpec::Core::RakeTask.new(:spec) do |spec|
15
+ spec.pattern = FileList['spec/**/*_spec.rb']
16
+ end
17
+
18
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
19
+ spec.pattern = 'spec/**/*_spec.rb'
20
+ spec.rcov = true
21
+ end
22
+
23
+ task :default => :spec
24
+
25
+ require 'rake/rdoctask'
26
+ Rake::RDocTask.new do |rdoc|
27
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
28
+
29
+ rdoc.rdoc_dir = 'rdoc'
30
+ rdoc.title = "mustache_wax #{version}"
31
+ rdoc.rdoc_files.include('README*')
32
+ rdoc.rdoc_files.include('lib/**/*.rb')
33
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "handlebar_wax/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "handlebar_wax"
7
+ s.version = HandlebarWax::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Burke Libbey"]
10
+ s.email = ["burke@burkelibbey.org"]
11
+ s.homepage = ""
12
+ s.summary = %q{write me later}
13
+ s.description = %q{write me later}
14
+
15
+ s.add_runtime_dependency 'yajl-ruby'
16
+ s.add_runtime_dependency 'rails', '~> 3.0'
17
+ s.add_runtime_dependency 'therubyracer', '~> 0.8.0'
18
+
19
+ s.add_development_dependency 'rspec', '~> 2.1.0'
20
+ s.add_development_dependency 'rcov', '>= 0'
21
+ s.add_development_dependency 'fakefs'
22
+
23
+ s.files = `git ls-files`.split("\n")
24
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
25
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
26
+ s.require_paths = ["lib"]
27
+ end
@@ -0,0 +1,24 @@
1
+ module HandlebarWax
2
+ class Middleware
3
+ def initialize(app)
4
+ @app=app
5
+ @newest = Time.now
6
+ end
7
+
8
+ def call(env)
9
+ mtimes = HandlebarWax.template_files.map do |f|
10
+ File.mtime(f)
11
+ end
12
+
13
+ if mtimes.any? && (newest = mtimes.max) > @newest
14
+ HandlebarWax.generate_templates
15
+ @newest = newest
16
+
17
+ # kill caching of old versions.
18
+ HandlebarWax.register_expansion(@newest)
19
+ end
20
+
21
+ @app.call(env)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,12 @@
1
+ module HandlebarWax
2
+ class Railtie < Rails::Railtie
3
+ initializer 'handlebar_wax.initialize' do |app|
4
+ HandlebarWax.generate_templates
5
+ HandlebarWax.register_expansion
6
+ if Rails.env.development?
7
+ app.config.middleware.use HandlebarWax::Middleware
8
+ end
9
+ end
10
+ end
11
+ end
12
+
@@ -0,0 +1,56 @@
1
+ require 'handlebar_wax/v8'
2
+ require "active_support"
3
+
4
+ module HandlebarWax
5
+ class TemplateHandler
6
+
7
+ def self.js
8
+ handlebars = File.join(Rails.root, "vendor", "javascripts", "handlebars.js")
9
+
10
+ unless File.exists?(handlebars)
11
+ raise "Could not find handlebars.js. Please copy it to #{handlebars}"
12
+ end
13
+
14
+ Thread.current[:v8_context] ||= begin
15
+ V8::Context.new do |js|
16
+ js.load(handlebars)
17
+ js.eval("Templates = {}")
18
+
19
+ js["puts"] = method(:puts)
20
+
21
+ js.eval(%{
22
+ Handlebars.registerHelper('helperMissing', function(helper) {
23
+ var params = Array.prototype.slice.call(arguments, 1);
24
+ return actionview[helper].apply(actionview, params);
25
+ })
26
+ })
27
+ end
28
+ end
29
+ end
30
+
31
+ def self.call(template)
32
+ # Here, we're sticking the compiled template somewhere in V8 where
33
+ # we can get back to it
34
+ js.eval(%{Templates["#{template.identifier}"] = Handlebars.compile(#{template.source.inspect}) })
35
+
36
+ if defined?(Encoding)
37
+ %{
38
+ js = ::HandlebarWax::TemplateHandler.js
39
+ js['actionview'] = self
40
+ js.eval("Templates['#{template.identifier}']").call(assigns.merge(locals)).force_encoding(Encoding.default_external)
41
+ }
42
+ else
43
+ %{
44
+ js = ::HandlebarWax::TemplateHandler.js
45
+ js['actionview'] = self
46
+ js.eval("Templates['#{template.identifier}']").call(assigns.merge(locals))
47
+ }
48
+ end
49
+ end
50
+
51
+ end
52
+ end
53
+
54
+ ActiveSupport.on_load(:action_view) do
55
+ ActionView::Template.register_template_handler(:hbs, ::HandlebarWax::TemplateHandler)
56
+ end
@@ -0,0 +1,28 @@
1
+ require 'v8'
2
+
3
+ # Monkey patches due to bugs in RubyRacer
4
+ class V8::JSError
5
+ def initialize(try, to)
6
+ @to = to
7
+ begin
8
+ super(initialize_unsafe(try))
9
+ rescue Exception => e
10
+ # Original code does not make an Array here
11
+ @boundaries = [Boundary.new(:rbframes => e.backtrace)]
12
+ @value = e
13
+ super("BUG! please report. JSError#initialize failed!: #{e.message}")
14
+ end
15
+ end
16
+
17
+ def parse_js_frames(try)
18
+ raw = @to.rb(try.StackTrace())
19
+ if raw && !raw.empty?
20
+ raw.split("\n")[1..-1].tap do |frames|
21
+ # Original code uses strip!, and the frames are not guaranteed to be strippable
22
+ frames.each {|frame| frame.strip.chomp!(",")}
23
+ end
24
+ else
25
+ []
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,3 @@
1
+ module HandlebarWax
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,40 @@
1
+ require 'handlebar_wax/template_handler'
2
+ require 'handlebar_wax/railtie'
3
+ require 'yajl'
4
+
5
+ module HandlebarWax
6
+
7
+ autoload :Middleware, "handlebar_wax/middleware"
8
+
9
+ def self.register_expansion(timestamp=nil)
10
+ querystring = "?#{timestamp.to_i}" if timestamp
11
+ expansions = {
12
+ :handlebar_wax => [
13
+ "/javascripts/handlebars.js",
14
+ "/javascripts/handlebars_templates.js#{querystring}"
15
+ ]
16
+ }
17
+ ActionView::Helpers::AssetTagHelper.register_javascript_expansion(expansions)
18
+ end
19
+
20
+ def self.template_files
21
+ Dir[File.join(%w(app views), '**', '*.hbs')]
22
+ end
23
+
24
+ def self.output_path
25
+ base = defined?(Rails.root) ? Rails.root : '.'
26
+ "#{base}/public/javascripts/handlebars_templates.js"
27
+ end
28
+
29
+ def self.generate_templates
30
+ templates = {}
31
+ template_files.each do |template_file|
32
+ name = template_file.scan(/app\/views\/(.*)\.[^\.]+\.hbs$/).flatten.first
33
+ templates[name] = File.read(template_file)
34
+ end
35
+
36
+ script = "window.HandlebarsTemplates = #{Yajl::Encoder.encode(templates)};"
37
+
38
+ File.open(output_path, 'w') { |f| f.puts script }
39
+ end
40
+ end
@@ -0,0 +1,17 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "HandlebarWax" do
4
+ include FakeFS::SpecHelpers
5
+ before(:each) do
6
+ FileUtils.mkdir_p('app/views/something')
7
+ File.open('app/views/something/example.html.hbs', 'w') do |f|
8
+ f.write(%(<div>{{example}}</div>))
9
+ end
10
+ end
11
+
12
+ it 'should generate a handlebars_templates.js from templates in app/views' do
13
+ HandlebarWax.generate_templates
14
+ File.read('public/javascripts/handlebars_templates.js').should == "window.HandlebarsTemplates = {\"something/example\":\"<div>{{example}}</div>\"};\n"
15
+ end
16
+
17
+ end
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'handlebar_wax'
5
+ require 'fakefs/spec_helpers'
6
+
7
+ # Requires supporting files with custom matchers and macros, etc,
8
+ # in ./support/ and its subdirectories.
9
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
10
+
11
+ RSpec.configure do |config|
12
+
13
+ end
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: handlebar_wax
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Burke Libbey
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-25 00:00:00 -06:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: yajl-ruby
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rails
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 7
44
+ segments:
45
+ - 3
46
+ - 0
47
+ version: "3.0"
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: therubyracer
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ hash: 63
59
+ segments:
60
+ - 0
61
+ - 8
62
+ - 0
63
+ version: 0.8.0
64
+ type: :runtime
65
+ version_requirements: *id003
66
+ - !ruby/object:Gem::Dependency
67
+ name: rspec
68
+ prerelease: false
69
+ requirement: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ~>
73
+ - !ruby/object:Gem::Version
74
+ hash: 11
75
+ segments:
76
+ - 2
77
+ - 1
78
+ - 0
79
+ version: 2.1.0
80
+ type: :development
81
+ version_requirements: *id004
82
+ - !ruby/object:Gem::Dependency
83
+ name: rcov
84
+ prerelease: false
85
+ requirement: &id005 !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
93
+ version: "0"
94
+ type: :development
95
+ version_requirements: *id005
96
+ - !ruby/object:Gem::Dependency
97
+ name: fakefs
98
+ prerelease: false
99
+ requirement: &id006 !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: 3
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ type: :development
109
+ version_requirements: *id006
110
+ description: write me later
111
+ email:
112
+ - burke@burkelibbey.org
113
+ executables: []
114
+
115
+ extensions: []
116
+
117
+ extra_rdoc_files: []
118
+
119
+ files:
120
+ - .gitignore
121
+ - Gemfile
122
+ - Gemfile.lock
123
+ - LICENSE.txt
124
+ - README
125
+ - Rakefile
126
+ - handlebar_wax.gemspec
127
+ - lib/handlebar_wax.rb
128
+ - lib/handlebar_wax/middleware.rb
129
+ - lib/handlebar_wax/railtie.rb
130
+ - lib/handlebar_wax/template_handler.rb
131
+ - lib/handlebar_wax/v8.rb
132
+ - lib/handlebar_wax/version.rb
133
+ - spec/handlebar_wax_spec.rb
134
+ - spec/spec_helper.rb
135
+ has_rdoc: true
136
+ homepage: ""
137
+ licenses: []
138
+
139
+ post_install_message:
140
+ rdoc_options: []
141
+
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ hash: 3
150
+ segments:
151
+ - 0
152
+ version: "0"
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
+ none: false
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ hash: 3
159
+ segments:
160
+ - 0
161
+ version: "0"
162
+ requirements: []
163
+
164
+ rubyforge_project:
165
+ rubygems_version: 1.5.0
166
+ signing_key:
167
+ specification_version: 3
168
+ summary: write me later
169
+ test_files:
170
+ - spec/handlebar_wax_spec.rb
171
+ - spec/spec_helper.rb