Class: SSC::DirectoryManager::LocalStorageFile

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

Direct Known Subclasses

FileListFile, PackageFile, RepositoryFile

Instance Method Summary collapse

Constructor Details

#initialize(file_name, path = nil) ⇒ LocalStorageFile

Returns a new instance of LocalStorageFile.



5
6
7
8
# File 'lib/directory_manager.rb', line 5

def initialize(file_name, path= nil)
  path = path ? path : Dir.pwd
  @location= File.join(path, file_name)
end

Instance Method Details

#[](section) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/directory_manager.rb', line 19

def [](section)
  read
  if @parsed_file[section]
    @parsed_file[section]
  else
    []
  end
end

#empty_list?Boolean

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/directory_manager.rb', line 55

def empty_list?
  read
  (!@parsed_file['list']) || (@parsed_file['list'] == []) || (@parsed_file['list'] == {})
end

#pop(section) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/directory_manager.rb', line 28

def pop(section)
  read
  if @parsed_file[section].is_a?(Array)
    @parsed_file[section].pop
  else
    nil
  end
end

#push(section, item) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/directory_manager.rb', line 37

def push(section, item)
  read
  if @parsed_file[section].is_a?(Array)
    @parsed_file[section] |= [ item ]
  else
    @parsed_file[section] = [ item ]
  end
  item
end

#readObject



14
15
16
17
# File 'lib/directory_manager.rb', line 14

def read
  # default error is informative enough if the file is not found
  @parsed_file = @parsed_file || YAML::load(File.read(@location)) || {}
end

#saveObject



48
49
50
51
52
53
# File 'lib/directory_manager.rb', line 48

def save
  contents= @parsed_file.to_yaml
  @parsed_file= nil
  File.open(@location, 'w') {|f| f.write contents}
  contents
end

#valid?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/directory_manager.rb', line 10

def valid?
  File.exist?(@location)
end