Class: Fog::Collection

Inherits:
Array
  • Object
show all
Extended by:
Attributes::ClassMethods
Includes:
Attributes::InstanceMethods, Fog::Core::DeprecatedConnectionAccessors
Defined in:
lib/fog/core/collection.rb

Direct Known Subclasses

AWS::AutoScaling::Activities, AWS::AutoScaling::Configurations, AWS::AutoScaling::Groups, AWS::AutoScaling::Instances, AWS::AutoScaling::Policies, AWS::CloudWatch::AlarmData, AWS::CloudWatch::AlarmHistories, AWS::CloudWatch::Alarms, AWS::CloudWatch::MetricStatistics, AWS::CloudWatch::Metrics, AWS::DataPipeline::Pipelines, AWS::ELB::Listeners, AWS::ELB::LoadBalancers, AWS::ELB::Policies, AWS::ElasticBeanstalk::Applications, AWS::ElasticBeanstalk::Environments, AWS::ElasticBeanstalk::Events, AWS::ElasticBeanstalk::Templates, AWS::ElasticBeanstalk::Versions, AWS::Elasticache::Clusters, AWS::Elasticache::ParameterGroups, AWS::Elasticache::SecurityGroups, AWS::Glacier::Archives, AWS::Glacier::Jobs, AWS::Glacier::Vaults, AWS::IAM::AccessKeys, AWS::IAM::Policies, AWS::IAM::Users, AWS::RDS::ParameterGroups, AWS::RDS::Parameters, AWS::RDS::SecurityGroups, AWS::RDS::Servers, AWS::RDS::Snapshots, AWS::RDS::SubnetGroups, Bluebox::BLB::LbApplications, Bluebox::BLB::LbBackends, Bluebox::BLB::LbServices, Fog::CDN::AWS::Distributions, Fog::CDN::AWS::Invalidations, Fog::CDN::AWS::StreamingDistributions, Fog::Compute::AWS::Addresses, Fog::Compute::AWS::DhcpOptions, Fog::Compute::AWS::Flavors, Fog::Compute::AWS::Images, Fog::Compute::AWS::InternetGateways, Fog::Compute::AWS::KeyPairs, Fog::Compute::AWS::NetworkInterfaces, Fog::Compute::AWS::SecurityGroups, Fog::Compute::AWS::Servers, Fog::Compute::AWS::Snapshots, Fog::Compute::AWS::SpotRequests, Fog::Compute::AWS::Subnets, Fog::Compute::AWS::Tags, Fog::Compute::AWS::Volumes, Fog::Compute::AWS::Vpcs, Fog::Compute::Bluebox::Flavors, Fog::Compute::Bluebox::Images, Fog::Compute::Bluebox::Locations, Fog::Compute::Bluebox::Servers, Fog::Compute::Brightbox::Accounts, Fog::Compute::Brightbox::ApiClients, Fog::Compute::Brightbox::Applications, Fog::Compute::Brightbox::CloudIps, Fog::Compute::Brightbox::FirewallPolicies, Fog::Compute::Brightbox::FirewallRules, Fog::Compute::Brightbox::Flavors, Fog::Compute::Brightbox::Images, Fog::Compute::Brightbox::LoadBalancers, Fog::Compute::Brightbox::ServerGroups, Fog::Compute::Brightbox::Servers, Fog::Compute::Brightbox::Users, Fog::Compute::Brightbox::Zones, Fog::Compute::Clodo::Images, Fog::Compute::Clodo::Servers, Fog::Compute::Cloudstack::Flavors, Fog::Compute::Cloudstack::Images, Fog::Compute::Cloudstack::Jobs, Fog::Compute::Cloudstack::SecurityGroupRules, Fog::Compute::Cloudstack::SecurityGroups, Fog::Compute::Cloudstack::Servers, Fog::Compute::Cloudstack::Snapshots, Fog::Compute::Cloudstack::Volumes, Fog::Compute::Cloudstack::Zones, Fog::Compute::DigitalOcean::Flavors, Fog::Compute::DigitalOcean::Images, Fog::Compute::DigitalOcean::Regions, Fog::Compute::DigitalOcean::Servers, Fog::Compute::DigitalOcean::SshKeys, Fog::Compute::Glesys::Ips, Fog::Compute::Glesys::Servers, Fog::Compute::Glesys::Templates, Fog::Compute::GoGrid::Images, Fog::Compute::GoGrid::Passwords, Fog::Compute::GoGrid::Servers, Fog::Compute::HP::Addresses, Fog::Compute::HP::Flavors, Fog::Compute::HP::Images, Fog::Compute::HP::KeyPairs, Fog::Compute::HP::Metadata, Fog::Compute::HP::SecurityGroups, Fog::Compute::HP::Servers, Fog::Compute::IBM::Addresses, Fog::Compute::IBM::Images, Fog::Compute::IBM::InstanceTypes, Fog::Compute::IBM::Keys, Fog::Compute::IBM::Locations, Fog::Compute::IBM::Servers, Fog::Compute::IBM::Vlans, Fog::Compute::Joyent::Flavors, Fog::Compute::Joyent::Images, Fog::Compute::Joyent::Keys, Fog::Compute::Joyent::Servers, Fog::Compute::Joyent::Snapshots, Fog::Compute::Libvirt::Interfaces, Fog::Compute::Libvirt::Networks, Fog::Compute::Libvirt::Nics, Fog::Compute::Libvirt::Nodes, Fog::Compute::Libvirt::Pools, Fog::Compute::Libvirt::Servers, Fog::Compute::Libvirt::Volumes, Fog::Compute::Linode::DataCenters, Fog::Compute::Linode::Disks, Fog::Compute::Linode::Flavors, Fog::Compute::Linode::Images, Fog::Compute::Linode::Ips, Fog::Compute::Linode::Kernels, Fog::Compute::Linode::Servers, Fog::Compute::Linode::StackScripts, Fog::Compute::Ninefold::Addresses, Fog::Compute::Ninefold::Flavors, Fog::Compute::Ninefold::Images, Fog::Compute::Ninefold::IpForwardingRules, Fog::Compute::Ninefold::Servers, Fog::Compute::OpenStack::Addresses, Fog::Compute::OpenStack::Flavors, Fog::Compute::OpenStack::Hosts, Fog::Compute::OpenStack::Images, Fog::Compute::OpenStack::KeyPairs, Fog::Compute::OpenStack::Metadata, Fog::Compute::OpenStack::Networks, Fog::Compute::OpenStack::SecurityGroups, Fog::Compute::OpenStack::Servers, Fog::Compute::OpenStack::Snapshots, Fog::Compute::OpenStack::Tenants, Fog::Compute::OpenStack::Volumes, Fog::Compute::Ovirt::Clusters, Fog::Compute::Ovirt::Interfaces, Fog::Compute::Ovirt::Quotas, Fog::Compute::Ovirt::Servers, Fog::Compute::Ovirt::Templates, Fog::Compute::Ovirt::Volumes, Fog::Compute::Rackspace::Flavors, Fog::Compute::Rackspace::Images, Fog::Compute::Rackspace::Servers, Fog::Compute::RackspaceV2::Attachments, Fog::Compute::RackspaceV2::Flavors, Fog::Compute::RackspaceV2::Images, Fog::Compute::RackspaceV2::Metadata, Fog::Compute::RackspaceV2::Networks, Fog::Compute::RackspaceV2::Servers, Fog::Compute::Serverlove::Images, Fog::Compute::Serverlove::Servers, Fog::Compute::StormOnDemand::Balancers, Fog::Compute::StormOnDemand::Configs, Fog::Compute::StormOnDemand::Images, Fog::Compute::StormOnDemand::PrivateIps, Fog::Compute::StormOnDemand::Servers, Fog::Compute::StormOnDemand::Stats, Fog::Compute::StormOnDemand::Templates, Fog::Compute::Vmfusion::Servers, Fog::Compute::Voxel::Images, Fog::Compute::Voxel::Servers, Fog::Compute::Vsphere::Clusters, Fog::Compute::Vsphere::Datacenters, Fog::Compute::Vsphere::Datastores, Fog::Compute::Vsphere::Folders, Fog::Compute::Vsphere::Interfaces, Fog::Compute::Vsphere::Networks, Fog::Compute::Vsphere::ResourcePools, Fog::Compute::Vsphere::Servers, Fog::Compute::Vsphere::Templates, Fog::Compute::Vsphere::Volumes, Fog::Compute::XenServer::Hosts, Fog::Compute::XenServer::Networks, Fog::Compute::XenServer::Pbds, Fog::Compute::XenServer::Pifs, Fog::Compute::XenServer::Pools, Fog::Compute::XenServer::Servers, Fog::Compute::XenServer::StorageRepositories, Fog::Compute::XenServer::Vbds, Fog::Compute::XenServer::Vdis, Fog::Compute::XenServer::Vifs, Fog::Compute::XenServer::Vlans, DNS::AWS::Records, DNS::AWS::Zones, DNS::Bluebox::Records, DNS::Bluebox::Zones, DNS::DNSMadeEasy::Records, DNS::DNSMadeEasy::Zones, DNS::DNSimple::Records, DNS::DNSimple::Zones, DNS::Dreamhost::Records, DNS::Dreamhost::Zones, DNS::Dynect::Records, DNS::Dynect::Zones, DNS::Linode::Records, DNS::Linode::Zones, DNS::Rackspace::Records, DNS::Rackspace::Zones, DNS::Zerigo::Records, DNS::Zerigo::Zones, Ecloud::Collection, HP::BlockStorage::BootableVolumes, HP::BlockStorage::Snapshots, HP::BlockStorage::Volumes, Identity::OpenStack::Ec2Credentials, Identity::OpenStack::Roles, Identity::OpenStack::Tenants, Identity::OpenStack::Users, Image::OpenStack::Images, Network::OpenStack::FloatingIps, Network::OpenStack::Networks, Network::OpenStack::Ports, Network::OpenStack::Routers, Network::OpenStack::Subnets, Rackspace::BlockStorage::Snapshots, Rackspace::BlockStorage::VolumeTypes, Rackspace::BlockStorage::Volumes, Rackspace::Databases::Databases, Rackspace::Databases::Flavors, Rackspace::Databases::Instances, Rackspace::Databases::Users, Rackspace::Identity::Credentials, Rackspace::Identity::Roles, Rackspace::Identity::Tenants, Rackspace::Identity::Users, Rackspace::LoadBalancers::AccessRules, Rackspace::LoadBalancers::LoadBalancers, Rackspace::LoadBalancers::Nodes, Rackspace::LoadBalancers::VirtualIps, Storage::AWS::Directories, Storage::AWS::Files, Storage::AWS::Versions, Storage::Atmos::Directories, Storage::Atmos::Files, Storage::Google::Directories, Storage::Google::Files, Storage::HP::Directories, Storage::HP::Files, Storage::HP::SharedDirectories, Storage::HP::SharedFiles, Storage::IBM::Offerings, Storage::IBM::Volumes, Storage::InternetArchive::Directories, Storage::InternetArchive::Files, Storage::Local::Directories, Storage::Local::Files, Storage::OpenStack::Directories, Storage::OpenStack::Files, Storage::Rackspace::Directories, Storage::Rackspace::Files, Terremark::Shared::Addresses, Terremark::Shared::Images, Terremark::Shared::InternetServices, Terremark::Shared::Networks, Terremark::Shared::NodeServices, Terremark::Shared::Servers, Terremark::Shared::Tasks, Terremark::Shared::Vdcs, Vcloud::Collection, Volume::OpenStack::Volumes

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Attributes::ClassMethods

