handlebarsjs 0.1.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8048e42043ef2254d3c7e271e3469f6609b19ad08c9e92cf39ac5c1419b457c6
4
- data.tar.gz: 51e120956c3b647d250e0ec49a2d2ef6eb6591cdcc98c526cbf33a40726f034f
3
+ metadata.gz: dc03074aedaca262df64a9aeb596c892c7e77f48ead79724cda8bb85d4d0aeb9
4
+ data.tar.gz: 5674236c6816c6f6ce4e51c1d13f5ad5ebb097f627cb1e7c5ea6ecf1b423b780
5
5
  SHA512:
6
- metadata.gz: 36ebcfef4508ed601fa5297970b7dadc17daa18da2cd65ab1345c742e0367138ccdf11d48e759efec54aad93733fba3d1564b05f451ba8536b928a428b52af52
7
- data.tar.gz: 315e2de217dc22e97b6bff861ddb255ec75dfde16d76627250f01677b6e471e562b76a9be9f310dab2e90162c6c2404753ee7f97e54eec0ecd445cb1bc614596
6
+ metadata.gz: 0eceb601e4df54be59f5736b2f16098b9ad7b61ddc918b6f0a07593ba1373d490f312e8be92d5aa85deeac751e66ad0704edce890813506eae0bb30933a5d80f
7
+ data.tar.gz: e3fba2540c9dd5cabcf17b8b525e2cddd8d360a003ade9e2a578950cae139488df41fb350c771491c55f6365339de6ab26db81660ad2d137e2625745c348113a
data/CHANGELOG.md CHANGED
@@ -1,3 +1,26 @@
1
+ # [0.3.0](https://github.com/klueless-io/handlebarsjs/compare/v0.2.0...v0.3.0) (2022-07-06)
2
+
3
+
4
+ ### Features
5
+
6
+ * refactor snapshot builder to handlebars snapshot ([bc6d079](https://github.com/klueless-io/handlebarsjs/commit/bc6d079d12b87f0198ae2a32c94c6349929c8c70))
7
+
8
+ # [0.2.0](https://github.com/klueless-io/handlebarsjs/compare/v0.1.2...v0.2.0) (2022-07-02)
9
+
10
+
11
+ ### Features
12
+
13
+ * add support for basic template processing ([13d9649](https://github.com/klueless-io/handlebarsjs/commit/13d9649c03fa7de0326bed71d08441453194e4e1))
14
+ * add support for basic template processing ([64966e5](https://github.com/klueless-io/handlebarsjs/commit/64966e5ad396910dd0e7edc11a4d8427a10cae29))
15
+
16
+ ## [0.1.2](https://github.com/klueless-io/handlebarsjs/compare/v0.1.1...v0.1.2) (2022-07-01)
17
+
18
+
19
+ ### Bug Fixes
20
+
21
+ * add snapshot builder ([f4dd8e8](https://github.com/klueless-io/handlebarsjs/commit/f4dd8e8e6a1c40da3e232bce93a33f252cc02a08))
22
+ * add snapshot builder ([c1e31c7](https://github.com/klueless-io/handlebarsjs/commit/c1e31c7e0f4402873fcbd1c3b065564bcb9457a4))
23
+
1
24
  ## [0.1.1](https://github.com/klueless-io/handlebarsjs/compare/v0.1.0...v0.1.1) (2022-04-17)
2
25
 
3
26
 
@@ -1,15 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Handlebarsjs
4
- class Handlebars
5
- # @helpers = {}
6
- # @partials = {}
7
- # @partial_missing = nil
8
- # @precompile = nil
9
- # @template_cache = {}
4
+ # API for interacting with Handlebars.js while providing native Ruby helpers
5
+ class Handlebars < Handlebarsjs::Javascript
6
+ def initialize
7
+ super
8
+ # Handlebars 4.7.7
9
+ handlebars_snapshot.add_library('handlebars', path: Handlebarsjs::HANDLEBARS_LIBRARY_PATH)
10
10
 
11
- attr_reader :snapshot
11
+ # Support functions for working with
12
+ handlebars_snapshot.add_library('handlebars-api', path: Handlebarsjs::HANDLEBARS_API_PATH)
13
+ end
12
14
 
13
- # def initialize
15
+ def process_template(template, options = {})
16
+ # TODO: process template function may be improved with some type of caching
17
+ context.call('process_template', template, options)
18
+ end
14
19
  end
15
20
  end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Handlebarsjs
4
+ # Wraps MiniRacer snapshot with specific emphasis on
5
+ # loading Handlebars helpers onto the MiniRacer context
6
+ # in the correct order. So that new contexts are preloaded
7
+ # with the handlebars library and the configured helpers.
8
+ class HandlebarsSnapshot
9
+ attr_reader :scripts
10
+ attr_reader :helpers
11
+
12
+ def initialize
13
+ @scripts = []
14
+ @helpers = []
15
+ end
16
+
17
+ def add_library(name, script: nil, path: nil)
18
+ add_script(name, 'library', script: script, path: path)
19
+ end
20
+
21
+ def add_snippet(name, script: nil, path: nil)
22
+ add_script(name, 'snippet', script: script, path: path)
23
+ end
24
+
25
+ def add_helper(name, callback)
26
+ @helpers << {
27
+ name: name,
28
+ callback: callback
29
+ }
30
+ end
31
+
32
+ def register_helper(name)
33
+ add_script(name, 'helper', script: "Handlebars.registerHelper('#{name}', ruby_#{name})")
34
+ end
35
+
36
+ def script
37
+ scripts.map { |script| "// #{script[:type]} - #{script[:name]}\n#{script[:script]}" }.join("\n\n")
38
+ end
39
+
40
+ def snapshot
41
+ @snapshot ||= MiniRacer::Snapshot.new(script)
42
+ end
43
+
44
+ # rubocop:disable Style/DocumentDynamicEvalDefinition
45
+ def new_context
46
+ context = MiniRacer::Context.new(snapshot: snapshot)
47
+
48
+ helpers.each do |helper|
49
+ context.attach("ruby_#{helper[:name]}", helper[:callback])
50
+ context.eval("Handlebars.registerHelper('#{helper[:name]}', ruby_#{helper[:name]})")
51
+ end
52
+
53
+ context
54
+ end
55
+ # rubocop:enable Style/DocumentDynamicEvalDefinition
56
+
57
+ # not currently used
58
+ def dirty?
59
+ @snapshot.nil?
60
+ end
61
+
62
+ def debug
63
+ puts script
64
+ end
65
+
66
+ private
67
+
68
+ def add_script(name, type, script: nil, path: nil)
69
+ raise Handlebarsjs::Error, 'script or path is required' if script.nil? && path.nil?
70
+ raise Handlebarsjs::Error, 'script and path are mutually exclusive' if script && path
71
+
72
+ script ||= File.read(path)
73
+ add_script_item(name, type, script, path)
74
+ end
75
+
76
+ def add_script_item(name, type, script, path = nil)
77
+ @snapshot = nil
78
+ scripts << {
79
+ name: name,
80
+ type: type,
81
+ script: script,
82
+ path: path
83
+ }
84
+ end
85
+ end
86
+ end
@@ -1,11 +1,11 @@
1
- // Is this name ok? would compile_and_process_template be better?
2
- // Also, should this be at the top level namespace?
3
-
4
- function compile_template(template) {
5
- return Handlebars.compile(template);
6
- }
1
+ // This function will be needed in future when I can figure out how to cache templates.
2
+ // function compile_template(template) {
3
+ // return Handlebars.compile(template);
4
+ // }
7
5
 
6
+ // This is the main function that is called by the client.
8
7
  function process_template(template, data) {
8
+ // The process template function may be improved with some type of caching
9
9
  const compiled_template = Handlebars.compile(template);
10
10
  return compiled_template(data);
11
11
  }
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Handlebarsjs
4
+ # API for interacting with Javascript while providing native Ruby helpers
5
+ class Javascript
6
+ attr_reader :handlebars_snapshot
7
+
8
+ def initialize
9
+ @handlebars_snapshot = HandlebarsSnapshot.new
10
+ end
11
+
12
+ def eval(script)
13
+ context.eval(script)
14
+ end
15
+
16
+ def attach(script, block)
17
+ context.attach(script, block)
18
+ end
19
+
20
+ def call(function_name, *arguments)
21
+ context.call(function_name, *arguments)
22
+ end
23
+
24
+ private
25
+
26
+ def context
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 ||= handlebars_snapshot.new_context
29
+ end
30
+ end
31
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Handlebarsjs
4
- VERSION = '0.1.2'
4
+ VERSION = '0.4.0'
5
5
  end
data/lib/handlebarsjs.rb CHANGED
@@ -2,11 +2,13 @@
2
2
 
3
3
  require 'mini_racer'
4
4
  require_relative 'handlebarsjs/version'
5
- require_relative 'handlebarsjs/snapshot_builder'
5
+ require_relative 'handlebarsjs/javascript'
6
+ require_relative 'handlebarsjs/handlebars_snapshot'
6
7
  require_relative 'handlebarsjs/handlebars'
7
8
 
8
9
  module Handlebarsjs
9
- JAVASCRIPT_PATH = 'lib/handlebarsjs/javascript/handlebars-4.7.7.js'
10
+ HANDLEBARS_LIBRARY_PATH = 'lib/handlebarsjs/javascript/handlebars-4.7.7.js'
11
+ HANDLEBARS_API_PATH = 'lib/handlebarsjs/javascript/handlebars-api.js'
10
12
 
11
13
  # raise Handlebarsjs::Error, 'Sample message'
12
14
  Error = Class.new(StandardError)
data/package-lock.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "handlebarsjs",
3
- "version": "0.1.2",
3
+ "version": "0.4.0",
4
4
  "lockfileVersion": 2,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "handlebarsjs",
9
- "version": "0.1.2",
9
+ "version": "0.4.0",
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.1.2",
3
+ "version": "0.4.0",
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,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: handlebarsjs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Cruwys
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-07-01 00:00:00.000000000 Z
11
+ date: 2022-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: k_log
@@ -72,11 +72,12 @@ files:
72
72
  - lib/handlebarsjs.rb
73
73
  - lib/handlebarsjs/api.rb
74
74
  - lib/handlebarsjs/handlebars.rb
75
+ - lib/handlebarsjs/handlebars_snapshot.rb
75
76
  - lib/handlebarsjs/helpers/comparison/and.rb
77
+ - lib/handlebarsjs/javascript.rb
76
78
  - lib/handlebarsjs/javascript/handlebars-4.7.7.js
77
79
  - lib/handlebarsjs/javascript/handlebars-api.js
78
80
  - lib/handlebarsjs/javascript/handlebars-helpers.js
79
- - lib/handlebarsjs/snapshot_builder.rb
80
81
  - lib/handlebarsjs/version.rb
81
82
  - package-lock.json
82
83
  - package.json
@@ -1,51 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Handlebarsjs
4
- # Wraps MiniRacer snapshot and provides clean API
5
- class SnapshotBuilder
6
- attr_reader :scripts
7
-
8
- def initialize
9
- @scripts = []
10
- end
11
-
12
- def add_handlebars_js
13
- add_script('handlebars', 'library', path: Handlebarsjs::JAVASCRIPT_PATH)
14
- end
15
-
16
- def add_library(name, script: nil, path: nil)
17
- add_script(name, 'library', script: script, path: path)
18
- end
19
-
20
- def register_helper(name)
21
- add_script(name, 'helper', script: "Handlebars.registerHelper('#{name}', ruby_#{name})")
22
- end
23
-
24
- def script
25
- scripts.map { |script| "# #{script[:type]} - #{script[:name]}\n#{script[:script]}" }.join("\n\n")
26
- end
27
-
28
- def build
29
- MiniRacer::Snapshot.new(script)
30
- end
31
-
32
- private
33
-
34
- def add_script(name, type, script: nil, path: nil)
35
- raise Handlebarsjs::Error, 'script or path is required' if script.nil? && path.nil?
36
- raise Handlebarsjs::Error, 'script and path are mutually exclusive' if script && path
37
-
38
- script ||= File.read(path)
39
- add_script_item(name, type, script, path)
40
- end
41
-
42
- def add_script_item(name, type, script, path = nil)
43
- scripts << {
44
- name: name,
45
- type: type,
46
- script: script,
47
- path: path
48
- }
49
- end
50
- end
51
- end