module Toolchain::Attributes::Helpers

Public Instance Methods

each_key(klass) { |key| ... } click to toggle source

@param klass [Class]

@yield [Symbol] Each defined attribute (key) name.

# File lib/toolchain/attributes/helpers.rb, line 8
def each_key(klass)
  while ![Class, Module, Object, BasicObject, nil].include?(klass)
    klass.keys.each { |key| yield key }
    klass = klass.superclass
  end
end
invalid_value?(value, *types) click to toggle source

@param value [Object] @param types [Array<Class>]

@return [Boolean] true if the provided value doesn't

match any of the provided type classes.
# File lib/toolchain/attributes/helpers.rb, line 21
def invalid_value?(value, *types)
  value = value.call if value.kind_of?(Proc)

  return false if value.nil?

  types.flatten.each do |type|
    return false if value.kind_of?(type)
  end

  true
end
stringify_keys(value) click to toggle source

Converts all keys to String-type, including nested hashes.

@param value [Hash] @return [Hash]

# File lib/toolchain/attributes/helpers.rb, line 49
def stringify_keys(value)
  deep_transform_keys(value) { |key| key.to_s rescue key }
end
symbolize_keys(value) click to toggle source

Converts all keys to Symbol-type, including nested hashes.

@param value [Hash] @return [Hash]

# File lib/toolchain/attributes/helpers.rb, line 39
def symbolize_keys(value)
  deep_transform_keys(value) { |key| key.to_sym rescue key }
end

Private Instance Methods

deep_transform_keys(value) { |key| ... } click to toggle source

Recursive key transformation method.

@param value [Hash] @param block [Proc]

# File lib/toolchain/attributes/helpers.rb, line 60
def deep_transform_keys(value, &block)
  Hash.new.tap do |result|
    value.each do |key, value|
      result[yield(key)] =
        if value.kind_of?(Hash)
          deep_transform_keys(value, &block)
        else
          value
        end
    end
  end
end