Class: Aef::Hosts::Section

Inherits:
Element
  • Object
show all
Defined in:
lib/hosts/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/hosts/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/hosts/aef/hosts/section.rb', line 40

def elements
  @elements
end

#nameString

Title of the section

Returns:

  • (String)


32
33
34
# File 'lib/hosts/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/hosts/aef/hosts/section.rb', line 78

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

#inspectString

A String representation for debugging purposes

Returns:

  • (String)


99
100
101
# File 'lib/hosts/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/hosts/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