Configuration is evil- lets not make it a public API.

This commit is contained in:
Netscape Navigator 2020-04-26 09:18:30 -05:00
parent da6ee085b6
commit 20532c7e54
6 changed files with 43 additions and 23 deletions

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"cSpell.words": [
"configurability"
]
}

View File

@ -82,23 +82,23 @@ module Pigeon
# === DRAFTS
def delete_current_draft
add_config(CURRENT_DRAFT, nil)
_add_config(CURRENT_DRAFT, nil)
end
def new_draft(kind:, body: {})
old = get_config(CURRENT_DRAFT)
old = _get_config(CURRENT_DRAFT)
raise "PUBLISH OR RESET CURRENT DRAFT (#{old.kind}) FIRST" if old
_replace_draft(Draft.new(kind: kind, body: body))
end
def _replace_draft(draft)
add_config(CURRENT_DRAFT, draft)
_add_config(CURRENT_DRAFT, draft)
draft
end
def get_draft
draft = store.get_config(CURRENT_DRAFT)
draft = store._get_config(CURRENT_DRAFT)
unless draft
raise "THERE IS NO DRAFT. CREATE ONE FIRST."
end
@ -110,7 +110,7 @@ module Pigeon
end
def delete_current_draft
add_config(CURRENT_DRAFT, nil)
_add_config(CURRENT_DRAFT, nil)
end
# Author a new message.
@ -169,12 +169,12 @@ module Pigeon
end
# === DB Management
def get_config(k)
store.get_config(k)
def _get_config(k)
store._get_config(k)
end
def add_config(k, v)
store.add_config(k, v)
def _add_config(k, v)
store._add_config(k, v)
end
def reset_database
@ -187,12 +187,12 @@ module Pigeon
attr_reader :store
def init_ident
secret = get_config(SEED_CONFIG_KEY)
secret = _get_config(SEED_CONFIG_KEY)
if secret
@who_am_i = LocalIdentity.new(secret)
else
new_seed = SecureRandom.random_bytes(Ed25519::KEY_SIZE)
add_config(SEED_CONFIG_KEY, new_seed)
_add_config(SEED_CONFIG_KEY, new_seed)
@who_am_i = LocalIdentity.new(new_seed)
end
end

View File

@ -39,11 +39,11 @@ module Pigeon
read { store[BLCK_NS].to_a }
end
def get_config(key)
def _get_config(key)
read { store[CONF_NS][key] }
end
def add_config(key, value)
def _add_config(key, value)
write do
a = store.fetch(CONF_NS)
raise "FIX SAVED DRAFTS" if value.instance_variable_get(:@db)

View File

@ -17,16 +17,31 @@ This document will teach you how to:
This guide assumes you are familiar with Ruby and the Pigeon Protocol. For an introduction to the protocol, see our protocol specification [here](https://tildegit.org/PigeonProtocolConsortium/protocol_spec).
## Installation
Installation steps change over time. Please see [README.md](README.md) for the most up-to-date information.
## Creating a Database Object
When building Pigeon-based applications, a `Pigeon::Database` object controls nearly all interactions with the database.
For the rest of the tutorial we will use the variable name `db` to refer to the current database.
You can create your own database with the following steps:
```ruby
require_relative "pigeon"
db = Pigeon::Database.new(path: "pigeon.db")
# => #<Pigeon::Database:0x000055a1ecca45e8>
```
reset_database
add_config
get_config
An optional `path:` argument can be passed to `Pigeon::Database.new`. This arg will default to `pigeon.db` within the local directory if not provided. We recommend this default as it will allow you to use the [command line interface](cli_tutorial.md) more effectively.
If at any point you with to start the tutorial over, you can reset your local database with the following command:
```ruby
db.reset_database
```
- Don't share this file (use bundles instead!)
- Where do blobs live?

View File

@ -107,7 +107,7 @@ RSpec.describe Pigeon::Message do
it "verifies accuracy of signatures" do
# === Initial setup
secret = db.get_config(Pigeon::SEED_CONFIG_KEY)
secret = db._get_config(Pigeon::SEED_CONFIG_KEY)
expect(secret).to be_kind_of(String)
message = templated_message
plaintext = template.render_without_signature

View File

@ -12,17 +12,17 @@ RSpec.describe Pigeon::Storage do
end
it "sets a config" do
db.add_config("FOO", "BAR")
value = db.get_config("FOO")
db._add_config("FOO", "BAR")
value = db._get_config("FOO")
expect(value).to eq("BAR")
db.add_config("FOO", nil)
value = db.get_config("FOO")
db._add_config("FOO", nil)
value = db._get_config("FOO")
expect(value).to eq(nil)
end
it "manages configs" do
db.add_config("FOO", "BAR")
value = db.get_config("FOO")
db._add_config("FOO", "BAR")
value = db._get_config("FOO")
expect(value).to eq("BAR")
end