handlebars-engine 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0b6bc490b95cc268a0cc7ee638660caecbfe9d978a13335b8c3588f89e16c3db
4
- data.tar.gz: 014eb30c05c4edba8b9f9a340e3539a8542edabd94954efa189b2611456e10d8
3
+ metadata.gz: cad7fc20a466c4635a87db80dcd3996ef8753bfb6fdbea1d9f90e4463403850b
4
+ data.tar.gz: c3613182647712f6b9dc8d4da55721e546c1cd4786956524aa5596338e656686
5
5
  SHA512:
6
- metadata.gz: a092e8fdae258e997d6afb3505b20a67898caf7d08bfd37efee1a50f663b0e084648902b6d396fe392acb46a8e5587deee277e2ec750f31ce103ad8dd42d1d6b
7
- data.tar.gz: 99c271de356730e76c660fb806d8dd619edb0f522951a5e822f6460e0e415c02e20311aab2e5be55e89f9b2ffd161e87b75784726512df26d5c59d002ec06f9a
6
+ metadata.gz: 7d28c6fe7442a645a4f4c463776bde45cfece64c431538fe799380f769c520f9ff38a7f3b4bbc619dd366e81d21ba46a0b8c8466e15000c6eec852a3fba499b1
7
+ data.tar.gz: d2cf108b5e1a029de806fba2366e0a5ff6060d1ac1898e51c98b1242ed9f918ef95f2ef92bb90c57703f11ac4d41044833797f30e97ebea6877b0ae7e9050011
data/CHANGELOG.md CHANGED
@@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.3.0] - 2022-01-31
11
+
12
+ ### Added
13
+ - `initialize`: add path parameter ([#5](https://github.com/gi/handlebars-ruby/pull/5))
14
+ - `register_helper`: accept multiple helpers as keyword parameters ([#6](https://github.com/gi/handlebars-ruby/pull/6))
15
+ - `register_helper`: accept javascript function as string ([#7](https://github.com/gi/handlebars-ruby/pull/7))
16
+ - `ci`: verify gem builds ([#8](https://github.com/gi/handlebars-ruby/pull/8))
17
+ - `require`: allow loading from `handlebars-engine` and `handlebars/engine` ([#8](https://github.com/gi/handlebars-ruby/pull/8))
18
+
10
19
  ## [0.2.0] - 2022-01-27
11
20
 
12
21
  This is the initial implementation, wrapping the JavaScript Handlebars.
data/README.md CHANGED
@@ -82,6 +82,32 @@ See https://handlebarsjs.com/guide/#custom-helpers.
82
82
 
83
83
  ### Block Helpers
84
84
 
85
+ Block helpers make it possible to define custom iterators and other
86
+ functionality that can invoke the passed block with a new context.
87
+
88
+ Currently, there is a limitation with the underlying JavaScript engine: it does
89
+ not allow for reentrant calls from within attached Ruby functions: see
90
+ [MiniRacer#225](https://github.com/rubyjs/mini_racer/issues/225). Thus, the
91
+ block function returned to the helper (in `options.fn`) cannot be invoked.
92
+
93
+ Thus, for block helpers, a string of JavaScript must define the helper function:
94
+ ```ruby
95
+ handlebars = Handlebars::Engine.new
96
+ handlebars.register_helper(map: <<~JS)
97
+ function(...args) {
98
+ const ctx = this;
99
+ const opts = args.pop();
100
+ const items = args[0];
101
+ const separator = args[1];
102
+ const mapped = items.map((item) => opts.fn(item));
103
+ return mapped.join(separator);
104
+ }
105
+ JS
106
+ template = handlebars.compile("{{#map items '|'}}'{{this}}'{{/map}}")
107
+ template.call({ items: [1, 2, 3] })
108
+ # => "'1'|2'|'3'"
109
+ ```
110
+
85
111
  See https://handlebarsjs.com/guide/#block-helpers.
86
112
 
87
113
  ### Partials
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Handlebars
4
4
  class Engine
5
- VERSION = "0.2.0"
5
+ VERSION = "0.3.0"
6
6
  end
7
7
  end
@@ -16,7 +16,10 @@ module Handlebars
16
16
  #
17
17
  # @param lazy [true, false] immediately loads and initializes the JavaScript
18
18
  # environment.
19
- def initialize(lazy: false)
19
+ # @param path [String, nil] the path to the version of Handlebars to load.
20
+ # If `nil`, the contents of `Handlebars::Source.bundled_path` is loaded.
21
+ def initialize(lazy: false, path: nil)
22
+ @path = path
20
23
  init! unless lazy
21
24
  end
22
25
 
@@ -60,14 +63,29 @@ module Handlebars
60
63
 
61
64
  # Registers helpers accessible by any template in the environment.
62
65
  #
66
+ # The function can be either a proc or a string:
67
+ # * When the function is a proc, it can be either passed in as a normal
68
+ # parameter or as a block.
69
+ # * When the function is a string, it is interpreted as a JavaScript
70
+ # function.
71
+ #
63
72
  # @param name [String, Symbol] the name of the helper
73
+ # @param function [Proc, String] the helper function
64
74
  # @yieldparam context [Hash] the current context
65
75
  # @yieldparam arguments [Object] the arguments (optional)
66
76
  # @yieldparam options [Hash] the options hash (optional)
67
77
  # @see https://handlebarsjs.com/api-reference/runtime.html#handlebars-registerhelper-name-helper
68
- def register_helper(name, &block)
69
- attach(name, &block)
70
- call(:registerHelper, [name.to_s, name.to_sym], eval: true)
78
+ def register_helper(name = nil, function = nil, **helpers, &block)
79
+ helpers[name] = block || function if name
80
+ helpers.each do |n, f|
81
+ case f
82
+ when Proc
83
+ attach(n, &f)
84
+ evaluate("registerHelper('#{n}', #{n})")
85
+ when String, Symbol
86
+ evaluate("Handlebars.registerHelper('#{n}', #{f})")
87
+ end
88
+ end
71
89
  end
72
90
 
73
91
  # Unregisters a previously registered helper.
@@ -204,21 +222,14 @@ module Handlebars
204
222
  return if @init
205
223
 
206
224
  @context = MiniRacer::Context.new
207
- @context.load(::Handlebars::Source.bundled_path)
225
+ @context.load(@path || ::Handlebars::Source.bundled_path)
208
226
  @context.load(File.absolute_path("engine/init.js", __dir__))
209
227
 
210
228
  @init = true
211
229
  end
212
230
 
213
231
  def js_args(args)
214
- args.map { |arg|
215
- case arg
216
- when Symbol
217
- arg
218
- else
219
- JSON.generate(arg)
220
- end
221
- }
232
+ args.map { |arg| JSON.generate(arg) }
222
233
  end
223
234
  end
224
235
  end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "handlebars/engine"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: handlebars-engine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zach Gianos
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-27 00:00:00.000000000 Z
11
+ date: 2022-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: handlebars-source
@@ -50,6 +50,7 @@ files:
50
50
  - LICENSE
51
51
  - README.md
52
52
  - exe/handlebars
53
+ - lib/handlebars-engine.rb
53
54
  - lib/handlebars/engine.rb
54
55
  - lib/handlebars/engine/init.js
55
56
  - lib/handlebars/engine/version.rb
@@ -61,7 +62,7 @@ metadata:
61
62
  github_repo: https://github.com/gi/handlebars-ruby
62
63
  homepage_uri: https://github.com/gi/handlebars-ruby
63
64
  source_code_uri: https://github.com/gi/handlebars-ruby
64
- post_install_message:
65
+ post_install_message:
65
66
  rdoc_options: []
66
67
  require_paths:
67
68
  - lib
@@ -76,8 +77,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
77
  - !ruby/object:Gem::Version
77
78
  version: '0'
78
79
  requirements: []
79
- rubygems_version: 3.2.22
80
- signing_key:
80
+ rubygems_version: 3.3.3
81
+ signing_key:
81
82
  specification_version: 4
82
83
  summary: A simple interface to Handlebars.js for Ruby.
83
84
  test_files: []