Class: Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/renderer.rb

Overview

Renderer is the base class for all text renderer plugins

It defines methods for rendering data which will be called from the specialized ‘do_render` methods in the subclasses.

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 do_render
  return if @system_description.packages.empty?

  list "Packages" do
    @system_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

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bufferObject

Returns the value of attribute buffer.



52
53
54
# File 'lib/renderer.rb', line 52

def buffer
  @buffer
end

#system_descriptionObject

Returns the value of attribute system_description.



51
52
53
# File 'lib/renderer.rb', line 51

def system_description
  @system_description
end

Class Method Details

.allObject



63
64
65
# File 'lib/renderer.rb', line 63

def all
  @@renderers.map { |renderer| renderer.new }
end

.for(scope) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/renderer.rb', line 67

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



59
60
61
# File 'lib/renderer.rb', line 59

def inherited(klass)
  @@renderers << klass
end

Instance Method Details

#heading(s) ⇒ Object



167
168
169
# File 'lib/renderer.rb', line 167

def heading(s)
  @buffer += "# #{s}\n\n"
end

#item(s, &block) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/renderer.rb', line 198

def item(s, &block)
  unless @stack.last == :list
    raise InvalidStructureError.new(
      "'item' was called outside of a list block"
    )
  end

  print_indented "* #{s}"

  if block_given?
    @stack << :item
    indent do
      block.call
    end
    @buffer += "\n"
    @stack.pop
  end
end

#list(name = nil, &block) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/renderer.rb', line 175

def list(name = nil, &block)
  unless block_given?
    raise InvalidStructureError.new(
      "'list' was called without a block"
    )
  end

  @stack << :list


  if name && !name.empty?
    print_indented "#{name}:"
    indent do
      block.call
    end
  else
    block.call
  end
  @buffer += "\n" unless @buffer.end_with?("\n\n")

  @stack.pop
end

#puts(s) ⇒ Object



171
172
173
# File 'lib/renderer.rb', line 171

def puts(s)
  print_indented "#{s}"
end

#render(system_description, options = {}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/renderer.rb', line 76

def render(system_description, options = {})
  @system_description = system_description
  @options = options
  @buffer = ""
  @indent = 2
  @stack = []

  if system_description[scope]
    header = display_name
    meta = system_description[scope].meta

    if meta
        header += " [#{meta.hostname}]"
        date = Time.parse(meta.modified).localtime
        date_human = date.strftime "%Y-%m-%d %H:%M:%S"
        header += " (#{date_human})"
    end

    heading(header)
  end

  do_render

  @buffer += "\n" unless @buffer.empty? || @buffer.end_with?("\n\n")

  @buffer
end

#render_comparison(description1, description2, description_common, options = {}) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/renderer.rb', line 124

def render_comparison(description1, description2, description_common, options = {})
  @options = options
  @buffer = ""
  @indent = 0
  @stack = []

  show_heading = if options[:show_all]
    description1[scope] || description2[scope] || description_common[scope]
  else
    description1[scope] || description2[scope]
  end

  heading(display_name) if show_heading

  render_comparison_only_in(description1, scope)
  render_comparison_only_in(description2, scope)
  render_comparison_common(description_common, scope) if options[:show_all]

  @buffer
end

#render_comparison_common(description, scope) ⇒ Object



117
118
119
120
121
122
# File 'lib/renderer.rb', line 117

def render_comparison_common(description, scope)
  if description[scope]
    puts "Common to both systems:"
    render_comparison_section(description)
  end
end

#render_comparison_missing_scope(description1, description2) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/renderer.rb', line 145

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, scope) ⇒ Object



110
111
112
113
114
115
# File 'lib/renderer.rb', line 110

def render_comparison_only_in(description, scope)
  if description[scope]
    puts "Only in '#{description.name}':"
    render_comparison_section(description)
  end
end

#render_comparison_section(description) ⇒ Object



104
105
106
107
108
# File 'lib/renderer.rb', line 104

def render_comparison_section(description)
  @system_description = description
  indent { do_render }
  @buffer += "\n" unless @buffer.empty? || @buffer.end_with?("\n\n")
end

#scopeObject



217
218
219
220
221
222
# File 'lib/renderer.rb', line 217

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