Sha256: 163f138575a3c948d895a22ab5380ec7b13ca6acd2d498f0c10288bf35c59907

Contents?: true

Size: 1.88 KB

Versions: 5

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

# @api private
module Phlex::Rails::HelperMacros
	def define_output_helper(method_name)
		class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
			# frozen_string_literal: true

			def #{method_name}(...)
				output = helpers.#{method_name}(...)

				case output
				when ActiveSupport::SafeBuffer
					@_target << output
				end

				nil
			end
		RUBY
	end

	def define_output_helper_with_capture_block(method_name)
		class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
			# frozen_string_literal: true

			def #{method_name}(*args, **kwargs, &block)
				output = if block
					helpers.#{method_name}(*args, **kwargs) { capture(&block) }
				else
					helpers.#{method_name}(*args, **kwargs)
				end

				case output
				when ActiveSupport::SafeBuffer
					@_target << output
				end

				nil
			end
		RUBY
	end

	def define_value_helper(method_name)
		class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
			# frozen_string_literal: true

			def #{method_name}(...)
				helpers.#{method_name}(...)
			end
		RUBY
	end

	def define_value_helper_with_capture_block(method_name)
		class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
			# frozen_string_literal: true

			def #{method_name}(*args, **kwargs, &block)
				if block
					helpers.#{method_name}(*args, **kwargs) { capture(&block) }
				else
					helpers.#{method_name}(*args, **kwargs)
				end
			end
		RUBY
	end

	def define_builder_yielding_helper(method_name, builder)
		class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
			# frozen_string_literal: true

			def #{method_name}(*args, **kwargs)
				output = if block_given?
					helpers.#{method_name}(*args, **kwargs) { |form|
						capture do
							yield(
								#{builder.name}.new(form,
									view: self
								)
							)
						end
					}
				else
					helpers.#{method_name}(*args, **kwargs)
				end

				case output
				when ActiveSupport::SafeBuffer
					@_target << output
				end

				nil
			end
		RUBY
	end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
phlex-rails-0.8.0 lib/phlex/rails/helper_macros.rb
phlex-rails-0.7.1 lib/phlex/rails/helper_macros.rb
phlex-rails-0.7.0 lib/phlex/rails/helper_macros.rb
phlex-rails-0.6.1 lib/phlex/rails/helper_macros.rb
phlex-rails-0.6.0 lib/phlex/rails/helper_macros.rb