From 55fa5820c72aa1686d459506721045d3ccb7aa08 Mon Sep 17 00:00:00 2001 From: joecabezas Date: Wed, 3 Oct 2018 14:32:47 -0700 Subject: [PATCH] Add more documentation for 'set' method and updated error message, add client.set case using force option in order to clarify how to use this option since they are 2 positional hash arguments --- .gitignore | 1 + README.md | 10 ++++++++++ lib/stasche/store.rb | 2 +- spec/stasche/client_spec.rb | 4 ++++ 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index c21ed17..dc3c835 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .bundle Gemfile.lock coverage/ +.byebug_history diff --git a/README.md b/README.md index 87c3bde..aa7f098 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,16 @@ Stasche.set(user_emails: User.where(id: ids).pluck(:email)) user_emails = Stasche.get(:user_emails) ``` +### Setting options +The set method allows you to add an `options` hash as a second parameter, for example to set a given key even if already exists +```rb +# Session A +Stasche.set({foo: 'bar'}, force: true) + +# Session B +foo_value = Stasche.get(:foo) +``` + ### Pushing/Peeking/Popping values ```rb diff --git a/lib/stasche/store.rb b/lib/stasche/store.rb index 8d09a6b..0b17f2d 100644 --- a/lib/stasche/store.rb +++ b/lib/stasche/store.rb @@ -4,7 +4,7 @@ class KeyAlreadyExistsError < StandardError def initialize(key) super( - "Key #{key} already set in stasche. Use `force: true` to override." + "Key #{key} already set in stasche. Use `force: true` to override, example: Stasche.set({foo: 'bar'}, force: true)" ) end diff --git a/spec/stasche/client_spec.rb b/spec/stasche/client_spec.rb index 58f8674..112e993 100644 --- a/spec/stasche/client_spec.rb +++ b/spec/stasche/client_spec.rb @@ -74,6 +74,10 @@ error_class = Stasche::Store::KeyAlreadyExistsError expect { client.set(foo: 'bar') }.to raise_error(error_class) end + + it 'does not throws an exception if forced' do + expect { client.set({ foo: 'bar' }, force: true) }.to_not raise_error + end end end