Class: MyPrecious::ColumnOrder
- Inherits:
-
Object
- Object
- MyPrecious::ColumnOrder
- 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 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 (in days)' => :age, 'license type' => :license, /change ?log/ => :changelog, 'recommended version' => :recommended_version, }
Class Method Summary collapse
-
.col_from_text_name(n) ⇒ Object
Given a text name, derive the equivalent column attribute.
Instance Method Summary collapse
-
#[](n) ⇒ Object
Get the
n
-th column attribute Symbol. - #each(&blk) ⇒ Object
-
#initialize ⇒ ColumnOrder
constructor
A new instance of ColumnOrder.
- #length ⇒ Object
-
#markdown_columns(dependency) ⇒ Object
Render a line to include in a Markdown table for the given dependency.
-
#read_order_from_headers(headers_line) ⇒ Object
Update the column order to match those in the given line.
Constructor Details
#initialize ⇒ ColumnOrder
Returns a new instance of ColumnOrder.
349 350 351 352 |
# File 'lib/myprecious.rb', line 349 def initialize super @order = DEFAULT end |
Class Method Details
.col_from_text_name(n) ⇒ Object
Given a text name, derive the equivalent column attribute
400 401 402 403 404 |
# File 'lib/myprecious.rb', line 400 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
357 358 359 |
# File 'lib/myprecious.rb', line 357 def [](n) @order[n] end |
#each(&blk) ⇒ Object
365 366 367 |
# File 'lib/myprecious.rb', line 365 def each(&blk) @order.each(&blk) end |
#length ⇒ Object
361 362 363 |
# File 'lib/myprecious.rb', line 361 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.
393 394 395 |
# File 'lib/myprecious.rb', line 393 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.
376 377 378 379 380 381 382 383 384 |
# File 'lib/myprecious.rb', line 376 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 |