Class: Kube::Schema::Resource

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}, &block) ⇒ Resource

Subclasses generated by Instance#[] have a class-level .schema.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/kube/schema/resource.rb', line 12

def initialize(hash = {}, &block)
  hash = deep_stringify_keys(hash)

  if self.class.schema
    schemer = JSONSchemer.schema(self.class.schema, insert_property_defaults: true)
    schemer.valid?(hash)
  end

  @data = BlackHoleStruct.new(hash)
  @data.instance_exec(&block) if block
end

Class Method Details

.schemaObject

Gets overridden by the factory in Kube::Schema::Instance



25
26
27
# File 'lib/kube/schema/resource.rb', line 25

def self.schema
  nil
end

Instance Method Details

#==(other) ⇒ Object



40
41
42
# File 'lib/kube/schema/resource.rb', line 40

def ==(other)
  other.is_a?(Resource) && to_h == other.to_h
end

#to_hObject



36
37
38
# File 'lib/kube/schema/resource.rb', line 36

def to_h
  @data.to_h
end

#valid?Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
# File 'lib/kube/schema/resource.rb', line 29

def valid?
  return true if self.class.schema.nil?

  schemer = JSONSchemer.schema(self.class.schema)
  schemer.valid?(deep_stringify_keys(to_h))
end