Skip to content

Commit

Permalink
Refactor Settings and support code.
Browse files Browse the repository at this point in the history
  • Loading branch information
toddsundsted committed Dec 11, 2024
1 parent 1490b81 commit fabce05
Showing 3 changed files with 54 additions and 52 deletions.
22 changes: 11 additions & 11 deletions spec/framework/framework_spec.cr
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@ Spectator.describe Ktistec::Settings do

after_each do
# reset settings to initial values
Ktistec.settings.assign({"host" => "https://test.test/", "site" => "Test", "footer" => nil}).save
Ktistec.set_default_settings
end

it "initializes instance from the persisted values" do
@@ -66,16 +66,6 @@ Spectator.describe Ktistec::Settings do
expect(subject.footer).to eq("FOOTER")
end

describe "#save" do
it "persists assigned values to the database" do
subject.assign({"host" => "https://test.test/", "site" => "Test", "footer" => "Copyright"}).save

expect(Ktistec.database.scalar("SELECT value FROM options WHERE key = ?", "host")).to eq("https://test.test")
expect(Ktistec.database.scalar("SELECT value FROM options WHERE key = ?", "site")).to eq("Test")
expect(Ktistec.database.scalar("SELECT value FROM options WHERE key = ?", "footer")).to eq("Copyright")
end
end

describe "#assign" do
it "sets the host" do
subject.clear_host
@@ -93,6 +83,16 @@ Spectator.describe Ktistec::Settings do
end
end

describe "#save" do
it "persists assigned values to the database" do
subject.assign({"host" => "https://test.test/", "site" => "Test", "footer" => "Copyright"}).save

expect(Ktistec.database.scalar("SELECT value FROM options WHERE key = ?", "host")).to eq("https://test.test")
expect(Ktistec.database.scalar("SELECT value FROM options WHERE key = ?", "site")).to eq("Test")
expect(Ktistec.database.scalar("SELECT value FROM options WHERE key = ?", "footer")).to eq("Copyright")
end
end

describe "#valid?" do
it "expects host to be present" do
expect(subject.assign({"host" => ""}).valid?).to be_false
36 changes: 18 additions & 18 deletions spec/spec_helper/base.cr
Original file line number Diff line number Diff line change
@@ -96,28 +96,28 @@ module Ktistec
@@db_file = "sqlite3://#{File.tempname("ktistec-test", ".db")}"

class Settings
def clear_host
Ktistec.database.exec("DELETE FROM options WHERE key = ?", "host")
@host = nil
end

def clear_site
Ktistec.database.exec("DELETE FROM options WHERE key = ?", "site")
@site = nil
end

def clear_footer
Ktistec.database.exec("DELETE FROM options WHERE key = ?", "footer")
@footer = nil
end
{% for property in PROPERTIES %}
def clear_{{property.id}}
Ktistec.database.exec("DELETE FROM options WHERE key = ?", "{{property.id}}")
@{{property.id}} = nil
end
{% end %}
end

def self.clear_settings
settings.clear_host
settings.clear_site
settings.clear_footer
{% for property in Settings::PROPERTIES %}
settings.clear_{{property.id}}
{% end %}
@@settings = nil
end

def self.set_default_settings
Ktistec.settings.assign({
"host" => "https://test.test/",
"site" => "Test",
"footer" => nil,
}).save
end
end

BEFORE_PROCS = [] of Proc(Nil)
@@ -155,7 +155,7 @@ Kemal.config.public_folder = Dir.tempdir

Kemal.config.env = ENV["KEMAL_ENV"]? || "test"

Ktistec.settings.assign({"host" => "https://test.test", "site" => "Test"}).save
Ktistec.set_default_settings

# Spectator calls `setup_from_env` to set up logging. the dispatcher
# default (`DispatchMode::Async`) does not work -- probably due to:
48 changes: 25 additions & 23 deletions src/framework/framework.cr
Original file line number Diff line number Diff line change
@@ -57,9 +57,15 @@ module Ktistec
# Model-like class for managing site settings.
#
class Settings
property host : String?
property site : String?
property footer : String?
PROPERTIES = {
host: String,
site: String,
footer: String,
}

{% for property, type in PROPERTIES %}
property {{property.id}} : {{type.id}}?
{% end %}

getter errors = Hash(String, Array(String)).new

@@ -73,18 +79,20 @@ module Ktistec
assign(values)
end

def save
raise "invalid settings" unless valid?
{"host" => @host, "site" => @site, "footer" => @footer}.each do |key, value|
Ktistec.database.exec("INSERT OR REPLACE INTO options (key, value) VALUES (?, ?)", key, value)
end
def assign(options)
{% for property, type in PROPERTIES %}
@{{property.id}} = options["{{property.id}}"].as({{type.id}}?) if options.has_key?("{{property.id}}")
{% end %}
self
end

def assign(options)
@host = options["host"].as(String) if options.has_key?("host")
@site = options["site"].as(String) if options.has_key?("site")
@footer = options["footer"].as(String?) if options.has_key?("footer")
private SQL = "INSERT OR REPLACE INTO options (key, value) VALUES (?, ?)"

def save
raise "invalid settings" unless valid?
{% for property, _ in PROPERTIES %}
Ktistec.database.exec(SQL, "{{property.id}}", @{{property.id}})
{% end %}
self
end

@@ -129,17 +137,11 @@ module Ktistec
end
end

def self.host
settings.host.not_nil!
end

def self.site
settings.site.not_nil!
end

def self.footer
settings.footer.not_nil!
end
{% for property, _ in Settings::PROPERTIES %}
def self.{{property.id}}
settings.{{property.id}}.not_nil!
end
{% end %}

# An [ActivityPub](https://www.w3.org/TR/activitypub/) server.
#

0 comments on commit fabce05

Please sign in to comment.