Module: Haml::Util
Defined Under Namespace
Classes: StaticConditionalContext
Constant Summary
collapse
- RUBY_VERSION =
::RUBY_VERSION.split(".").map {|s| s.to_i}
Instance Method Summary
collapse
Instance Method Details
#def_static_method(klass, name, args, *vars) ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/haml/util.rb', line 77
def def_static_method(klass, name, args, *vars)
erb = vars.pop
powerset(vars).each do |set|
context = StaticConditionalContext.new(set).instance_eval {binding}
klass.class_eval(<<METHOD)
def #{static_method_name(name, *vars.map {|v| set.include?(v)})}(#{args.join(', ')})
#{ERB.new(erb).result(context)}
end
METHOD
end
end
|
#enum_with_index(enum) ⇒ Object
62
63
64
|
# File 'lib/haml/util.rb', line 62
def enum_with_index(enum)
ruby1_8? ? enum.enum_with_index : enum.each_with_index
end
|
#has?(attr, klass, method) ⇒ Boolean
58
59
60
|
# File 'lib/haml/util.rb', line 58
def has?(attr, klass, method)
klass.send("#{attr}s").include?(ruby1_8? ? method.to_s : method.to_sym)
end
|
#map_hash(hash, &block) ⇒ Object
28
29
30
|
# File 'lib/haml/util.rb', line 28
def map_hash(hash, &block)
to_hash(hash.map(&block))
end
|
#map_keys(hash) ⇒ Object
20
21
22
|
# File 'lib/haml/util.rb', line 20
def map_keys(hash)
to_hash(hash.map {|k, v| [yield(k), v]})
end
|
#map_vals(hash) ⇒ Object
24
25
26
|
# File 'lib/haml/util.rb', line 24
def map_vals(hash)
to_hash(hash.map {|k, v| [k, yield(v)]})
end
|
#merge_adjacent_strings(enum) ⇒ Object
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/haml/util.rb', line 43
def merge_adjacent_strings(enum)
e = enum.inject([]) do |a, e|
if e.is_a?(String) && a.last.is_a?(String)
a.last << e
else
a << e
end
a
end
end
|
#powerset(arr) ⇒ Object
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/haml/util.rb', line 32
def powerset(arr)
arr.inject([Set.new].to_set) do |powerset, el|
new_powerset = Set.new
powerset.each do |subset|
new_powerset << subset
new_powerset << subset + [el]
end
new_powerset
end
end
|
#scope(file) ⇒ Object
Returns the path of file relative to the Haml root.
12
13
14
|
# File 'lib/haml/util.rb', line 12
def scope(file)
File.expand_path File.join(File.dirname(__FILE__), '..', '..', file)
end
|
#static_method_name(name, *vars) ⇒ Object
89
90
91
|
# File 'lib/haml/util.rb', line 89
def static_method_name(name, *vars)
"#{name}_#{vars.map {|v| !!v}.join('_')}"
end
|
#to_hash(arr) ⇒ Object
16
17
18
|
# File 'lib/haml/util.rb', line 16
def to_hash(arr)
arr.compact.inject({}) {|h, (k, v)| h[k] = v; h}
end
|