tilt-handlebars 1.2.0 → 2.0.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.
Files changed (65) hide show
  1. checksums.yaml +5 -5
  2. data/.editorconfig +12 -0
  3. data/.github/workflows/ci.yml +76 -0
  4. data/.github/workflows/publish.yml +58 -0
  5. data/.gitignore +14 -15
  6. data/.rspec +1 -0
  7. data/.rubocop.yml +189 -0
  8. data/Appraisals +9 -0
  9. data/CONTRIBUTING.md +75 -0
  10. data/Gemfile +3 -1
  11. data/LICENSE +22 -0
  12. data/README.md +34 -19
  13. data/RELEASE_NOTES.md +50 -8
  14. data/Rakefile +4 -8
  15. data/bin/audit +8 -0
  16. data/bin/build +8 -0
  17. data/bin/bundle +114 -0
  18. data/bin/bundler-audit +29 -0
  19. data/bin/console +15 -0
  20. data/bin/doc +7 -0
  21. data/bin/lint +8 -0
  22. data/bin/rake +29 -0
  23. data/bin/release +25 -0
  24. data/bin/rspec +29 -0
  25. data/bin/rubocop +29 -0
  26. data/bin/setup +13 -0
  27. data/bin/tag +25 -0
  28. data/bin/test +7 -0
  29. data/bin/version +24 -0
  30. data/gemfiles/tilt_1.gemfile +7 -0
  31. data/gemfiles/tilt_2.gemfile +7 -0
  32. data/lib/sinatra/handlebars.rb +10 -6
  33. data/lib/tilt/handlebars/version.rb +3 -1
  34. data/lib/tilt/handlebars.rb +57 -46
  35. data/lib/tilt-handlebars.rb +3 -0
  36. data/spec/fixtures/helpers.rb +21 -0
  37. data/spec/fixtures/views/hello.hbs +1 -0
  38. data/spec/fixtures/views/hello_missing.hbs +1 -0
  39. data/spec/fixtures/views/hello_partial.hbs +1 -0
  40. data/spec/fixtures/views/hello_partial2.hbs +1 -0
  41. data/spec/fixtures/views/partial.hbs +1 -0
  42. data/spec/fixtures/views/partial2.handlebars +1 -0
  43. data/spec/sinatra/handlebars_spec.rb +47 -0
  44. data/spec/spec_coverage.rb +19 -0
  45. data/spec/spec_helper.rb +23 -0
  46. data/spec/tilt/handlebars_template_spec.rb +543 -0
  47. data/spec/tilt_spec.rb +40 -0
  48. data/tasks/doc.rake +8 -0
  49. data/tasks/gem.rake +7 -0
  50. data/tasks/lint.rake +7 -0
  51. data/tasks/test.rake +10 -0
  52. data/tilt-handlebars.gemspec +48 -23
  53. metadata +261 -65
  54. data/.travis.yml +0 -6
  55. data/LICENSE.txt +0 -22
  56. data/test/fixtures/views/hello.hbs +0 -1
  57. data/test/fixtures/views/missing_partial.hbs +0 -1
  58. data/test/fixtures/views/partial.hbs +0 -1
  59. data/test/fixtures/views/partial2.handlebars +0 -1
  60. data/test/fixtures/views/partial_test.hbs +0 -1
  61. data/test/fixtures/views/partial_test2.handlebars +0 -1
  62. data/test/fixtures/views/two_partials.hbs +0 -1
  63. data/test/sinatra_test.rb +0 -38
  64. data/test/test_helper.rb +0 -9
  65. data/test/tilt_handlebarstemplate_test.rb +0 -276
