Sha256: 1bba59650beb4f05be4fbf289769a5bd2316c81d5276713ec45aff3cb4e7f827

Contents?: true

Size: 1.19 KB

Versions: 5

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

# @api private
class Phlex::Context
	def initialize(user_context = {})
		@buffer = +""
		@capturing = false
		@user_context = user_context
		@fragments = nil
		@in_target_fragment = false
		@halt_signal = nil
	end

	attr_accessor :buffer, :capturing, :user_context, :in_target_fragment

	attr_reader :fragments

	# Added for backwards compatibility with phlex-rails. We can remove this with 2.0
	def target
		@buffer
	end

	def target_fragments(fragments)
		@fragments = fragments.to_h { |it| [it, true] }
	end

	def around_render
		return yield if !@fragments || @halt_signal

		catch do |signal|
			@halt_signal = signal
			yield
		end
	end

	def begin_target(id)
		@in_target_fragment = id
	end

	def end_target
		@fragments.delete(@in_target_fragment)
		@in_target_fragment = false
		throw @halt_signal if @fragments.length == 0
	end

	def capturing_into(new_buffer)
		original_buffer = @buffer
		original_capturing = @capturing
		original_fragments = @fragments

		begin
			@buffer = new_buffer
			@capturing = true
			@fragments = nil
			yield
		ensure
			@buffer = original_buffer
			@capturing = original_capturing
			@fragments = original_fragments
		end

		new_buffer
	end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
phlex-1.11.0 lib/phlex/context.rb
phlex-1.10.3 lib/phlex/context.rb
phlex-1.10.2 lib/phlex/context.rb
phlex-1.10.1 lib/phlex/context.rb
phlex-1.10.0 lib/phlex/context.rb