From 8f81f64a25bd7700cb55a06267b49f53e935d298 Mon Sep 17 00:00:00 2001 From: Zach Gianos Date: Fri, 4 Feb 2022 08:56:14 -1000 Subject: [PATCH] readme/specs: update description and test compile with options --- CONTRIBUTING.md | 13 +++++++++++++ README.md | 14 ++++++++++++-- handlebars-engine.gemspec | 12 ++++++++++-- lib/handlebars/engine.rb | 2 ++ spec/handlebars/engine_spec.rb | 27 ++++++++++++++++++++++++++- 5 files changed, 63 insertions(+), 5 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c518bbd..7f57b0e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,5 +1,18 @@ # Contributing +Bug reports and pull requests are welcome on GitHub: +https://github.com/gi/handlebars-ruby. + +1. Fork the repository: https://github.com/gi/handlebars-ruby. +1. Create an issue branch: `git checkout -b issue-N/summary origin/develop`) +1. Run [setup](#Setup): `bin/setup`. +1. Add tests for your updates. +1. Run [tests](#Test): `bin/test`. +1. Run linter: `bin/lint`. +1. Commit changes: `git commit -am '[#N] Summary'`. +1. Push changes: `git push origin head`. +1. Create a pull request targeting `develop`. + ## Branches This repository follows a modified version of the diff --git a/README.md b/README.md index 3cd6eb5..cbe8ea7 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,22 @@ # Handlebars::Engine [![Gem Version](https://badge.fury.io/rb/handlebars-engine.svg)](https://rubygems.org/gems/handlebars-engine) -[![Build Status](https://github.com/gi/handlebars-ruby/actions/workflows/ci.yml/badge.svg)](https://github.com/gi/handlebars-ruby/actions/workflows/ci.yml) +[![CI Status](https://github.com/gi/handlebars-ruby/actions/workflows/ci.yml/badge.svg)](https://github.com/gi/handlebars-ruby/actions/workflows/ci.yml) [![Test Coverage](https://api.codeclimate.com/v1/badges/45d98ad9e12ee3384161/test_coverage)](https://codeclimate.com/github/gi/handlebars-ruby/test_coverage) [![Maintainability](https://api.codeclimate.com/v1/badges/45d98ad9e12ee3384161/maintainability)](https://codeclimate.com/github/gi/handlebars-ruby/maintainability) [![MIT License](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE.txt) -A simple interface to [Handlebars.js](https://handlebarsjs.com) for Ruby. +A complete interface to [Handlebars.js](https://handlebarsjs.com) for Ruby. + +`Handlebars::Engine` provides a complete Ruby API for the official JavaScript +version of Handlebars, including the abilities to register Ruby blocks/procs as +Handlebars helper functions and to dynamically register partials. + +It uses [MiniRacer](https://github.com/rubyjs/mini_racer) for the bridge between +Ruby and the V8 JavaScript engine. + +`Handlebars::Engine` was created as a replacement for +[handlebars.rb](https://github.com/cowboyd/handlebars.rb). ## Installation diff --git a/handlebars-engine.gemspec b/handlebars-engine.gemspec index 29af496..a365baa 100644 --- a/handlebars-engine.gemspec +++ b/handlebars-engine.gemspec @@ -8,9 +8,17 @@ Gem::Specification.new do |spec| spec.authors = ["Zach Gianos"] spec.email = ["zach.gianos+git@gmail.com"] - spec.summary = "A simple interface to Handlebars.js for Ruby." + spec.summary = "A complete interface to Handlebars.js for Ruby." spec.description = <<-DESCRIPTION - A simple interface to Handlebars.js for Ruby. + A complete interface to Handlebars.js for Ruby. + + Handlebars::Engine provides a complete Ruby API for the official JavaScript + version of Handlebars, including the abilities to register Ruby blocks/procs + as Handlebars helper functions and to dynamically register partials. + + It uses MiniRacer for the bridge between Ruby and the V8 JavaScript engine. + + Handlebars::Engine was created as a replacement for handlebars.rb. DESCRIPTION spec.homepage = "https://github.com/gi/handlebars-ruby" diff --git a/lib/handlebars/engine.rb b/lib/handlebars/engine.rb index f3abca1..b6f0ae2 100644 --- a/lib/handlebars/engine.rb +++ b/lib/handlebars/engine.rb @@ -12,6 +12,8 @@ module Handlebars # This API follows the JavaScript API as closely as possible: # https://handlebarsjs.com/api-reference/. class Engine + Error = MiniRacer::RuntimeError + # Creates a new instance. # # @param lazy [true, false] immediately loads and initializes the JavaScript diff --git a/spec/handlebars/engine_spec.rb b/spec/handlebars/engine_spec.rb index 4daecab..fe0a5f0 100644 --- a/spec/handlebars/engine_spec.rb +++ b/spec/handlebars/engine_spec.rb @@ -92,7 +92,7 @@ shared_examples "rendering" do |error: false| if error it "raises an error" do - expect { render }.to raise_error(MiniRacer::RuntimeError) + expect { render }.to raise_error(Handlebars::Engine::Error) end else it "renders the template" do @@ -101,6 +101,27 @@ end end + shared_examples "compiling with options" do + let(:template_options) { {} } + + describe "knownHelpers" do + let(:render_context) { { age: -30 } } + let(:rendered) { "Hello, you are 30!" } + let(:template) { "Hello, you are {{abs age}}!" } + + before do + engine.register_helper(:abs) { |_ctx, age, _opts| age.abs } + template_options[:knownHelpers] = { + abs: true, + } + end + + describe "rendering" do + include_examples "rendering" + end + end + end + describe "#compile" do let(:renderer) { engine.compile(template, template_options) } @@ -111,6 +132,8 @@ describe "return value" do it_behaves_like "renderer" end + + it_behaves_like "compiling with options" end describe "#precompile" do @@ -138,6 +161,8 @@ describe "return value" do it_behaves_like "renderer" end + + it_behaves_like "compiling with options" end ###################################