Module: Dom

Defined in:
lib/dom.rb

Constant Summary collapse

Coder =
HTMLEntities.new
Indent =
"  "

Class Method Summary collapse

Class Method Details

.attr(h) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dom.rb', line 28

def self.attr h
	" ".concat(h.map do
		|k, v|
		v = case v
		when nil then next
		when false then "none".freeze
		when true then "".freeze
		else v
		end
		"%s=\"%s\"".freeze % [hyphenize(k), v]
	end.compact.join" ".freeze)
end

.compactObject



12
# File 'lib/dom.rb', line 12

def self.compact; singleton_class.class_eval{alias join join_compact} end

.hyphenize(sym) ⇒ Object



41
# File 'lib/dom.rb', line 41

def self.hyphenize sym; sym.to_s.tr("_".freeze, "-".freeze) end

.join_compact(a, tag) ⇒ Object



15
16
17
# File 'lib/dom.rb', line 15

def self.join_compact a, tag
	a.map{|e| e.to_s.escape_html(tag)}.join
end

.join_nested(a, tag) ⇒ Object



18
19
20
21
22
# File 'lib/dom.rb', line 18

def self.join_nested a, tag
	a.map{|e| e.to_s.escape_html(tag).concat($/)}
	.join.gsub(/^/o, Indent)
	.prepend($/)
end

.join_pre(a, tag) ⇒ Object



23
24
25
26
27
# File 'lib/dom.rb', line 23

def self.join_pre a, tag
	a.map{|e| e.to_s.escape_html(tag).prepend("-->".freeze).concat("<!--#$/".freeze)}
	.join.gsub(/^/o, Indent)
	.prepend("<!--#$/".freeze).concat("-->".freeze)
end

.json_attr(h) ⇒ Object



40
# File 'lib/dom.rb', line 40

def self.json_attr h; h.map{|k, v| [hyphenize(k), v]}.to_h end

.nestedObject



13
# File 'lib/dom.rb', line 13

def self.nested; singleton_class.class_eval{alias join join_nested} end

.preObject



14
# File 'lib/dom.rb', line 14

def self.pre; singleton_class.class_eval{alias join join_pre} end