Class: AgentClientProtocol::Types::Base
- Inherits:
-
Object
- Object
- AgentClientProtocol::Types::Base
- Defined in:
- lib/agent_client_protocol/types/base.rb
Class Attribute Summary collapse
-
.additional_properties ⇒ Object
readonly
Returns the value of attribute additional_properties.
-
.definition_name ⇒ Object
readonly
Returns the value of attribute definition_name.
-
.properties ⇒ Object
readonly
Returns the value of attribute properties.
-
.property_schemas ⇒ Object
readonly
Returns the value of attribute property_schemas.
-
.required_keys ⇒ Object
readonly
Returns the value of attribute required_keys.
-
.ruby_property_map ⇒ Object
readonly
Returns the value of attribute ruby_property_map.
-
.ruby_to_json_property_map ⇒ Object
readonly
Returns the value of attribute ruby_to_json_property_map.
-
.schema ⇒ Object
readonly
Returns the value of attribute schema.
-
.unstable ⇒ Object
readonly
Returns the value of attribute unstable.
Class Method Summary collapse
- .build(attributes = nil, **kwargs) ⇒ Object
- .coerce(value) ⇒ Object
- .coerce_for_schema(value, schema, unstable:) ⇒ Object
- .configure(definition_name:, schema:, unstable: false) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #[](key) ⇒ Object
- #fetch(key, *args) ⇒ Object
- #hash ⇒ Object
-
#initialize(attributes = nil) ⇒ Base
constructor
A new instance of Base.
- #key?(key) ⇒ Boolean
- #to_h ⇒ Object
- #to_json(*args) ⇒ Object
Constructor Details
#initialize(attributes = nil) ⇒ Base
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/agent_client_protocol/types/base.rb', line 85 def initialize(attributes = nil) unless attributes.nil? || attributes.is_a?(Hash) raise ArgumentError, "#{self.class.definition_name} expects a Hash payload" end attributes ||= {} normalized = normalize_attributes(attributes) missing = self.class.required_keys.reject { |key| normalized.key?(key) } unless missing.empty? raise ::AgentClientProtocol::Error.invalid_params( "#{self.class.definition_name} missing required keys: #{missing.join(', ')}" ) end if self.class.additional_properties == false unknown_keys = normalized.keys - self.class.properties unless unknown_keys.empty? raise ::AgentClientProtocol::Error.invalid_params( "#{self.class.definition_name} unknown keys: #{unknown_keys.join(', ')}" ) end end @attributes = normalized.freeze freeze end |
Class Attribute Details
.additional_properties ⇒ Object (readonly)
Returns the value of attribute additional_properties.
9 10 11 |
# File 'lib/agent_client_protocol/types/base.rb', line 9 def additional_properties @additional_properties end |
.definition_name ⇒ Object (readonly)
Returns the value of attribute definition_name.
9 10 11 |
# File 'lib/agent_client_protocol/types/base.rb', line 9 def definition_name @definition_name end |
.properties ⇒ Object (readonly)
Returns the value of attribute properties.
9 10 11 |
# File 'lib/agent_client_protocol/types/base.rb', line 9 def properties @properties end |
.property_schemas ⇒ Object (readonly)
Returns the value of attribute property_schemas.
9 10 11 |
# File 'lib/agent_client_protocol/types/base.rb', line 9 def property_schemas @property_schemas end |
.required_keys ⇒ Object (readonly)
Returns the value of attribute required_keys.
9 10 11 |
# File 'lib/agent_client_protocol/types/base.rb', line 9 def required_keys @required_keys end |
.ruby_property_map ⇒ Object (readonly)
Returns the value of attribute ruby_property_map.
9 10 11 |
# File 'lib/agent_client_protocol/types/base.rb', line 9 def ruby_property_map @ruby_property_map end |
.ruby_to_json_property_map ⇒ Object (readonly)
Returns the value of attribute ruby_to_json_property_map.
9 10 11 |
# File 'lib/agent_client_protocol/types/base.rb', line 9 def ruby_to_json_property_map @ruby_to_json_property_map end |
.schema ⇒ Object (readonly)
Returns the value of attribute schema.
9 10 11 |
# File 'lib/agent_client_protocol/types/base.rb', line 9 def schema @schema end |
.unstable ⇒ Object (readonly)
Returns the value of attribute unstable.
9 10 11 |
# File 'lib/agent_client_protocol/types/base.rb', line 9 def unstable @unstable end |
Class Method Details
.build(attributes = nil, **kwargs) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/agent_client_protocol/types/base.rb', line 36 def build(attributes = nil, **kwargs) if attributes && !kwargs.empty? raise ArgumentError, "pass either attributes Hash or keyword args, not both" end payload = attributes || kwargs new(payload) end |
.coerce(value) ⇒ Object
30 31 32 33 34 |
# File 'lib/agent_client_protocol/types/base.rb', line 30 def coerce(value) return value if value.is_a?(self) new(value) end |
.coerce_for_schema(value, schema, unstable:) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/agent_client_protocol/types/base.rb', line 195 def coerce_for_schema(value, schema, unstable:) return value if schema.nil? if schema["$ref"] return coerce_ref(value, schema["$ref"], unstable: unstable) end if schema["type"] == "array" && value.is_a?(Array) item_schema = schema["items"] return value.map { |item| coerce_for_schema(item, item_schema, unstable: unstable) } end if schema["type"] == "object" && value.is_a?(Hash) inline_properties = schema["properties"] || {} normalized = value.transform_keys(&:to_s) return normalized.each_with_object({}) do |(k, v), acc| acc[k] = coerce_for_schema(v, inline_properties[k], unstable: unstable) end end if schema["anyOf"].is_a?(Array) return coerce_union(value, schema["anyOf"], unstable: unstable) end if schema["oneOf"].is_a?(Array) return coerce_union(value, schema["oneOf"], unstable: unstable) end if schema["allOf"].is_a?(Array) return schema["allOf"].reduce(value) do |memo, sub_schema| coerce_for_schema(memo, sub_schema, unstable: unstable) end end value end |
.configure(definition_name:, schema:, unstable: false) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/agent_client_protocol/types/base.rb', line 12 def configure(definition_name:, schema:, unstable: false) @definition_name = definition_name @schema = schema @unstable = unstable @properties = (schema["properties"] || {}).keys.freeze @property_schemas = (schema["properties"] || {}).freeze @required_keys = (schema["required"] || []).freeze @additional_properties = schema["additionalProperties"] @ruby_property_map = build_ruby_property_map(@properties) @ruby_to_json_property_map = @ruby_property_map.each_with_object({}) do |(json_key, ruby_key), acc| acc[ruby_key.to_s] = json_key end.freeze define_property_readers! freeze end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
136 137 138 |
# File 'lib/agent_client_protocol/types/base.rb', line 136 def ==(other) other.is_a?(self.class) && other.to_h == to_h end |
#[](key) ⇒ Object
113 114 115 116 |
# File 'lib/agent_client_protocol/types/base.rb', line 113 def [](key) json_key = self.class.ruby_to_json_property_map[key.to_s] || key.to_s @attributes[json_key] end |
#fetch(key, *args) ⇒ Object
118 119 120 121 |
# File 'lib/agent_client_protocol/types/base.rb', line 118 def fetch(key, *args) json_key = self.class.ruby_to_json_property_map[key.to_s] || key.to_s @attributes.fetch(json_key, *args) end |
#hash ⇒ Object
142 143 144 |
# File 'lib/agent_client_protocol/types/base.rb', line 142 def hash [self.class, @attributes].hash end |
#key?(key) ⇒ Boolean
123 124 125 126 |
# File 'lib/agent_client_protocol/types/base.rb', line 123 def key?(key) json_key = self.class.ruby_to_json_property_map[key.to_s] || key.to_s @attributes.key?(json_key) end |
#to_h ⇒ Object
128 129 130 |
# File 'lib/agent_client_protocol/types/base.rb', line 128 def to_h deep_to_h(@attributes) end |
#to_json(*args) ⇒ Object
132 133 134 |
# File 'lib/agent_client_protocol/types/base.rb', line 132 def to_json(*args) to_h.to_json(*args) end |