Class: Aef::Hosts::File
- Inherits:
-
Object
- Object
- Aef::Hosts::File
- Defined in:
- lib/aef/hosts/file.rb
Overview
This class represents a hosts file and aggregates its elements.
It is able to parse host files from file-system or String and can generate a String representation of itself to String or file-system.
Instance Attribute Summary collapse
-
#elements ⇒ Array<Aef::Hosts::Element>
The hosts file’s elements.
-
#path ⇒ Pathname?
The filesystem path of the hosts file.
Class Method Summary collapse
-
.parse(data) ⇒ Aef::Hosts::File
Parses a hosts file given as String.
-
.read(path) ⇒ Aef::Hosts::File
Parses a hosts file given as path.
Instance Method Summary collapse
-
#initialize(path = nil) ⇒ File
constructor
Initializes a file.
-
#inspect ⇒ String
A String representation for debugging purposes.
-
#invalidate_cache! ⇒ Aef::Hosts::File
Deletes the cached String representations of all elements.
-
#parse(data) ⇒ Aef::Hosts::File
Parses a hosts file given as String.
-
#read(path = nil) ⇒ Aef::Hosts::File
Parses a hosts file given as path.
-
#reset ⇒ Aef::Hosts::File
Removes all elements.
-
#to_s(options = {}) ⇒ Object
A String representation of the hosts file.
-
#write(options = {}) ⇒ Object
Generates a hosts file and writes it to a path.
Constructor Details
#initialize(path = nil) ⇒ File
Initializes a file.
79 80 81 82 |
# File 'lib/aef/hosts/file.rb', line 79 def initialize(path = nil) reset self.path = path end |
Instance Attribute Details
#elements ⇒ Array<Aef::Hosts::Element>
The hosts file’s elements.
51 52 53 |
# File 'lib/aef/hosts/file.rb', line 51 def elements @elements end |
#path ⇒ Pathname?
The filesystem path of the hosts file.
56 57 58 |
# File 'lib/aef/hosts/file.rb', line 56 def path @path end |
Class Method Details
.parse(data) ⇒ Aef::Hosts::File
Parses a hosts file given as String.
71 72 73 |
# File 'lib/aef/hosts/file.rb', line 71 def parse(data) new.parse(data) end |
.read(path) ⇒ Aef::Hosts::File
Parses a hosts file given as path.
63 64 65 |
# File 'lib/aef/hosts/file.rb', line 63 def read(path) new(path).read end |
Instance Method Details
#inspect ⇒ String
A String representation for debugging purposes.
225 226 227 |
# File 'lib/aef/hosts/file.rb', line 225 def inspect generate_inspect(self, :path, :elements) end |
#invalidate_cache! ⇒ Aef::Hosts::File
Deletes the cached String representations of all elements.
96 97 98 99 100 101 102 |
# File 'lib/aef/hosts/file.rb', line 96 def invalidate_cache! elements.each do |element| element.invalidate_cache! end self end |
#parse(data) ⇒ Aef::Hosts::File
Parses a hosts file given as String.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/aef/hosts/file.rb', line 127 def parse(data) current_section = self data.to_s.lines.each_with_index do |line, line_number| line = Linebreak.encode(line, :unix) if COMMENT_LINE_PATTERN =~ line comment = $1 if SECTION_MARKER_PATTERN =~ comment type = $1 name = $2 case type when 'BEGIN' unless current_section.is_a?(Section) current_section = Section.new( name, :cache => {:header => line, :footer => nil} ) else raise ParserError, "Invalid cascading of sections. Cannot start new section '#{name}' without first closing section '#{current_section.name}' on line #{line_number + 1}." end when 'END' if name == current_section.name current_section.cache[:footer] = line elements << current_section current_section = self else raise ParserError, "Invalid closing of section. Found attempt to close section '#{name}' in body of section '#{current_section.name}' on line #{line_number + 1}." end end else current_section.elements << Comment.new( comment, :cache => line ) end else ENTRY_LINE_PATTERN =~ line entry = $1 comment = $2 if entry and not entry =~ /^\s+$/ split = entry.split(/\s+/) split.shift if split.first == '' address, name, *aliases = *split current_section.elements << Entry.new( address, name, :aliases => aliases, :comment => comment, :cache => line ) else current_section.elements << EmptyElement.new( :cache => line ) end end end self end |
#read(path = nil) ⇒ Aef::Hosts::File
Parses a hosts file given as path.
113 114 115 116 117 118 119 120 121 |
# File 'lib/aef/hosts/file.rb', line 113 def read(path = nil) path = path.nil? ? @path : to_pathname(path) raise ArgumentError, 'No path given' unless path parse(path.read) self end |
#reset ⇒ Aef::Hosts::File
Removes all elements.
87 88 89 90 91 |
# File 'lib/aef/hosts/file.rb', line 87 def reset @elements = [] self end |
#to_s(options = {}) ⇒ Object
A String representation of the hosts file.
238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/aef/hosts/file.rb', line 238 def to_s( = {}) (, :force_generation, :linebreak_encoding) string = '' @elements.each do |element| string << element.to_s() end string end |
#write(options = {}) ⇒ Object
Generates a hosts file and writes it to a path.
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/aef/hosts/file.rb', line 206 def write( = {}) (, :force_generation, :linebreak_encoding, :path) path = [:path].nil? ? @path : to_pathname([:path]) raise ArgumentError, 'No path given' unless path .delete(:path) path.open('w') do |file| file.write(to_s()) end true end |