Class: Miasma::Types::Collection

Inherits:
Object
  • Object
show all
Includes:
Utils::Memoization
Defined in:
lib/miasma/types/collection.rb

Overview

Base collection

Direct Known Subclasses

Models::AutoScale::Groups, Models::Compute::Servers, Models::LoadBalancer::Balancers, Models::Orchestration::Stack::Events, Models::Orchestration::Stack::Resources, Models::Orchestration::Stacks, Models::Storage::Buckets, Models::Storage::Files

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Collection) initialize(api)

Returns a new instance of Collection



14
15
16
17
# File 'lib/miasma/types/collection.rb', line 14

def initialize(api)
  @api = api
  @collection = nil
end

Instance Attribute Details

- (Miasma::Api) api (readonly)

Returns underlying service API

Returns:

  • (Miasma::Api)

    underlying service API



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

def api
  @api
end

Instance Method Details

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

- (Array<Model>) all

Returns:



20
21
22
23
24
# File 'lib/miasma/types/collection.rb', line 20

def all
  memoize(:collection) do
    perform_population
  end
end

- (Model) build(args = {})

Build a new model

Parameters:

  • args (Hash) (defaults to: {})

    creation options

Returns:



61
62
63
64
65
66
67
68
# File 'lib/miasma/types/collection.rb', line 61

def build(args={})
  instance = self.model.new(self.api)
  args.each do |m_name, m_value|
    m_name = "#{m_name}="
    instance.send(m_name, m_value)
  end
  instance
end

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

Remove all memoized values

Returns:

  • (TrueClass)

- (Array<Model>) filter(args = {})

TODO:

need to add helper to deep sort args, convert to string and hash to use as memoization key

Return models matching given filter

Parameters:

  • args (Hash) (defaults to: {})

    filter options

Returns:



50
51
52
53
54
55
# File 'lib/miasma/types/collection.rb', line 50

def filter(args={})
  key = "filter_#{args.to_smash.checksum}"
  memoize(key) do
    perform_filter(args)
  end
end

- (self) from_json(json)

Load collection via JSON

Parameters:

  • json (String)

Returns:

  • (self)


79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/miasma/types/collection.rb', line 79

def from_json(json)
  loaded = MultiJson.load(json)
  unless(loaded.is_a?(Array))
    raise TypeError.new "Expecting type `Array` but received `#{loaded.class}`"
  end
  unmemoize(:collection)
  memoize(:collection) do
    loaded.map do |item|
      model.from_json(self.api, MultiJson.dump(item))
    end
  end
  self
end

- (Model, NilClass) get(ident)

Return model with given name or ID

Parameters:

  • ident (String, Symbol)

    model identifier

Returns:



38
39
40
41
42
# File 'lib/miasma/types/collection.rb', line 38

def get(ident)
  memoize(ident) do
    perform_get(ident)
  end
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

- (Miasma::Types::Model) model

Returns model class within collection

Returns:

Raises:

  • (NotImplementedError)


94
95
96
# File 'lib/miasma/types/collection.rb', line 94

def model
  raise NotImplementedError
end

- (self) reload

Reload the collection

Returns:

  • (self)


29
30
31
32
# File 'lib/miasma/types/collection.rb', line 29

def reload
  clear_memoizations!
  self
end

- (String) to_json(*_)

Returns collection of models

Returns:

  • (String)

    collection of models



71
72
73
# File 'lib/miasma/types/collection.rb', line 71

def to_json(*_)
  self.all.to_json
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)