Class: RsyncConfig::Config

Inherits:
Object
  • Object
show all
Includes:
Propertiable, UserManagement
Defined in:
lib/rsync_config/config.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UserManagement

included, #load_secrets_file, #local_user_list, #local_users, #user?, #users, #users=, #write_secrets_file

Methods included from Propertiable

#[], #[]=, included, #properties_to_a, #sanitize_key

Class Method Details

.load_file(config_file) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rsync_config/config.rb', line 20

def self.load_file(config_file)
  raise 'File does not exist' unless File.exists? config_file
  raise 'File is not readable' unless File.readable? config_file

  content = nil

  File.open(config_file, 'r') do |file|
    content = file.read 
  end

  Config.parse content
end

.parse(content) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rsync_config/config.rb', line 33

def self.parse(content)
  raise 'Cannot process nil' if content.nil?

  Treetop.load File.join(__dir__, 'parser/config_file')
  parser = RsyncConfigFileParser.new
  p = parser.parse content 

  unless p.nil?   
    return p.to_config.after_parse
  else
    raise RuntimeError, parser.failure_reason
  end
end

Instance Method Details

#after_parseObject



86
87
88
# File 'lib/rsync_config/config.rb', line 86

def after_parse
  batch_process_secrets_files :load_secrets_file
end

#module(key) ⇒ Object

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
54
55
56
# File 'lib/rsync_config/config.rb', line 47

def module(key)
  raise ArgumentError if key.nil?
  key = key.to_s if key.is_a? Symbol
  key = key.strip
  raise ArgumentError if key.length == 0

  modules[key] ||= Module.new(self, key)

  modules[key]
end

#module_namesObject



82
83
84
# File 'lib/rsync_config/config.rb', line 82

def module_names
  modules.keys
end

#to_config_fileObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rsync_config/config.rb', line 62

def to_config_file
  out = []
  properties_to_s = properties_to_a.join("\n")
  out << properties_to_s if properties_to_s.length > 0

  modules.each do |key, mod|
    out << mod.to_config_file
  end

  out.join "\n"
end

#to_sObject



58
59
60
# File 'lib/rsync_config/config.rb', line 58

def to_s
  to_config_file
end

#write_to(file_path) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/rsync_config/config.rb', line 74

def write_to(file_path)
  File.open(file_path, 'w') do |file|
    file.write to_config_file
  end

  write_secrets_files
end