Sha256: d3c331c018f0e6088f23007ce2724ae1c97f0b65c48aeeaf33a8e40d4a679a2d

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

module TwitterBootstrapMarkup
  class Tag
    include Tooltip
    include Popover
    include SidePosition

    attr_reader :name
    attr_reader :attributes
    attr_reader :children

    def self.inline(name, attributes={})
      Tag.new(name, attributes)
    end

    def self.block(*args, &block)
      if block_given?
        Tag.new(*args, &block)
      else
        Tag.new(*args) {}
      end
    end

    def initialize(*args, &block)
      @name = args.shift
      content = args.shift unless args.first.is_a?(Hash)
      @attributes = args.shift || {}
      @children = []
      @is_block = content || block_given?
      append content if content
      evaluate(&block) if block_given?
    end

    def append(element=nil, &block)
      element = evaluate(&block) if block_given?
      @children << element
      element
    end

    def prepend(element)
      @children.insert 0, element
    end

    def to_s
      attributes_markup = attributes.empty? ? '' : " #{attributes.map{|key, value| "#{key}#{value ? "=\"#{value}\"" : ''}"}.join(' ')}"
      if @is_block
        "<#{name}#{attributes_markup}>#{children.map(&:to_s).join}</#{name}>"
      else
        "<#{name}#{attributes_markup}>"
      end
    end

    private

    def evaluate(&block)
      if block.arity == 0
        element = instance_eval(&block)
      else
        element = block.call(self)
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
twitter_bootstrap_markup-0.0.3 lib/twitter_bootstrap_markup/tag.rb