class BBLib::HTML::Tag

Constants

APPEND_ATTRIBUTES

Public Instance Methods

add(*childs) click to toggle source
# File lib/bblib/html/tag.rb, line 30
def add(*childs)
  [childs].flatten.each { |child| children.push(child) }
  nil
end
append_attribute(attribute, value) click to toggle source
# File lib/bblib/html/tag.rb, line 47
def append_attribute(attribute, value)
  attributes[attribute] = [attributes[attribute], value.to_s].compact.join(' ')
end
merge(attributes) click to toggle source
# File lib/bblib/html/tag.rb, line 70
def merge(attributes)
  raise ArgumentError, "Expected a Hash, got a #{attributes.class}" unless attributes.is_a?(Hash)
  attributes.each do |k, v|
    if APPEND_ATTRIBUTES.include?(k.to_sym)
      append_attribute(k, v)
    else
      set_attribute(k, v)
    end
  end
  self
end
render(pretty: false, tabs: 0) click to toggle source
# File lib/bblib/html/tag.rb, line 17
def render(pretty: false, tabs: 0)
  cont = render_content(pretty: pretty, tabs: tabs)
  tabbing = pretty ? ("\n" + ("\t" * tabs)) : ''
  "#{tabbing}<#{type}#{render_attributes}" +
  if cont || !BBLib::HTML.self_close?(type)
    ">#{cont}#{tabbing}</#{type}>"
  else
    "/>"
  end
end
Also aliased as: to_html
render_attributes() click to toggle source
# File lib/bblib/html/tag.rb, line 51
def render_attributes
  return nil if attributes.empty?
  attributes[:style] = attributes[:style].map { |k, v| "#{k}: #{v}" }.join('; ') if attributes[:style] && attributes[:style].is_a?(Hash)
  ' ' + attributes.map do | k, v|
    v = v.join(' ') if v.is_a?(Array)
    "#{k}=\"#{v.to_s.gsub('"', '&#34;')}\""
  end.join(' ')
end
render_content(pretty: false, tabs: 0) click to toggle source
# File lib/bblib/html/tag.rb, line 60
def render_content(pretty: false, tabs: 0)
  return nil if (content.nil? || content.empty?) && children.empty?
  tabbing = pretty ? ("\n" + ("\t" * (tabs + 1))) : ''
  text = if content && !content.empty?
    "#{tabbing}#{content.gsub("\n", pretty ? tabbing : "\n")}"
  end
  html = children.map { |tag| tag.render(pretty: pretty, tabs: tabs + 1) }.join
  [text, html].compact.join
end
set_attribute(attribute, value) click to toggle source
# File lib/bblib/html/tag.rb, line 43
def set_attribute(attribute, value)
  attributes[attribute] = value
end
to_html(pretty: false, tabs: 0)
Alias for: render
to_s(*args) click to toggle source
# File lib/bblib/html/tag.rb, line 35
def to_s(*args)
  render(*args)
end
to_str() click to toggle source
# File lib/bblib/html/tag.rb, line 39
def to_str
  to_s
end

Protected Instance Methods

method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/bblib/html/tag.rb, line 91
def method_missing(method, *args, &block)
  if context && context.respond_to?(method)
    context.send(method, *args, &block)
  elsif method != :to_ary
    if method.to_s.encap_by?('_')
      self.set_attribute(:id, method.to_s.uncapsulate('_'))
    else
      klass = method.to_s.gsub(/(?<=[^\_])\_(?=[^\_])/, '-').gsub('__', '_')
      self.append_attribute(:class, klass)
    end
    self._initialize(type, *args, &block)
    self
  else
    super
  end
end
respond_to_missing?(method, include_private = false) click to toggle source
Calls superclass method
# File lib/bblib/html/tag.rb, line 108
def respond_to_missing?(method, include_private = false)
  super || context && context.respond_to?(method)
end
simple_init(*args) click to toggle source
# File lib/bblib/html/tag.rb, line 84
def simple_init(*args)
  BBLib.named_args(*args).each do |k, v|
    next if _attrs.include?(k)
    self.attributes[k] = v
  end
end
simple_init_block_result(value) click to toggle source
# File lib/bblib/html/tag.rb, line 112
def simple_init_block_result(value)
  return false unless value && content.nil? && !value.is_a?(Tag)
  self.content = value.to_s
end