data/bin/audit ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ root = File.absolute_path("..", __dir__)
5
+ audit = File.absolute_path("bundler-audit", __dir__)
6
+
7
+ Dir.chdir(root)
8
+ exec([audit, "audit"], "--update", *ARGV)
data/bin/build ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ root = File.absolute_path("..", __dir__)
5
+ rake = File.absolute_path("rake", __dir__)
6
+
7
+ Dir.chdir(root)
8
+ exec([rake, "rake"], "build", *ARGV)
data/bin/bundle ADDED
@@ -0,0 +1,114 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'bundle' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "rubygems"
12
+
13
+ m = Module.new do
14
+ module_function
15
+
16
+ def invoked_as_script?
17
+ File.expand_path($0) == File.expand_path(__FILE__)
18
+ end
19
+
20
+ def env_var_version
21
+ ENV["BUNDLER_VERSION"]
22
+ end
23
+
24
+ def cli_arg_version
25
+ return unless invoked_as_script? # don't want to hijack other binstubs
26
+ return unless "update".start_with?(ARGV.first || " ") # must be running `bundle update`
27
+ bundler_version = nil
28
+ update_index = nil
29
+ ARGV.each_with_index do |a, i|
30
+ if update_index && update_index.succ == i && a =~ Gem::Version::ANCHORED_VERSION_PATTERN
31
+ bundler_version = a
32
+ end
33
+ next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/
34
+ bundler_version = $1
35
+ update_index = i
36
+ end
37
+ bundler_version
38
+ end
39
+
40
+ def gemfile
41
+ gemfile = ENV["BUNDLE_GEMFILE"]
42
+ return gemfile if gemfile && !gemfile.empty?
43
+
44
+ File.expand_path("../../Gemfile", __FILE__)
45
+ end
46
+
47
+ def lockfile
48
+ lockfile =
49
+ case File.basename(gemfile)
50
+ when "gems.rb" then gemfile.sub(/\.rb$/, gemfile)
51
+ else "#{gemfile}.lock"
52
+ end
53
+ File.expand_path(lockfile)
54
+ end
55
+
56
+ def lockfile_version
57
+ return unless File.file?(lockfile)
58
+ lockfile_contents = File.read(lockfile)
59
+ return unless lockfile_contents =~ /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/
60
+ Regexp.last_match(1)
61
+ end
62
+
63
+ def bundler_requirement
64
+ @bundler_requirement ||=
65
+ env_var_version || cli_arg_version ||
66
+ bundler_requirement_for(lockfile_version)
67
+ end
68
+
69
+ def bundler_requirement_for(version)
70
+ return "#{Gem::Requirement.default}.a" unless version
71
+
72
+ bundler_gem_version = Gem::Version.new(version)
73
+
74
+ requirement = bundler_gem_version.approximate_recommendation
75
+
76
+ return requirement unless Gem::Version.new(Gem::VERSION) < Gem::Version.new("2.7.0")
77
+
78
+ requirement += ".a" if bundler_gem_version.prerelease?
79
+
80
+ requirement
81
+ end
82
+
83
+ def load_bundler!
84
+ ENV["BUNDLE_GEMFILE"] ||= gemfile
85
+
86
+ activate_bundler
87
+ end
88
+
89
+ def activate_bundler
90
+ gem_error = activation_error_handling do
91
+ gem "bundler", bundler_requirement
92
+ end
93
+ return if gem_error.nil?
94
+ require_error = activation_error_handling do
95
+ require "bundler/version"
96
+ end
97
+ return if require_error.nil? && Gem::Requirement.new(bundler_requirement).satisfied_by?(Gem::Version.new(Bundler::VERSION))
98
+ warn "Activating bundler (#{bundler_requirement}) failed:\n#{gem_error.message}\n\nTo install the version of bundler this project requires, run `gem install bundler -v '#{bundler_requirement}'`"
99
+ exit 42
100
+ end
101
+
102
+ def activation_error_handling
103
+ yield
104
+ nil
105
+ rescue StandardError, LoadError => e
106
+ e
107
+ end
108
+ end
109
+
110
+ m.load_bundler!
111
+
112
+ if m.invoked_as_script?
113
+ load Gem.bin_path("bundler", "bundle")
114
+ end
data/bin/bundler-audit ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'bundler-audit' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("bundler-audit", "bundler-audit")
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "handlebars/engine"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/doc ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ root = File.absolute_path("..", __dir__)
4
+ rake = File.absolute_path("rake", __dir__)
5
+
6
+ Dir.chdir(root)
7
+ exec([rake, "rake"], "doc", *ARGV)
data/bin/lint ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ root = File.absolute_path("..", __dir__)
5
+ rubocop = File.absolute_path("rubocop", __dir__)
6
+
7
+ Dir.chdir(root)
8
+ exec([rubocop, "rubocop"], *ARGV)
data/bin/rake ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rake' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rake", "rake")
data/bin/release ADDED
@@ -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
data/bin/rspec ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rspec' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rspec-core", "rspec")
data/bin/rubocop ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rubocop' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rubocop", "rubocop")
data/bin/setup ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ if [[ -n "${CI:-}" ]]; then
7
+ gem install bundler
8
+ bundle config set --local deployment 'true'
9
+ fi
10
+
11
+ bundle install
12
+
13
+ # Do any other automated setup that you need to do here
data/bin/tag ADDED
@@ -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
data/bin/test ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ root = File.absolute_path("..", __dir__)
4
+ rake = File.absolute_path("rake", __dir__)
5
+
6
+ Dir.chdir(root)
7
+ exec([rake, "rake"], "test", *ARGV)
data/bin/version ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env bash
2
+
3
+ dir="$(cd "$(dirname "${BASH_SOURCE[0]}" )" && pwd)"
4
+ root="$(cd "${dir}/.." && pwd)"
5
+
6
+ file="${root}/lib/tilt/handlebars/version.rb"
7
+
8
+ current() {
9
+ ruby -r "${file}" -e 'puts Tilt::Handlebars::VERSION'
10
+ }
11
+
12
+ update() {
13
+ # sed -E -i '' "s/VERSION = \".*\"/VERSION = \"${version}\"/" "${file}"
14
+ ruby -i -p -e "gsub(/VERSION = \".*\"/, 'VERSION = \"${version}\"')" "${file}"
15
+ }
16
+
17
+ version=$1
18
+
19
+ if [[ "${version}" == "" ]]; then
20
+ current
21
+ else
22
+ update
23
+ current
24
+ fi
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "tilt", "~> 1"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "tilt", "~> 2"
6
+
7
+ gemspec path: "../"
@@ -1,9 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "tilt/handlebars"
4
+
1
5
  module Sinatra
