Sha256: 83958c3888118ccc7a9b12b65e550f4ffbcbdd4e9eb36a00d78c0353a06008ff

Contents?: true

Size: 1.85 KB

Versions: 4

Compression:

Stored size: 1.85 KB

Contents

require 'hyperstack/ext/component/string'
# see hyperstack/component/haml for this is to be included.
module Hyperstack
  module Internal
    module Component
      module HAMLTagInstanceMethods
        def self.included(base)
          base.const_get('HTML_TAGS').each do |tag|
            if tag == 'p'
              base.define_method(tag) do |*params, &children|
                if children || params.count == 0 || (params.count == 1 && params.first.is_a?(Hash))
                  RenderingContext.render(tag, *params, &children)
                else
                  Kernel.p(*params)
                end
              end
            else
              base.alias_method tag, tag.upcase
            end
          end
        end
      end

      module HAMLElementInstanceMethods
        def method_missing(class_name, args = {}, &new_block)
          return dup.render.method_missing(class_name, args, &new_block) unless rendered?
          Hyperstack::Internal::Component::RenderingContext.replace(
            self,
            Hyperstack::Internal::Component::RenderingContext.build do
              Hyperstack::Internal::Component::RenderingContext.render(type, build_new_properties(class_name, args), &new_block)
            end
          )
        end

        def rendered?
          Hyperstack::Internal::Component::RenderingContext.rendered? self
        end

        def haml_class_name(class_name)
          class_name.gsub(/__|_/, '__' => '_', '_' => '-')
        end

        private

        def build_new_properties(class_name, args)
          class_name = haml_class_name(class_name)
          new_props = @properties.dup
          new_props[:className] = "\
            #{class_name} #{new_props[:className]} #{args.delete(:class)} #{args.delete(:className)}\
          ".split(' ').uniq.join(' ')
          new_props.merge! args
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
hyper-component-1.0.alpha1.3 lib/hyperstack/internal/component/haml.rb
hyper-component-1.0.alpha1.2 lib/hyperstack/internal/component/haml.rb
hyper-component-1.0.alpha1.1 lib/hyperstack/internal/component/haml.rb
hyper-component-1.0.alpha1 lib/hyperstack/internal/component/haml.rb