Class: Miasma::Types::Model

Inherits:
Data
  • Object
show all
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)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (self) initialize(api, model_data = nil)

Build new model

Parameters:



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

- (Miasma::Types::Api) api (readonly)

Returns underlying service API

Returns:



12
13
14
# File 'lib/miasma/types/model.rb', line 12

def api
  @api
end

Class Method Details

+ (Model) from_json(api, json)

Build new model from JSON

Parameters:

Returns:



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

- (Object) _memo Originally defined in module Utils::Memoization

- (TrueClass) clear_memoizations! Originally defined in module Utils::Memoization

Remove all memoized values

Returns:

  • (TrueClass)

- (TrueClass, FalseClass) destroy

Destroy the model

Returns:

  • (TrueClass, FalseClass)

    destruction was performed



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

- (String, Integer) id?

Returns:

  • (String, Integer)


89
90
91
# File 'lib/miasma/types/model.rb', line 89

def id?
  data[:id] || dirty[:id]
end

- (Object) memoize(key, direct = false) { ... } Originally defined in module Utils::Memoization

Memoize data

Parameters:

  • key (String, Symbol)

    identifier for data

  • direct (Truthy, Falsey) (defaults to: false)

    direct skips key prepend of object id

Yields:

  • block to create data

Yield Returns:

  • data to memoize

Returns:

  • (Object)

    data

- (TrueClass, FalseClass) persisted?

Returns model is persisted

Returns:

  • (TrueClass, FalseClass)

    model is persisted



84
85
86
# File 'lib/miasma/types/model.rb', line 84

def persisted?
  id?
end

- (self) reload

Reload the underlying data for model

Returns:

  • (self)


77
78
79
80
81
# File 'lib/miasma/types/model.rb', line 77

def reload
  clear_memoizations!
  perform_reload
  self
end

- (TrueClass, FalseClass) save

Save changes to the model

Returns:

  • (TrueClass, FalseClass)

    save was performed



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

- (NilClass) unmemoize(key, direct = false) Originally defined in module Utils::Memoization

Remove memoized value

Parameters:

  • key (String, Symbol)

    identifier for data

  • direct (Truthy, Falsey) (defaults to: false)

    direct skips key prepend of object id

Returns:

  • (NilClass)