Sha256: 31b33635b13dd75a06e32d41d634edcc0f510872a7c7cb721e6778684cd84f68

Contents?: true

Size: 872 Bytes

Versions: 3

Compression:

Stored size: 872 Bytes

Contents

require 'delegate'

module WrapIt
  #
  # Provides hash-like access to HTML data.
  #
  # @author Alexey Ovchinnikov <alexiss@cybernetlab.ru>
  #
  class HTMLData < DelegateClass(Hash)
    #
    # Sanitizes html data
    #
    # @overload sanitize(values = {})
    #   @param  values [Hash] hash to sanitize
    #
    # @return [Hash] sanitized hash
    def self.sanitize(**values)
      Hash[values
        .map do |k, v|
          k = k.to_s
          if k.include?('-')
            k, n = k.split(/-/, 2)
            v = sanitize(n.to_sym => v)
          else
            k = k.downcase.gsub(/[^a-z0-9_]+/, '').gsub(/\A\d+/, '')
            v = v.is_a?(Hash) ? sanitize(v) : v.to_s
          end
          k.empty? ? nil : [k.to_sym, v]
        end
        .compact
      ]
    end

    def initialize(**value)
      super(HTMLData.sanitize(**value))
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
wrap_it-1.0.2 lib/wrap_it/html_data.rb
wrap_it-1.0.1 lib/wrap_it/html_data.rb
wrap_it-1.0.0 lib/wrap_it/html_data.rb