mirror of
https://github.com/usetrmnl/trmnl-liquid.git
synced 2026-04-29 13:43:18 -07:00
Refactored file system as memory system
Necessary to clearly identify what _kind_ of file system this is. Milestone: patch
This commit is contained in:
+2
-2
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user