Class: Aef::Hosts::Element Abstract

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/hosts/aef/hosts/element.rb

Overview

This class is abstract.

This class is not supposed to be instantiated.

The base class for elements which are aggregated by the Aef::Hosts::File class.

Direct Known Subclasses

Comment, EmptyElement, Entry, Section

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cacheString, ... (readonly)

Cached String representation

Returns:

  • (String, Hash, nil)


36
37
38
# File 'lib/hosts/aef/hosts/element.rb', line 36

def cache
  @cache
end

Instance Method Details

#cache_filled?true, false

Tells if a String representation is cached or not

Returns:

  • (true, false)

    true if cache is not empty



50
51
52
# File 'lib/hosts/aef/hosts/element.rb', line 50

def cache_filled?
  @cache ? true : false
end

#inspectString

A String representation for debugging purposes

Returns:

  • (String)


57
58
59
# File 'lib/hosts/aef/hosts/element.rb', line 57

def inspect
  generate_inspect(self, :cache)
end

#invalidate_cache!Aef::Hosts::Element

Deletes the cached String representation

Returns:



41
42
43
44
45
# File 'lib/hosts/aef/hosts/element.rb', line 41

def invalidate_cache!
  @cache = nil

  self
end

#to_s(options = {}) ⇒ Object

Provides a String representation of the element

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


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/hosts/aef/hosts/element.rb', line 70

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

  string = ''

  if !cache_filled? || options[:force_generation]
    string << generate_string(options)
  else
    string << cache_string(options)
  end

  if options[:linebreak_encoding]
    string = Aef::Linebreak.encode(string, options[:linebreak_encoding])
  end

  string
end