Module: TableTennis

Defined in:
lib/table_tennis/table.rb,
lib/table_tennis/row.rb,
lib/table_tennis/theme.rb,
lib/table_tennis/column.rb,
lib/table_tennis/config.rb,
lib/table_tennis/version.rb,
lib/table_tennis/stage/base.rb,
lib/table_tennis/table_data.rb,
lib/table_tennis/util/scale.rb,
lib/table_tennis/util/colors.rb,
lib/table_tennis/util/termbg.rb,
lib/table_tennis/stage/format.rb,
lib/table_tennis/stage/layout.rb,
lib/table_tennis/stage/render.rb,
lib/table_tennis/util/console.rb,
lib/table_tennis/util/strings.rb,
lib/table_tennis/stage/painter.rb,
lib/table_tennis/util/identify.rb,
lib/table_tennis/util/inspectable.rb,
lib/table_tennis/util/magic_options.rb

Overview

Helper class for validated option processing. This is used by Config but could probably be a custom gem at some point...

MagicOptions is created with a schema defining a list of attributes. Each attribute has a name and a type. options is a hash of values that will be validated against the schema. MagicOptions adds getters and setters for each attribute, and also supports [] and []=. The setters perform validation and raise ArgumentError if something is awry. Because setters always validate, it is not possible to populate MagicOptions with invalid values.

To use, subclass MagicOptions and construct with a schema:

class Config < MagicOptions
def initialize
  super(
    first_name: :str,
    colors: :strings,
    customer: :bool,
    age: (20..90)
  )
end
end

Then assign values:

config = Config.new
config.colors = %w[red white blue]
config.update!(first_name: "john", customer: false)

Here are the supported attribute types:

(1) A simple type like :bool, :int, :num, :float, :str or :sym. (2) An array type like :bools, :ints, :nums, :floats, :strs, or :syms. (3) A range, regexp or Class. (4) A custom validation lambda. The lambda should raise an ArgumentError if the value is invalid. (5) An array of possible values (typically numbers, strings, or symbols). The value must be one of those possibilities. (6) A hash with one element { class => class }. This specifies the hash signature, and the value must be a hash where the keys and values are those classes.

There is a bit of type coercion, but not much. For example, the string "true" or "1" will be coerced to true for boolean options. Integers can be used when the schema calls for floats.

Defined Under Namespace

Modules: Stage, Util Classes: Column, Config, Row, Table, TableData, Theme

Constant Summary collapse

VERSION =
"1.0.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.defaultsObject

Returns the value of attribute defaults.



3
4
5
# File 'lib/table_tennis/config.rb', line 3

def defaults
  @defaults
end

Class Method Details

.new(*args, &block) ⇒ Object

Welcome to TableTennis! Use as follows:

puts TableTennis.new(array_of_hashes_or_records, options = {})



79
# File 'lib/table_tennis/table.rb', line 79

def new(*args, &block) = Table.new(*args, &block)