2
- module Handlebars
3
- def handlebars(*args)
4
- render(:handlebars, *args)
5
- end
6
- end
6
+ module Handlebars
7
+ def handlebars(*args)
8
+ render(:handlebars, *args)
9
+ end
10
+ end
7
11
 
8
- helpers Handlebars
12
+ helpers Handlebars
9
13
  end
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Tilt
2
4
  module Handlebars
3
- VERSION = "1.2.0"
5
+ VERSION = "2.0.0"
4
6
  end
5
7
  end
@@ -1,85 +1,96 @@
1
- require 'tilt' unless defined? Tilt
2
- require 'handlebars'
1
+ # frozen_string_literal: true
2
+
3
+ require "handlebars/engine"
4
+ require "pathname"
5
+ require "tilt"
3
6
 
4
7
  module Tilt
8
+ module Handlebars
9
+ class Error < RuntimeError; end
10
+ end
5
11
 
6
- # Handlebars.rb template implementation. See:
7
- # https://github.com/cowboyd/handlebars.rb
8
- # and http://handlebarsjs.com
9
- #
10
- # Handlebars is a logic-less template rendered with JavaScript.
11
- # Handlebars.rb is a Ruby wrapper around Handlebars, that allows
12
- # Handlebars templates to be rendered server side.
12
+ # A Tilt interface for the official JavaScript implementation of the
13
+ # Handlebars.js template engine.
13
14
  #
15
+ # @see https://github.com/rtomayko/tilt
16
+ # @see https://handlebarsjs.com
14
17
  class HandlebarsTemplate < Template
15
- def initialize_engine
16
- return if defined? ::Handlebars
17
- require_template_library 'handlebars'
18
- end
18
+ EXTENSIONS = ["handlebars", "hbs"].freeze
19
+
20
+ # Loads the template engine, if necessary.
21
+ #
22
+ # This method is needed in Tilt 1 but was removed in Tilt 2. It is provided
23
+ # here for backwards compatibility.
24
+ # @see https://github.com/rtomayko/tilt/blob/tilt-1/lib/tilt/template.rb#L58
25
+ def initialize_engine; end
19
26
 
20
27
  def prepare
21
- @context = ::Handlebars::Context.new
22
- @context.partial_missing { |partial_name| load_partial partial_name }
23
- @template = @context.compile(data)
28
+ @engine = ::Handlebars::Engine.new(**options.slice(:lazy, :path))
29
+ @engine.register_partial_missing { |name| load_partial(name) }
30
+ @template = @engine.compile(data)
24
31
  end
25
32
 
26
-
27
- def evaluate(scope, locals = {}, &block)
33
+ # rubocop:disable Metrics/AbcSize
34
+ def evaluate(scope, locals, &block)
28
35
  # Based on LiquidTemplate
29
- locals = locals.inject({}){ |h,(k,v)| h[k.to_s] = v ; h }
36
+ locals = locals.transform_keys(&:to_s)
30
37
  if scope.respond_to?(:to_h)
31
- scope = scope.to_h.inject({}){ |h,(k,v)| h[k.to_s] = v ; h }
38
+ scope = scope.to_h.transform_keys(&:to_s)
32
39
  locals = scope.merge(locals)
33
40
  else
34
41
  scope.instance_variables.each do |var|
35
42
  key = var.to_s.delete("@")
36
- locals[key] = scope.instance_variable_get(var) unless locals.has_key? key
43
+ locals[key] = scope.instance_variable_get(var) unless locals.key? key
37
44
  end
38
45
  end
39
46
 
40
- locals['yield'] = block.nil? ? '' : yield
41
- locals['content'] = locals['yield']
42
-
43
- @template.call(locals);
44
- end
45
-
46
- def register_helper(name, &fn)
47
- @context.register_helper(name, &fn)
48
- end
47
+ locals["yield"] = block.nil? ? "" : yield
48
+ locals["content"] = locals["yield"]
49
49
 
