Sha256: 28a146acfe07e56730f2f94cd065c2bb86a1d45fd78ee7ca5071a1b335d11bef

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module Phlex
  module Context
    def text(content)
      self << Text.new(content)
    end

    def component(component, *args, **kwargs, &block)
      assigns = instance_variables
        .reject { _1.start_with? "@_" }
        .map { [_1, instance_variable_get(_1)] }.to_h

      comp = component.new(*args, assigns:, **kwargs, &block)
      self << comp
    end

    Tag::StandardElement.subclasses.each do |tag|
      class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
        def #{tag.value}(content = nil, **kwargs, &block)
          raise ArgumentError if content && block_given?
          tag = #{tag.name}.new(**kwargs)
          self << tag
          render_tag(tag, &block) if block_given?
          render_tag(tag) { text content } if content
        end
      RUBY
    end

    Tag::VoidElement.subclasses.each do |tag|
      class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
        def #{tag.value}(**kwargs)
          tag = #{tag.name}.new(**kwargs)
          self << tag
        end
      RUBY
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
phlex-0.1.0 lib/phlex/context.rb