Module: UmbrellioUtils::Formatting

Extended by:
Formatting
Included in:
Formatting
Defined in:
lib/umbrellio_utils/formatting.rb

Instance Method Summary collapse

Instance Method Details

#cache_key(*parts) ⇒ Object



42
43
44
# File 'lib/umbrellio_utils/formatting.rb', line 42

def cache_key(*parts)
  parts.flatten.compact.join("-")
end

#deeply_expand_hash(hash, **expand_hash_options) ⇒ Hash

Expands a nested hash whose keys contain the path.

Parameters:

  • hash (Hash)

    hash which you want to expand

  • **expand_hash_options (Hash)

    options, that the #expand_hash method accepts

Returns:

  • (Hash)

    expanded hash



99
100
101
102
103
104
105
106
107
# 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) ⇒ Object



56
57
58
# 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) ⇒ Hash

Expands a hash whose keys contain the path.

Parameters:

  • hash (Hash)

    hash which you want to expand

  • delimiter (String) (defaults to: ".")

    separator which is used in the value of the keys

  • key_converter (Proc, Lambda, Symbol) (defaults to: :to_sym)

    converter for key’s value. Defaults to :to_sym

Returns:

  • (Hash)

    expanded hash



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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) ⇒ Object



50
51
52
53
54
# 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) ⇒ Object



11
12
13
14
15
16
# 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) ⇒ Object



7
8
9
# File 'lib/umbrellio_utils/formatting.rb', line 7

def pluralize(symbol)
  symbol.to_s.pluralize.to_sym
end

#render_money(money) ⇒ Object



46
47
48
# File 'lib/umbrellio_utils/formatting.rb', line 46

def render_money(money)
  "#{money.round} #{money.currency}"
end

#to_date_part_string(part) ⇒ Object



60
61
62
# File 'lib/umbrellio_utils/formatting.rb', line 60

def to_date_part_string(part)
  format("%<part>02d", part:)
end

#to_query(hash, namespace = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
# 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) ⇒ Object



18
19
20
21
22
23
24
# 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) ⇒ Object



36
37
38
39
40
# File 'lib/umbrellio_utils/formatting.rb', line 36

def uncapitalize_string(string)
  string = string.dup
  string[0] = string[0].downcase
  string
end