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/interpolation.rb,
lib/twitter_cldr/utils/regexp_sampler.rb
Defined Under Namespace
Modules: CodePoints, RegexpAst Classes: RangeSet, RegexpSampler, YAML
Constant Summary collapse
- HASH_INTERPOLATION_REGEXP =
Regexp.union( /%\{(\w+)\}/, /%<(\w+)>(.*?\d*\.?\d*[bBdiouxXeEfgGcps])/ )
- HASH_INTERPOLATION_WITH_ESCAPE_REGEXP =
Regexp.union( /%%/, HASH_INTERPOLATION_REGEXP )
Class Method Summary collapse
- .compute_cache_key(*pieces) ⇒ Object
- .deep_merge!(first, second) ⇒ Object
- .deep_merge_hash(first, second, &block) ⇒ Object
-
.deep_symbolize_keys(arg) ⇒ Object
adapted from: snippets.dzone.com/posts/show/11121 (first comment).
-
.interpolate(string, args) ⇒ Object
Uses
string
as a format specification and returns the result of applying it toargs
. - .traverse_hash(hash, path) ⇒ Object
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 |
.interpolate(string, args) ⇒ Object
Uses string
as a format specification and returns the result of applying it to args
.
There are three ways to use it:
-
Using a single argument or Array of arguments.
This is the default behaviour of the String#% method. See Kernel#sprintf for more details about the format specification.
Example:
TwitterCldr::Utils.interpolate('%d %s', [1, 'message']) # => "1 message"
-
Using a Hash as an argument and unformatted, named placeholders (Ruby 1.9 syntax).
When you pass a Hash as an argument and specify placeholders with %foo it will interpret the hash values as named arguments.
Example:
TwitterCldr::Utils.interpolate('%{firstname}, %{lastname}', :firstname => 'Masao', :lastname => 'Mutoh') # => "Masao Mutoh"
-
Using a Hash as an argument and formatted, named placeholders (Ruby 1.9 syntax).
When you pass a Hash as an argument and specify placeholders with %<foo>d it will interpret the hash values as named arguments and format the value according to the formatting instruction appended to the closing >.
Example:
TwitterCldr::Utils.interpolate('%<integer>d, %<float>.1f', :integer => 10, :float => 43.4) # => "10, 43.3"
An exception can be thrown in two cases when Ruby 1.9 interpolation syntax is used:
-
ArgumentError is thrown if Ruby 1.9. interpolation syntax is used in
string
, butargs
is not a Hash; -
KeyError is thrown if the value for one of the placeholders in
string
is missing inargs
hash.
78 79 80 |
# File 'lib/twitter_cldr/utils/interpolation.rb', line 78 def interpolate(string, args) string =~ HASH_INTERPOLATION_REGEXP ? interpolate_hash(string, args) : interpolate_value_or_array(string, args) 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 |