Class: Renderer
Overview
Renderer is the base class for all text renderer plugins
It defines methods for rendering data which will be called from the specialized content methods in the subclasses.
Subclasses can define the output for the ‘show` and `compare` commands using the following `content` methods:
For ‘machinery show`:
- content
-
(required) Defines the output for a scope in ‘machinery show`
For ‘machinery compare`:
- compare_content_only_in
-
Defines the output of the “only in x” sections. The default behavior is to fall back to ‘content`.
- compare_content_common
-
Defines the output of the “in both” section. The default behavior is to fall back to ‘content`.
- compare_content_changed
-
Defines the output of the “in both with changed attributes” section for scopes where this is supported.
The names of the subclasses are 1:1 mappings of the scope areas, e.g. the PackagesRenderer class would be used for rendering when the user specifies “–scope=packages”.
The rendering methods are: def heading(s) - Render a heading def puts(s) - Print one line def list(name) - Start a list. This method takes a block where the items are
created.
def item(s) - Print a list item
Accordingly a simple renderer for the “packages” scope could look like this:
class PackagesRenderer < Renderer
def content(description)
return if description.packages.empty?
list "Packages" do
description.packages.each do |p|
item "#{p.name} (#{p.version})"
end
end
end
end
Defined Under Namespace
Classes: InvalidStructureError
Constant Summary collapse
- @@renderers =
[]
Instance Attribute Summary collapse
-
#buffer ⇒ Object
Returns the value of attribute buffer.
-
#system_description ⇒ Object
Returns the value of attribute system_description.
Class Method Summary collapse
Instance Method Summary collapse
- #compare_content_common(description) ⇒ Object
- #compare_content_only_in(description) ⇒ Object
-
#render(system_description, options = {}) ⇒ Object
Renders one system description using the specialized ‘content` method.
-
#render_comparison(comparison, options = {}) ⇒ Object
Renders the result of a comparison of two system descriptions.
- #render_comparison_changed(comparison) ⇒ Object
- #render_comparison_common(description) ⇒ Object
- #render_comparison_missing_scope(description1, description2) ⇒ Object
- #render_comparison_only_in(description) ⇒ Object
- #scope ⇒ Object
Instance Attribute Details
#buffer ⇒ Object
Returns the value of attribute buffer.
67 68 69 |
# File 'lib/renderer.rb', line 67 def buffer @buffer end |
#system_description ⇒ Object
Returns the value of attribute system_description.
66 67 68 |
# File 'lib/renderer.rb', line 66 def system_description @system_description end |
Class Method Details
.all ⇒ Object
79 80 81 |
# File 'lib/renderer.rb', line 79 def all @@renderers.map { |renderer| renderer.new } end |
.for(scope) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/renderer.rb', line 83 def for(scope) class_name = "#{scope.split("_").map(&:capitalize).join}Renderer" if Object.const_defined?(class_name) Object.const_get(class_name).new end end |
.inherited(klass) ⇒ Object
75 76 77 |
# File 'lib/renderer.rb', line 75 def inherited(klass) @@renderers << klass end |
Instance Method Details
#compare_content_common(description) ⇒ Object
175 176 177 |
# File 'lib/renderer.rb', line 175 def compare_content_common(description) content(description) end |
#compare_content_only_in(description) ⇒ Object
171 172 173 |
# File 'lib/renderer.rb', line 171 def compare_content_only_in(description) content(description) end |
#render(system_description, options = {}) ⇒ Object
Renders one system description using the specialized ‘content` method
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/renderer.rb', line 100 def render(system_description, = {}) @options = @buffer = "" @indent = 2 @stack = [] if system_description[scope] header = display_name = system_description[scope]. if header += " [#{.hostname}]" date = Time.parse(.modified).localtime date_human = date.strftime "%Y-%m-%d %H:%M:%S" header += " (#{date_human})" end heading(header) end content(system_description) @buffer += "\n" unless @buffer.empty? || @buffer.end_with?("\n\n") @buffer end |
#render_comparison(comparison, options = {}) ⇒ Object
Renders the result of a comparison of two system descriptions.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/renderer.rb', line 128 def render_comparison(comparison, = {}) @options = @buffer = "" @indent = 0 @stack = [] show_heading = if [:show_all] comparison.only_in1 || comparison.only_in2 || comparison.changed || comparison.common else comparison.only_in1 || comparison.only_in2 || comparison.changed end heading(display_name) if show_heading render_comparison_only_in(comparison.as_description(:one)) render_comparison_only_in(comparison.as_description(:two)) render_comparison_changed(comparison) if comparison.changed render_comparison_common(comparison.as_description(:common)) if @options[:show_all] @buffer end |
#render_comparison_changed(comparison) ⇒ Object
157 158 159 160 161 |
# File 'lib/renderer.rb', line 157 def render_comparison_changed(comparison) puts "In both with different attributes ('#{comparison.name1}' <> '#{comparison.name2}'):" indent { compare_content_changed(comparison.changed) } @buffer += "\n" unless @buffer.empty? || @buffer.end_with?("\n\n") end |
#render_comparison_common(description) ⇒ Object
163 164 165 166 167 168 169 |
# File 'lib/renderer.rb', line 163 def render_comparison_common(description) return if !description[scope] puts "Common to both systems:" indent { compare_content_common(description) } @buffer += "\n" unless @buffer.empty? || @buffer.end_with?("\n\n") end |
#render_comparison_missing_scope(description1, description2) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/renderer.rb', line 179 def render_comparison_missing_scope(description1, description2) @buffer = "" @indent = 0 @stack = [] missing_descriptions = Array.new if !description1[scope] missing_descriptions << description1.name end if !description2[scope] missing_descriptions << description2.name end if missing_descriptions.count == 1 @buffer += "# #{display_name}\n" indent { puts "Unable to compare, no data in '#{missing_descriptions.join("', '")}'" } end @buffer += "\n" unless @buffer.empty? || @buffer.end_with?("\n\n") @buffer end |
#render_comparison_only_in(description) ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/renderer.rb', line 149 def render_comparison_only_in(description) return if !description[scope] puts "Only in '#{description.name}':" indent { compare_content_only_in(description) } @buffer += "\n" unless @buffer.empty? || @buffer.end_with?("\n\n") end |
#scope ⇒ Object
92 93 94 95 96 97 |
# File 'lib/renderer.rb', line 92 def scope # Return the un-camelcased name of the inspector, # e.g. "foo_bar" for "FooBarInspector" scope = self.class.name.match(/^(.*)Renderer$/)[1] scope.gsub(/([^A-Z])([A-Z])/, "\\1_\\2").downcase end |