Class: Aef::Hosts::File

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ File

Initializes a file.

Parameters:

  • path (Pathname) (defaults to: nil)

    path to the hosts 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

#elementsArray<Aef::Hosts::Element>

The hosts file's elements.

Returns:



51
52
53
# File 'lib/aef/hosts/file.rb', line 51

def elements
  @elements
end

#pathPathname?

The filesystem path of the hosts file.

Returns:

  • (Pathname, nil)


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.

Parameters:

  • data (String)

    a String representation of the hosts file

Returns:



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.

Parameters:

  • path (Pathname)

    the hosts file path

Returns:



63
64
65
# File 'lib/aef/hosts/file.rb', line 63

def read(path)
  new(path).read
end

Instance Method Details

#inspectString

A String representation for debugging purposes.

Returns:

  • (String)


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.

Returns:



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.

Parameters:

  • data (String)

    a String representation of the hosts file

Returns:



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.

Parameters:

  • path (Pathname) (defaults to: nil)

    override the path attribute for this operation

Returns:

Raises:

  • (ArgumentError)


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

#resetAef::Hosts::File

Removes all elements.

Returns:



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.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :force_generation (true, false)

    if set to true, the cache won't be used, even if it not empty

  • :linebreak_encoding (:unix, :windows, :mac)

    the linebreak encoding of the result. If nothing is specified the result will be encoded as if :unix was specified.

See Also:

  • Linebreak#encode


238
239
240
241
242
243
244
245
246
247
248
# File 'lib/aef/hosts/file.rb', line 238

def to_s(options = {})
  validate_options(options, :force_generation, :linebreak_encoding)

  string = ''

  @elements.each do |element|
    string << element.to_s(options)
  end

  string
end

#write(options = {}) ⇒ Object

Generates a hosts file and writes it to a path.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :path (Pathname)

    overrides the path attribute for this operation

  • :force_generation (true, false)

    if set to true, the cache won't be used, even if it not empty

  • :linebreak_encoding (:unix, :windows, :mac)

    the linebreak encoding of the result. If nothing is specified the result will be encoded as if :unix was specified.

Raises:

  • (ArgumentError)

See Also:

  • Linebreak#encode


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(options = {})
  validate_options(options, :force_generation, :linebreak_encoding, :path)

  path = options[:path].nil? ? @path : to_pathname(options[:path])

  raise ArgumentError, 'No path given' unless path

  options.delete(:path)

  path.open('w') do |file|
    file.write(to_s(options))
  end

  true
end