_load, aliases, attribute, attributes, identity, ignore_attributes, ignored_attributes

Methods included from Fog::Core::DeprecatedConnectionAccessors

#connection, #connection=, #prepare_service_value

Methods included from Attributes::InstanceMethods

#_dump, #attributes, #dup, #identity, #identity=, #merge_attributes, #new_record?, #persisted?, #requires, #requires_one

Constructor Details

#initialize(attributes = {}) ⇒ Fog::Collection

Creates a new Fog::Collection based around the passed service

Parameters:

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

Options Hash (attributes):



68
69
70
71
72
# File 'lib/fog/core/collection.rb', line 68

def initialize(attributes = {})
  @service = attributes.delete(:service)
  @loaded = false
  merge_attributes(attributes)
end

Instance Attribute Details

#serviceObject (readonly)

Returns the value of attribute service.



9
10
11
# File 'lib/fog/core/collection.rb', line 9

def service
  @service
end

Class Method Details

.model(new_model = nil) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/fog/core/collection.rb', line 36

def self.model(new_model=nil)
  if new_model == nil
    @model
  else
    @model = new_model
  end
end

Instance Method Details

#clearObject



45
46
47
48
# File 'lib/fog/core/collection.rb', line 45

def clear
  @loaded = true
  super
end

#create(attributes = {}) ⇒ Object