50
- def register_partial(*args)
51
- @context.register_partial(*args)
50
+ @template.call(locals)
52
51
  end
52
+ # rubocop:enable Metrics/AbcSize
53
53
 
54
- def partial_missing(&fn)
55
- @context.partial_missing(&fn)
54
+ def partial_missing(*args, **opts, &block)
55
+ @engine.register_partial_missing(*args, **opts, &block)
56
56
  end
57
57
 
58
58
  def allows_script?
59
59
  false
60
60
  end
61
61
 
62
+ private
63
+
62
64
  def load_partial(partial_name)
63
- if Pathname.new(partial_name).absolute?
65
+ if Pathname.new(partial_name).absolute?
64
66
  dir = ""
65
67
  elsif file
66
68
  dir = File.dirname file
67
69
  end
68
70
 
69
- partial_file = File.expand_path("#{partial_name}.hbs", dir)
70
- partial_file = File.expand_path("#{partial_name}.handlebars", dir) unless File.file? partial_file
71
- if File.file? partial_file
72
- return IO.read(partial_file)
73
- end
71
+ paths = EXTENSIONS.map { |ext|
72
+ File.expand_path("#{partial_name}.#{ext}", dir)
73
+ }
74
+ path = paths.find { |p|
75
+ File.file?(p)
76
+ }
77
+
78
+ return File.read(path) if path
79
+
80
+ message =
81
+ "The partial '#{partial_name}' could not be found. No such files "\
82
+ "#{paths.join(", ")}"
83
+ raise Handlebars::Error, message
84
+ end
74
85
 
75
- raise "The partial '#{partial_name}' could not be found. No such file #{partial_file}"
86
+ def method_missing(name, *args, &block)
87
+ @engine.send(name, *args, &block)
76
88
  end
77
89
 
78
- private :load_partial
79
-
90
+ def respond_to_missing?(name, *)
91
+ @engine.respond_to?(name) || super
92
+ end
80
93
  end
81
94
 
82
- register HandlebarsTemplate, 'handlebars', 'hbs'
95
+ register HandlebarsTemplate, *HandlebarsTemplate::EXTENSIONS
83
96
  end
84
-
85
-
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "tilt/handlebars"
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Fixtures
4
+ module Helpers
5
+ def fixture_dir
6
+ __dir__
7
+ end
8
+
9
+ def fixture_data(path)
10
+ File.read(fixture_path(path))
11
+ end
12
+
13
+ def fixture_file(path)
14
+ File.open(fixture_path(path))
15
+ end
16
+
17
+ def fixture_path(path)
18
+ File.expand_path(path, __dir__)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1 @@
1
+ Hello, {{name}}.
@@ -0,0 +1 @@
1
+ Hello, {{> missing}}.
@@ -0,0 +1 @@
1
+ Hello, {{> partial}}.
@@ -0,0 +1 @@
1
+ Hello, {{> partial2}}.
@@ -0,0 +1 @@
1
+ {{name}} (a '{{type}}')
@@ -0,0 +1 @@
1
+ {{name}} (a '{{type}}')
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rack/test"
4
+ require "sinatra/base"
5
+ require "sinatra/handlebars"
6
+
7
+ RSpec.describe Sinatra::Handlebars do
8
+ let(:app) {
9
+ Sinatra.new(Sinatra::Base) do
10
+ extend Fixtures::Helpers
11
+
12
+ set :environment, :test
13
+ set :root, fixture_dir
14
+ helpers Sinatra::Handlebars # rubocop:disable RSpec/DescribedClass
15
+
16
+ get "/:name" do
17
+ template = params["name"].to_sym
18
+ handlebars template, locals: { name: "World", type: "planet" }
19
+ end
20
+ end
21
+ }
22
+ let(:client) {
23
+ Rack::Test::Session.new(Rack::MockSession.new(app))
24
+ }
25
+
26
+ describe "#handlebars" do
27
+ context "with a simple template" do
28
+ let(:rendered) { "Hello, World." }
29
+ let(:response) { client.get("/hello") }
30
+ let(:response_data) { response.body.strip }
31
+
32
+ it "renders correctly" do
33
+ expect(response_data).to eq(rendered)
34
+ end
35
+ end
36
+
37
+ context "with a partial template" do
38
+ let(:rendered) { "Hello, World (a 'planet')\n." }
39
+ let(:response) { client.get("/hello_partial") }
40
+ let(:response_data) { response.body.strip }
41
+
42
+ it "renders correctly" do
43
+ expect(response_data).to eq(rendered)
44
+ end
45
+ end
46
+ end
47
+ end