Refactored file system as memory system

Necessary to clearly identify what _kind_ of file system this is.

Milestone: patch
This commit is contained in:
Brooke Kuhlmann
2026-02-02 08:30:16 -07:00
parent 8614d5c02f
commit a2514481fa
5 changed files with 29 additions and 29 deletions
+2 -2
View File
@@ -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
-25
View File
@@ -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
+25
View File
@@ -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
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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)
)