Class: Pangea::Internal::Type::BaseModel Abstract Private
- Inherits:
-
Object
- Object
- Pangea::Internal::Type::BaseModel
- Extended by:
- Converter
- Defined in:
- lib/pangea/internal/type/base_model.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Direct Known Subclasses
Models::AiGuard::ClassificationResult, Models::AiGuard::ClassificationResult::Classification, Models::AiGuard::HardeningResult, Models::AiGuard::LanguageResult, Models::AiGuard::MaliciousEntityResult, Models::AiGuard::MaliciousEntityResult::Entity, Models::AiGuard::PromptInjectionResult, Models::AiGuard::PromptInjectionResult::AnalyzerResponse, Models::AiGuard::RedactEntityResult, Models::AiGuard::RedactEntityResult::Entity, Models::AiGuard::SingleEntityResult, Models::AiGuard::TextGuardMessageParam, Models::AiGuard::TextGuardParams, Models::AiGuard::TextGuardResult, Models::AiGuard::TextGuardResult::Detectors, Models::AiGuard::TextGuardResult::Detectors::CodeDetection, Models::AiGuard::TextGuardResult::Detectors::Competitors, Models::AiGuard::TextGuardResult::Detectors::CustomEntity, Models::AiGuard::TextGuardResult::Detectors::Gibberish, Models::AiGuard::TextGuardResult::Detectors::Hardening, Models::AiGuard::TextGuardResult::Detectors::LanguageDetection, Models::AiGuard::TextGuardResult::Detectors::MaliciousEntity, Models::AiGuard::TextGuardResult::Detectors::PiiEntity, Models::AiGuard::TextGuardResult::Detectors::ProfanityAndToxicity, Models::AiGuard::TextGuardResult::Detectors::PromptInjection, Models::AiGuard::TextGuardResult::Detectors::SecretsDetection, Models::AiGuard::TextGuardResult::Detectors::SelfHarm, Models::AiGuard::TextGuardResult::Detectors::Sentiment, Models::AiGuard::TextGuardResult::Detectors::Topic, Models::AiGuard::TopicResult, Models::AiGuard::TopicResult::Topic, RequestOptions
Class Method Summary collapse
- .coerce(value, state:) ⇒ Pangea::Internal::Type::BaseModel, Object private
- .dump(value, state:) ⇒ Hash{Object=>Object}, Object private
- .fields ⇒ Hash{Symbol=>Hash{Symbol=>Object}} private
- .known_fields ⇒ Hash{Symbol=>Hash{Symbol=>Object}} private
- .optional(name_sym, type_info, spec = {}) ⇒ Object private
- .required(name_sym, type_info, spec = {}) ⇒ Object private
Instance Method Summary collapse
- #==(other) ⇒ Boolean private
- #deconstruct_keys(keys) ⇒ Hash{Symbol=>Object} private
-
#initialize(data = {}) ⇒ BaseModel
constructor
private
Create a new instance of a model.
- #inspect ⇒ String private
- #to_h ⇒ Hash{Symbol=>Object} (also: #to_hash) private
Methods included from Converter
Constructor Details
#initialize(data = {}) ⇒ BaseModel
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Create a new instance of a model.
272 273 274 275 276 277 278 279 280 |
# File 'lib/pangea/internal/type/base_model.rb', line 272 def initialize(data = {}) case Pangea::Internal::Util.coerce_hash(data) in Hash => coerced @data = coerced else = "Expected a #{Hash} or #{Pangea::Internal::Type::BaseModel}, got #{data.inspect}" raise ArgumentError.new() end end |
Class Method Details
.coerce(value, state:) ⇒ Pangea::Internal::Type::BaseModel, Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/pangea/internal/type/base_model.rb', line 49 def coerce(value, state:) exactness = state.fetch(:exactness) if value.is_a?(self.class) exactness[:yes] += 1 return value end unless (val = Pangea::Internal::Util.coerce_hash(value)).is_a?(Hash) exactness[:no] += 1 return value end exactness[:yes] += 1 keys = val.keys.to_set instance = new data = instance.to_h # rubocop:disable Metrics/BlockLength fields.each do |name, field| mode, required, target = field.fetch_values(:mode, :required, :type) api_name, nilable, const = field.fetch_values(:api_name, :nilable, :const) unless val.key?(api_name) if required && mode != :dump && const == Pangea::Internal::OMIT exactness[nilable ? :maybe : :no] += 1 else exactness[:yes] += 1 end next end item = val.fetch(api_name) keys.delete(api_name) converted = if item.nil? && (nilable || !required) exactness[nilable ? :yes : :maybe] += 1 nil else coerced = Pangea::Internal::Type::Converter.coerce(target, item, state: state) case target in Pangea::Internal::Type::Converter | Symbol coerced else item end end data.store(name, converted) end # rubocop:enable Metrics/BlockLength keys.each { data.store(_1, val.fetch(_1)) } instance end |
.dump(value, state:) ⇒ Hash{Object=>Object}, Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/pangea/internal/type/base_model.rb', line 114 def dump(value, state:) unless (coerced = Pangea::Internal::Util.coerce_hash(value)).is_a?(Hash) return super end acc = {} coerced.each do |key, val| name = key.is_a?(String) ? key.to_sym : key case (field = known_fields[name]) in nil acc.store(name, super(val, state: state)) else api_name, mode, type_fn = field.fetch_values(:api_name, :mode, :type_fn) case mode in :coerce next else target = type_fn.call acc.store(api_name, Pangea::Internal::Type::Converter.dump(target, val, state: state)) end end end known_fields.each_value do |field| api_name, mode, const = field.fetch_values(:api_name, :mode, :const) next if mode == :coerce || acc.key?(api_name) || const == Pangea::Internal::OMIT acc.store(api_name, const) end acc end |
.fields ⇒ Hash{Symbol=>Hash{Symbol=>Object}}
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
155 156 157 158 159 |
# File 'lib/pangea/internal/type/base_model.rb', line 155 def fields known_fields.transform_values do |field| {**field.except(:type_fn), type: field.fetch(:type_fn).call} end end |
.known_fields ⇒ Hash{Symbol=>Hash{Symbol=>Object}}
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
148 149 150 |
# File 'lib/pangea/internal/type/base_model.rb', line 148 def known_fields @known_fields ||= (self < Pangea::Internal::Type::BaseModel ? superclass.known_fields.dup : {}) end |
.optional(name_sym, type_info, spec = {}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
195 196 197 |
# File 'lib/pangea/internal/type/base_model.rb', line 195 def optional(name_sym, type_info, spec = {}) add_field(name_sym, required: false, type_info: type_info, spec: spec) end |
.required(name_sym, type_info, spec = {}) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
176 177 178 |
# File 'lib/pangea/internal/type/base_model.rb', line 176 def required(name_sym, type_info, spec = {}) add_field(name_sym, required: true, type_info: type_info, spec: spec) end |
Instance Method Details
#==(other) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
13 |
# File 'lib/pangea/internal/type/base_model.rb', line 13 def ==(other) = self.class == other.class && @data == other.to_h |
#deconstruct_keys(keys) ⇒ Hash{Symbol=>Object}
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/pangea/internal/type/base_model.rb', line 23 def deconstruct_keys(keys) (keys || self.class.known_fields.keys) .filter_map do |k| unless self.class.known_fields.key?(k) next end [k, public_send(k)] end .to_h end |
#inspect ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
285 |
# File 'lib/pangea/internal/type/base_model.rb', line 285 def inspect = "#<#{self.class}:0x#{object_id.to_s(16)} #{self}>" |
#to_h ⇒ Hash{Symbol=>Object} Also known as: to_hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
16 |
# File 'lib/pangea/internal/type/base_model.rb', line 16 def to_h = @data |