Module: Toolchain::Attributes::Helpers
Instance Method Summary collapse
- #each_key(klass) {|Symbol| ... } ⇒ Object
-
#invalid_value?(value, *types) ⇒ Boolean
True if the provided value doesn’t match any of the provided type classes.
-
#stringify_keys(value) ⇒ Hash
Converts all keys to String-type, including nested hashes.
-
#symbolize_keys(value) ⇒ Hash
Converts all keys to Symbol-type, including nested hashes.
Instance Method Details
#each_key(klass) {|Symbol| ... } ⇒ Object
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.
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.
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.
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 |