Sha256: 7a88e26fe8cf032ab85647c17bacea49943a7bc1aacaa6c48e206aac79c9acd9

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

# Hash
unless {}.respond_to?(:tag)
  class Hash
    def tag node_name, inner_html=nil, &block
      HtmlTag::Inbound.new.tag(node_name, inner_html, self, &block).join('')
    end
  end
end

# String
unless ''.respond_to?(:tag)
  class String
    def tag node_name, opts = nil, &block
      HtmlTag::Inbound.new.tag(node_name, self, opts, &block).join('')
    end
  end
end

# HtmlTag do ...
module HtmlTag
  class Proxy
    def initialize scope = nil
      @pointer = HtmlTag::Inbound.new scope
    end

    def method_missing name, *args, &block
      @pointer
        .send(name, *args, &block)
        .join('')
    end
  end
end

def HtmlTag *args, &block
  if [Class, Module].include?(args[0].class)
    # imports tag method without poluting ancesstors namespace
    # class SomeClass
    #   HtmlTag self
    args[0].define_method :tag do |*tag_args, &tag_block|
      HtmlTag *tag_args, &tag_block
    end
  else
    # HtmlTag do ...
    args[0] ||= :div

    if args[0].class == Hash
      args[1] = args[0]
      args[0] = :div
    end

    if block
      # HtmlTag(:ul) { li ... }
      out = HtmlTag::Inbound.new self
      out.send(*args, &block)
      out.render
    else
      # HtmlTag._foo 123
      HtmlTag::Proxy.new self
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
html-tag-3.0.6 ./lib/html-tag/globals.rb
html-tag-3.0.5 ./lib/html-tag/globals.rb