steering 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -73,6 +73,20 @@ Report bugs on the [GitHub issue tracker](http://github.com/pixeltrix/steering/i
73
73
 
74
74
  ## Changelog
75
75
 
76
+ ### 1.2.0
77
+
78
+ * The `compile_to_file` method now takes an options hash which takes the following options:
79
+
80
+ `:extension` - the extension of the file to which the template is compiled
81
+ `:partial` - register the template as a partial with Handlebars
82
+
83
+ *Daniel Demmel*
84
+
85
+ * The `render` method now takes `extra` argument which is passed on to `context_for`.
86
+ This can be used to pass precompiled partials or helpers to the ExecJS context.
87
+
88
+ *Daniel Demmel*
89
+
76
90
  ### 1.1.1
77
91
 
78
92
  * Bumped steering-source version to 1.0.rc.1
data/lib/steering.rb CHANGED
@@ -48,12 +48,21 @@ module Steering
48
48
  Source.context.call("Handlebars.precompile", template, { :knownHelpers => known_helpers })
49
49
  end
50
50
 
51
- def compile_to_file(template, file, extension = ".handlebars")
51
+ def compile_to_file(template, file, options = {})
52
+ if options.is_a?(String)
53
+ extension = options
54
+ partial = false
55
+ else
56
+ extension = options[:extension] || ".handlebars"
57
+ partial = options[:partial] || false
58
+ end
59
+
52
60
  File.open(file, 'w') do |f|
53
61
  name = File.basename(template, extension)
54
62
  template = File.read(template)
55
63
  f.write("\nHandlebars.templates = Handlebars.templates || {};")
56
64
  f.write("\nHandlebars.templates['#{name}'] = Handlebars.template(#{compile(template)});\n")
65
+ f.write("Handlebars.registerPartial('#{name}', Handlebars.templates['#{name}']);\n") if partial
57
66
  end
58
67
  end
59
68
 
@@ -65,8 +74,10 @@ module Steering
65
74
  Source.known_helpers
66
75
  end
67
76
 
68
- def render(template, locals = {})
69
- context_for(template).call("template", locals)
77
+ def render(template, *args)
78
+ locals = args.last.is_a?(Hash) ? args.pop : {}
79
+ extra = args.first.to_s
80
+ context_for(template, extra).call("template", locals)
70
81
  end
71
82
  end
72
83
  end
@@ -1,3 +1,3 @@
1
1
  module Steering
2
- VERSION = "1.1.1"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -0,0 +1 @@
1
+ Hello {{ name }}
@@ -0,0 +1 @@
1
+ Hello {{ name }}
@@ -0,0 +1 @@
1
+ Hello {{ name }}
@@ -3,11 +3,14 @@ require "stringio"
3
3
  require "test/unit"
4
4
 
5
5
  class SteeringTest < Test::Unit::TestCase
6
- JS_FUNCTION_PATTERN = /^function\s*\(.*?\)\s*\{.*\}$/m
7
- HB_PREAMBLE = /Handlebars\.templates\s*=\s*Handlebars.templates\s\|\|\s*\{\};/
8
- HB_ASSIGNMENT = /Handlebars\.templates\['\w+'\]\s*=/
9
- HB_TEMPLATE = /Handlebars\.template\(function\s*\(.*?\)\s*\{.*\}\);/m
10
- HB_TEMPLATE_PATTERN = /^\n#{HB_PREAMBLE}\n#{HB_ASSIGNMENT}\s*#{HB_TEMPLATE}\s*$/m
6
+ FIXTURE_PATH = File.expand_path("../fixtures", __FILE__)
7
+ JS_FUNCTION_PATTERN = /^function\s*\(.*?\)\s*\{.*\}$/m
8
+ HB_PREAMBLE = /Handlebars\.templates\s*=\s*Handlebars.templates\s\|\|\s*\{\};/
9
+ HB_ASSIGNMENT = /Handlebars\.templates\['\w+'\]\s*=/
10
+ HB_TEMPLATE = /Handlebars\.template\(function\s*\(.*?\)\s*\{.*\}\);/m
11
+ HB_PARTIAL = /Handlebars\.registerPartial\('\w+',\sHandlebars\.templates\['\w+'\]\);/
12
+ HB_TEMPLATE_PATTERN = /^\n#{HB_PREAMBLE}\n#{HB_ASSIGNMENT}\s*#{HB_TEMPLATE}\s*$/m
13
+ HB_TEMPLATE_PATTERN_PARTIAL = /^\n#{HB_PREAMBLE}\n#{HB_ASSIGNMENT}\s*#{HB_TEMPLATE}\n#{HB_PARTIAL}\n*$/m
11
14
 
12
15
  def test_version
13
16
  assert_equal Steering::Source::VERSION, Steering.version
@@ -29,8 +32,8 @@ class SteeringTest < Test::Unit::TestCase
29
32
  end
30
33
 
31
34
  def test_compile_file
32
- file = "example/mytemplate.handlebars"
33
- compiled_file = "example/mytemplate.js"
35
+ file = fixture_path("hello.handlebars")
36
+ compiled_file = fixture_path("hello.js")
34
37
 
35
38
  Steering.compile_to_file(file, compiled_file)
36
39
  compiled_source = File.read(compiled_file)
@@ -40,6 +43,42 @@ class SteeringTest < Test::Unit::TestCase
40
43
  File.delete(compiled_file) if File.exists?(compiled_file)
41
44
  end
42
45
 
46
+ def test_compile_file_extension
47
+ file = fixture_path("foo.hb")
48
+ compiled_file = fixture_path("foo.js")
49
+
50
+ Steering.compile_to_file(file, compiled_file, ".hb")
51
+ compiled_source = File.read(compiled_file)
52
+
53
+ assert_match HB_TEMPLATE_PATTERN, compiled_source
54
+ ensure
55
+ File.delete(compiled_file) if File.exists?(compiled_file)
56
+ end
57
+
58
+ def test_compile_file_extension_option
59
+ file = fixture_path("bar.hb")
60
+ compiled_file = fixture_path("bar.js")
61
+
62
+ Steering.compile_to_file(file, compiled_file, :extension => ".hb")
63
+ compiled_source = File.read(compiled_file)
64
+
65
+ assert_match HB_TEMPLATE_PATTERN, compiled_source
66
+ ensure
67
+ File.delete(compiled_file) if File.exists?(compiled_file)
68
+ end
69
+
70
+ def test_compile_file_partial
71
+ file = fixture_path("hello.handlebars")
72
+ compiled_file = fixture_path("hello.js")
73
+
74
+ Steering.compile_to_file(file, compiled_file, :partial => true)
75
+ compiled_source = File.read(compiled_file)
76
+
77
+ assert_match HB_TEMPLATE_PATTERN_PARTIAL, compiled_source
78
+ ensure
79
+ File.delete(compiled_file) if File.exists?(compiled_file)
80
+ end
81
+
43
82
  def test_context_for
44
83
  context = Steering.context_for("Hello {{ name }}")
45
84
  assert_equal "Hello Andrew", context.call("template", :name => "Andrew")
@@ -59,4 +98,10 @@ class SteeringTest < Test::Unit::TestCase
59
98
  assert_equal "bar", e.message
60
99
  end
61
100
  end
101
+
102
+ private
103
+
104
+ def fixture_path(path)
105
+ File.join(FIXTURE_PATH, path)
106
+ end
62
107
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: steering
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -61,6 +61,9 @@ files:
61
61
  - lib/steering.rb
62
62
  - lib/steering/version.rb
63
63
  - steering.gemspec
64
+ - test/fixtures/bar.hb
65
+ - test/fixtures/foo.hb
66
+ - test/fixtures/hello.handlebars
64
67
  - test/steering_test.rb
65
68
  homepage: https://github.com/pixeltrix/steering
66
69
  licenses: []
@@ -76,7 +79,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
76
79
  version: '0'
77
80
  segments:
78
81
  - 0
79
- hash: -1384459607151128044
82
+ hash: 113054895620168692
80
83
  required_rubygems_version: !ruby/object:Gem::Requirement
81
84
  none: false
82
85
  requirements:
@@ -85,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
88
  version: '0'
86
89
  segments:
87
90
  - 0
88
- hash: -1384459607151128044
91
+ hash: 113054895620168692
89
92
  requirements: []
90
93
  rubyforge_project:
91
94
  rubygems_version: 1.8.24
@@ -93,4 +96,7 @@ signing_key:
93
96
  specification_version: 3
94
97
  summary: Ruby Handlebars.js Compiler
95
98
  test_files:
99
+ - test/fixtures/bar.hb
100
+ - test/fixtures/foo.hb
101
+ - test/fixtures/hello.handlebars
96
102
  - test/steering_test.rb