class PgDice::ApprovedTables
Hash-like object to contain approved tables. Adds some convenience validation and a simpleish interface.
Attributes
tables[R]
Public Class Methods
new(*args)
click to toggle source
# File lib/pgdice/approved_tables.rb, line 11 def initialize(*args) @tables = args.flatten.compact raise ArgumentError, 'Objects must be a PgDice::Table!' unless tables.all? { |item| item.is_a?(PgDice::Table) } end
Public Instance Methods
<<(object)
click to toggle source
# File lib/pgdice/approved_tables.rb, line 37 def <<(object) raise ArgumentError, 'Objects must be a PgDice::Table!' unless object.is_a?(PgDice::Table) object.validate! return self if include?(object.name) @tables << object self end
==(other)
click to toggle source
# File lib/pgdice/approved_tables.rb, line 51 def ==(other) tables.sort == other.tables.sort end
[](arg)
click to toggle source
# File lib/pgdice/approved_tables.rb, line 17 def [](arg) key = check_string_args(arg) tables.select { |table| table.name == key }.first end
fetch(arg)
click to toggle source
# File lib/pgdice/approved_tables.rb, line 29 def fetch(arg) key = check_string_args(arg) found_table = self.[](key) raise PgDice::IllegalTableError, "Table name: '#{key}' is not in the list of approved tables!" unless found_table found_table end
include?(arg)
click to toggle source
# File lib/pgdice/approved_tables.rb, line 22 def include?(arg) key = check_string_args(arg) return true if self.[](key) false end
smash(table_name, override_parameters)
click to toggle source
# File lib/pgdice/approved_tables.rb, line 47 def smash(table_name, override_parameters) fetch(table_name).smash(override_parameters) end
Private Instance Methods
check_string_args(key)
click to toggle source
# File lib/pgdice/approved_tables.rb, line 57 def check_string_args(key) raise ArgumentError, 'key must be a String' unless key.is_a?(String) key end