Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/kube/monkey_patches.rb
Overview
Class Method Summary collapse
- ._deep_stringify_keys(obj) ⇒ Object
-
.vivify(&block) ⇒ Object
Build a Hash with autovivification via a block DSL.
Instance Method Summary collapse
- #method_missing(name, *args) ⇒ Object
-
#to_yaml ⇒ Object
Deep-stringify keys so symbol keys don’t leak into YAML as ‘:key:`.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/kube/monkey_patches.rb', line 3 def method_missing(name, *args) key = name.to_s if key.end_with?("=") self[key.chomp("=").to_sym] = args.first elsif key?(name.to_sym) self[name.to_sym] elsif key.start_with?("to_") super else self[name.to_sym] = {} end end |
Class Method Details
._deep_stringify_keys(obj) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/kube/monkey_patches.rb', line 42 def self._deep_stringify_keys(obj) case obj when Hash obj.each_with_object({}) do |(k, v), result| result[k.to_s] = _deep_stringify_keys(v) end when Array obj.map { |v| _deep_stringify_keys(v) } else obj end end |
.vivify(&block) ⇒ Object
Build a Hash with autovivification via a block DSL.
Hash.vivify {
.name = "web"
spec.replicas = 3
spec.selector.matchLabels.app = "web"
}
# => { metadata: { name: "web" }, spec: { replicas: 3, selector: { matchLabels: { app: "web" } } } }
33 34 35 |
# File 'lib/kube/monkey_patches.rb', line 33 def self.vivify(&block) new.tap { |h| h.instance_exec(&block) } end |
Instance Method Details
#to_yaml ⇒ Object
Deep-stringify keys so symbol keys don’t leak into YAML as ‘:key:`.
38 39 40 |
# File 'lib/kube/monkey_patches.rb', line 38 def to_yaml(*) Hash._deep_stringify_keys(self).then { |h| Psych.dump(h) } end |