From 20532c7e5439f0ba961c9ba9f61700ab01a3fb8c Mon Sep 17 00:00:00 2001 From: Rick Carlino Date: Sun, 26 Apr 2020 09:18:30 -0500 Subject: [PATCH] Configuration is evil- lets not make it a public API. --- .vscode/settings.json | 5 +++++ lib/pigeon/database.rb | 22 +++++++++++----------- lib/pigeon/storage.rb | 4 ++-- ruby_tutorial.md | 21 ++++++++++++++++++--- spec/pigeon/message_spec.rb | 2 +- spec/pigeon/storage_spec.rb | 12 ++++++------ 6 files changed, 43 insertions(+), 23 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..acc6c93 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "cSpell.words": [ + "configurability" + ] +} \ No newline at end of file diff --git a/lib/pigeon/database.rb b/lib/pigeon/database.rb index d291003..aa0b4a0 100644 --- a/lib/pigeon/database.rb +++ b/lib/pigeon/database.rb @@ -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 diff --git a/lib/pigeon/storage.rb b/lib/pigeon/storage.rb index d7e1107..6a517b9 100644 --- a/lib/pigeon/storage.rb +++ b/lib/pigeon/storage.rb @@ -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) diff --git a/ruby_tutorial.md b/ruby_tutorial.md index 8cb9e3d..14f0c54 100644 --- a/ruby_tutorial.md +++ b/ruby_tutorial.md @@ -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") # => # ``` -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? diff --git a/spec/pigeon/message_spec.rb b/spec/pigeon/message_spec.rb index 369d60a..3b086c1 100644 --- a/spec/pigeon/message_spec.rb +++ b/spec/pigeon/message_spec.rb @@ -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 diff --git a/spec/pigeon/storage_spec.rb b/spec/pigeon/storage_spec.rb index 1f22dc7..6f668b1 100644 --- a/spec/pigeon/storage_spec.rb +++ b/spec/pigeon/storage_spec.rb @@ -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