Class: Machinery::ScopeFileStore

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

Overview

The responsibility of the ScopeFileStore class is to represent a sub directory of a system description which is used to hold files belonging to a specific scope.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_path, store_name) ⇒ ScopeFileStore

Returns a new instance of ScopeFileStore.



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

def initialize(base_path, store_name)
  @base_path = base_path
  @store_name = store_name
end

Instance Attribute Details

#base_pathObject

Returns the value of attribute base_path.



22
23
24
# File 'lib/scope_file_store.rb', line 22

def base_path
  @base_path
end

#store_nameObject

Returns the value of attribute store_name.



22
23
24
# File 'lib/scope_file_store.rb', line 22

def store_name
  @store_name
end

Instance Method Details

#createObject



29
30
31
32
# File 'lib/scope_file_store.rb', line 29

def create
  dir = File.join(base_path, store_name)
  create_dir(dir, new_dir_mode)
end

#create_dir(dir, mode = 0700) ⇒ Object



69
70
71
72
73
# File 'lib/scope_file_store.rb', line 69

def create_dir(dir, mode = 0700)
  unless Dir.exist?(dir)
    FileUtils.mkdir_p(dir, mode: mode)
  end
end

#create_sub_directory(sub_dir) ⇒ Object



48
49
50
51
# File 'lib/scope_file_store.rb', line 48

def create_sub_directory(sub_dir)
  dir = File.join(base_path, store_name, sub_dir)
  create_dir(dir, new_dir_mode)
end

#list_contentObject



53
54
55
56
57
58
59
# File 'lib/scope_file_store.rb', line 53

def list_content
  files = Dir.
    glob(File.join(path, "**/*"), File::FNM_DOTMATCH).
    reject { |path| [".", ".."].include?(File.basename(path)) }
  # filter parent directories because they should not be listed separately
  files.reject { |f| files.index { |e| e =~ /^#{f}\/.+/ } }
end

#new_dir_modeObject



61
62
63
64
65
66
67
# File 'lib/scope_file_store.rb', line 61

def new_dir_mode
  mode = 0700
  if Dir.exist?(base_path)
    mode = File.stat(base_path).mode & 0777
  end
  mode
end

#pathObject



34
35
36
37
# File 'lib/scope_file_store.rb', line 34

def path
  dir = File.join(base_path, store_name)
  Dir.exist?(dir) ? dir : nil
end

#removeObject



39
40
41
# File 'lib/scope_file_store.rb', line 39

def remove
  FileUtils.rm_rf(File.join(base_path, store_name))
end

#rename(new_store_name) ⇒ Object



43
44
45
46
# File 'lib/scope_file_store.rb', line 43

def rename(new_store_name)
  FileUtils.mv(path, File.join(base_path, new_store_name))
  @store_name = new_store_name
end