Created
November 29, 2022 22:11
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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