Module: TwitterCldr::Utils

Defined in:
lib/twitter_cldr/utils.rb,
lib/twitter_cldr/utils/yaml.rb,
lib/twitter_cldr/utils/range_set.rb,
lib/twitter_cldr/utils/regexp_ast.rb,
lib/twitter_cldr/utils/code_points.rb,
lib/twitter_cldr/utils/regexp_sampler.rb

Defined Under Namespace

Modules: CodePoints, RegexpAst Classes: RangeSet, RegexpSampler, YAML

Class Method Summary collapse

Class Method Details

.compute_cache_key(*pieces) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/twitter_cldr/utils.rb', line 55

def compute_cache_key(*pieces)
  if pieces && pieces.size > 0
    pieces.join("|").hash
  else
    0
  end
end

.deep_merge!(first, second) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/twitter_cldr/utils.rb', line 29

def deep_merge!(first, second)
  if first.is_a?(Hash) && second.is_a?(Hash)
    second.each { |key, val| first[key] = deep_merge!(first[key], val) }
  elsif first.is_a?(Array) && second.is_a?(Array)
    second.each_with_index { |elem, index| first[index] = deep_merge!(first[index], elem) }
  else
    return second
  end
  first
end

.deep_merge_hash(first, second, &block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/twitter_cldr/utils.rb', line 40

def deep_merge_hash(first, second, &block)
  target = first.dup

  second.keys.each do |key|
    if second[key].is_a?(Hash) && first[key].is_a?(Hash)
      target[key] = deep_merge_hash(target[key], second[key], &block)
      next
    end

    target[key] = block_given? ? yield(first[key], second[key]) : second[key]
  end

  target
end

.deep_symbolize_keys(arg) ⇒ Object

adapted from: snippets.dzone.com/posts/show/11121 (first comment)



18
19
20
21
22
23
24
25
26
27
# File 'lib/twitter_cldr/utils.rb', line 18

def deep_symbolize_keys(arg)
  case arg
    when Array
      arg.map { |elem| deep_symbolize_keys(elem) }
    when Hash
      Hash[arg.map { |k, v| [k.is_a?(String) ? k.to_sym : k, deep_symbolize_keys(v)] }]
    else
      arg
  end
end

.traverse_hash(hash, path) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/twitter_cldr/utils.rb', line 63

def traverse_hash(hash, path)
  return if path.empty?

  path.inject(hash) do |current, key|
    current.is_a?(Hash) ? current[key] : return
  end
end