Module: MyPrecious::Reporting

Defined in:
lib/myprecious.rb

Overview

Common behavior in dependency report generation

The methods here are not specific to any language or dependency management framework. They do work with ColumnOrder and an expected set of attributes on the dependency information objects.

Class Method Summary collapse

Class Method Details

.common_col_title(attr) ⇒ Object

Converts an attribute name to the column title for generic attributes

Dependency info classes (like RubyGemInfo) can delegate common column title generation to this function.



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/myprecious.rb', line 273

def common_col_title(attr)
  case attr
  when :current_version then 'Our Version'
  when :age then 'Age (in days)'
  when :latest_version then 'Latest Version'
  when :latest_released then 'Date Available'
  when :recommended_version then 'Recommended Version'
  when :license then 'License Type'
  when :changelog then 'Change Log'
  when :obsolescence then 'How Bad'
  else
    warn("'#{attr}' column does not have a mapped name")
    attr
  end
end

.default_output_fpath(dir) ⇒ Object

Compute the default output filepath for a directory



214
215
216
# File 'lib/myprecious.rb', line 214

def default_output_fpath(dir)
  dir / (GitInfoExtractor.new(dir).repo_name + "-dependency-tracking.md")
end

.header_lines(order, titlizer) ⇒ Object

Generate header lines for the output Markdown table

Returns an Array of strings, currently two lines giving header text and divider row.



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/myprecious.rb', line 249

def header_lines(order, titlizer)
  col_titles = order.map {|c| titlizer.call(c)}
  
  # Check that all attributes in #order round-trip through the title name
  order.zip(col_titles) do |attr, title|
    unless title.kind_of?(Symbol) || ColumnOrder.col_from_text_name(title) == attr
      raise "'#{attr}' does not round-trip (rendered as #{result.inspect})"
    end
  end
  
  return [
    col_titles.join(" | "),
    (["---"] * col_titles.length).join(" | "),
  ]
end

.obsolescence_by_age(days, at_least_moderate: false) ⇒ Object

Determine obsolescence level from days

Returns one of nil, :mild, :moderate, or :severe.

at_least_moderate: allows putting a floor of :moderate obsolescence on the result.



298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/myprecious.rb', line 298

def obsolescence_by_age(days, at_least_moderate: false)
  return case 
  when days < 270
    at_least_moderate ? :moderate : nil
  when days < 500
    at_least_moderate ? :moderate : :mild
  when days < 730
    :moderate
  else
    :severe
  end
end

.on_dependency(name) ⇒ Object

Wrap output from processing of an individual dependency with intro and outro messages



316
317
318
319
320
321
# File 'lib/myprecious.rb', line 316

def on_dependency(name)
  progress_out = $stdout
  progress_out.puts("--- Reporting on #{name}...")
  yield
  progress_out.puts("    (done)")
end

.read_column_order_from(fpath) ⇒ Object

Read the column order from the file at the given path

If fpath indicates a file that does not exist, return the default ColumnOrder.



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/myprecious.rb', line 225

def read_column_order_from(fpath)
  result = ColumnOrder.new
  begin
    prev_line = nil
    fpath.open {|inf| inf.each_line do |line|
      if prev_line && /^-+(?:\|-+)*$/ =~ line.gsub(' ', '')
        result.read_order_from_headers(prev_line)
        break
      end
      prev_line = line
    end}
  rescue Errno::ENOENT
    # No problem
  end
  return result
end