Class: People

Inherits:
Object
  • Object
show all
Defined in:
lib/terraorg/model/people.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(okta: nil, cache_file: nil) ⇒ People

Returns a new instance of People.



20
21
22
23
24
25
26
27
# File 'lib/terraorg/model/people.rb', line 20

def initialize(okta: nil, cache_file: nil)
  @okta = okta
  @people = {}
  @inactive = []
  @cache_file = cache_file

  load_cache! if @cache_file
end

Instance Attribute Details

#inactiveObject

Returns the value of attribute inactive.



18
19
20
# File 'lib/terraorg/model/people.rb', line 18

def inactive
  @inactive
end

Instance Method Details

#get_or_create!(id) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/terraorg/model/people.rb', line 29

def get_or_create!(id)
  p = @people[id]
  return p if p

  p = Person.new(id, okta: @okta)
  if p.active?
    @people[id] = p
  else
    @people.delete(id)
    @inactive.push p
  end
  save_cache! if @cache_file
  return p
end

#load_cache!Object



44
45
46
47
48
49
50
# File 'lib/terraorg/model/people.rb', line 44

def load_cache!
  return unless File.exist?(@cache_file)

  JSON.parse(File.read(@cache_file)).each do |id, cache|
    @people[id] = Person.new(id, cached: cache)
  end
end

#save_cache!Object



52
53
54
55
56
57
# File 'lib/terraorg/model/people.rb', line 52

def save_cache!
  # Atomic write cache file
  cf_new = "#{@cache_file}.new"
  File.write(cf_new, @people.to_json)
  File.rename(cf_new, @cache_file)
end