Class: SshdConfig::SshdConfig
- Inherits:
-
Object
- Object
- SshdConfig::SshdConfig
show all
- Defined in:
- lib/sshd_config.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(filename) ⇒ SshdConfig
Returns a new instance of SshdConfig.
5
6
7
8
|
# File 'lib/sshd_config.rb', line 5
def initialize(filename)
@filename = filename
@file = File.open(@filename, "r")
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
85
86
87
88
89
90
91
92
|
# File 'lib/sshd_config.rb', line 85
def method_missing(name, *args, &block)
return case name
when /^.*=$/ then
set_value(name.to_s[0..-2], args[0])
else
get_value(name.to_s)
end
end
|
Instance Attribute Details
#lines ⇒ Object
Returns the value of attribute lines.
3
4
5
|
# File 'lib/sshd_config.rb', line 3
def lines
@lines
end
|
Class Method Details
.read(filename) ⇒ Object
25
26
27
28
29
|
# File 'lib/sshd_config.rb', line 25
def self.read(filename)
sshd_config = SshdConfig.new(filename)
sshd_config.load
return sshd_config
end
|
Instance Method Details
#change_property(prop, value) ⇒ Object
36
37
38
39
|
# File 'lib/sshd_config.rb', line 36
def change_property(prop, value)
prop = prop.gsub("\n", "").split(" ")
([prop[0]].concat value.split(" ")).join(" ")
end
|
#close ⇒ Object
51
52
53
|
# File 'lib/sshd_config.rb', line 51
def close
@file.close
end
|
#get_value(name) ⇒ Object
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/sshd_config.rb', line 74
def get_value(name)
prop = @lines.select do |line|
(line[:type] == :property) and (property(line[:content])[:name].chomp.upcase == name.chomp.upcase)
end
if prop.length == 1
return property(prop[0][:content])[:value]
else
raise NoMethodError
end
end
|
#load ⇒ Object
10
11
12
13
14
15
|
# File 'lib/sshd_config.rb', line 10
def load
@lines = []
@file.readlines.each do |line|
@lines << {content: line, type: type(line)}
end
end
|
#property(prop) ⇒ Object
31
32
33
34
|
# File 'lib/sshd_config.rb', line 31
def property(prop)
content = prop.gsub("\n", "").split(" ")
return {name: content[0], value: content[1..-1].join(' ')}
end
|
#save ⇒ Object
41
42
43
44
45
46
47
48
49
|
# File 'lib/sshd_config.rb', line 41
def save
@file.close
@file = File.open(@filename, "w")
@lines.each do |line|
@file.write(line[:content])
end
@file.close
@file = File.open(@filename, "r")
end
|
#set_value(name, value) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/sshd_config.rb', line 55
def set_value(name, value)
prop = @lines.select do |line|
(line[:type] == :property) and (property(line[:content])[:name].chomp.upcase == (name).chomp.upcase)
end
if prop.length == 1
new_value = change_property(prop[0][:content], value)
@lines.each_with_index do |line, i|
if line[:content] == prop[0][:content]
@lines[i][:content] = "#{new_value}\n"
end
end
return new_value
else
new_value = "#{name} #{value}\n"
@lines << {content: new_value, type: :property}
return new_value
end
end
|
#type(line) ⇒ Object
17
18
19
20
21
22
23
|
# File 'lib/sshd_config.rb', line 17
def type(line)
case line[0]
when '#' then :comment
when "\n" then :empty
else :property
end
end
|