Skip to content

Commit

Permalink
Implement #[]
Browse files Browse the repository at this point in the history
  • Loading branch information
biqqles committed Oct 16, 2021
1 parent 9b9fc83 commit cd9820d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
25 changes: 23 additions & 2 deletions lib/geode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,39 @@ 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
end

# "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
10 changes: 9 additions & 1 deletion test/geode_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

0 comments on commit cd9820d

Please sign in to comment.