module Ariadne::ViewComponent::HTMLAttrs
def accepts_html_attributes(**defaults, &block)
def accepts_html_attributes(**defaults, &block) accepts_html_attributes_for(:html, **defaults, &block) end
def accepts_html_attributes_for(name, **defaults, &block)
def accepts_html_attributes_for(name, **defaults, &block) html_attribute_accessors[name] = defaults name = name.to_sym method_name = :"#{name}_attrs" ivar_name = :"@#{method_name}" attr_reader(method_name) mod = Module.new do define_method(:initialize) do |**options| value = self.class.default_html_attributes_for(name).deep_merge!(options.delete(method_name).to_h) super(**options) instance_exec(value, &block) if block instance_variable_set(ivar_name, value) end end prepend(mod) end
def default_html_attributes_for(name)
def default_html_attributes_for(name) html_attribute_accessors.fetch(name).each_with_object(AttributesHash.new) do |(k, v), acc| acc[k] = v.is_a?(Proc) ? v.call : v acc end end
def html_attribute_accessors
def html_attribute_accessors @html_attribute_accessors ||= if superclass.respond_to?(:html_attribute_accessors) superclass.html_attribute_accessors.deep_dup else {} end end