Sha256: 5a04a614f7778adcd0f79697d9d569bdf73fe4ad8952eba398fb3fde690da75a

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

module Navigator
  # Renders a HTML tag.
  class Tag
    attr_reader :name, :content

    def initialize name, content = nil, attributes = {}, activator = Navigator::TagActivator.new
      @name = name
      @content = content
      @attributes = attributes.with_indifferent_access
      @activator = activator
    end

    def prefix
      ["<#{name}", format_attributes, '>'].compact * ''
    end

    def suffix
      "</#{name}>"
    end

    def render
      [prefix, content, suffix].compact * ''
    end

    private

    attr_reader :attributes, :activator

    def expand_data_attributes! attrs
      if attrs.key? :data
        attrs.delete(:data).each { |key, value| attrs["data-#{key}"] = value }
      end
    end

    def concatenate_attributes attrs
      activator.activate(attrs).map { |key, value| %(#{key}="#{value}") } * ' '
    end

    def format_attributes
      attrs = attributes.clone
      expand_data_attributes! attrs
      attrs = concatenate_attributes attrs
      attrs.empty? ? nil : " #{attrs}"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
navigator-0.9.1 lib/navigator/tag.rb
navigator-0.9.0 lib/navigator/tag.rb