Class: K8s::Resource

Inherits:
RecursiveOpenStruct
  • Object
show all
Extended by:
Forwardable
Includes:
Comparable
Defined in:
lib/k8s/resource.rb

Overview

generic untyped resource

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash, recurse_over_arrays: true, **options) ⇒ Resource

Returns a new instance of Resource.

Parameters:

  • attrs (Hash)


35
36
37
38
39
40
# File 'lib/k8s/resource.rb', line 35

def initialize(hash, recurse_over_arrays: true, **options)
  super(hash,
    recurse_over_arrays: recurse_over_arrays,
    **options
  )
end

Class Method Details

.from_file(filename) ⇒ K8s::Resource

Parameters:

  • filename (String)

    file path

Returns:



18
19
20
# File 'lib/k8s/resource.rb', line 18

def self.from_file(filename)
  new(YAML.load_file(filename))
end

.from_files(path) ⇒ Array<K8s::Resource>

Parameters:

  • filename (String)

    file path

Returns:



24
25
26
27
28
29
30
31
32
# File 'lib/k8s/resource.rb', line 24

def self.from_files(path)
  stat = File.stat(path)

  if stat.directory?
    Dir.glob("#{path}/*.{yml,yaml}").map{|path| self.load_files(path) }.flatten
  else
    ::YAML.load_stream(File.read(path), path).map{|doc| new(doc) }
  end
end

.from_json(data) ⇒ self

Parameters:

  • data (Hash)

Returns:

  • (self)


12
13
14
# File 'lib/k8s/resource.rb', line 12

def self.from_json(data)
  return new(data)
end

Instance Method Details

#<=>(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


49
50
51
# File 'lib/k8s/resource.rb', line 49

def <=>(other)
  to_hash <=> other.to_hash
end

#merge(attrs) ⇒ K8s::Resource

merge in fields

Parameters:

Returns:



57
58
59
60
61
62
63
64
65
# File 'lib/k8s/resource.rb', line 57

def merge(attrs)
  # deep clone of attrs
  h = to_hash

  # merge in-place
  h.deep_merge!(attrs.to_hash, overwrite_arrays: true)

  self.class.new(h)
end

#to_json(**options) ⇒ String

Returns:

  • (String)


43
44
45
# File 'lib/k8s/resource.rb', line 43

def to_json(**options)
  to_hash.to_json(**options)
end