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)


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

#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)


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

Returns:

  • (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