sht_rails 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a6e28441b4e0c90c74a3520216d36f8966c180bb
4
+ data.tar.gz: 9035f8bee67957c318c5f35c97366c5b0db70dc2
5
+ SHA512:
6
+ metadata.gz: 4330f6dd875b91b41ae8360b32a6390fe3045c2729e1e10c92cf13c202791d2229b89736277d8e582ccdba4cca2d6bdc2129295faa00b9bb40d224b6262ce311
7
+ data.tar.gz: 71078dfe27afc03f922d887a2be1b97c07ccf546fb0d39c9640956f41a31afa0b970a07e463958124c6abbb0fa5a1470036f78a9fc6d2cc6177a2521b5568c6f
@@ -10,12 +10,12 @@ module ShtRails
10
10
  # end
11
11
 
12
12
  module Config
13
- attr_accessor :template_base_path, :template_extension, :action_view_key, :template_namespace
13
+ attr_accessor :template_base_path, :template_extension, :action_view_key, :template_namespace, :helper_path
14
14
 
15
15
  def configure
16
16
  yield self
17
17
  end
18
-
18
+
19
19
  def template_base_path
20
20
  @template_base_path ||= Rails.root.join("app", "templates")
21
21
  end
@@ -23,13 +23,17 @@ module ShtRails
23
23
  def template_extension
24
24
  @template_extension ||= 'handlebars'
25
25
  end
26
-
26
+
27
27
  def action_view_key
28
28
  @action_view_key ||= 'handlebars'
29
29
  end
30
-
30
+
31
31
  def template_namespace
32
32
  @template_namespace ||= 'SHT'
33
33
  end
34
+
35
+ def helper_path
36
+ @helper_path ||= 'templates/helpers.js'
37
+ end
34
38
  end
35
- end
39
+ end
@@ -4,13 +4,26 @@ require "active_support"
4
4
  module ShtRails
5
5
 
6
6
  module Handlebars
7
+ def self.context(partials = nil)
8
+ @context = nil unless ActionView::Resolver.caching?
9
+ @context ||= begin
10
+ context = ::Handlebars::Context.new
11
+ if helpers = Rails.application.assets.find_asset(ShtRails.helper_path)
12
+ context.runtime.eval helpers.source
13
+ end
14
+ partials.each { |key, value| context.register_partial(key, value) } if partials
15
+ context
16
+ end
17
+ end
18
+
7
19
  def self.call(template)
8
20
  if template.locals.include?(ShtRails.action_view_key.to_s) || template.locals.include?(ShtRails.action_view_key.to_sym)
9
21
  <<-SHT
10
- hbs_context_for_sht = Handlebars::Context.new
11
- partials.each do |key, value|
12
- hbs_context_for_sht.register_partial(key, value)
13
- end if defined?(partials) && partials.is_a?(Hash)
22
+ hbs_context_for_sht = if defined?(partials) && partials.is_a?(Hash)
23
+ ShtRails::Handlebars.context(partials)
24
+ else
25
+ ShtRails::Handlebars.context
26
+ end
14
27
  hbs_context_for_sht.compile(#{template.source.inspect}).call(#{ShtRails.action_view_key.to_s} || {}).html_safe
15
28
  SHT
16
29
  else
@@ -1,4 +1,5 @@
1
1
  require 'tilt'
2
+ require 'execjs'
2
3
 
3
4
  module ShtRails
4
5
  class Tilt < Tilt::Template
@@ -12,20 +13,20 @@ module ShtRails
12
13
 
13
14
  attr_reader :namespace
14
15
 
16
+ def precompile(template)
17
+ @context ||= begin
18
+ handlebars_path = File.expand_path("../../../vendor/assets/javascripts/handlebars.js", __FILE__)
19
+ ExecJS.compile File.read(handlebars_path)
20
+ end
21
+ @context.call("Handlebars.precompile", template)
22
+ end
23
+
15
24
  def evaluate(scope, locals, &block)
16
25
  template_key = path_to_key scope
17
26
  <<-HandlebarsTemplate
18
27
  (function() {
19
28
  #{namespace} || (#{namespace} = {});
20
- #{namespace}CachedShtTemplates || (#{namespace}CachedShtTemplates = {});
21
- #{namespace}CachedShtTemplates[#{template_key.inspect}] = Handlebars.compile(#{data.inspect});
22
- #{namespace}[#{template_key.inspect}] = function(object) {
23
- if (object == null){
24
- return #{ShtRails.template_namespace}CachedShtTemplates[#{template_key.inspect}];
25
- } else {
26
- return #{ShtRails.template_namespace}CachedShtTemplates[#{template_key.inspect}](object);
27
- }
28
- };
29
+ #{namespace}[#{template_key.inspect}] = Handlebars.template(#{precompile(data)});
29
30
  }).call(this);
