module Haml::Util
def assert_html_safe!(text)
-
text
(Object
) --
def assert_html_safe!(text) return unless rails_xss_safe? && text && !text.to_s.html_safe? raise Haml::Error.new("Expected #{text.inspect} to be HTML-safe.") end
def av_template_class(name)
-
name
(#to_s
) -- The name of the class to get.
def av_template_class(name) return ActionView.const_get("Template#{name}") if ActionView.const_defined?("Template#{name}") return ActionView::Template.const_get(name.to_s) end
def def_static_method(klass, name, args, *vars)
-
erb
(String
) -- The template for the method code -
vars
(Array
) -- The names of the static boolean variables -
args
(Array
) -- The names of the arguments to the defined methods -
name
(#to_s
) -- The (base) name of the static method -
klass
(Module
) -- The class on which to define the static method
Overloads:
-
def_static_method(klass, name, args, *vars, erb)
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) #{static_method_name(name, *vars.map {|v| set.include?(v)})}(#{args.join(', ')}) ERB.new(erb).result(context)} OD end end
def enum_with_index(enum)
-
(Enumerator)
- The with-index enumerator
Parameters:
-
enum
(Enumerable
) -- The enumerable to get the enumerator for
def enum_with_index(enum) ruby1_8? ? enum.enum_with_index : enum.each_with_index end
def has?(attr, klass, method)
-
(Boolean)
- Whether or not the given collection has the given method
Parameters:
-
method
(String, Symbol
) -- The name of the method do check for -
klass
(Module
) -- The class to check the methods of which to check -
attr
(#to_s
) -- The (singular) name of the method-collection method
def has?(attr, klass, method) klass.send("#{attr}s").include?(ruby1_8? ? method.to_s : method.to_sym) end
def map_hash(hash, &block)
- See: #map_vals -
See: #map_keys -
Returns:
-
(Hash)
- The mapped hash
Other tags:
- Yieldreturn: - The new value for the `[key, value]` pair
Other tags:
- Yieldparam: The - hash value
Yieldparam: The - hash key
Other tags:
- Yield: - A block in which the key-value pairs are transformed
Parameters:
-
hash
(Hash
) -- The hash to map
def map_hash(hash, &block) to_hash(hash.map(&block)) end
def map_keys(hash)
- See: #map_hash -
See: #map_vals -
Returns:
-
(Hash)
- The mapped hash
Other tags:
- Yieldreturn: - The new value for the key
Other tags:
- Yieldparam: key - The key that should be mapped
Other tags:
- Yield: - A block in which the keys are transformed
Parameters:
-
hash
(Hash
) -- The hash to map
def map_keys(hash) to_hash(hash.map {|k, v| [yield(k), v]}) end
def map_vals(hash)
- See: #map_hash -
See: #map_keys -
Returns:
-
(Hash)
- The mapped hash
Other tags:
- Yieldreturn: - The new value for the value
Other tags:
- Yieldparam: value - The value that should be mapped
Other tags:
- Yield: - A block in which the values are transformed
Parameters:
-
hash
(Hash
) -- The hash to map
def map_vals(hash) to_hash(hash.map {|k, v| [k, yield(v)]}) end
def merge_adjacent_strings(enum)
-
(Array)
- The enumerable with strings merged
Parameters:
-
enum
(Enumerable
) --
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
def powerset(arr)
-
(Set
- The subsets of `arr`)
Parameters:
-
arr
(Enumerable
) --
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
def rails_root
-
(String, nil)
-
def rails_root return Rails.root.to_s if defined?(Rails.root) return RAILS_ROOT.to_s if defined?(RAILS_ROOT) return nil end
def rails_xss_safe?
-
(Boolean)
-
def rails_xss_safe? false end
def ruby1_8?
-
(Boolean)
-
def ruby1_8? Haml::Util::RUBY_VERSION[0] == 1 && Haml::Util::RUBY_VERSION[1] < 9 end
def scope(file)
-
(String)
- The filename relative to the the working directory
Parameters:
-
file
(String
) -- The filename relative to the Haml root
def scope(file) File.join(File.dirname(File.dirname(File.dirname(File.expand_path(__FILE__)))), file) end
def silence_warnings
- Yield: - A block in which no output will be printed to STDERR
def silence_warnings the_real_stderr, $stderr = $stderr, StringIO.new yield ensure $stderr = the_real_stderr end
def static_method_name(name, *vars)
-
(String)
- The real name of the static method
Parameters:
-
vars
(Array
) -- The static variable assignment -
name
(String
) -- The base name of the static method
def static_method_name(name, *vars) "#{name}_#{vars.map {|v| !!v}.join('_')}" end
def to_hash(arr)
-
(Hash)
- A hash
Parameters:
-
arr
(Array<(Object, Object)>
) -- An array of pairs
def to_hash(arr) arr.compact.inject({}) {|h, (k, v)| h[k] = v; h} end