Class: IdStore

Inherits:
Object
  • Object
show all
Defined in:
lib/ea_support/id_store.rb

Instance Method Summary collapse

Constructor Details

#initialize(fileName = nil) ⇒ IdStore

Returns a new instance of IdStore.



4
5
6
7
8
9
10
11
# File 'lib/ea_support/id_store.rb', line 4

def initialize(fileName=nil)
  if fileName
    raise "Base directory does not exist: #{File.dirname(fileName)}" \
      unless File.exist?(File.dirname(fileName))
    @idsFileName = fileName
  end
  @idHash = nil
end

Instance Method Details

#idHashObject



13
14
15
16
# File 'lib/ea_support/id_store.rb', line 13

def idHash
  load unless @idHash
  @idHash
end

#loadObject



18
19
20
21
22
23
24
# File 'lib/ea_support/id_store.rb', line 18

def load
  if @idsFileName && File.exist?(@idsFileName)
    @idHash = YAML.load_file(@idsFileName) || {}
  else
    @idHash = {}
  end
end

#storeObject



26
27
28
29
30
31
# File 'lib/ea_support/id_store.rb', line 26

def store
  return unless @idsFileName
  File.open(@idsFileName,"w") do |f|
    YAML.dump(@idHash, f)
  end
end