Class: Cline::Schema
- Inherits:
-
Shale::Mapper
- Object
- Shale::Mapper
- Cline::Schema
- Defined in:
- lib/cline/schema.rb
Overview
Base class for any Cline domain object that defines some attributes that are serializable in JSON. Handle the following features:
- Provide Shale attributes interface.
- Automatically transforms Cline camelCase naming.
- Keep track of extra attributes to serialize them back if needed.
Direct Known Subclasses
GlobalSettings, GlobalState, GlobalState::AutoApproval::AutoApprovalSettings, GlobalState::AutoApproval::AutoApprovalSettings::AutoApprovalActions, GlobalState::Browser::BrowserSettings, GlobalState::Browser::BrowserSettings::BrowserViewport, GlobalState::Features::FocusChainSettings, GlobalState::General::DismissedBanner, GlobalState::Models::OpenRouterModelInfo, GlobalState::Models::OpenRouterModelInfo::ThinkingConfig, GlobalState::Workspace::WorkspaceRoot, Log, Log::ApiError, Log::Error, Log::ErrorCause, Log::Properties, McpSettings, McpSettings::McpServer, Model, Model::ThinkingConfig, Providers, Providers::ProviderEntry, Providers::ProviderEntry::ProviderSettings, Providers::ProviderEntry::ProviderSettings::ReasoningSettings, SecretString, Secrets, SessionData, Cline::SessionData::Metadata, Cline::SessionData::Metadata::Checkpoint, Cline::SessionData::Metadata::Checkpoint::CheckpointEntry, Cline::SessionData::Metadata::Usage, SessionMessage, Cline::SessionMessage::MessageContent, Cline::SessionMessage::MessageContent::ToolUseInput, Cline::SessionMessage::Metrics, Cline::SessionMessage::ModelInfo, SessionMessages, TaskMessage, TaskMessage::ModelInfo, WorkspaceSettings
Internal collapse
-
#extra_attributes ⇒ Hash
Store all extra values from a JSON parse.
Internal collapse
-
.as_hash(instance, *args, **kwargs) ⇒ Hash
Get a Hash object from an instance.
-
.cast(value) ⇒ Schema?
Cast an input value to this Schema object.
-
.cline_snake_attributes(*attributes) ⇒ Object
Define the attributes that are already in snake case in Cline files.
-
.of_hash(hash, *args, **kwargs) ⇒ Schema
Parse a Hash object and instantiate the proper instance from it.
-
#==(other) ⇒ Boolean
Equality check.
-
#to_cline_json ⇒ String
Output this object as Cline JSON.
-
#to_hash ⇒ Hash
Output this object as a Hash.
Instance Attribute Details
#extra_attributes ⇒ Hash
Returns Store all extra values from a JSON parse.
14 15 16 |
# File 'lib/cline/schema.rb', line 14 def extra_attributes @extra_attributes end |
Class Method Details
.as_hash(instance, *args, **kwargs) ⇒ Hash
Get a Hash object from an instance.
57 58 59 60 61 62 |
# File 'lib/cline/schema.rb', line 57 def as_hash(instance, *args, **kwargs) complete_hash_mapping hash = super hash.merge!(instance.extra_attributes) if instance.extra_attributes hash end |
.cast(value) ⇒ Schema?
Cast an input value to this Schema object. Allow the attribute to be initialized directly using its Hash form.
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/cline/schema.rb', line 69 def cast(value) return nil if value.nil? # We expect the value to be either a Hash that can be used to initialize a new instance, or a new instance already initialized. if value.is_a?(self) value elsif value.is_a?(Hash) new(**value) else raise "Unable to cast #{value} into #{name}" end end |
.cline_snake_attributes(*attributes) ⇒ Object
Define the attributes that are already in snake case in Cline files
20 21 22 23 24 |
# File 'lib/cline/schema.rb', line 20 def cline_snake_attributes(*attributes) @snake_attributes ||= [] @snake_attributes.concat(attributes) @snake_attributes.uniq! end |
.of_hash(hash, *args, **kwargs) ⇒ Schema
Parse a Hash object and instantiate the proper instance from it.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/cline/schema.rb', line 32 def of_hash(hash, *args, **kwargs) complete_hash_mapping known = hash_mapping.keys.keys # Separate unknown attributes. known_hash = {} extra_hash = {} hash.each do |key, value| if known.include?(key) known_hash[key] = value else extra_hash[key] = value end end # Give Shale the data it knows about, without extra attributes instance = super(known_hash, *args, **kwargs) instance.extra_attributes = extra_hash unless extra_hash.empty? instance end |
Instance Method Details
#==(other) ⇒ Boolean
Equality check
139 140 141 142 |
# File 'lib/cline/schema.rb', line 139 def ==(other) other.is_a?(Schema) && other.to_hash == to_hash end |
#to_cline_json ⇒ String
Output this object as Cline JSON.
116 117 118 |
# File 'lib/cline/schema.rb', line 116 def to_cline_json JSON.dump(self.class.as_hash(self)) end |
#to_hash ⇒ Hash
Output this object as a Hash.
123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/cline/schema.rb', line 123 def to_hash hash = self.class.attributes.to_h do |attribute| value = send(attribute.to_sym) [ attribute, value.respond_to?(:to_hash) ? value.to_hash : value ] end hash.merge!(extra_attributes:) if extra_attributes hash end |