Sha256: 844bb53bbda9a85ea8d23164b5e0cf3b26c4d9ca642eb3aa83d54e870306e19c

Contents?: true

Size: 1.83 KB

Versions: 4

Compression:

Stored size: 1.83 KB

Contents

class Grover
  #
  # Utility class for Grover helper methods
  #
  class Utils
    ACRONYMS = {
      'css' => 'CSS'
    }.freeze
    private_constant :ACRONYMS

    #
    # Removes leading/trailing whitespaces and squishes inner whitespace with a single space
    #
    # N.B. whitespace includes all 'blank' characters as well as newlines/carriage returns etc.
    #
    def self.squish(string)
      string.
        gsub(/\A[[:space:]]+/, '').
        gsub(/[[:space:]]+\z/, '').
        gsub(/[[:space:]]+/, ' ')
    end

    #
    # Remove minimum spaces from the front of all lines within a string
    #
    # Based on active_support/core_ext/string/strip.rb
    #
    def self.strip_heredoc(string, inline: false)
      string = string.gsub(/^#{string.scan(/^[ \t]*(?=\S)/).min}/, ''.freeze)
      inline ? string.delete("\n") : string
    end

    #
    # Assign value to a hash using an array of keys to traverse
    #
    def self.deep_assign(hash, keys, value)
      if keys.length == 1
        hash[keys.first] = value
      else
        key = keys.shift
        hash[key] ||= {}
        deep_assign hash[key], keys, value
      end
    end

    #
    # Recursively normalizes hash objects with camelized string keys
    #
    def self.normalize_object(object)
      if object.is_a? Hash
        object.each_with_object({}) do |(k, v), acc|
          acc[normalize_key(k)] = normalize_object(v)
        end
      else
        object
      end
    end

    #
    # Normalizes hash keys into camelized strings, including up-casing known acronyms
    #
    # Regex sourced from ActiveSupport camelize
    #
    def self.normalize_key(key)
      key.to_s.downcase.gsub(%r{(?:_|(/))([a-z\d]*)}) do
        "#{Regexp.last_match(1)}#{ACRONYMS[Regexp.last_match(2)] || Regexp.last_match(2).capitalize}"
      end
    end
    private_class_method :normalize_key
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
grover-0.4.4 lib/grover/utils.rb
grover-0.4.3 lib/grover/utils.rb
grover-0.4.2 lib/grover/utils.rb
grover-0.4.1 lib/grover/utils.rb