class Paggio::HTML::Element

Public Class Methods

defhelper(name, &block) click to toggle source
# File lib/paggio/html/helpers.rb, line 14
def self.defhelper(name, &block)
  define_method name do |*args, &body|
    instance_exec(*args, &block)

    self.do(&body) if body
    self
  end
end
defhelper!(name, attribute = name) click to toggle source
# File lib/paggio/html/helpers.rb, line 23
def self.defhelper!(name, attribute = name)
  defhelper "#{name}!" do
    @attributes[attribute] = true
  end
end
new(owner, name, attributes = {}) click to toggle source
Calls superclass method
# File lib/paggio/html/element.rb, line 29
def self.new(owner, name, attributes = {})
  return super unless self == Element

  const = name.capitalize

  if !const.to_s.include?('-') && const_defined?(const)
    const_get(const).new(owner, name, attributes)
  else
    super
  end
end
new(owner, name, attributes = {}) click to toggle source
# File lib/paggio/html/element.rb, line 41
def initialize(owner, name, attributes = {})
  @owner       = owner
  @name        = name
  @attributes  = attributes
  @children    = []
  @class_names = []
end

Public Instance Methods

<<(what) click to toggle source
# File lib/paggio/html/element.rb, line 53
def <<(what)
  @children << what

  self
end
>>(content) click to toggle source
# File lib/paggio/html/element.rb, line 95
def >>(content)
  self << ::Paggio::Utils.heredoc(content.to_s)
  self
end
[](*names) click to toggle source
# File lib/paggio/html/element.rb, line 81
def [](*names)
  if last = @class_names.pop
    @class_names << [last, *names].join('-')
  end

  self
end
do(&block) click to toggle source
# File lib/paggio/html/element.rb, line 89
def do(&block)
  @owner.extend!(self, &block)

  self
end
each(&block) click to toggle source
# File lib/paggio/html/element.rb, line 49
def each(&block)
  @children.each(&block)
end
inspect() click to toggle source
# File lib/paggio/html/element.rb, line 112
def inspect
  if @children.empty?
    "#<HTML::Element(#{@name.upcase})>"
  else
    "#<HTML::Element(#{@name.upcase}): #{@children.inspect[1 .. -2]}>"
  end
end
method_missing(name, content = nil, &block) click to toggle source
# File lib/paggio/html/element.rb, line 59
def method_missing(name, content = nil, &block)
  if name.to_s.end_with? ?!
    @attributes[:id] = name[0 .. -2]
  else
    @class_names << name
  end

  if ::Hash === content
    if content.has_key?(:class) || content.has_key?(:classes)
      @class_names.unshift(*(content.delete(:class).to_s.split | content.delete(:classes).to_a))
    end

    ::Paggio::Utils.deep_merge!(@attributes, content)
  elsif content
    self >> content
  end

  @owner.extend!(self, &block) if block

  self
end