Skip to content

Commit da7f253

Browse files
committed
Merge pull request #9 from gi:release/0.3.0
v0.3.0
2 parents eed21f2 + 9348103 commit da7f253

11 files changed

Lines changed: 269 additions & 24 deletions

File tree

.github/workflows/ci.yml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ on:
1111
- main
1212

1313
jobs:
14+
build:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Checkout
18+
uses: actions/checkout@v2
19+
- name: Install
20+
uses: ruby/setup-ruby@v1
21+
with:
22+
ruby-version: ruby-3
23+
bundler-cache: true
24+
- name: Build
25+
run: bin/build
26+
1427
coverage:
1528
runs-on: ubuntu-latest
1629
steps:
@@ -41,7 +54,7 @@ jobs:
4154
ruby-version: ruby-3
4255
bundler-cache: true
4356
- name: Lint
44-
run: bundle exec rubocop
57+
run: bin/lint
4558

4659
test:
4760
strategy:
@@ -66,4 +79,4 @@ jobs:
6679
ruby-version: ${{ matrix.ruby }}
6780
bundler-cache: true
6881
- name: Test
69-
run: bundle exec rake
82+
run: bin/rake

.github/workflows/publish.yml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ on:
44
push:
55
branches:
66
- main
7-
paths:
8-
- lib/**/version
97

108
permissions:
119
contents: write
@@ -14,6 +12,7 @@ permissions:
1412
jobs:
1513
github:
1614
name: GitHub
15+
needs: tag
1716
runs-on: ubuntu-latest
1817
steps:
1918
- name: Checkout
@@ -29,6 +28,7 @@ jobs:
2928
run: bin/release
3029
rubygems:
3130
name: RubyGems
31+
needs: tag
3232
runs-on: ubuntu-latest
3333
steps:
3434
- name: Checkout
@@ -42,3 +42,16 @@ jobs:
4242
env:
4343
GEM_HOST_API_KEY: "${{secrets.RUBYGEMS_TOKEN}}"
4444
run: bin/release
45+
tag:
46+
name: Git Tag
47+
runs-on: ubuntu-latest
48+
steps:
49+
- name: Checkout
50+
uses: actions/checkout@v2
51+
- name: Install
52+
uses: ruby/setup-ruby@v1
53+
with:
54+
ruby-version: ruby-3
55+
bundler-cache: true
56+
- name: Tag
57+
run: bin/tag

.rubocop.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ Metrics/ModuleLength:
138138
Metrics/ParameterLists:
139139
Max: 10
140140

141+
Naming/FileName:
142+
Exclude:
143+
- lib/handlebars-engine.rb
144+
141145
RSpec/ExampleLength:
142146
CountAsOne:
143147
- array

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

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+
1019
## [0.2.0] - 2022-01-27
1120

1221
This is the initial implementation, wrapping the JavaScript Handlebars.

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,32 @@ See https://handlebarsjs.com/guide/#custom-helpers.
8282

8383
### Block Helpers
8484

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+
85111
See https://handlebarsjs.com/guide/#block-helpers.
86112

87113
### Partials
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#
55
# This file was generated by Bundler.
66
#
7-
# The application 'handlebars-engine' is installed as part of a gem, and
7+
# The application 'handlebars' is installed as part of a gem, and
88
# this file is here to facilitate running it.
99
#
1010

@@ -26,4 +26,4 @@ end
2626
require "rubygems"
2727
require "bundler/setup"
2828

29-
load Gem.bin_path("handlebars-engine", "handlebars-engine")
29+
load Gem.bin_path("handlebars-engine", "handlebars")

bin/tag

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env bash
2+
3+
dir="$(cd "$(dirname "${BASH_SOURCE[0]}" )" && pwd)"
4+
5+
branch=$(git rev-parse --abbrev-ref HEAD)
6+
7+
if [[ "${branch}" != "main" ]]; then
8+
echo "Releases are restricted to the main branch."
9+
exit 1
10+
fi
11+
12+
diff=$(git diff HEAD --quiet; echo $?)
13+
14+
if [[ "${diff}" != "0" ]]; then
15+
echo "Releases are restricted to clean branches."
16+
exit 1
17+
fi
18+
19+
if [[ -n "${CI}" ]]; then
20+
git config --global user.email "gi+handlebars-ruby@users.noreply.github.com"
21+
git config --global user.name "handlebars-ruby"
22+
git fetch --tags
23+
fi
24+
25+
"${dir}/rake" release:source_control_push

lib/handlebars-engine.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# frozen_string_literal: true
2+
3+
require "handlebars/engine"

lib/handlebars/engine.rb

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ class Engine
1616
#
1717
# @param lazy [true, false] immediately loads and initializes the JavaScript
1818
# 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
2023
init! unless lazy
2124
end
2225

@@ -60,14 +63,29 @@ def template(*args)
6063

6164
# Registers helpers accessible by any template in the environment.
6265
#
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+
#
6372
# @param name [String, Symbol] the name of the helper
73+
# @param function [Proc, String] the helper function
6474
# @yieldparam context [Hash] the current context
6575
# @yieldparam arguments [Object] the arguments (optional)
6676
# @yieldparam options [Hash] the options hash (optional)
6777
# @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
7189
end
7290

7391
# Unregisters a previously registered helper.
@@ -204,21 +222,14 @@ def init!
204222
return if @init
205223

206224
@context = MiniRacer::Context.new
207-
@context.load(::Handlebars::Source.bundled_path)
225+
@context.load(@path || ::Handlebars::Source.bundled_path)
208226
@context.load(File.absolute_path("engine/init.js", __dir__))
209227

210228
@init = true
211229
end
212230

213231
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) }
222233
end
223234
end
224235
end

lib/handlebars/engine/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
module Handlebars
44
class Engine
5-
VERSION = "0.2.0"
5+
VERSION = "0.3.0"
66
end
77
end

0 commit comments

Comments
 (0)