Class: Miasma::Types::Model
- Includes:
- Utils::ApiMethoding, Utils::Memoization
- Defined in:
- lib/miasma/types/model.rb
Overview
Base model
Direct Known Subclasses
Models::AutoScale::Group, Models::Compute::Server, Models::LoadBalancer::Balancer, Models::Orchestration::Stack, Models::Orchestration::Stack::Event, Models::Orchestration::Stack::Plan, Models::Orchestration::Stack::Resource, Models::Queuing::Queue, Models::Storage::Bucket, Models::Storage::File
Instance Attribute Summary collapse
-
#api ⇒ Miasma::Types::Api
readonly
Underlying service API.
Class Method Summary collapse
-
.from_json(api, json) ⇒ Model
Build new model from JSON.
Instance Method Summary collapse
-
#destroy ⇒ TrueClass, FalseClass
Destroy the model.
- #id? ⇒ String, Integer
-
#initialize(api, model_data = nil) ⇒ self
constructor
Build new model.
-
#persisted? ⇒ TrueClass, FalseClass
Model is persisted.
-
#reload ⇒ self
Reload the underlying data for model.
-
#save ⇒ TrueClass, FalseClass
Save changes to the model.
Methods included from Utils::ApiMethoding
Methods inherited from Data
Constructor Details
#initialize(api, model_data = nil) ⇒ self
Build new model
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/miasma/types/model.rb', line 35 def initialize(api, model_data = nil) @api = api @data = Smash.new @dirty = Smash.new if model_data if model_data.is_a?(Hash) load_data(model_data) unless model_data.empty? else raise TypeError.new "Expecting `model_data` to be of type `Hash`. Received: `#{model_data.class}`" end end self.custom = {} end |
Instance Attribute Details
#api ⇒ Miasma::Types::Api (readonly)
Returns underlying service API.
14 15 16 |
# File 'lib/miasma/types/model.rb', line 14 def api @api end |
Class Method Details
.from_json(api, json) ⇒ Model
Build new model from JSON
23 24 25 26 27 |
# File 'lib/miasma/types/model.rb', line 23 def from_json(api, json) instance = self.new(api) instance.from_json(json) instance end |
Instance Method Details
#destroy ⇒ TrueClass, FalseClass
Destroy the model
66 67 68 69 70 71 72 73 74 |
# File 'lib/miasma/types/model.rb', line 66 def destroy if persisted? perform_destroy reload true else false end end |
#id? ⇒ String, Integer
91 92 93 |
# File 'lib/miasma/types/model.rb', line 91 def id? data[:id] || dirty[:id] end |
#persisted? ⇒ TrueClass, FalseClass
Returns model is persisted.
86 87 88 |
# File 'lib/miasma/types/model.rb', line 86 def persisted? id? end |
#reload ⇒ self
Reload the underlying data for model
79 80 81 82 83 |
# File 'lib/miasma/types/model.rb', line 79 def reload clear_memoizations! perform_reload self end |
#save ⇒ TrueClass, FalseClass
Save changes to the model
53 54 55 56 57 58 59 60 |
# File 'lib/miasma/types/model.rb', line 53 def save if dirty? perform_save reload else false end end |