Class: Machinery::SystemDescriptionStore

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

Overview

The responsibility of the SystemDescriptionStore class is to handle the directory where the system description is stored. It provides methods to create, delete, and copy descriptions within the top-level directory.

System descriptions are represented by sub directories of this top-level directory. They are handled by the SystemDescription class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_path = default_path) ⇒ SystemDescriptionStore

Returns a new instance of SystemDescriptionStore.



35
36
37
38
# File 'lib/system_description_store.rb', line 35

def initialize(base_path = default_path)
  @base_path = base_path
  create_dir(@base_path)
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



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

def base_path
  @base_path
end

Instance Method Details

#backup(description_name) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/system_description_store.rb', line 88

def backup(description_name)
  Machinery::SystemDescription.validate_name(description_name)
  validate_existence_of_description(description_name)

  backup_name = get_backup_name(description_name)

  FileUtils.cp_r(description_path(description_name), description_path(backup_name))
  backup_name
end

#copy(from, to) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/system_description_store.rb', line 68

def copy(from, to)
  Machinery::SystemDescription.validate_name(from)
  Machinery::SystemDescription.validate_name(to)

  validate_existence_of_description(from)
  validate_nonexistence_of_description(to)

  FileUtils.cp_r(description_path(from), description_path(to))
end

#default_pathObject



27
28
29
# File 'lib/system_description_store.rb', line 27

def default_path
  Machinery::DEFAULT_CONFIG_DIR
end

#description_path(name) ⇒ Object



40
41
42
# File 'lib/system_description_store.rb', line 40

def description_path(name)
  File.join(@base_path, name)
end

#directory_for(name) ⇒ Object



119
120
121
122
123
# File 'lib/system_description_store.rb', line 119

def directory_for(name)
  dir = description_path(name)
  create_dir(dir)
  dir
end

#html_path(name) ⇒ Object



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

def html_path(name)
  File.join(description_path(name), "index.html")
end

#listObject



52
53
54
55
56
57
# File 'lib/system_description_store.rb', line 52

def list
  Dir["#{@base_path}/*"].
    select { |item| File.exist?(manifest_path(File.basename(item))) }.map { |item|
      File.basename(item)
    }.sort
end

#manifest_path(name) ⇒ Object



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

def manifest_path(name)
  File.join(description_path(name), "manifest.json")
end

#move(from, to) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/system_description_store.rb', line 78

def move(from, to)
  Machinery::SystemDescription.validate_name(from)
  Machinery::SystemDescription.validate_name(to)

  validate_existence_of_description(from)
  validate_nonexistence_of_description(to)

  FileUtils.mv(description_path(from), description_path(to))
end

#persistent?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/system_description_store.rb', line 31

def persistent?
  true
end

#remove(name) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/system_description_store.rb', line 59

def remove(name)
  unless name.empty?
    Machinery::SystemDescription.validate_name(name)
    FileUtils.rm_rf(description_path(name))
  else
    raise "The system description has no name specified and thus can't be deleted."
  end
end

#rename(from, to) ⇒ Object



98
99
100
101
102
103
104
105
106
# File 'lib/system_description_store.rb', line 98

def rename(from, to)
  Machinery::SystemDescription.validate_name(from)
  Machinery::SystemDescription.validate_name(to)

  validate_existence_of_description(from)
  validate_nonexistence_of_description(to)

  FileUtils.mv(description_path(from), description_path(to))
end

#swap(description_name_1, description_name_2) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/system_description_store.rb', line 108

def swap(description_name_1, description_name_2)
  validate_existence_of_description(description_name_1)
  validate_existence_of_description(description_name_2)

  tmp_description_name = "#{description_name_1}-#{Time.now.to_i}-#{rand(1000)}"

  FileUtils.mv(description_path(description_name_1), description_path(tmp_description_name))
  FileUtils.mv(description_path(description_name_2), description_path(description_name_1))
  FileUtils.mv(description_path(tmp_description_name), description_path(description_name_2))
end