Class: Aef::Hosts::Section

Inherits:
Element
  • Object
show all
Defined in:
lib/aef/hosts/section.rb

Overview

This represents a section as element of a hosts file. It consists of a header, futher included elements and a footer

Instance Attribute Summary collapse

Attributes inherited from Element

#cache

Instance Method Summary collapse

Methods inherited from Element

#to_s

Constructor Details

#initialize(name, options = {}) ⇒ Section

Initializes a section

Parameters:

  • name (String)

    title of the section

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

Options Hash (options):

  • :cache (String)

    sets a cached String representation

  • :elements (Array<Aef::Hosts::Element>)

    a list of elements in the section

Raises:

  • (ArgumentError)


49
50
51
52
53
54
55
56
57
# File 'lib/aef/hosts/section.rb', line 49

def initialize(name, options = {})
  validate_options(options, :cache, :elements)

  raise ArgumentError, 'Name cannot be empty' unless name

  @name     = name.to_s
  @elements = options[:elements] || []
  @cache    = options[:cache] || {:header => nil, :footer => nil}
end

Instance Attribute Details

#elementsArray<Aef::Host::Element>

Note:

If the Array is modified in place, you need to manually invalidate the cache with option :only_section => true.

Elements inside the section

Returns:

  • (Array<Aef::Host::Element>)

See Also:



40
41
42
# File 'lib/aef/hosts/section.rb', line 40

def elements
  @elements
end

#nameString

Title of the section

Returns:

  • (String)


32
33
34
# File 'lib/aef/hosts/section.rb', line 32

def name
  @name
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



78
79
80
# File 'lib/aef/hosts/section.rb', line 78

def cache_filled?
  !!@cache[:header] && !!@cache[:footer]
end

#cache_string(options = {}) ⇒ String (protected)

Defines the algorithm to construct the String representation from cache

Returns:

  • (String)

    the cached String representation



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/aef/hosts/section.rb', line 125

def cache_string(options = {})
  string = ''

  string << @cache[:header]

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

  string << @cache[:footer]

  string
end

#generate_string(options = {}) ⇒ String (protected)

Defines the algorithm to generate a String representation from scratch.

Returns:

  • (String)

    a generated String representation



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/aef/hosts/section.rb', line 108

def generate_string(options = {})
  string = ''

  string << "# -----BEGIN SECTION #{name}-----\n"

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

  string << "# -----END SECTION #{name}-----\n"

  string
end

#inspectString

A String representation for debugging purposes

Returns:

  • (String)


99
100
101
# File 'lib/aef/hosts/section.rb', line 99

def inspect
  generate_inspect(self, :name, :cache, :elements)
end

#invalidate_cache!(options = {}) ⇒ Aef::Hosts::Section

Deletes the cached String representation

Parameters:

  • options (Hash) (defaults to: {})
  • [true, (Hash)

    a customizable set of options

Returns:



65
66
67
68
69
70
71
72
73
# File 'lib/aef/hosts/section.rb', line 65

def invalidate_cache!(options = {})
  @cache = {:header => nil, :footer => nil}

  unless options[:only_section]
    elements.each do |element|
      element.invalidate_cache!
    end
  end
end