Class: Aef::Hosts::Section
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
-
#elements ⇒ Array<Aef::Host::Element>
Elements inside the section.
-
#name ⇒ String
Title of the section.
Attributes inherited from Element
Instance Method Summary collapse
-
#cache_filled? ⇒ true, false
Tells if a String representation is cached or not.
-
#cache_string(options = {}) ⇒ String
protected
Defines the algorithm to construct the String representation from cache.
-
#generate_string(options = {}) ⇒ String
protected
Defines the algorithm to generate a String representation from scratch.
-
#initialize(name, options = {}) ⇒ Section
constructor
Initializes a section.
-
#inspect ⇒ String
A String representation for debugging purposes.
-
#invalidate_cache!(options = {}) ⇒ Aef::Hosts::Section
Deletes the cached String representation.
Methods inherited from Element
Constructor Details
#initialize(name, options = {}) ⇒ Section
Initializes a section
49 50 51 52 53 54 55 56 57 |
# File 'lib/aef/hosts/section.rb', line 49 def initialize(name, = {}) (, :cache, :elements) raise ArgumentError, 'Name cannot be empty' unless name @name = name.to_s @elements = [:elements] || [] @cache = [:cache] || {:header => nil, :footer => nil} end |
Instance Attribute Details
#elements ⇒ Array<Aef::Host::Element>
If the Array is modified in place, you need to manually invalidate the cache with option :only_section => true.
Elements inside the section
40 41 42 |
# File 'lib/aef/hosts/section.rb', line 40 def elements @elements end |
#name ⇒ String
Title of the section
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
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
125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/aef/hosts/section.rb', line 125 def cache_string( = {}) string = '' string << @cache[:header] @elements.each do |element| string << element.to_s() end string << @cache[:footer] string end |
#generate_string(options = {}) ⇒ String (protected)
Defines the algorithm to generate a String representation from scratch.
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/aef/hosts/section.rb', line 108 def generate_string( = {}) string = '' string << "# -----BEGIN SECTION #{name}-----\n" @elements.each do |element| string << element.to_s() end string << "# -----END SECTION #{name}-----\n" string end |
#inspect ⇒ String
A String representation for debugging purposes
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
65 66 67 68 69 70 71 72 73 |
# File 'lib/aef/hosts/section.rb', line 65 def invalidate_cache!( = {}) @cache = {:header => nil, :footer => nil} unless [:only_section] elements.each do |element| element.invalidate_cache! end end end |