R18n::Utils

Constants

HTML_ENTRIES

Public Class Methods

deep_merge!(a, b) click to toggle source

Recursively hash merge.

# File lib/r18n-core/utils.rb, line 56
def self.deep_merge!(a, b)
  b.each_pair do |key, value|
    another = a[key]
    a[key] = if another.is_a?(Hash) && value.is_a?(Hash)
      deep_merge!(another, value)
    else
      value
    end
  end
  a
end
escape_html(content) click to toggle source

Escape HTML entries (<, >, &). Copy from HAML helper.

# File lib/r18n-core/utils.rb, line 36
def self.escape_html(content)
  if defined? ActiveSupport::SafeBuffer
    ActiveSupport::SafeBuffer.new + content
  else
    content.to_s.gsub(/[><&]/) { |s| HTML_ENTRIES[s] }
  end
end
hash_map(hash, &block) click to toggle source

Invokes block once for each key and value of hash. Creates a new hash with the keys and values returned by the block.

# File lib/r18n-core/utils.rb, line 46
def self.hash_map(hash, &block)
  result = {}
  hash.each_pair do |key, val|
    new_key, new_value = block.call(key, val)
    result[new_key] = new_value
  end
 result
end
to_date(time) click to toggle source

Convert Time to Date. Backport from Ruby 1.9.

# File lib/r18n-core/utils.rb, line 24
def self.to_date(time)
  if '1.8.' == RUBY_VERSION[0..3]
    d = Date.send(:civil_to_jd, time.year, time.mon, time.mday, Date::ITALY)
    Date.new!(Date.send(:jd_to_ajd, d, 0, 0), 0, Date::ITALY)
  else
    time.to_date
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.