Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/kube/monkey_patches.rb

Overview

Class Method Summary collapse

Instance Method Summary collapse

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

.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