Class: AgentClientProtocol::Schema::BaseModel

Inherits:
Object
  • Object
show all
Defined in:
lib/agent_client_protocol/schema/base_model.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ BaseModel

Returns a new instance of BaseModel.



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/agent_client_protocol/schema/base_model.rb', line 94

def initialize(**kwargs)
  self.class.properties.each do |ruby_name, meta|
    if kwargs.key?(ruby_name)
      instance_variable_set(:"@#{ruby_name}", kwargs[ruby_name])
    elsif meta[:default] != :_no_default
      default = meta[:default]
      default = default.dup if default.is_a?(::Hash) || default.is_a?(::Array)
      instance_variable_set(:"@#{ruby_name}", default)
    end
  end
end

Class Method Details

.discriminator(field = nil, value = nil) ⇒ Object

Register a discriminator field + value for this class. E.g., discriminator "type", "text" means to_h includes => "text"



17
18
19
20
21
22
23
# File 'lib/agent_client_protocol/schema/base_model.rb', line 17

def discriminator(field = nil, value = nil)
  if field
    @discriminator_field = field
    @discriminator_value = value
  end
  @discriminator_field ? [@discriminator_field, @discriminator_value] : nil
end

.from_hash(hash) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/agent_client_protocol/schema/base_model.rb', line 32

def from_hash(hash)
  return nil if hash.nil?

  kwargs = {}
  properties.each do |ruby_name, meta|
    json_name = meta[:json_name]
    if hash.key?(json_name)
      kwargs[ruby_name] = deserialize_value(hash[json_name], meta[:type])
    elsif hash.key?(ruby_name.to_s)
      kwargs[ruby_name] = deserialize_value(hash[ruby_name.to_s], meta[:type])
    end
  end
  new(**kwargs)
end

.inherited(subclass) ⇒ Object



47
48
49
50
51
# File 'lib/agent_client_protocol/schema/base_model.rb', line 47

def inherited(subclass)
  super
  subclass.instance_variable_set(:@properties, properties.dup)
  subclass.instance_variable_set(:@required_properties, required_properties.dup)
end

.propertiesObject



7
8
9
# File 'lib/agent_client_protocol/schema/base_model.rb', line 7

def properties
  @properties ||= {}
end

.property(ruby_name, json_name: nil, type: nil, default: :_no_default, required: false) ⇒ Object



25
26
27
28
29
30
# File 'lib/agent_client_protocol/schema/base_model.rb', line 25

def property(ruby_name, json_name: nil, type: nil, default: :_no_default, required: false)
  json_name ||= to_camel_case(ruby_name.to_s)
  properties[ruby_name] = {json_name: json_name, type: type, default: default}
  required_properties << ruby_name if required
  attr_reader ruby_name
end

.required_propertiesObject



11
12
13
# File 'lib/agent_client_protocol/schema/base_model.rb', line 11

def required_properties
  @required_properties ||= Set.new
end

Instance Method Details

#==(other) ⇒ Object



128
129
130
# File 'lib/agent_client_protocol/schema/base_model.rb', line 128

def ==(other)
  other.is_a?(self.class) && to_h == other.to_h
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/agent_client_protocol/schema/base_model.rb', line 136

def eql?(other)
  self == other
end

#hashObject



132
133
134
# File 'lib/agent_client_protocol/schema/base_model.rb', line 132

def hash
  [self.class, to_h].hash
end

#inspectObject



140
141
142
143
# File 'lib/agent_client_protocol/schema/base_model.rb', line 140

def inspect
  pairs = self.class.properties.keys.map { |k| "#{k}: #{instance_variable_get(:"@#{k}").inspect}" }
  "#<#{self.class.name} #{pairs.join(", ")}>"
end

#to_hObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/agent_client_protocol/schema/base_model.rb', line 106

def to_h
  result = {}

  # Include discriminator if set on this class
  disc = self.class.discriminator
  if disc
    result[disc[0]] = disc[1]
  end

  self.class.properties.each do |ruby_name, meta|
    value = instance_variable_get(:"@#{ruby_name}")
    next if value.nil? && !self.class.required_properties.include?(ruby_name)

    result[meta[:json_name]] = serialize_value(value)
  end
  result
end

#to_jsonObject



124
125
126
# File 'lib/agent_client_protocol/schema/base_model.rb', line 124

def to_json(*)
  JSON.generate(to_h, *)
end