Class: PgDice::ApprovedTables

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ ApprovedTables

Returns a new instance of ApprovedTables.

Raises:

  • (ArgumentError)


11
12
13
14
15
# 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

Instance Attribute Details

#tablesObject (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

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
44
45
# 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) ⇒ Object



51
52
53
# File 'lib/pgdice/approved_tables.rb', line 51

def ==(other)
  tables.sort == other.tables.sort
end

#[](arg) ⇒ Object



17
18
19
20
# 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) ⇒ Object



29
30
31
32
33
34
35
# 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) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
25
26
27
# 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) ⇒ Object



47
48
49
# File 'lib/pgdice/approved_tables.rb', line 47

def smash(table_name, override_parameters)
  fetch(table_name).smash(override_parameters)
end