Class: Machinery::Ui::Renderer

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

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.



69
70
71
# File 'lib/renderer.rb', line 69

def buffer
  @buffer
end

#system_descriptionObject

Returns the value of attribute system_description.



68
69
70
# File 'lib/renderer.rb', line 68

def system_description
  @system_description
end

Class Method Details

.allObject



81
82
83
# File 'lib/renderer.rb', line 81

def all
  @@renderers.map(&:new)
end

.for(scope) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/renderer.rb', line 85

def for(scope)
  class_name = "Machinery::Ui::#{scope.split("_").map(&:capitalize).join}Renderer"

  begin
    Object.const_get(class_name).new
  rescue NameError
  end
end

.inherited(klass) ⇒ Object



77
78
79
# File 'lib/renderer.rb', line 77

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

Instance Method Details

#compare_content_common(description) ⇒ Object



178
179
180
# File 'lib/renderer.rb', line 178

def compare_content_common(description)
  content(description)
end

#compare_content_only_in(description) ⇒ Object



174
175
176
# File 'lib/renderer.rb', line 174

def compare_content_only_in(description)
  content(description)
end

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

Renders one system description using the specialized ‘content` method



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/renderer.rb', line 103

def render(system_description, options = {})
  @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

  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.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/renderer.rb', line 131

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

  show_heading = if options[: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



160
161
162
163
164
# File 'lib/renderer.rb', line 160

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



166
167
168
169
170
171
172
# File 'lib/renderer.rb', line 166

def render_comparison_common(description)
  return unless 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



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/renderer.rb', line 182

def render_comparison_missing_scope(description1, description2)
  @buffer = ""
  @indent = 0
  @stack = []
  missing_descriptions = Array.new

  unless description1[scope]
    missing_descriptions << description1.name
  end
  unless 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



152
153
154
155
156
157
158
# File 'lib/renderer.rb', line 152

def render_comparison_only_in(description)
  return if !description[scope] || description[scope].elements.try(:empty?)

  puts "Only in '#{description.name}':"
  indent { compare_content_only_in(description) }
  @buffer += "\n" unless @buffer.empty? || @buffer.end_with?("\n\n")
end

#scopeObject



95
96
97
98
99
100
# File 'lib/renderer.rb', line 95

def scope
  # Return the un-camelcased name of the inspector,
  # e.g. "foo_bar" for "FooBarInspector"
  scope = self.class.name.match(/^Machinery::Ui::(.*)Renderer$/)[1]
  scope.gsub(/([^A-Z])([A-Z])/, "\\1_\\2").downcase
end