Class: MyPrecious::ColumnOrder

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/myprecious.rb

Overview

Order of columns in a Markdown table

Contains the default column ordering when constructed. Columns are identified by the Symbol commonly used as an attribute on a dependency info object (e.g. RubyGemInfo instance). Objects of this class behave to some extent like frozen Array instances.

Constant Summary collapse

DEFAULT =
i[name current_version age latest_version latest_released recommended_version cves license changelog].freeze
COLUMN_FROM_TEXT_NAME =
{
  'gem' => :name,
  'package' => :name,
  'module' => :name,
  'our version' => :current_version,
  'how bad' => :obsolescence,
  'latest version' => :latest_version,
  'date available' => :latest_released,
  'age (days)' => :age,
  'license type' => :license,
  /change ?log/ => :changelog,
  'recommended version' => :recommended_version,
  'cves' => :cves,
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeColumnOrder

Returns a new instance of ColumnOrder.



357
358
359
360
# File 'lib/myprecious.rb', line 357

def initialize
  super
  @order = DEFAULT
end

Class Method Details

.col_from_text_name(n) ⇒ Object

Given a text name, derive the equivalent column attribute



408
409
410
411
412
# File 'lib/myprecious.rb', line 408

def self.col_from_text_name(n)
  n = n.downcase
  entry = COLUMN_FROM_TEXT_NAME.find {|k, v| k === n}
  return entry && entry[1]
end

Instance Method Details

#[](n) ⇒ Object

Get the n-th column attribute Symbol



365
366
367
# File 'lib/myprecious.rb', line 365

def [](n)
  @order[n]
end

#each(&blk) ⇒ Object



373
374
375
# File 'lib/myprecious.rb', line 373

def each(&blk)
  @order.each(&blk)
end

#lengthObject



369
370
371
# File 'lib/myprecious.rb', line 369

def length
  @order.length
end

#markdown_columns(dependency) ⇒ Object

Render a line to include in a Markdown table for the given dependency

The dependency must know how to respond to (Ruby) messages (i.e. have attributes) for all columns currently included in the order as represented by this instance.



401
402
403
# File 'lib/myprecious.rb', line 401

def markdown_columns(dependency)
  map {|attr| dependency.send(attr)}.join(" | ")
end

#read_order_from_headers(headers_line) ⇒ Object

Update the column order to match those in the given line

Columns not included in the line are appended in the order they appear in the default order.



384
385
386
387
388
389
390
391
392
# File 'lib/myprecious.rb', line 384

def read_order_from_headers(headers_line)
  headers = headers_line.split('|').map {|h| h.strip.squeeze(' ')}
  @order = headers.map {|h| self.class.col_from_text_name(h)}.compact
  
  # Add in any missing columns at the end
  @order.concat(DEFAULT - @order)
  
  return @order.dup
end