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.



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

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



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

def self.schema
  nil
end

Instance Method Details

#==(other) ⇒ Object



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

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

#to_hObject



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

def to_h
  @data.to_h
end

#valid?Boolean

Returns:

  • (Boolean)


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

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

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