Module: TwitterCldr::Utils

Included in:
Shared::PropertiesDatabase
Defined in:
lib/twitter_cldr/utils.rb,
lib/twitter_cldr/utils/trie.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,
lib/twitter_cldr/utils/script_detector.rb,
lib/twitter_cldr/utils/file_system_trie.rb

Defined Under Namespace

Modules: CodePoints, RegexpAst Classes: FileSystemTrie, RangeSet, RegexpSampler, ScriptDetectionResult, ScriptDetector, Trie, YAML

Class Method Summary collapse

Class Method Details

.compute_cache_key(*pieces) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/twitter_cldr/utils.rb', line 59

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

.deep_merge!(first, second) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/twitter_cldr/utils.rb', line 33

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/twitter_cldr/utils.rb', line 44

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)



22
23
24
25
26
27
28
29
30
31
# File 'lib/twitter_cldr/utils.rb', line 22

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, &block) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/twitter_cldr/utils.rb', line 67

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

  path.inject(hash) do |current, key|
    return unless current.is_a?(Hash)

    if block
      yield key, current[key]
    else
      current[key]
    end
  end
end