Module: Toolchain::Attributes::Helpers

Extended by:
Helpers
Included in:
Helpers
Defined in:
lib/toolchain/attributes/helpers.rb

Instance Method Summary collapse

Instance Method Details

#each_key(klass) {|Symbol| ... } ⇒ Object

Parameters:

  • klass (Class)

Yields:

  • (Symbol)

    Each defined attribute (key) name.



8
9
10
11
12
13
# 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) ⇒ Boolean

Returns true if the provided value doesn’t match any of the provided type classes.

Parameters:

  • value (Object)
  • types (Array<Class>)

Returns:

  • (Boolean)

    true if the provided value doesn’t match any of the provided type classes.



21
22
23
24
25
26
27
28
29
30
31
# 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) ⇒ Hash

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

Parameters:

  • value (Hash)

Returns:

  • (Hash)


49
50
51
# 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) ⇒ Hash

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

Parameters:

  • value (Hash)

Returns:

  • (Hash)


39
40
41
# File 'lib/toolchain/attributes/helpers.rb', line 39

def symbolize_keys(value)
  deep_transform_keys(value) { |key| key.to_sym rescue key }
end