In Files

Parent

Mab::Mixin::Tag

Attributes

_attributes[RW]
_block[RW]
_content[RW]
_context[R]
_has_content[RW]
_instance[R]
_name[RW]
_options[R]

Public Class Methods

new(name, options, context, instance = nil) click to toggle source
# File lib/mab/mixin.rb, line 10
def initialize(name, options, context, instance = nil)
  @_name = name
  @_options = options
  @_context = context
  @_instance = instance
  @_done = false

  @_content = nil
  @_has_content = nil

  @_attributes = {}

  @_pos = @_context.size
end

Public Instance Methods

_attrs_to_s() click to toggle source
# File lib/mab/mixin.rb, line 104
def _attrs_to_s
  @_attributes.inject("") do |res, (name, value)|
    if value
      value = (value == true) ? name : CGI.escapeHTML(value.to_s)
      res << " #{name}=\"#{value}\""
    end
    res
  end
end
_insert(*args, &blk) click to toggle source
# File lib/mab/mixin.rb, line 53
def _insert(*args, &blk)
  raise Error, "This tag is already closed" if @_done

  if !args.empty? && !args[0].is_a?(Hash)
    content = args.shift
    raise Error, "Tag doesn't allow content" if @_has_content == false
    @_has_content = true
  end

  if content
    @_content = CGI.escapeHTML(content.to_s)
    @_done = true
  end

  if !args.empty?
    _merge_attributes(*args)
    @_done = true
  end

  if block_given?
    raise Error, "Tag doesn't allow content" if @_has_content == false
    @_has_content = true
    @_block = blk
    @_done = true
  end

  if @_content && @_block
    raise Error, "Both content and _block is not allowed"
  end

  @_instance.mab_done(self) if @_done

  if @_block
    before = @_context.children
    res = @_block.call

    if before >= @_context.children
      @_content = res.to_s
    else
      # Turn the node into just an opening tag.
      @_has_content = false
      @_instance.mab_insert("</#{@_name}>")
    end
  end

  self
end
_merge_attributes(*args) click to toggle source
# File lib/mab/mixin.rb, line 31
def _merge_attributes(*args)
  args.each do |attrs|
    @_attributes.merge!(attrs)
  end
end
method_missing(name, *args, &blk) click to toggle source
# File lib/mab/mixin.rb, line 37
def method_missing(name, *args, &blk)
  name = name.to_s

  if name[-1] == !!
    @_attributes[:id] = name[0..-2]
  else
    if @_attributes.has_key?(:class)
      @_attributes[:class] += " #{name}"
    else
      @_attributes[:class] = name
    end
  end

  _insert(*args, &blk)
end
to_ary() click to toggle source
# File lib/mab/mixin.rb, line 101
def to_ary() nil end
to_s() click to toggle source
# File lib/mab/mixin.rb, line 114
def to_s
  if !@_context.joining? && @_context[@_pos]
    @_context[@_pos] = nil
    @_context.children -= 1
  end

  res = "<#{@_name}#{_attrs_to_s}"
  res << (@_options[:xml] && !@_block && !@_has_content ? ' />' : '>')
  res << "#{@_content}</#{@_name}>" if @_has_content
  res
end
to_str() click to toggle source
# File lib/mab/mixin.rb, line 102
def to_str() to_s end

[Validate]

Generated with the Darkfish Rdoc Generator 2.