50
51
52
53
54
# File 'lib/fog/core/collection.rb', line 50

def create(attributes = {})
  object = new(attributes)
  object.save
  object
end

#destroy(identity) ⇒ Object



56
57
58
59
# File 'lib/fog/core/collection.rb', line 56

def destroy(identity)
  object = new(:identity => identity)
  object.destroy
end

#inspectObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/fog/core/collection.rb', line 76

def inspect
  Thread.current[:formatador] ||= Formatador.new
  data = "#{Thread.current[:formatador].indentation}<#{self.class.name}\n"
  Thread.current[:formatador].indent do
    unless self.class.attributes.empty?
      data << "#{Thread.current[:formatador].indentation}"
      data << self.class.attributes.map {|attribute| "#{attribute}=#{send(attribute).inspect}"}.join(",\n#{Thread.current[:formatador].indentation}")
      data << "\n"
    end
    data << "#{Thread.current[:formatador].indentation}["
    unless self.empty?
      data << "\n"
      Thread.current[:formatador].indent do
        data << self.map {|member| member.inspect}.join(",\n")
        data << "\n"
      end
      data << Thread.current[:formatador].indentation
    end
    data << "]\n"
  end
  data << "#{Thread.current[:formatador].indentation}>"
  data
end

#load(objects) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/fog/core/collection.rb', line 100

def load(objects)
  clear
  for object in objects
    self << new(object)
  end
  self
end

#modelObject



108
109
110
# File 'lib/fog/core/collection.rb', line 108

def model
  self.class.instance_variable_get('@model')
end

#new(attributes = {}) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/fog/core/collection.rb', line 112

def new(attributes = {})
  unless attributes.is_a?(::Hash)
    raise(ArgumentError.new("Initialization parameters must be an attributes hash, got #{attributes.class} #{attributes.inspect}"))
  end
  model.new(
    {
      :collection => self,
      :service => service
    }.merge(attributes)
  )
end

#reloadObject



124
125
126
127
128
# File 'lib/fog/core/collection.rb', line 124

def reload
  clear
  lazy_load
  self
end

#table(attributes = nil) ⇒ Object



130
131
132
# File 'lib/fog/core/collection.rb', line 130

def table(attributes = nil)
  Formatador.display_table(self.map {|instance| instance.attributes}, attributes)
end

#to_json(options = {}) ⇒ Object



134
135
136
# File 'lib/fog/core/collection.rb', line 134

def to_json(options = {})
  Fog::JSON.encode(self.map {|member| member.attributes})
end