Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/hoshi/monkey_patches.rb

Overview

TODO: When 1.9 goes away, this monkey-patch should be changed to refinements, or just moved into a private instance method for Hoshi::Tag. See: blog.headius.com/2012/11/refining-ruby.html

Instance Method Summary collapse

Instance Method Details

#to_html_options(double_quotes = true) ⇒ Object

Makes this hash fit to be put into a tag. 1, b: “two”, c: true, d: false.to_html_options # => ‘a=“one” b=“two” c’ Note that true and false are special-cased, true to allow for attributes that do not have a value and false for convenience, to allow for, e.g., inline conditionals.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/hoshi/monkey_patches.rb', line 11

def to_html_options double_quotes = true
	qchar = double_quotes ? '"' : "'"
	map { |k,v|
		if v == true # Intentional.
			k.to_s
		elsif v == false
			''
		else
			"#{k}=#{qchar}#{v.to_s.gsub(qchar, CGI.escapeHTML(qchar))}#{qchar}"
		end
	}.join(' ')
end