30
31
  HandlebarsTemplate
31
32
  end
@@ -1,3 +1,3 @@
1
1
  module ShtRails
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
data/sht_rails.gemspec CHANGED
@@ -21,7 +21,8 @@ Gem::Specification.new do |gem|
21
21
  gem.add_runtime_dependency "rails", ">= 3.1.0"
22
22
  gem.add_runtime_dependency "tilt", ">= 1.3.3"
23
23
  gem.add_runtime_dependency "sprockets", ">= 2.0.3"
24
- gem.add_runtime_dependency "handlebars", ">= 0.3.0"
24
+ gem.add_runtime_dependency "handlebars", ">= 0.4.0"
25
+ gem.add_runtime_dependency "execjs", ">= 0.3.0"
25
26
 
26
27
  gem.add_development_dependency "rake"
27
28
  end
@@ -0,0 +1,321 @@
1
+ /*
2
+
3
+ Copyright (C) 2011 by Yehuda Katz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
22
+
23
+ */
24
+
25
+ // lib/handlebars/base.js
26
+
27
+ /*jshint eqnull:true*/
28
+ this.Handlebars = {};
29
+
30
+ (function(Handlebars) {
31
+
32
+ Handlebars.VERSION = "1.0.0-rc.3";
33
+ Handlebars.COMPILER_REVISION = 2;
34
+
35
+ Handlebars.REVISION_CHANGES = {
36
+ 1: '<= 1.0.rc.2', // 1.0.rc.2 is actually rev2 but doesn't report it
37
+ 2: '>= 1.0.0-rc.3'
38
+ };
39
+
40
+ Handlebars.helpers = {};
41
+ Handlebars.partials = {};
42
+
43
+ Handlebars.registerHelper = function(name, fn, inverse) {
44
+ if(inverse) { fn.not = inverse; }
45
+ this.helpers[name] = fn;
46
+ };
47
+
48
+ Handlebars.registerPartial = function(name, str) {
49
+ this.partials[name] = str;
50
+ };
51
+
52
+ Handlebars.registerHelper('helperMissing', function(arg) {
53
+ if(arguments.length === 2) {
54
+ return undefined;
55
+ } else {
56
+ throw new Error("Could not find property '" + arg + "'");
57
+ }
58
+ });
59
+
60
+ var toString = Object.prototype.toString, functionType = "[object Function]";
61
+
62
+ Handlebars.registerHelper('blockHelperMissing', function(context, options) {
63
+ var inverse = options.inverse || function() {}, fn = options.fn;
64
+
65
+
66
+ var ret = "";
67
+ var type = toString.call(context);
68
+
69
+ if(type === functionType) { context = context.call(this); }
70
+
71
+ if(context === true) {
72
+ return fn(this);
73
+ } else if(context === false || context == null) {
74
+ return inverse(this);
75
+ } else if(type === "[object Array]") {
76
+ if(context.length > 0) {
77
+ return Handlebars.helpers.each(context, options);
78
+ } else {
79
+ return inverse(this);
80
+ }
81
+ } else {
82
+ return fn(context);
83
+ }
84
+ });
85
+
86
+ Handlebars.K = function() {};
87
+
88
+ Handlebars.createFrame = Object.create || function(object) {
89
+ Handlebars.K.prototype = object;
90
+ var obj = new Handlebars.K();
91
+ Handlebars.K.prototype = null;
92
+ return obj;
93
+ };
94
+
95
+ Handlebars.logger = {
96
+ DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3, level: 3,
97
+
98
+ methodMap: {0: 'debug', 1: 'info', 2: 'warn', 3: 'error'},
99
+
100
+ // can be overridden in the host environment
101
+ log: function(level, obj) {
102
+ if (Handlebars.logger.level <= level) {
103
+ var method = Handlebars.logger.methodMap[level];
104
+ if (typeof console !== 'undefined' && console[method]) {
105
+ console[method].call(console, obj);
106
+ }
107
+ }
108
+ }
109
+ };
110
+
111
+ Handlebars.log = function(level, obj) { Handlebars.logger.log(level, obj); };
112
+
113
+ Handlebars.registerHelper('each', function(context, options) {
114
+ var fn = options.fn, inverse = options.inverse;
115
+ var i = 0, ret = "", data;
116
+
117
+ if (options.data) {
118
+ data = Handlebars.createFrame(options.data);
119
+ }
120
+
121
+ if(context && typeof context === 'object') {
122
+ if(context instanceof Array){
123
+ for(var j = context.length; i<j; i++) {
124
+ if (data) { data.index = i; }
125
+ ret = ret + fn(context[i], { data: data });
126
+ }
127
+ } else {
128
+ for(var key in context) {
129
+ if(context.hasOwnProperty(key)) {
130
+ if(data) { data.key = key; }
131
+ ret = ret + fn(context[key], {data: data});
132
+ i++;
133
+ }
134
+ }
135
+ }
136
+ }
137
+
138
+ if(i === 0){
139
+ ret = inverse(this);
140
+ }
141
+
142
+ return ret;
143
+ });
144
+
145
+ Handlebars.registerHelper('if', function(context, options) {
146
+ var type = toString.call(context);
147
+ if(type === functionType) { context = context.call(this); }
148
+
149
+ if(!context || Handlebars.Utils.isEmpty(context)) {
150
+ return options.inverse(this);
151
+ } else {
152
+ return options.fn(this);
153
+ }
154
+ });
155
+
156
+ Handlebars.registerHelper('unless', function(context, options) {
157
+ var fn = options.fn, inverse = options.inverse;
158
+ options.fn = inverse;
159
+ options.inverse = fn;
160
+
161
+ return Handlebars.helpers['if'].call(this, context, options);
162
+ });
163
+
164
+ Handlebars.registerHelper('with', function(context, options) {
165
+ return options.fn(context);
166
+ });
167
+
168
+ Handlebars.registerHelper('log', function(context, options) {
169
+ var level = options.data && options.data.level != null ? parseInt(options.data.level, 10) : 1;
170
+ Handlebars.log(level, context);
171
+ });
172
+
173
+ }(this.Handlebars));
174
+ ;
175
+ // lib/handlebars/utils.js
176
+
177
+ var errorProps = ['description', 'fileName', 'lineNumber', 'message', 'name', 'number', 'stack'];
178
+
179
+ Handlebars.Exception = function(message) {
180
+ var tmp = Error.prototype.constructor.apply(this, arguments);
181
+
182
+ // Unfortunately errors are not enumerable in Chrome (at least), so `for prop in tmp` doesn't work.
183
+ for (var idx = 0; idx < errorProps.length; idx++) {
184
+ this[errorProps[idx]] = tmp[errorProps[idx]];
185
+ }
186
+ };
187
+ Handlebars.Exception.prototype = new Error();
188
+
189
+ // Build out our basic SafeString type
190
+ Handlebars.SafeString = function(string) {
191
+ this.string = string;
192
+ };
193
+ Handlebars.SafeString.prototype.toString = function() {
194
+ return this.string.toString();
195
+ };
196
+
197
+ (function() {
198
+ var escape = {
199
+ "&": "&amp;",
200
+ "<": "&lt;",
201
+ ">": "&gt;",
202
+ '"': "&quot;",
203
+ "'": "&#x27;",
204
+ "`": "&#x60;"
205
+ };
206
+
207
+ var badChars = /[&<>"'`]/g;
208
+ var possible = /[&<>"'`]/;
209
+
210
+ var escapeChar = function(chr) {
211
+ return escape[chr] || "&amp;";
212
+ };
213
+
214
+ Handlebars.Utils = {
215
+ escapeExpression: function(string) {
216
+ // don't escape SafeStrings, since they're already safe
217
+ if (string instanceof Handlebars.SafeString) {
218
+ return string.toString();
219
+ } else if (string == null || string === false) {
220
+ return "";
221
+ }
222
+
223
+ if(!possible.test(string)) { return string; }
224
+ return string.replace(badChars, escapeChar);
225
+ },
226
+
227
+ isEmpty: function(value) {
228
+ if (!value && value !== 0) {
229
+ return true;
230
+ } else if(Object.prototype.toString.call(value) === "[object Array]" && value.length === 0) {
231
+ return true;
232
+ } else {
233
+ return false;
234
+ }
235
+ }
236
+ };
237
+ })();;
238
+ // lib/handlebars/runtime.js
239
+ Handlebars.VM = {
240
+ template: function(templateSpec) {
241
+ // Just add water
242
+ var container = {
243
+ escapeExpression: Handlebars.Utils.escapeExpression,
244
+ invokePartial: Handlebars.VM.invokePartial,
245
+ programs: [],
246
+ program: function(i, fn, data) {
247
+ var programWrapper = this.programs[i];
248
+ if(data) {
249
+ return Handlebars.VM.program(fn, data);
250
+ } else if(programWrapper) {
251
+ return programWrapper;
252
+ } else {
253
+ programWrapper = this.programs[i] = Handlebars.VM.program(fn);
254
+ return programWrapper;
255
+ }
256
+ },
257
+ programWithDepth: Handlebars.VM.programWithDepth,
258
+ noop: Handlebars.VM.noop,
259
+ compilerInfo: null
260
+ };
261
+
262
+ return function(context, options) {
263
+ options = options || {};
264
+ var result = templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data);
265
+
266
+ var compilerInfo = container.compilerInfo || [],
267
+ compilerRevision = compilerInfo[0] || 1,
268
+ currentRevision = Handlebars.COMPILER_REVISION;
269
+
270
+ if (compilerRevision !== currentRevision) {
271
+ if (compilerRevision < currentRevision) {
272
+ var runtimeVersions = Handlebars.REVISION_CHANGES[currentRevision],
273
+ compilerVersions = Handlebars.REVISION_CHANGES[compilerRevision];
274
+ throw "Template was precompiled with an older version of Handlebars than the current runtime. "+
275
+ "Please update your precompiler to a newer version ("+runtimeVersions+") or downgrade your runtime to an older version ("+compilerVersions+").";
276
+ } else {
277
+ // Use the embedded version info since the runtime doesn't know about this revision yet
278
+ throw "Template was precompiled with a newer version of Handlebars than the current runtime. "+
279
+ "Please update your runtime to a newer version ("+compilerInfo[1]+").";
280
+ }
281
+ }
282
+
283
+ return result;
284
+ };
285
+ },
286
+
287
+ programWithDepth: function(fn, data, $depth) {
288
+ var args = Array.prototype.slice.call(arguments, 2);
289
+
290
+ return function(context, options) {
291
+ options = options || {};
292
+
293
+ return fn.apply(this, [context, options.data || data].concat(args));
294
+ };
295
+ },
296
+ program: function(fn, data) {
297
+ return function(context, options) {
298
+ options = options || {};
299
+
300
+ return fn(context, options.data || data);
301
+ };
302
+ },
303
+ noop: function() { return ""; },
304
+ invokePartial: function(partial, name, context, helpers, partials, data) {
305
+ var options = { helpers: helpers, partials: partials, data: data };
306
+
307
+ if(partial === undefined) {
308
+ throw new Handlebars.Exception("The partial " + name + " could not be found");
309
+ } else if(partial instanceof Function) {
310
+ return partial(context, options);
311
+ } else if (!Handlebars.compile) {
312
+ throw new Handlebars.Exception("The partial " + name + " could not be compiled when running in runtime-only mode");
313
+ } else {
314
+ partials[name] = Handlebars.compile(partial, {data: data !== undefined});
315
+ return partials[name](context, options);
316
+ }
317
+ }
318
+ };
319
+
320
+ Handlebars.template = Handlebars.VM.template;
321
+ ;
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sht_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Alexey Vasiliev
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-04-04 00:00:00.000000000 Z
11
+ date: 2013-06-04 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rails
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - '>='
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - '>='
28
25
  - !ruby/object:Gem::Version
@@ -30,7 +27,6 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: tilt
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - '>='
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - '>='
44
39
  - !ruby/object:Gem::Version
@@ -46,7 +41,6 @@ dependencies:
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: sprockets
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
45
  - - '>='
52
46
  - !ruby/object:Gem::Version
@@ -54,7 +48,6 @@ dependencies:
54
48
  type: :runtime
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
52
  - - '>='
60
53
  - !ruby/object:Gem::Version
@@ -62,7 +55,20 @@ dependencies:
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: handlebars
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.4.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: 0.4.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: execjs
71
+ requirement: !ruby/object:Gem::Requirement
66
72
  requirements:
67
73
  - - '>='
68
74
  - !ruby/object:Gem::Version
@@ -70,7 +76,6 @@ dependencies:
70
76
  type: :runtime
71
77
  prerelease: false
72
78
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
79
  requirements:
75
80
  - - '>='
76
81
  - !ruby/object:Gem::Version
@@ -78,7 +83,6 @@ dependencies:
78
83
  - !ruby/object:Gem::Dependency
79
84
  name: rake
80
85
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
86
  requirements:
83
87
  - - '>='
84
88
  - !ruby/object:Gem::Version
@@ -86,7 +90,6 @@ dependencies:
86
90
  type: :development
87
91
  prerelease: false
88
92
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
93
  requirements:
91
94
  - - '>='
92
95
  - !ruby/object:Gem::Version
@@ -116,35 +119,29 @@ files:
116
119
  - lib/sht_rails/version.rb
117
120
  - sht_rails.gemspec
118
121
  - vendor/assets/javascripts/handlebars.js
122
+ - vendor/assets/javascripts/handlebars.runtime.js
119
123
  homepage: https://github.com/railsware/sht_rails
120
124
  licenses: []
125
+ metadata: {}
121
126
  post_install_message:
122
127
  rdoc_options:
123
128
  - --charset=UTF-8
124
129
  require_paths:
125
130
  - lib
126
131
  required_ruby_version: !ruby/object:Gem::Requirement
127
- none: false
128
132
  requirements:
129
133
  - - '>='
130
134
  - !ruby/object:Gem::Version
131
135
  version: '0'
132
- segments:
133
- - 0
134
- hash: 2847208824331488472
135
136
  required_rubygems_version: !ruby/object:Gem::Requirement
136
- none: false
137
137
  requirements:
138
138
  - - '>='
139
139
  - !ruby/object:Gem::Version
140
140
  version: '0'
141
- segments:
142
- - 0
143
- hash: 2847208824331488472
144
141
  requirements: []
145
142
  rubyforge_project:
146
- rubygems_version: 1.8.25
143
+ rubygems_version: 2.0.3
147
144
  signing_key:
148
- specification_version: 3
145
+ specification_version: 4
149
146
  summary: Shared handlebars templates for rails 3
150
147
  test_files: []