Class: Chef::OrgIdCache

Inherits:
Object
  • Object
show all
Defined in:
lib/chef/org_id_cache.rb

Constant Summary collapse

NO_ORG =
"not here"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(db) ⇒ OrgIdCache

Returns a new instance of OrgIdCache.



24
25
26
27
# File 'lib/chef/org_id_cache.rb', line 24

def initialize(db)
  @db = db
  @cache = {}
end

Instance Attribute Details

#cacheObject

Returns the value of attribute cache.



23
24
25
# File 'lib/chef/org_id_cache.rb', line 23

def cache
  @cache
end

#dbObject

Returns the value of attribute db.



23
24
25
# File 'lib/chef/org_id_cache.rb', line 23

def db
  @db
end

Instance Method Details

#fetch(org_name) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/chef/org_id_cache.rb', line 29

def fetch(org_name)
  if cache.key?(org_name) && cache[org_name] != NO_ORG
    cache[org_name]
  elsif cache.key?(org_name) && cache[org_name] == NO_ORG
    nil
  else
    r = db.select(:id).from(:orgs).where(:name => org_name).first
    if r.nil?
      store(org_name, NO_ORG)
      nil
    else
      store(org_name, r[:id])
      r[:id]
    end
  end
end

#store(org_name, org_guid) ⇒ Object



46
47
48
# File 'lib/chef/org_id_cache.rb', line 46

def store(org_name, org_guid)
  cache[org_name] = org_guid
end