Module: Tater::Utils

Defined in:
lib/tater/utils.rb

Overview

Utility methods that require no state.

Constant Summary collapse

HASH =
{}.freeze
FORMAT_CURLY =
'%{'
FORMAT_NAMED =
'%<'

Class Method Summary collapse

Class Method Details

.deep_freeze(hash) ⇒ Hash

Freeze all the way down.

Parameters:

  • hash (Hash)

Returns:

  • (Hash)


45
46
47
48
49
50
51
52
53
# File 'lib/tater/utils.rb', line 45

def self.deep_freeze(hash)
  hash.transform_keys(&:freeze).transform_values do |value|
    if value.is_a?(Hash)
      Utils.deep_freeze(value)
    else
      value.freeze
    end
  end.freeze
end

.deep_merge(to, from) ⇒ Hash

Merge all the way down.

Parameters:

  • to (Hash)

    The target Hash to merge into.

  • from (Hash)

    The Hash to copy values from.

Returns:

  • (Hash)


16
17
18
19
20
21
22
23
24
# File 'lib/tater/utils.rb', line 16

def self.deep_merge(to, from)
  to.merge(from) do |_key, left, right|
    if left.is_a?(Hash) && right.is_a?(Hash)
      Utils.deep_merge(left, right)
    else
      right
    end
  end
end

.deep_stringify_keys(hash) ⇒ Hash

Transform keys all the way down.

Parameters:

  • hash (Hash)

    The Hash to stringify keys for.

Returns:

  • (Hash)


31
32
33
34
35
36
37
38
39
# File 'lib/tater/utils.rb', line 31

def self.deep_stringify_keys(hash)
  hash.transform_keys(&:to_s).transform_values do |value|
    if value.is_a?(Hash)
      Utils.deep_stringify_keys(value)
    else
      value
    end
  end
end

.interpolate(string, options = HASH) ⇒ String

Format values into a string, conditionally checking the string and options before interpolating.

Parameters:

  • string (String)

    The target string to interpolate into.

  • options (Hash) (defaults to: HASH)

    The values to interpolate into the target string.

Returns:

  • (String)


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

def self.interpolate(string, options = HASH)
  return string if options.empty?
  return string unless interpolation_string?(string)

  interpolate!(string, options)
end

.interpolate!(string, options) ⇒ String

Format values into a string unconditionally.

Parameters:

  • string (String)

    The target string to interpolate into.

  • options (Hash)

    The values to interpolate into the target string.

Returns:

  • (String)


79
80
81
# File 'lib/tater/utils.rb', line 79

def self.interpolate!(string, options)
  format(string, options)
end

.interpolation_string?(string) ⇒ Boolean

Determine whether a string includes any interpolation placeholders e.g. “%{” or “%<”

Parameters:

  • string (String)

Returns:

  • (Boolean)


88
89
90
# File 'lib/tater/utils.rb', line 88

def self.interpolation_string?(string)
  string.include?(FORMAT_CURLY) || string.include?(FORMAT_NAMED)
end

.string_from_numeric(numeric) ⇒ String

Convert a Numeric to a string, particularly formatting BigDecimals to a Float-like string representation.

Parameters:

  • numeric (Numeric)

Returns:

  • (String)


98
99
100
101
102
103
104
# File 'lib/tater/utils.rb', line 98

def self.string_from_numeric(numeric)
  if numeric.is_a?(BigDecimal)
    numeric.to_s('F')
  else
    numeric.to_s
  end
end