Class: Miasma::Types::Model
- Includes:
- 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::Resource, 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::Memoization
#_memo, #clear_memoizations!, #memoize, #unmemoize
Methods inherited from Data
Methods included from Utils::Lazy
Constructor Details
#initialize(api, model_data = nil) ⇒ self
Build new model
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/miasma/types/model.rb', line 34 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 end |
Instance Attribute Details
#api ⇒ Miasma::Types::Api (readonly)
Returns underlying service API.
12 13 14 |
# File 'lib/miasma/types/model.rb', line 12 def api @api end |
Class Method Details
.from_json(api, json) ⇒ Model
Build new model from JSON
21 22 23 24 25 |
# File 'lib/miasma/types/model.rb', line 21 def from_json(api, json) instance = self.new(api) instance.from_json(json) instance end |
Instance Method Details
#destroy ⇒ TrueClass, FalseClass
Destroy the model
64 65 66 67 68 69 70 71 72 |
# File 'lib/miasma/types/model.rb', line 64 def destroy if(persisted?) perform_destroy reload true else false end end |
#id? ⇒ String, Integer
89 90 91 |
# File 'lib/miasma/types/model.rb', line 89 def id? data[:id] || dirty[:id] end |
#persisted? ⇒ TrueClass, FalseClass
Returns model is persisted.
84 85 86 |
# File 'lib/miasma/types/model.rb', line 84 def persisted? id? end |
#reload ⇒ self
Reload the underlying data for model
77 78 79 80 81 |
# File 'lib/miasma/types/model.rb', line 77 def reload clear_memoizations! perform_reload self end |
#save ⇒ TrueClass, FalseClass
Save changes to the model
51 52 53 54 55 56 57 58 |
# File 'lib/miasma/types/model.rb', line 51 def save if(dirty?) perform_save reload else false end end |