class HTML::AutoAttr

Attributes

hash[RW]
sorted[RW]

Public Class Methods

new( hash = {}, sorted = 0 ) click to toggle source

expects the following arguments

* hash
    Contains attribute keys and values

* sorted
    Boolean. Whether or not to render attributes in alphabetical order.
# File lib/HTML/AutoAttr.rb, line 14
def initialize( hash = {}, sorted = 0 )
    @hash   = hash
    @sorted = sorted
end

Public Instance Methods

key( key ) click to toggle source

expects one argument: the key to scrub

# File lib/HTML/AutoAttr.rb, line 43
def key( key )
    key = key.gsub( /\s+/, '' )
    key = key.gsub( /["'>=\/]/, '' )
    return key
end
rotate( array ) click to toggle source

expects one argument: the array to rotate

returns the first element before array is rotated

# File lib/HTML/AutoAttr.rb, line 60
def rotate( array )
    val = array.shift
    array.push( val )
    return val
end
stringify( hash ) click to toggle source

expects one argument: the hash to 'stringify'

# File lib/HTML/AutoAttr.rb, line 68
def stringify( hash )

    keys = @sorted ? hash.keys.sort : hash.keys
    vals = keys.map{ |key|
        val = ''
        if hash[key].kind_of?( Array ) 
            val = rotate( hash[key] )
        elsif hash[key].kind_of?( Hash )
            val = @sorted ? hash[key].keys.sort[0] : hash[key].keys[0]
        else
            val = hash[key]
        end
        "#{key}: #{val}"
    }
    return vals.join( '; ' ) + (vals.length ? ';' : '')

end
to_s() click to toggle source

expects no arguments, emits string containing rendered key/value pairs

# File lib/HTML/AutoAttr.rb, line 21
def to_s

    return '' unless @hash.kind_of?( Hash )
    keys = @sorted ? @hash.keys.sort : @hash.keys
    str  = ''
    seen = {}

    keys.each do |key|
        unless seen.has_key?( key )
            val = @hash[key]
            val = stringify( val )  if val.kind_of?( Hash )
            val = rotate( val )     if val.kind_of?( Array )
            str += sprintf( ' %s="%s"', key( key ), val( val ) )
        end
        seen[key] = 1
    end

    return str
end
val( val ) click to toggle source

expects one argument: the value to scrub

# File lib/HTML/AutoAttr.rb, line 50
def val( val )
    return '' if val.to_s.match( /^\s+$/ )
    val = val.to_s.gsub( /"/, '' )
    return val
end