diff --git a/README.md b/README.md index c58fe2b..eaf85d3 100644 --- a/README.md +++ b/README.md @@ -55,8 +55,14 @@ Connect to a store within Redis. #### `#open` "Open" the store for reading and/or writing. +##### Examples: +```ruby +store.open { |table| table[:key] = 5 } +store.open { |table| table[:key] } #=> 5 +``` ##### Parameters: -- a block which receives `table` as its sole parameter +- a block which receives `table` as its sole parameter. Changes to this Hash will + be persisted in the store. - `table` (Hash) The table belonging to `@name` ##### Returns: (Object) The return value of the block @@ -65,9 +71,28 @@ Connect to a store within Redis. "Peek" inside the store, returning a copy of its table. Changes to this copy will NOT be persisted in the store. Use this if you simply need to fetch a value from the store. +##### Examples: +```ruby +store.peek.key?(:test) #=> false +``` ##### Returns: (Hash) A copy of the store's table +#### `#[]` +Retrieve the object at `key` from the store. +This is implemented using `#peek` and therefore +changes to the object returned by this method will NOT +be persisted in the store. +Use this if you simply need to fetch a value from the store. +##### Examples: +```ruby +store[:key] #=> 5 +``` +##### Parameters: +- `key` (Object) The key to look up +##### Returns: +(Object) The object at `key` + --- ### Geode::SequelStore @@ -100,6 +125,17 @@ Use this if you simply need to fetch a value from the store. ##### Returns: (Hash) A copy of the store's table +#### `#[]` +Retrieve the object at `key` from the store. +This is implemented using `#peek` and therefore +changes to the object returned by this method will NOT +be persisted in the store. +Use this if you simply need to fetch a value from the store. +##### Parameters: +- `key` (Object) The key to look up +##### Returns: +(Object) The object at `key` + --- ## Real-world example diff --git a/lib/geode.rb b/lib/geode.rb index 2f7a2ff..6569cd1 100644 --- a/lib/geode.rb +++ b/lib/geode.rb @@ -10,7 +10,13 @@ def initialize(name, connection = nil) # "Open" the store for reading and/or writing. # @yield a block which receives `table` as its sole parameter - # @yieldparam table [Hash] The table belonging to `@name` + # @yieldparam table [Hash] The store's table. Changes to this Hash will + # be persisted in the store + # When in doubt, use this method. + # @example + # "store.open { |table| table[:key] = 5 }" + # @example + # "store.open { |table| table[:key] } #=> 5" # @return [Object] The return value of the block def open raise NotImplementedError @@ -18,10 +24,25 @@ def open # "Peek" inside the store, returning a copy of its table. # Changes to this copy will NOT be persisted in the store. - # Use this if you simply need to fetch a value from the store. + # Use this if you simply want to view the store's table. + # @example + # "store.peek.key?(:test) #=> false" # @return [Hash] A copy of the store's table def peek open(&:itself) end + + # Retrieve the object at `key` from the store. + # This is implemented using `#peek` and therefore + # changes to the object returned by this method will NOT + # be persisted in the store. + # Use this if you simply need to fetch a value from the store. + # @example + # "store[:key] #=> 5" + # @param key [Object] The key to look up + # @return [Object] The object at `key` + def [](key) + peek[key] + end end end diff --git a/test/geode_test.rb b/test/geode_test.rb index 29d6b4d..c98d28f 100644 --- a/test/geode_test.rb +++ b/test/geode_test.rb @@ -39,7 +39,7 @@ def test_readme table[:time][:now] = Time.now end - p store.open { |table| table[:time] } + p store[:time] # {:now=>2021-10-15 16:34:22.664022392 +0100} end @@ -53,4 +53,12 @@ def test_peek # but changes to it are not persisted refute_equal(@store.peek.clear, @store.peek) end + + def test_reference + assert_nil @store[:anything] + + @store.open { |table| table[:contents] = :here } + + assert_equal(:here, @store[:contents]) + end end