module UmbrellioUtils::Formatting
Public Instance Methods
cache_key(*parts)
click to toggle source
# File lib/umbrellio_utils/formatting.rb, line 42 def cache_key(*parts) parts.flatten.compact.join("-") end
deeply_expand_hash(hash, **expand_hash_options)
click to toggle source
Expands a nested hash whose keys contain the path.
@param hash [Hash] hash which you want to expand @param **expand_hash_options [Hash] options, that the
{#expand_hash} method accepts
@return [Hash] expanded hash
# File lib/umbrellio_utils/formatting.rb, line 99 def deeply_expand_hash(hash, **expand_hash_options) transformed_hash = hash.transform_values do |value| next deeply_expand_hash(value, **expand_hash_options) if value.is_a?(Hash) value end expand_hash(transformed_hash, **expand_hash_options) end
encode_key(key)
click to toggle source
# File lib/umbrellio_utils/formatting.rb, line 56 def encode_key(key) Base64.strict_encode64(key.to_der) end
expand_hash(hash, delimiter: ".", key_converter: :to_sym)
click to toggle source
Expands a hash whose keys contain the path.
@param hash [Hash] hash which you want to expand @param delimiter [String] separator which is used in the value of the keys @param key_converter [Proc, Lambda, Symbol] converter for key's value.
Defaults to :to_sym
@return [Hash] expanded hash
# File lib/umbrellio_utils/formatting.rb, line 74 def expand_hash(hash, delimiter: ".", key_converter: :to_sym) result = hash.each_with_object(Misc.build_infinite_hash) do |entry, memo| path, value = entry *path_to_key, key = path.to_s.split(delimiter).map(&key_converter) if path_to_key.empty? memo[key] = value else resolved_hash = memo.dig(*path_to_key) resolved_hash[key] = value end end Misc.reset_defaults_for_hash(result) end
match_or_nil(str, regex)
click to toggle source
# File lib/umbrellio_utils/formatting.rb, line 50 def match_or_nil(str, regex) return if str.blank? return unless str.match?(regex) str end
merge_query_into_url(url, query)
click to toggle source
# File lib/umbrellio_utils/formatting.rb, line 11 def merge_query_into_url(url, query) uri = Addressable::URI.parse(url) url = uri.omit(:query) original_query = uri.query_values || {} to_url(url, **original_query, **query.stringify_keys) end
pluralize(symbol)
click to toggle source
# File lib/umbrellio_utils/formatting.rb, line 7 def pluralize(symbol) symbol.to_s.pluralize.to_sym end
render_money(money)
click to toggle source
# File lib/umbrellio_utils/formatting.rb, line 46 def render_money(money) "#{money.round} #{money.currency}" end
to_date_part_string(part)
click to toggle source
# File lib/umbrellio_utils/formatting.rb, line 60 def to_date_part_string(part) format("%<part>02d", part: part) end
to_query(hash, namespace = nil)
click to toggle source
# File lib/umbrellio_utils/formatting.rb, line 26 def to_query(hash, namespace = nil) pairs = hash.map do |key, value| key = CGI.escape(key.to_s) ns = namespace ? "#{namespace}[#{key}]" : key value.is_a?(Hash) ? to_query(value, ns) : "#{CGI.escape(ns)}=#{CGI.escape(value.to_s)}" end pairs.join("&") end
to_url(*parts)
click to toggle source
# File lib/umbrellio_utils/formatting.rb, line 18 def to_url(*parts) params = parts.select { |x| x.is_a?(Hash) } parts -= params params = params.reduce(&:merge) query = to_query(params).presence if params.present? [File.join(*parts), query].compact.join("?") end
uncapitalize_string(string)
click to toggle source
# File lib/umbrellio_utils/formatting.rb, line 36 def uncapitalize_string(string) string = string.dup string[0] = string[0].downcase string end