memory_storage 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +66 -0
- data/Rakefile +11 -0
- data/lib/memory_storage.rb +31 -0
- data/memory_storage.gemspec +19 -0
- data/spec/memory_storage_spec.rb +61 -0
- metadata +53 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 56311d5feeccda10214e8278a96cd5f59cef2d83
|
4
|
+
data.tar.gz: 081354bddac2a3620662535637b11d17085b9f3d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d254c7960a4671c86049ae5e05c1979aa828117226620ca58365fb40590aad42d51524b2d6f668d190c3e2694046882b355d6623b10c55439f5bb18036bbb21a
|
7
|
+
data.tar.gz: 927835ff4c49e255afb83d68c9edb687b836876a0bdb37484c48aaa350320dbcaa8026dbb3103c2b1ddd6bd1ebf08ae6f14579570e9994be3b131d64d364c353
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Fernando Martínez
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
# MemoryStorage
|
2
|
+
|
3
|
+
A very simple in memory storage utility class.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add the gem to your Gemfile
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'memory_storage'
|
11
|
+
```
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
The only requirement to store objects with MemoryStorage is that persistent
|
16
|
+
objects must respond to `#id`
|
17
|
+
|
18
|
+
Examples:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
class Item
|
22
|
+
attr_accessor :id, :name, :amount
|
23
|
+
|
24
|
+
def initialize(id, name, amount)
|
25
|
+
@id = id
|
26
|
+
@name = name
|
27
|
+
@amount = amount
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
item = Item.new(1, 'Item one', 10)
|
32
|
+
|
33
|
+
# saving an object
|
34
|
+
MemoryStorage.save(item)
|
35
|
+
|
36
|
+
# retrieving object by id
|
37
|
+
MemoryStorage.find(1) #=> <Item @id: 1 @name: 'Item one' @amount: 10>
|
38
|
+
|
39
|
+
# retrieving all stored objects
|
40
|
+
MemoryStorage.all #=> [<Item @id: 1 @name: 'Item one' @amount: 10>]
|
41
|
+
```
|
42
|
+
|
43
|
+
## Enumerable
|
44
|
+
|
45
|
+
MemoryStorage includes Enumerable, so every method defined in Enumerable can be
|
46
|
+
used:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
MemoryStorage.save(Item.new(1,'one', 1))
|
50
|
+
MemoryStorage.save(Item.new(2,'one', 2))
|
51
|
+
MemoryStorage.save(Item.new(3,'one', 3))
|
52
|
+
|
53
|
+
MemoryStorage.inject(0) {|sum, item| sum + item.amount } #=> 6
|
54
|
+
```
|
55
|
+
|
56
|
+
## Contributing
|
57
|
+
|
58
|
+
1. Fork it
|
59
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
60
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
61
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
62
|
+
5. Create new Pull Request
|
63
|
+
|
64
|
+
## License
|
65
|
+
|
66
|
+
See the [LICENSE](https://github.com/F-3r/memory_storage/blob/master/LICENSE).
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
class MemoryStorage
|
2
|
+
VERSION = '0.0.1'
|
3
|
+
|
4
|
+
extend Enumerable
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def save(object, identifier_method = :id)
|
8
|
+
storage[object.send identifier_method] = object
|
9
|
+
end
|
10
|
+
|
11
|
+
def find(identifier)
|
12
|
+
storage[identifier]
|
13
|
+
end
|
14
|
+
|
15
|
+
def flush
|
16
|
+
@storage = {}
|
17
|
+
end
|
18
|
+
|
19
|
+
def all
|
20
|
+
storage.values
|
21
|
+
end
|
22
|
+
|
23
|
+
def each(&block)
|
24
|
+
storage.values.each &block
|
25
|
+
end
|
26
|
+
|
27
|
+
def storage
|
28
|
+
@storage ||= {}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'memory_storage'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'memory_storage'
|
7
|
+
spec.version = MemoryStorage::VERSION
|
8
|
+
spec.authors = ['f-3r']
|
9
|
+
spec.email = ['']
|
10
|
+
spec.description = 'Basic in memory persistence for fast prototyping'
|
11
|
+
spec.summary = 'Basic in memory persistence for fast prototyping'
|
12
|
+
spec.homepage = 'https://github.com/F-3r/memory_storage'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'memory_storage'
|
3
|
+
|
4
|
+
Item = Struct.new(:id, :name)
|
5
|
+
|
6
|
+
describe MemoryStorage do
|
7
|
+
before do
|
8
|
+
MemoryStorage.flush
|
9
|
+
end
|
10
|
+
|
11
|
+
let :item do
|
12
|
+
Item.new 1, 'item name'
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '::save' do
|
16
|
+
before do
|
17
|
+
MemoryStorage.save item
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'stores the object' do
|
21
|
+
MemoryStorage.storage[1].must_equal item
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'overwrites object under the same key' do
|
25
|
+
MemoryStorage.save Item.new(1, 'other item')
|
26
|
+
MemoryStorage.storage[1].name.must_equal 'other item'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '::find' do
|
31
|
+
it 'retrieves the object under id' do
|
32
|
+
MemoryStorage.save item
|
33
|
+
MemoryStorage.find(1).must_equal item
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'returns nil if there is no object under such key' do
|
37
|
+
MemoryStorage.find(1).must_be_nil
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '::all' do
|
42
|
+
it 'returns all stored object in an array' do
|
43
|
+
MemoryStorage.save item
|
44
|
+
MemoryStorage.all.must_equal [item]
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'returns empty array if theres no objects in the store' do
|
48
|
+
MemoryStorage.all.must_equal []
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe 'includes enumerable' do
|
53
|
+
before do
|
54
|
+
MemoryStorage.save item
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'can be injected' do
|
58
|
+
MemoryStorage.inject(0) { |sum, item| sum + item.id }.must_equal 1
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: memory_storage
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- f-3r
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-06 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Basic in memory persistence for fast prototyping
|
14
|
+
email:
|
15
|
+
- ''
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- Gemfile
|
22
|
+
- LICENSE
|
23
|
+
- README.md
|
24
|
+
- Rakefile
|
25
|
+
- lib/memory_storage.rb
|
26
|
+
- memory_storage.gemspec
|
27
|
+
- spec/memory_storage_spec.rb
|
28
|
+
homepage: https://github.com/F-3r/memory_storage
|
29
|
+
licenses:
|
30
|
+
- MIT
|
31
|
+
metadata: {}
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
requirements: []
|
47
|
+
rubyforge_project:
|
48
|
+
rubygems_version: 2.2.2
|
49
|
+
signing_key:
|
50
|
+
specification_version: 4
|
51
|
+
summary: Basic in memory persistence for fast prototyping
|
52
|
+
test_files:
|
53
|
+
- spec/memory_storage_spec.rb
|