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
-
.deep_freeze(hash) ⇒ Hash
Freeze all the way down.
-
.deep_merge(to, from) ⇒ Hash
Merge all the way down.
-
.deep_stringify_keys(hash) ⇒ Hash
Transform keys all the way down.
-
.interpolate(string, options = HASH) ⇒ String
Format values into a string, conditionally checking the string and options before interpolating.
-
.interpolate!(string, options) ⇒ String
Format values into a string unconditionally.
-
.interpolation_string?(string) ⇒ Boolean
Determine whether a string includes any interpolation placeholders e.g.
-
.string_from_numeric(numeric) ⇒ String
Convert a Numeric to a string, particularly formatting BigDecimals to a Float-like string representation.
Class Method Details
.deep_freeze(hash) ⇒ Hash
Freeze all the way down.
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.
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.
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.
64 65 66 67 68 69 |
# File 'lib/tater/utils.rb', line 64 def self.interpolate(string, = HASH) return string if .empty? return string unless interpolation_string?(string) interpolate!(string, ) end |
.interpolate!(string, options) ⇒ String
Format values into a string unconditionally.
79 80 81 |
# File 'lib/tater/utils.rb', line 79 def self.interpolate!(string, ) format(string, ) end |
.interpolation_string?(string) ⇒ Boolean
Determine whether a string includes any interpolation placeholders e.g. “%{” or “%<”
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.
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 |