Class: PgDice::ApprovedTables
- Inherits:
-
Object
- Object
- PgDice::ApprovedTables
- Extended by:
- Forwardable
- Defined in:
- lib/pgdice/approved_tables.rb
Overview
Hash-like object to contain approved tables. Adds some convenience validation and a simpleish interface.
Instance Attribute Summary collapse
-
#tables ⇒ Object
readonly
Returns the value of attribute tables.
Instance Method Summary collapse
- #<<(object) ⇒ Object
- #==(other) ⇒ Object
- #[](arg) ⇒ Object
- #fetch(arg) ⇒ Object
- #include?(arg) ⇒ Boolean
-
#initialize(*args) ⇒ ApprovedTables
constructor
A new instance of ApprovedTables.
- #smash(table_name, override_parameters) ⇒ Object
Constructor Details
#initialize(*args) ⇒ ApprovedTables
Returns a new instance of ApprovedTables.
12 13 14 15 16 |
# File 'lib/pgdice/approved_tables.rb', line 12 def initialize(*args) @tables = args.flatten.compact raise ArgumentError, 'Objects must be a PgDice::Table!' unless tables.all?(PgDice::Table) end |
Instance Attribute Details
#tables ⇒ Object (readonly)
Returns the value of attribute tables.
6 7 8 |
# File 'lib/pgdice/approved_tables.rb', line 6 def tables @tables end |
Instance Method Details
#<<(object) ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/pgdice/approved_tables.rb', line 38 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) ⇒ Object
52 53 54 |
# File 'lib/pgdice/approved_tables.rb', line 52 def ==(other) tables.sort == other.tables.sort end |
#[](arg) ⇒ Object
18 19 20 21 |
# File 'lib/pgdice/approved_tables.rb', line 18 def [](arg) key = check_string_args(arg) tables.find { |table| table.name == key } end |
#fetch(arg) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/pgdice/approved_tables.rb', line 30 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) ⇒ Boolean
23 24 25 26 27 28 |
# File 'lib/pgdice/approved_tables.rb', line 23 def include?(arg) key = check_string_args(arg) return true if self.[](key) false end |
#smash(table_name, override_parameters) ⇒ Object
48 49 50 |
# File 'lib/pgdice/approved_tables.rb', line 48 def smash(table_name, override_parameters) fetch(table_name).smash(override_parameters) end |