class PgDice::Table

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

Attributes

column_name[RW]
future[RW]
past[RW]
period[RW]
schema[RW]
table_name[R]

Public Class Methods

from_hash(hash) click to toggle source
# 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
new(table_name:, past: 90, future: 7, column_name: 'created_at', period: 'day', schema: 'public') click to toggle source
# 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

Public Instance Methods

<=>(other) click to toggle source
# File lib/pgdice/table.rb, line 63
def <=>(other)
  table_name <=> other.table_name
end
==(other) click to toggle source
# File lib/pgdice/table.rb, line 59
def ==(other)
  to_h == other.to_h
end
full_name() click to toggle source
# File lib/pgdice/table.rb, line 38
def full_name
  "#{schema}.#{name}"
end
name() click to toggle source
# File lib/pgdice/table.rb, line 34
def name
  table_name
end
size() click to toggle source

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

# File lib/pgdice/table.rb, line 68
def size
  past + future + 1
end
smash(override_parameters) click to toggle source
# File lib/pgdice/table.rb, line 55
def smash(override_parameters)
  to_h.merge!(override_parameters)
end
to_h() click to toggle source
# 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_s() click to toggle source
# File lib/pgdice/table.rb, line 51
def to_s
  "#{schema}.#{name}: <past: #{past}, future: #{future}, column_name: #{column_name}, period: #{period}>"
end
validate!() click to toggle source
# 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

Private Instance Methods

check_type(field, expected_type) click to toggle source
# File lib/pgdice/table.rb, line 78
def check_type(field, expected_type)
  unless send(field).is_a?(expected_type)
    raise ArgumentError,
          "PgDice::Table: #{name} failed validation on field: #{field}. "\
            "Expected type of: #{expected_type} but found #{send(field).class}"
  end
  true
end