Class: Ubicloud::Model
- Inherits:
-
Object
- Object
- Ubicloud::Model
- Defined in:
- lib/ubicloud/model.rb
Overview
Ubicloud::Model is the abstract base class for model classes. There is a separate model class for each primary object type in Ubicloud’s API.
Direct Known Subclasses
Class Attribute Summary collapse
-
.associations ⇒ Object
readonly
A hash of associations for the model.
-
.fragment ⇒ Object
readonly
The path fragment for this model in the Ubicloud API.
-
.id_regexp ⇒ Object
readonly
A regexp for valid id format for instances of this model.
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Return the adapter used for this model instance.
-
#values ⇒ Object
readonly
A hash of values for the model instance.
Class Method Summary collapse
-
.[](adapter, values) ⇒ Object
Return a new model instance for the given values, tied to the related adapter, if the model instance exists and is accessible.
-
.create(adapter, location:, name:, **params) ⇒ Object
Create a new model object in Ubicloud with the given location, name, and params.
-
.list(adapter, location: nil) ⇒ Object
Return an array of all model instances you have access to in Ubicloud.
-
.resolve_associations ⇒ Object
Resolve associations.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Return the value of a specific key for the model instance.
-
#check_exists ⇒ Object
Check whether the current instance exists in Ubicloud.
-
#destroy ⇒ Object
Destroy the given model instance in Ubicloud.
-
#id ⇒ Object
The model’s id, which will be a 26 character string.
-
#info ⇒ Object
Fully populate the model instance by making a request to the Ubicloud API.
-
#initialize(adapter, values) ⇒ Model
constructor
Create a new model instance, which should represent an object that already exists in Ubicloud.
-
#inspect ⇒ Object
Show the class name and values hash.
-
#location ⇒ Object
The model’s location, as a string.
-
#name ⇒ Object
The model’s name.
-
#to_h ⇒ Object
Return hash of data for this model instance.
Constructor Details
#initialize(adapter, values) ⇒ Model
Create a new model instance, which should represent an object that already exists in Ubicloud. values
can be:
-
a string in a valid id format for the model
-
a string in location/name format
-
a hash with symbol keys (must contain either :id key or :location and :name keys)
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/ubicloud/model.rb', line 114 def initialize(adapter, values) @adapter = adapter case values when String @values = if self.class.id_regexp.match?(values) {id: values} else location, name, extra = values.split("/", 3) raise Error, "invalid #{self.class.fragment} location/name: #{values.inspect}" if extra || !name {location:, name:} end when Hash if !values[:id] && !(values[:location] && values[:name]) raise Error, "hash must have :id key or :location and :name keys" end @values = {} merge_into_values(values) else raise Error, "unsupported value initializing #{self.class}: #{values.inspect}" end end |
Class Attribute Details
.associations ⇒ Object (readonly)
A hash of associations for the model. This is used by instances to automatically wrap returned objects in model instances.
10 11 12 |
# File 'lib/ubicloud/model.rb', line 10 def associations @associations end |
.fragment ⇒ Object (readonly)
The path fragment for this model in the Ubicloud API.
13 14 15 |
# File 'lib/ubicloud/model.rb', line 13 def fragment @fragment end |
.id_regexp ⇒ Object (readonly)
A regexp for valid id format for instances of this model.
16 17 18 |
# File 'lib/ubicloud/model.rb', line 16 def id_regexp @id_regexp end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Return the adapter used for this model instance. Each model instance is tied to a specific adapter, and requests to the Ubicloud API are made through the adapter.
103 104 105 |
# File 'lib/ubicloud/model.rb', line 103 def adapter @adapter end |
#values ⇒ Object (readonly)
A hash of values for the model instance.
106 107 108 |
# File 'lib/ubicloud/model.rb', line 106 def values @values end |
Class Method Details
.[](adapter, values) ⇒ Object
Return a new model instance for the given values, tied to the related adapter, if the model instance exists and is accessible. Return nil if the model instance does not exist or is not accessible. values
can be:
-
a string in a valid id format for the model
-
a string in location/name format
-
a hash of values (must contain either :id key or :location and :name keys)
26 27 28 |
# File 'lib/ubicloud/model.rb', line 26 def [](adapter, values) new(adapter, values).check_exists end |
.create(adapter, location:, name:, **params) ⇒ Object
Create a new model object in Ubicloud with the given location, name, and params.
31 32 33 |
# File 'lib/ubicloud/model.rb', line 31 def create(adapter, location:, name:, **params) new(adapter, adapter.post("location/#{location}/#{fragment}/#{name}", _create_params(params))) end |
.list(adapter, location: nil) ⇒ Object
Return an array of all model instances you have access to in Ubicloud. If the location
keyword argument is given, only return model instances for that location.
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ubicloud/model.rb', line 37 def list(adapter, location: nil) path = if location raise Error, "invalid location: #{location.inspect}" if location.include?("/") "location/#{location}/#{fragment}" else fragment end adapter.get(path)[:items].map { new(adapter, _1) } end |
.resolve_associations ⇒ Object
Resolve associations. This is called after all models have been loaded. This approach is taken to avoid the need for autoload or const_get.
50 51 52 |
# File 'lib/ubicloud/model.rb', line 50 def resolve_associations # :nodoc: @associations = @association_block&.call || {} end |
Instance Method Details
#[](key) ⇒ Object
Return the value of a specific key for the model instance.
143 144 145 |
# File 'lib/ubicloud/model.rb', line 143 def [](key) @values[key] end |
#check_exists ⇒ Object
Check whether the current instance exists in Ubicloud. Returns nil if the object does not exist.
202 203 204 |
# File 'lib/ubicloud/model.rb', line 202 def check_exists @values[:name] ? _info(missing: nil) : load_object_info_from_id(missing: nil) end |
#destroy ⇒ Object
Destroy the given model instance in Ubicloud. It is not possible to restore objects that have been destroyed, so only use this if you are sure you want to destroy the object.
150 151 152 |
# File 'lib/ubicloud/model.rb', line 150 def destroy adapter.delete(_path) end |
#id ⇒ Object
The model’s id, which will be a 26 character string. This will load the id from Ubicloud if the model instance doesn’t currently store the id (such as when it was initialized with a location and name).
157 158 159 160 161 162 163 164 |
# File 'lib/ubicloud/model.rb', line 157 def id unless (id = @values[:id]) info id = @values[:id] end id end |
#info ⇒ Object
Fully populate the model instance by making a request to the Ubicloud API. This can also be used to refresh an already populated instance.
191 192 193 |
# File 'lib/ubicloud/model.rb', line 191 def info _info end |
#inspect ⇒ Object
Show the class name and values hash.
196 197 198 |
# File 'lib/ubicloud/model.rb', line 196 def inspect "#<#{self.class.name} #{@values.inspect}>" end |
#location ⇒ Object
The model’s location, as a string. This will load the location from Ubicloud if the model instance does not currently store it (such as when it was initialized with an id).
169 170 171 172 173 174 175 176 |
# File 'lib/ubicloud/model.rb', line 169 def location unless (location = @values[:location]) load_object_info_from_id location = @values[:location] end location end |
#name ⇒ Object
The model’s name. This will load the name from Ubicloud if the model instance does not currently store it (such as when it was initialized with an id).
180 181 182 183 184 185 186 187 |
# File 'lib/ubicloud/model.rb', line 180 def name unless (name = @values[:name]) load_object_info_from_id name = @values[:name] end name end |
#to_h ⇒ Object
Return hash of data for this model instance.
138 139 140 |
# File 'lib/ubicloud/model.rb', line 138 def to_h @values end |