Class: Sambot::Domain::Ssh::ConfigSection
- Inherits:
-
Object
- Object
- Sambot::Domain::Ssh::ConfigSection
- Defined in:
- lib/sambot/domain/ssh/config_section.rb
Instance Attribute Summary collapse
-
#aliases ⇒ Object
Returns the value of attribute aliases.
-
#lines ⇒ Object
Returns the value of attribute lines.
-
#name ⇒ Object
Returns the value of attribute name.
-
#settings ⇒ Object
Returns the value of attribute settings.
Instance Method Summary collapse
- #[](setting) ⇒ Object
- #[]=(setting, value) ⇒ Object
- #has_alias?(text) ⇒ Boolean
- #header ⇒ Object
-
#initialize(name) ⇒ ConfigSection
constructor
A new instance of ConfigSection.
- #matches?(text) ⇒ Boolean
- #matches_exactly?(text) ⇒ Boolean
- #to_s ⇒ Object
- #unset(setting) ⇒ Object
Constructor Details
#initialize(name) ⇒ ConfigSection
Returns a new instance of ConfigSection.
8 9 10 11 12 13 14 |
# File 'lib/sambot/domain/ssh/config_section.rb', line 8 def initialize(name) all_names = name.split(/\s+/) @name = all_names.shift @aliases = all_names @settings = {} @lines = [] end |
Instance Attribute Details
#aliases ⇒ Object
Returns the value of attribute aliases.
6 7 8 |
# File 'lib/sambot/domain/ssh/config_section.rb', line 6 def aliases @aliases end |
#lines ⇒ Object
Returns the value of attribute lines.
6 7 8 |
# File 'lib/sambot/domain/ssh/config_section.rb', line 6 def lines @lines end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/sambot/domain/ssh/config_section.rb', line 6 def name @name end |
#settings ⇒ Object
Returns the value of attribute settings.
6 7 8 |
# File 'lib/sambot/domain/ssh/config_section.rb', line 6 def settings @settings end |
Instance Method Details
#[](setting) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/sambot/domain/ssh/config_section.rb', line 16 def [](setting) unless @settings.key? setting if line = lines[setting_index(setting)] key,val = line.split(nil, 2) @settings[key] = val end end @settings[setting] end |
#[]=(setting, value) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/sambot/domain/ssh/config_section.rb', line 42 def []=(setting, value) if value.is_a?(Array) value.each do |val| line_num = lines.length lines[line_num] = format_line(setting, val) end elsif value != '-' line_num = lines.length lines[line_num] = format_line(setting, value) else @settings.delete(setting) @lines.delete_at(setting_index(setting)) end end |
#has_alias?(text) ⇒ Boolean
66 67 68 |
# File 'lib/sambot/domain/ssh/config_section.rb', line 66 def has_alias?(text) aliases.member?(text) end |
#header ⇒ Object
33 34 35 36 |
# File 'lib/sambot/domain/ssh/config_section.rb', line 33 def header name_with_aliases = [@name, *aliases].join(" ") "Host #{name_with_aliases}" end |
#matches?(text) ⇒ Boolean
57 58 59 60 |
# File 'lib/sambot/domain/ssh/config_section.rb', line 57 def matches?(text) r = Regexp.new text name =~ r || aliases.any? {|a| a =~ r} || lines.any? {|line| line =~ r} end |
#matches_exactly?(text) ⇒ Boolean
62 63 64 |
# File 'lib/sambot/domain/ssh/config_section.rb', line 62 def matches_exactly?(text) name == text || has_alias?(text) end |
#to_s ⇒ Object
38 39 40 |
# File 'lib/sambot/domain/ssh/config_section.rb', line 38 def to_s [header, *lines] * "\n" end |
#unset(setting) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/sambot/domain/ssh/config_section.rb', line 26 def unset(setting) if line_num = setting_index(setting) @settings.delete setting @lines.delete_at line_num end end |