class MyPrecious::ColumnOrder

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.

Constants

COLUMN_FROM_TEXT_NAME
DEFAULT

Public Class Methods

col_from_text_name(n) click to toggle source

Given a text name, derive the equivalent column attribute

# 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
new() click to toggle source
Calls superclass method
# File lib/myprecious.rb, line 357
def initialize
  super
  @order = DEFAULT
end

Public Instance Methods

[](n) click to toggle source

Get the n-th column attribute Symbol

# File lib/myprecious.rb, line 365
def [](n)
  @order[n]
end
each(&blk) click to toggle source
# File lib/myprecious.rb, line 373
def each(&blk)
  @order.each(&blk)
end
length() click to toggle source
# File lib/myprecious.rb, line 369
def length
  @order.length
end
markdown_columns(dependency) click to toggle source

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.

# File lib/myprecious.rb, line 401
def markdown_columns(dependency)
  map {|attr| dependency.send(attr)}.join(" | ")
end
read_order_from_headers(headers_line) click to toggle source

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.

# 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