From f155108a6c3af0bf4eb1f2941fc87faa3f27eac3 Mon Sep 17 00:00:00 2001 From: Joel Drapper Date: Thu, 6 Feb 2025 22:37:59 +0000 Subject: [PATCH] Rename `context` to `state` (#846) A bunch of misc refactors and renaming. Note: this will break `phlex-rails` and should be merged at the same time as an update over there. --- lib/phlex.rb | 1 - lib/phlex/fifo.rb | 2 +- lib/phlex/html.rb | 6 +- lib/phlex/sgml.rb | 134 ++++++++++++------------ lib/phlex/sgml/elements.rb | 37 +++---- lib/phlex/{context.rb => sgml/state.rb} | 36 ++++--- 6 files changed, 112 insertions(+), 104 deletions(-) rename lib/phlex/{context.rb => sgml/state.rb} (84%) diff --git a/lib/phlex.rb b/lib/phlex.rb index 3cceb6ad..a87728f1 100644 --- a/lib/phlex.rb +++ b/lib/phlex.rb @@ -6,7 +6,6 @@ module Phlex autoload :ArgumentError, "phlex/errors/argument_error" autoload :CSV, "phlex/csv" - autoload :Context, "phlex/context" autoload :DoubleRenderError, "phlex/errors/double_render_error" autoload :Error, "phlex/error" autoload :FIFO, "phlex/fifo" diff --git a/lib/phlex/fifo.rb b/lib/phlex/fifo.rb index 64854b7f..05b3165e 100644 --- a/lib/phlex/fifo.rb +++ b/lib/phlex/fifo.rb @@ -20,7 +20,7 @@ def expand(bytes) def [](key) k, v = @store[key.hash] - v if k == key + v if k.eql?(key) end def []=(key, value) diff --git a/lib/phlex/html.rb b/lib/phlex/html.rb index 58973d83..e9cc4ba9 100644 --- a/lib/phlex/html.rb +++ b/lib/phlex/html.rb @@ -9,10 +9,10 @@ class Phlex::HTML < Phlex::SGML # Output an HTML doctype. def doctype - context = @_context - return unless context.should_render? + state = @_state + return unless state.should_render? - context.buffer << "" + state.buffer << "" nil end diff --git a/lib/phlex/sgml.rb b/lib/phlex/sgml.rb index 2c7f151a..816da74c 100644 --- a/lib/phlex/sgml.rb +++ b/lib/phlex/sgml.rb @@ -8,6 +8,7 @@ class Phlex::SGML autoload :Elements, "phlex/sgml/elements" autoload :SafeObject, "phlex/sgml/safe_object" autoload :SafeValue, "phlex/sgml/safe_value" + autoload :State, "phlex/sgml/state" include Phlex::Helpers @@ -42,25 +43,36 @@ def to_proc proc { |c| c.render(self) } end - def call(buffer = +"", context: {}, view_context: nil, parent: nil, fragments: nil, &block) - @_buffer = buffer - @_context = phlex_context = parent&.__context__ || Phlex::Context.new(user_context: context, view_context:) - @_parent = parent + def call(buffer = +"", context: {}, view_context: nil, fragments: nil, &) + state = Phlex::SGML::State.new( + user_context: context, + view_context:, + output_buffer: buffer, + fragments: fragments&.to_set, + ) - raise Phlex::DoubleRenderError.new("You can't render a #{self.class.name} more than once.") if @_rendered - @_rendered = true + internal_call(parent: nil, state:, &) - if fragments - phlex_context.target_fragments(fragments) + state.output_buffer << state.buffer + end + + def internal_call(parent: nil, state: nil, &block) + return "" unless render? + + if @_context + raise Phlex::DoubleRenderError.new( + "You can't render a #{self.class.name} more than once." + ) end - block ||= @_content_block + @_context = state + @_state = state - return "" unless render? + block ||= @_content_block Thread.current[:__phlex_component__] = [self, Fiber.current.object_id].freeze - phlex_context.around_render do + state.around_render(self) do before_template(&block) around_template do @@ -79,18 +91,12 @@ def call(buffer = +"", context: {}, view_context: nil, parent: nil, fragments: n after_template(&block) end - - unless parent - buffer << phlex_context.buffer - end ensure Thread.current[:__phlex_component__] = [parent, Fiber.current.object_id].freeze end - protected def __context__ = @_context - def context - @_context.user_context + @_state._state end # Output plain text. @@ -104,10 +110,10 @@ def plain(content) # Output a single space character. If a block is given, a space will be output before and after the block. def whitespace(&) - context = @_context - return unless context.should_render? + state = @_state + return unless state.should_render? - buffer = context.buffer + buffer = state.buffer buffer << " " @@ -123,10 +129,10 @@ def whitespace(&) # # [MDN Docs](https://developer.mozilla.org/en-US/docs/Web/HTML/Comments) def comment(&) - context = @_context - return unless context.should_render? + state = @_state + return unless state.should_render? - buffer = context.buffer + buffer = state.buffer buffer << "