handlebarsjs 0.3.0 → 0.5.1

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: 15c79e4eeab6a4fd09c9bd0cea9e7026915b0ebda02cb674f5b6f9452cebbdfb
4
- data.tar.gz: d6585183a584d0547012feebb57c8e2fb80168bc91d1e7a153022f71c7c346e4
3
+ metadata.gz: 6264c529d68086fcf2d2abc20838e7c46ff5a2cb176ebf211d5f44f32b15c5bd
4
+ data.tar.gz: 05726eb4303fc7b54e5fe8a07afd5392f1f48c2efadedcbb12d99dc3a88b11a2
5
5
  SHA512:
6
- metadata.gz: 0be0808881aa15b12ffecce41193bb6a293440e71f0b197c8f2844cba51c635080e13db93bf445a97fb462811404a14733c8ee46b58eb7aa41b669630ab87f54
7
- data.tar.gz: 4c5c159dec697cf909c17eb5e28582f6eb04d87381444c8c963b024bbfd62517dbe98c561c98b5dbe4d09835281c6e992154bdbf11ca90e60aa9e385ec273936
6
+ metadata.gz: 4d28aa4b1af80536b8a9e26390a5e0e926568bc4fb82bc9d08d5d1509dfc00ba6a017e25b9d52d219fd4baf70b17de5d8d4a7426feef5248d1246f04e45e03e9
7
+ data.tar.gz: 61ab8b27c8a27326d91ae3726d348aa987825db3a1638e5807949055819e073073a564cf6fcb042ea44d603bc3f38332c3557797c340f48859fa46320af83633
data/CHANGELOG.md CHANGED
@@ -1,3 +1,24 @@
1
+ # [0.5.0](https://github.com/klueless-io/handlebarsjs/compare/v0.4.0...v0.5.0) (2022-07-06)
2
+
3
+
4
+ ### Features
5
+
6
+ * add base_helper ([d2900ae](https://github.com/klueless-io/handlebarsjs/commit/d2900ae6c2ce27a7a8ac4c92d219f0e9150354f5))
7
+
8
+ # [0.4.0](https://github.com/klueless-io/handlebarsjs/compare/v0.3.0...v0.4.0) (2022-07-06)
9
+
10
+
11
+ ### Features
12
+
13
+ * add handlebars add_helper support ([19a5636](https://github.com/klueless-io/handlebarsjs/commit/19a5636698515c6c3128040d708d54527ba62dec))
14
+
15
+ # [0.3.0](https://github.com/klueless-io/handlebarsjs/compare/v0.2.0...v0.3.0) (2022-07-06)
16
+
17
+
18
+ ### Features
19
+
20
+ * refactor snapshot builder to handlebars snapshot ([bc6d079](https://github.com/klueless-io/handlebarsjs/commit/bc6d079d12b87f0198ae2a32c94c6349929c8c70))
21
+
1
22
  # [0.2.0](https://github.com/klueless-io/handlebarsjs/compare/v0.1.2...v0.2.0) (2022-07-02)
2
23
 
3
24
 
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Handlebarsjs
4
+ # Extend base helper for each of your custom handlebars-helpers
5
+ class BaseHelper
6
+ # All child classes will generally implement this method
7
+ def parse(value)
8
+ value
9
+ end
10
+
11
+ # If you need to wrap the return value in a specific
12
+ # Handlebars Type, eg. SafeString, then you can override this method
13
+ def wrapper(value)
14
+ value
15
+ end
16
+
17
+ # Wrap the parse method in a handlebars context aware block
18
+ # and return as a lambda/proc so that it is available to the
19
+ # Handlebars template engine
20
+ def to_proc
21
+ ->(value, _opts) { wrapper(parse(value)) }
22
+ end
23
+ end
24
+ end
@@ -22,11 +22,14 @@ module Handlebarsjs
22
22
  add_script(name, 'snippet', script: script, path: path)
23
23
  end
24
24
 
25
- def add_helper(name, callback)
26
- @helpers << {
27
- name: name,
28
- callback: callback
29
- }
25
+ def add_helper(name, helper)
26
+ callback = nil
27
+ callback = helper if helper.is_a?(Proc)
28
+ callback = helper.to_proc if callback.nil? && helper.respond_to?(:to_proc)
29
+
30
+ raise Handlebarsjs::Error, 'helper must be a callback or implement to_proc method' if callback.nil?
31
+
32
+ add_helper_callback(name, callback)
30
33
  end
31
34
 
32
35
  def register_helper(name)
@@ -41,6 +44,20 @@ module Handlebarsjs
41
44
  @snapshot ||= MiniRacer::Snapshot.new(script)
42
45
  end
43
46
 
47
+ # rubocop:disable Style/DocumentDynamicEvalDefinition
48
+ def new_context
49
+ context = MiniRacer::Context.new(snapshot: snapshot)
50
+
51
+ helpers.each do |helper|
52
+ context.attach("ruby_#{helper[:name]}", helper[:callback])
53
+ context.eval("Handlebars.registerHelper('#{helper[:name]}', ruby_#{helper[:name]})")
54
+ end
55
+
56
+ context
57
+ end
58
+ # rubocop:enable Style/DocumentDynamicEvalDefinition
59
+
60
+ # not currently used
44
61
  def dirty?
45
62
  @snapshot.nil?
46
63
  end
@@ -59,6 +76,13 @@ module Handlebarsjs
59
76
  add_script_item(name, type, script, path)
60
77
  end
61
78
 
79
+ def add_helper_callback(name, callback)
80
+ @helpers << {
81
+ name: name,
82
+ callback: callback
83
+ }
84
+ end
85
+
62
86
  def add_script_item(name, type, script, path = nil)
63
87
  @snapshot = nil
64
88
  scripts << {
@@ -25,7 +25,7 @@ module Handlebarsjs
25
25
 
26
26
  def context
27
27
  puts 'Snapshot and context are out of date, calls to snapshot should happen before any calls to context' if !@context.nil? && handlebars_snapshot.dirty?
28
- @context ||= MiniRacer::Context.new(snapshot: handlebars_snapshot.snapshot)
28
+ @context ||= handlebars_snapshot.new_context
29
29
  end
30
30
  end
31
31
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Handlebarsjs
4
- VERSION = '0.3.0'
4
+ VERSION = '0.5.1'
5
5
  end
data/lib/handlebarsjs.rb CHANGED
@@ -5,6 +5,7 @@ require_relative 'handlebarsjs/version'
5
5
  require_relative 'handlebarsjs/javascript'
6
6
  require_relative 'handlebarsjs/handlebars_snapshot'
7
7
  require_relative 'handlebarsjs/handlebars'
8
+ require_relative 'handlebarsjs/base_helper'
8
9
 
9
10
  module Handlebarsjs
10
11
  HANDLEBARS_LIBRARY_PATH = 'lib/handlebarsjs/javascript/handlebars-4.7.7.js'
data/package-lock.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "handlebarsjs",
3
- "version": "0.3.0",
3
+ "version": "0.5.1",
4
4
  "lockfileVersion": 2,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "handlebarsjs",
9
- "version": "0.3.0",
9
+ "version": "0.5.1",
10
10
  "devDependencies": {
11
11
  "@klueless-js/semantic-release-rubygem": "github:klueless-js/semantic-release-rubygem",
12
12
  "@semantic-release/changelog": "^6.0.1",
data/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "handlebarsjs",
3
- "version": "0.3.0",
3
+ "version": "0.5.1",
4
4
  "description": "handlebarsjs GEM wraps the handlebars.js library and provides ruby/javascript interoperability",
5
5
  "scripts": {
6
6
  "release": "semantic-release"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: handlebarsjs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Cruwys
@@ -71,9 +71,9 @@ files:
71
71
  - docs/project-plan/project_in_progress.svg
72
72
  - lib/handlebarsjs.rb
73
73
  - lib/handlebarsjs/api.rb
74
+ - lib/handlebarsjs/base_helper.rb
74
75
  - lib/handlebarsjs/handlebars.rb
75
76
  - lib/handlebarsjs/handlebars_snapshot.rb
76
- - lib/handlebarsjs/helpers/comparison/and.rb
77
77
  - lib/handlebarsjs/javascript.rb
78
78
  - lib/handlebarsjs/javascript/handlebars-4.7.7.js
79
79
  - lib/handlebarsjs/javascript/handlebars-api.js
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # reference: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
4
- # require 'active_support/core_ext/string'
5
-
6
- # require 'handlebars/helpers/base_helper'
7
-
8
- module Handlebarsjs
9
- module Helpers
10
- # Comparison helpers, eg. or, and, equal, not equal, less than, greater than etc.
11
- module Comparison
12
- # And: Block helper that renders a block if **all of** the given values are truthy. If an inverse block is specified it will be rendered when falsy.
13
- # < Handlebars::Helpers::BaseHelper
14
- class And
15
- # Parse will And: Block helper that renders a block if **all of** the given values are truthy. If an inverse block is specified it will be rendered when falsy.
16
- #
17
- # @example
18
- #
19
- # {{#if (and p1 p2 p3 p4 p5)}}
20
- # found
21
- # {{/if}}
22
- #
23
- # @example
24
- #
25
- # @example
26
- # {{#if (and name age)}}
27
- # {{name}}-{{age}}
28
- # {{else}}
29
- # no name or age
30
- # {{/if}}
31
- #
32
- # @param values list of values (via *splat) to be checked via AND condition
33
- # @return [String] return block when every value is truthy
34
- def parse(values)
35
- values.all? { |value| value }
36
- end
37
-
38
- def handlebars_helper
39
- # Exclude last paramater which is the context V8::Object
40
- proc { |_context, *values| wrapper(parse(values[0..-2])) }
41
- end
42
- end
43
- end
44
- end
45
- end