Class: Rancher::Collection

Inherits:
Resource show all
Includes:
Enumerable
Defined in:
lib/rancher/collection.rb

Overview

A Collection of Resources

Instance Attribute Summary

Attributes inherited from Resource

#links, #meta

Instance Method Summary collapse

Methods inherited from Resource

#action, #action?, #get_link, #in_meta?, #method_missing, #save!

Constructor Details

#initialize(data) ⇒ Collection

Returns a new instance of Collection.



7
8
9
10
# File 'lib/rancher/collection.rb', line 7

def initialize(data)
  @data = data[:data] if data.key?(:data)
  super(data)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Rancher::Resource

Instance Method Details

#create(attrs) ⇒ Object



12
13
14
15
16
# File 'lib/rancher/collection.rb', line 12

def create(attrs)
  attrs = attrs.meta if attrs.is_a?(Rancher::Resource)

  Rancher.post get_link('self'), attrs
end

#eachObject



25
26
27
28
29
# File 'lib/rancher/collection.rb', line 25

def each
  return @data.enum_for(:each) unless block_given?

  @data.each { |d| yield d }
end

#remove!(id_or_obj) ⇒ Object



18
19
20
21
22
23
# File 'lib/rancher/collection.rb', line 18

def remove!(id_or_obj)
  id = id_or_obj.get_id if id_or_obj.is_a?(Rancher::Resource)
  link = get_link('self') + "/#{id}"

  Rancher.delete link
end