Sha256: 62de7d5617bdde6750a99a54b2f61e61e299e1de969450f1d80e324045432ae2

Contents?: true

Size: 684 Bytes

Versions: 4

Compression:

Stored size: 684 Bytes

Contents

# frozen_string_literal: true
module Arbre
  module HTML

    class Attributes < Hash
      def to_s
        flatten_hash.compact.map do |name, value|
          "#{html_escape(name)}=\"#{html_escape(value)}\""
        end.join ' '
      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 html_escape(s)
        ERB::Util.html_escape(s)
      end
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
arbre-2.2.0 lib/arbre/html/attributes.rb
arbre-2.1.0 lib/arbre/html/attributes.rb
arbre-2.0.2 lib/arbre/html/attributes.rb
arbre-2.0.1 lib/arbre/html/attributes.rb