diff --git a/lib/trmnl/liquid.rb b/lib/trmnl/liquid.rb index 859b5de..2f18fc6 100644 --- a/lib/trmnl/liquid.rb +++ b/lib/trmnl/liquid.rb @@ -1,13 +1,13 @@ # frozen_string_literal: true require "liquid" -require "trmnl/liquid/file_system" require "trmnl/liquid/filters" +require "trmnl/liquid/memory_system" require "trmnl/liquid/template_tag" module TRMNL module Liquid - def self.build_environment(file_system: TRMNL::Liquid::FileSystem.new, **) + def self.build_environment(file_system: TRMNL::Liquid::MemorySystem.new, **) ::Liquid::Environment.build(file_system:, **) do |environment| environment.register_filter TRMNL::Liquid::Filters environment.register_tag "template", TRMNL::Liquid::TemplateTag diff --git a/lib/trmnl/liquid/file_system.rb b/lib/trmnl/liquid/file_system.rb deleted file mode 100644 index 472a46c..0000000 --- a/lib/trmnl/liquid/file_system.rb +++ /dev/null @@ -1,25 +0,0 @@ -# frozen_string_literal: true - -module TRMNL - module Liquid - # This in-memory "file system" is the backing storage for custom templates - # defined {% template [name] %} tags. - class FileSystem < ::Liquid::BlankFileSystem - def initialize - super - @templates = {} - end - - # called by Markup::LiquidTemplateTag to save users' custom shared templates via our - # custom {% template %} tag - def register name, body - @templates[name] = body - end - - # called by ::Liquid::Template for {% render 'foo' %} when rendering screen markup - def read_template_file name - @templates[name] || fail(::Liquid::FileSystemError, "Template not found: #{name}") - end - end - end -end diff --git a/lib/trmnl/liquid/memory_system.rb b/lib/trmnl/liquid/memory_system.rb new file mode 100644 index 0000000..bfdd946 --- /dev/null +++ b/lib/trmnl/liquid/memory_system.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +module TRMNL + module Liquid + # An in-memory file system for storing custom templates defined with {% template [name] %} tags. + class MemorySystem < ::Liquid::BlankFileSystem + def initialize + super + @templates = {} + end + + def register name, body + templates[name] = body + end + + def read_template_file name + templates[name] || fail(::Liquid::FileSystemError, "Template not found: #{name}.") + end + + private + + attr_reader :templates + end + end +end diff --git a/spec/trmnl/liquid/template_tag_spec.rb b/spec/trmnl/liquid/template_tag_spec.rb index 3d7cd05..f40a2eb 100644 --- a/spec/trmnl/liquid/template_tag_spec.rb +++ b/spec/trmnl/liquid/template_tag_spec.rb @@ -40,7 +40,7 @@ RSpec.describe TRMNL::Liquid::TemplateTag do it "answers error for undefined template" do content = renderer.call %({% render "bogus" %}), {} - expect(content).to eq("Liquid error: Template not found: bogus") + expect(content).to eq("Liquid error: Template not found: bogus.") end end end diff --git a/spec/trmnl/liquid_spec.rb b/spec/trmnl/liquid_spec.rb index 73681e6..d769276 100644 --- a/spec/trmnl/liquid_spec.rb +++ b/spec/trmnl/liquid_spec.rb @@ -6,7 +6,7 @@ RSpec.describe TRMNL::Liquid do describe ".build_environment" do it "builds with defaults" do expect(described_class.build_environment).to have_attributes( - file_system: be_a(TRMNL::Liquid::FileSystem), + file_system: be_a(TRMNL::Liquid::MemorySystem), error_mode: :lax, tags: hash_including("template" => TRMNL::Liquid::TemplateTag) )