Sha256: aa65cccb49289b035d978ce4905673b3422cfff322efdb1dac3b591ce8c37f9d

Contents?: true

Size: 858 Bytes

Versions: 3

Compression:

Stored size: 858 Bytes

Contents

module Arbo
  module HTML

    class Attributes < Hash

      def to_s
        flatten_hash.map do |name, value|
          next if value_empty?(value)
          "#{html_escape(name)}=\"#{html_escape(value)}\""
        end.compact.join ' '
      end

      def any?
        super{ |k,v| !value_empty?(v) }
      end

      protected

      def flatten_hash(hash = self, old_path = [], accumulator = {})
        hash.each do |key, value|
          path = old_path + [key]
          if value.is_a? Hash
            flatten_hash(value, path, accumulator)
          else
            accumulator[path.join('-')] = value
          end
        end
        accumulator
      end

      def value_empty?(value)
        value.respond_to?(:empty?) ? value.empty? : !value
      end

      def html_escape(s)
        ERB::Util.html_escape(s)
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
arbo-1.3.1 lib/arbo/html/attributes.rb
arbo-1.3.0 lib/arbo/html/attributes.rb
arbo-1.2.0 lib/arbo/html/attributes.rb