mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
make catalogs lazy_load
This commit is contained in:
parent
5ee8682499
commit
512f3bba40
3 changed files with 56 additions and 24 deletions
|
@ -12,13 +12,35 @@ module Fog
|
|||
attribute :type
|
||||
attribute :href
|
||||
attribute :description, :aliases => :Description
|
||||
attribute :is_published, :aliases => :IsPublished
|
||||
attribute :is_published, :aliases => :IsPublished, :type => :boolean
|
||||
|
||||
def catalog_items
|
||||
requires :id
|
||||
service.catalog_items(:catalog => self)
|
||||
end
|
||||
|
||||
def initialize(attrs={})
|
||||
super(attrs)
|
||||
[:description, :is_published].each { |attr| attributes[attr]= NonLoaded if attributes[attr].nil? }
|
||||
end
|
||||
|
||||
def description
|
||||
reload if attributes[:description] == NonLoaded and !@inspecting
|
||||
attributes[:description]
|
||||
end
|
||||
|
||||
def is_published
|
||||
reload if attributes[:is_published] == NonLoaded and !@inspecting
|
||||
attributes[:is_published]
|
||||
end
|
||||
|
||||
def inspect
|
||||
@inspecting = true
|
||||
out = super
|
||||
@inspecting = false
|
||||
out
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -10,35 +10,45 @@ module Fog
|
|||
|
||||
attribute :organization
|
||||
|
||||
def index(organization_id = organization.id)
|
||||
catalog_links(organization_id).map{ |catalog| new(catalog)}
|
||||
def all(everyone=false)
|
||||
everyone ? get_everyone : index
|
||||
end
|
||||
|
||||
def get(catalog_id)
|
||||
catalog = get_by_id(catalog_id)
|
||||
return nil unless catalog
|
||||
new(catalog)
|
||||
end
|
||||
|
||||
def get_by_name(catalog_name)
|
||||
catalog = catalog_links.detect{|catalog_link| catalog_link[:name] == catalog_name }
|
||||
return nil unless catalog
|
||||
get(catalog[:id])
|
||||
end
|
||||
|
||||
def index
|
||||
load(catalog_links)
|
||||
end
|
||||
|
||||
def all(organization_id = organization.id)
|
||||
catalog_ids = catalog_links(organization_id).map {|catalog| catalog[:id] }
|
||||
catalog_ids.map{ |catalog_id| get(catalog_id)}
|
||||
def get_everyone
|
||||
catalog_ids = catalog_links.map {|catalog| catalog[:id] }
|
||||
catalogs = catalog_ids.map{ |catalog_id| get_by_id(catalog_id)}
|
||||
load(catalogs)
|
||||
end
|
||||
|
||||
def get(catalog_id)
|
||||
data = service.get_catalog(catalog_id).body
|
||||
%w(:CatalogItems :Link).each {|key_to_delete| data.delete(key_to_delete) }
|
||||
data[:id] = data[:href].split('/').last
|
||||
new(data)
|
||||
end
|
||||
|
||||
def get_by_name(catalog_name, organization_id = organization.id)
|
||||
catalog = catalog_links(organization_id).detect{|catalog_link| catalog_link[:name] == catalog_name }
|
||||
return nil unless catalog
|
||||
catalog_id = catalog[:id]
|
||||
get(catalog_id)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def catalog_links(organization_id)
|
||||
data = service.get_organization(organization_id).body
|
||||
def get_by_id(catalog_id)
|
||||
catalog = service.get_catalog(catalog_id).body
|
||||
%w(:CatalogItems :Link).each {|key_to_delete| catalog.delete(key_to_delete) }
|
||||
service.add_id_from_href!(catalog)
|
||||
catalog
|
||||
end
|
||||
|
||||
def catalog_links
|
||||
data = service.get_organization(organization.id).body
|
||||
catalogs = data[:Link].select { |link| link[:type] == "application/vnd.vmware.vcloud.catalog+xml" }
|
||||
catalogs.each{|catalog| catalog[:id] = catalog[:href].split('/').last }
|
||||
catalogs.each{|catalog| service.add_id_from_href!(catalog) }
|
||||
catalogs
|
||||
end
|
||||
|
||||
|
|
|
@ -30,7 +30,7 @@ module Fog
|
|||
|
||||
def initialize(attrs={})
|
||||
super(attrs)
|
||||
attributes[:description]= NonLoaded if attributes[:description].nil?
|
||||
[:description].each { |attr| attributes[attr]= NonLoaded if attributes[attr].nil? }
|
||||
end
|
||||
|
||||
def description
|
||||
|
|
Loading…
Reference in a new issue