Skip to content

Instantly share code, notes, and snippets.

@plambert
Created November 29, 2022 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plambert/c9431fd48306dceb868c9d5f5b512c18 to your computer and use it in GitHub Desktop.
Save plambert/c9431fd48306dceb868c9d5f5b512c18 to your computer and use it in GitHub Desktop.
A singleton module that tries to enforce that the first object being assigned as the singleton
class Ytdb::Database
SCHEMA_VERSION = 1_i64
@@singleton : self? = nil
property db : DB::Database
property dbfile : String
def initialize(@dbfile)
@db = DB.open "sqlite3:#{@dbfile}?max_idle_pool_size=3&initial_pool_size=3"
@@singleton ||= self
end
def self.singleton
@@singleton || raise "#{self}: no singleton defined yet"
end
def self.singleton=(db : self)
@@singleton = db
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment