Class: Miasma::Types::Collection

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

Overview

Base collection

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::ApiMethoding

#api_method_for

Constructor Details

#initialize(api) ⇒ Collection

Returns a new instance of Collection.



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

def initialize(api)
  @api = api
end

Instance Attribute Details

#apiMiasma::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

#allArray<Model>

Returns:



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

def all
  memoize(:collection) do
    perform_population
  end
end

#build(args = {}) ⇒ Model

Build a new model

Parameters:

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

    creation options

Returns:



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

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

#filter(args = {}) ⇒ Array<Model>

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:



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

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

#from_json(json) ⇒ self

Load collection via JSON

Parameters:

  • json (String)

Returns:

  • (self)


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

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

#get(ident) ⇒ Model, NilClass

Return model with given name or ID

Parameters:

  • ident (String, Symbol)

    model identifier

Returns:



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

def get(ident)
  memoize(ident) do
    perform_get(ident)
  end
end

#modelMiasma::Types::Model

Returns model class within collection.

Returns:

Raises:

  • (NotImplementedError)


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

def model
  raise NotImplementedError
end

#reloadself

Reload the collection

Returns:

  • (self)


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

def reload
  clear_memoizations!
  self
end

#to_json(*_) ⇒ String

Returns collection of models.

Returns:

  • (String)

    collection of models



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

def to_json(*_)
  self.all.to_json
end