Sha256: e356020263d7874e906ff936b5939cda6deeb72260dbfc07033a8ba5a1bd26f3

Contents?: true

Size: 1.09 KB

Versions: 4

Compression:

Stored size: 1.09 KB

Contents

module Navigator
  # Represents a HTML tag.
  class Tag
    attr_accessor :name, :content, :attributes

    # Initalizes the HTML tag.
    # ==== Parameters
    # * +name+ - Required. The tag name.
    # * +content+ - Optional. The tag content. Default: nil
    # * +attributes+ - Optional. The tag attributes. Default: {}
    def initialize name, content = nil, attributes = {}
      @name, @content, @attributes = name, content, attributes
    end

    # Answers the attributes as fully formed HTML attributes for the tag.
    def html_attributes
      @attributes.map {|key, value| "#{key}=\"#{value}\""}.compact * ' '
    end

    # Answers the HTML tag prefix (i.e. the opening tag). Example: <li>.
    def prefix
      attrs = html_attributes.empty? ? nil : " #{html_attributes}"
      ["<#{@name}", attrs, '>'].compact * ''
    end

    # Answers the HTML tag suffix (i.e. the closing tag). Example: </li>.
    def suffix
      "</#{@name}>"
    end

    # Renders the fully assembled HTML tag with attributes and content (if apt).
    def render
      [prefix, @content, suffix].compact * ''
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
navigator-0.6.0 lib/navigator/tag.rb
navigator-0.5.0 lib/navigator/tag.rb
navigator-0.4.0 lib/navigator/tag.rb
navigator-0.3.0 lib/navigator/tag.rb