Class: PgDice::Table

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/pgdice/table.rb

Overview

Object to represent a table’s configuration in the context of PgDice.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name:, past: 90, future: 7, column_name: 'created_at', period: 'day', schema: 'public') ⇒ Table

Returns a new instance of Table.

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
# File 'lib/pgdice/table.rb', line 10

def initialize(table_name:, past: 90, future: 7, column_name: 'created_at', period: 'day', schema: 'public')
  raise ArgumentError, 'table_name must be a string' unless table_name.is_a?(String)

  @table_name = table_name
  @past = past
  @future = future
  @column_name = column_name
  @period = period
  @schema = schema
end

Instance Attribute Details

#column_nameObject

Returns the value of attribute column_name.



8
9
10
# File 'lib/pgdice/table.rb', line 8

def column_name
  @column_name
end

#futureObject

Returns the value of attribute future.



8
9
10
# File 'lib/pgdice/table.rb', line 8

def future
  @future
end

#pastObject

Returns the value of attribute past.



8
9
10
# File 'lib/pgdice/table.rb', line 8

def past
  @past
end

#periodObject

Returns the value of attribute period.



8
9
10
# File 'lib/pgdice/table.rb', line 8

def period
  @period
end

#schemaObject

Returns the value of attribute schema.



8
9
10
# File 'lib/pgdice/table.rb', line 8

def schema
  @schema
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



7
8
9
# File 'lib/pgdice/table.rb', line 7

def table_name
  @table_name
end

Class Method Details

.from_hash(hash) ⇒ Object



72
73
74
# File 'lib/pgdice/table.rb', line 72

def self.from_hash(hash)
  Table.new(**hash.each_with_object({}) { |(k, v), memo| memo[k.to_sym] = v; })
end

Instance Method Details

#<=>(other) ⇒ Object



63
64
65
# File 'lib/pgdice/table.rb', line 63

def <=>(other)
  table_name <=> other.table_name
end

#==(other) ⇒ Object



59
60
61
# File 'lib/pgdice/table.rb', line 59

def ==(other)
  to_h == other.to_h
end

#full_nameObject



38
39
40
# File 'lib/pgdice/table.rb', line 38

def full_name
  "#{schema}.#{name}"
end

#nameObject



34
35
36
# File 'lib/pgdice/table.rb', line 34

def name
  table_name
end

#sizeObject

Get expected size of this configured table (past + present + future table counts)



68
69
70
# File 'lib/pgdice/table.rb', line 68

def size
  past + future + 1
end

#smash(override_parameters) ⇒ Object



55
56
57
# File 'lib/pgdice/table.rb', line 55

def smash(override_parameters)
  to_h.merge!(override_parameters)
end

#to_hObject



42
43
44
45
46
47
48
49
# File 'lib/pgdice/table.rb', line 42

def to_h
  { table_name: table_name,
    past: past,
    future: future,
    column_name: column_name,
    period: period,
    schema: schema }
end

#to_sObject



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

def to_s
  "#{schema}.#{name}: <past: #{past}, future: #{future}, column_name: #{column_name}, period: #{period}>"
end

#validate!Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pgdice/table.rb', line 21

def validate!
  check_type(:past, Integer)
  check_type(:future, Integer)
  check_type(:column_name, String)
  check_type(:period, String)
  check_type(:schema, String)
  unless PgDice::SUPPORTED_PERIODS.include?(period)
    raise ArgumentError,
          "Period must be one of: #{PgDice::SUPPORTED_PERIODS.keys}. Value: #{period} is not valid."
  end
  true
end