Module: Haml::Util

Extended by:
Util
Included in:
Buffer, Precompiler, Util, Sass::Engine, Sass::Script::Color
Defined in:
lib/haml/util.rb

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



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/haml/util.rb', line 61

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



46
47
48
# File 'lib/haml/util.rb', line 46

def enum_with_index(enum)
  ruby1_8? ? enum.enum_with_index : enum.each_with_index
end

#has?(attr, klass, method) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/haml/util.rb', line 42

def has?(attr, klass, method)
  klass.send("#{attr}s").include?(ruby1_8? ? method.to_s : method.to_sym)
end

#map_hash(hash, &block) ⇒ Object



23
24
25
# File 'lib/haml/util.rb', line 23

def map_hash(hash, &block)
  to_hash(hash.map(&block))
end

#map_keys(hash) ⇒ Object



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

def map_keys(hash)
  to_hash(hash.map {|k, v| [yield(k), v]})
end

#map_vals(hash) ⇒ Object



19
20
21
# File 'lib/haml/util.rb', line 19

def map_vals(hash)
  to_hash(hash.map {|k, v| [k, yield(v)]})
end

#powerset(arr) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/haml/util.rb', line 27

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

#ruby1_8?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/haml/util.rb', line 38

def ruby1_8?
  Haml::Util::RUBY_VERSION[0] == 1 && Haml::Util::RUBY_VERSION[1] < 9
end

#static_method_name(name, *vars) ⇒ Object



73
74
75
# File 'lib/haml/util.rb', line 73

def static_method_name(name, *vars)
  "#{name}_#{vars.map {|v| !!v}.join('_')}"
end

#to_hash(arr) ⇒ Object



11
12
13
# File 'lib/haml/util.rb', line 11

def to_hash(arr)
  arr.compact.inject({}) {|h, (k, v)| h[k] = v; h}
end