memstore 1.2.1 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ module Refinements
2
+
3
+ refine Array do
4
+ def ===(obj)
5
+ include?(obj)
6
+ end
7
+ end
8
+
9
+ end
@@ -1,5 +1,3 @@
1
- # encoding: utf-8
2
-
3
- module MemStore
4
- VERSION = "1.2.1"
1
+ class MemStore
2
+ VERSION = "2.0.1"
5
3
  end
data/memstore.gemspec CHANGED
@@ -1,18 +1,17 @@
1
- # -*- encoding: utf-8 -*-
2
- lib = File.expand_path("../lib", __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
1
+ $:.push File.expand_path("../lib", __FILE__)
4
2
  require "memstore/version"
5
3
 
6
4
  Gem::Specification.new do |gem|
7
- gem.name = "memstore"
8
- gem.version = MemStore::VERSION
9
- gem.summary = %q{A simple, in-memory data store.}
10
- gem.description = %q{MemStore is a simple in-memory data store that supports adding, retrieving and deleting items as well as complex search queries and easy serialization.}
11
- gem.authors = ["Sebastian Klepper"]
12
- gem.email = ["sk@sebastianklepper.com"]
13
- gem.homepage = "https://github.com/sklppr/memstore"
14
- gem.files = `git ls-files`.split($/)
15
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
- gem.require_paths = ["lib"]
5
+ gem.name = "memstore"
6
+ gem.version = MemStore::VERSION
7
+ gem.required_ruby_version = ">= 2.1.0dev"
8
+ gem.summary = "A simple in-memory data store."
9
+ gem.description = "MemStore is a simple in-memory data store that supports complex search queries."
10
+ gem.authors = ["Sebastian Klepper"]
11
+ gem.license = "MIT"
12
+ gem.homepage = "https://github.com/sklppr/memstore"
13
+ gem.files = `git ls-files`.split($/)
14
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
+ gem.require_paths = ["lib"]
18
17
  end
@@ -0,0 +1,67 @@
1
+ require "minitest/autorun"
2
+ require "memstore"
3
+
4
+ describe MemStore do
5
+
6
+ before do
7
+ @store = MemStore.new(key: :to_i)
8
+ 10.times { |i| @store << i.to_f }
9
+ end
10
+
11
+ it "provides access to its internal hash" do
12
+ hash = { 1 => 2.0, 2 => 4.0, 3 => 6.0 }
13
+ @store.items = hash
14
+ @store.items.must_equal(hash)
15
+ end
16
+
17
+ it "returns all items as an array" do
18
+ @store.all.must_equal(@store.items.values)
19
+ end
20
+
21
+ it "returns the overall number of items" do
22
+ @store.size.must_equal(10)
23
+ end
24
+
25
+ it "accepts a single item to add" do
26
+ @store << 10.0
27
+ @store[10].must_equal(10.0)
28
+ end
29
+
30
+ it "accepts multiple items to add" do
31
+ @store.add(10.0, 11.0, 12.0)
32
+ @store[10].must_equal(10.0)
33
+ @store[11].must_equal(11.0)
34
+ @store[12].must_equal(12.0)
35
+ end
36
+
37
+ it "returns a single item by itself" do
38
+ @store[3].must_equal(3.0)
39
+ end
40
+
41
+ it "returns a single item as an array" do
42
+ @store.get(3).must_equal([3.0])
43
+ end
44
+
45
+ it "returns multiple items as an array" do
46
+ @store.get(3, 4, 5, 6).must_equal([3.0, 4.0, 5.0, 6.0])
47
+ end
48
+
49
+ it "deletes a single item by reference and returns it" do
50
+ @store.delete_item(3.0).must_equal(3.0)
51
+ end
52
+
53
+ it "deletes multiple items by reference and returns them" do
54
+ @store.delete_items(3.0, 4.0, 5.0, 6.0).must_equal([3.0, 4.0, 5.0, 6.0])
55
+ @store.all.must_equal([0.0, 1.0, 2.0, 7.0, 8.0, 9.0])
56
+ end
57
+
58
+ it "deletes a single item by key and returns it by itself" do
59
+ @store.delete_key(3).must_equal(3.0)
60
+ end
61
+
62
+ it "deletes multiple items by key and returns them as an array" do
63
+ @store.delete_keys(3, 4, 5, 6).must_equal([3.0, 4.0, 5.0, 6.0])
64
+ @store.all.must_equal([0.0, 1.0, 2.0, 7.0, 8.0, 9.0])
65
+ end
66
+
67
+ end
@@ -0,0 +1,66 @@
1
+ require "minitest/autorun"
2
+ require "memstore"
3
+
4
+ describe MemStore do
5
+
6
+ SimpleDummy = Struct.new(:id)
7
+
8
+ def id(dummy)
9
+ dummy.id
10
+ end
11
+
12
+ before do
13
+ @dummy = SimpleDummy.new(42)
14
+ @hash = { id: 42 }
15
+ end
16
+
17
+ it "accepts items to add at instantiation" do
18
+ items = [1, 2, 3, 4, 5]
19
+ store = MemStore.new(items: items)
20
+ store.items.values.must_equal(items)
21
+ end
22
+
23
+ it "indexes items using Object#hash by default" do
24
+ store = MemStore.new
25
+ store.add(@dummy)
26
+ store[@dummy.hash].must_equal(@dummy)
27
+ end
28
+
29
+ it "indexes items using a given method name" do
30
+ store = MemStore.new(key: :id)
31
+ store.add(@dummy)
32
+ store[@dummy.id].must_equal(@dummy)
33
+ end
34
+
35
+ it "indexes items using a given lambda" do
36
+ store = MemStore.new(key: -> item { item.id })
37
+ store.add(@dummy)
38
+ store[@dummy.id].must_equal(@dummy)
39
+ end
40
+
41
+ it "indexes items using a given method" do
42
+ store = MemStore.new(key: method(:id))
43
+ store.add(@dummy)
44
+ store[@dummy.id].must_equal(@dummy)
45
+ end
46
+
47
+ it "accesses attributes using a given method name" do
48
+ store = MemStore.new(key: :id, access: :[])
49
+ store.add(@hash)
50
+ store[@hash[:id]].must_equal(@hash)
51
+ end
52
+
53
+ it "accesses attributes using a given lambda" do
54
+ store = MemStore.new(key: :id, access: -> item, attribute { item[attribute] })
55
+ store.add(@hash)
56
+ store[@hash[:id]].must_equal(@hash)
57
+ end
58
+
59
+ it "indexes items using Object#hash when an access method but no key is specified" do
60
+ store = MemStore.new(access: :[])
61
+ store.add(@hash)
62
+ store[@hash.hash].must_equal(@hash)
63
+ store[@hash[:id]].must_equal(nil)
64
+ end
65
+
66
+ end
data/spec/queries_spec.rb CHANGED
@@ -1,107 +1,135 @@
1
1
  require "minitest/autorun"
2
- require "tempfile"
2
+ require "memstore"
3
3
 
4
- class Dummy
4
+ describe MemStore do
5
5
 
6
- attr_accessor :id, :name, :child
7
-
8
- def initialize(id, name=nil, child=nil)
9
- @id, @name, @child = id, name, child
10
- end
11
-
12
- def ==(obj)
13
- obj.is_a?(Dummy) &&
14
- obj.id == @id &&
15
- obj.name == @name &&
16
- obj.child == @child
17
- end
18
-
19
- end
20
-
21
- describe MemStore::ObjectStore do
6
+ ComplexDummy = Struct.new(:id, :name, :child)
22
7
 
23
8
  before do
24
- @store = MemStore::ObjectStore.new
25
- strings = %w(foo moo boo faa maa baa foa moa boa lao)
9
+ @store = MemStore.new(key: :id)
10
+ strings = %w[foo moo boo faa maa baa foa moa boa lao]
26
11
  classes = [String, Array]
27
- 10.times do |i|
28
- @store << Dummy.new(i, strings[i], classes[i%2].new)
29
- end
12
+ 10.times { |i| @store << ComplexDummy.new(i, strings[i], classes[i%2].new) }
30
13
  end
31
14
 
15
+ # find
16
+
32
17
  it "finds all items fulfilling all conditions" do
33
18
  matches = @store.find_all(id: 3..7, child: String)
34
- matches.collect{ |m| m.id }.must_equal [4, 6]
19
+ matches.collect(&:id).must_equal([4, 6])
35
20
  end
36
21
 
37
22
  it "finds all items fulfilling at least one condition" do
38
23
  matches = @store.find_any(id: 3..7, child: String)
39
- matches.collect{ |m| m.id }.must_equal [0, 2, 3, 4, 5, 6, 7, 8]
24
+ matches.collect(&:id).must_equal([0, 2, 3, 4, 5, 6, 7, 8])
40
25
  end
41
26
 
42
27
  it "finds all items fulfilling exactly one condition" do
43
28
  matches = @store.find_one(name: /o/, child: String)
44
- matches.collect{ |m| m.id }.must_equal [1, 4, 7, 9]
29
+ matches.collect(&:id).must_equal([1, 4, 7, 9])
45
30
  end
46
31
 
47
32
  it "finds all items violating at least one condition" do
48
33
  matches = @store.find_not_all(name: /o/, child: String)
49
- matches.collect{ |m| m.id }.must_equal [1, 3, 4, 5, 7, 9]
34
+ matches.collect(&:id).must_equal([1, 3, 4, 5, 7, 9])
50
35
  end
51
36
 
52
37
  it "finds all items violating all conditions" do
53
38
  matches = @store.find_none(name: /o/, child: String)
54
- matches.collect{ |m| m.id }.must_equal [3, 5]
39
+ matches.collect(&:id).must_equal([3, 5])
55
40
  end
56
41
 
42
+ # first
43
+
57
44
  it "finds the first item fulfilling all conditions" do
58
45
  match = @store.first_all(id: 3..7, child: String)
59
- match.id.must_equal 4
46
+ match.id.must_equal(4)
60
47
  end
61
48
 
62
49
  it "finds the first item fulfilling at least one condition" do
63
50
  match = @store.first_any(id: 3..7, child: String)
64
- match.id.must_equal 0
51
+ match.id.must_equal(0)
65
52
  end
66
53
 
67
54
  it "finds the first item fulfilling exactly one condition" do
68
55
  match = @store.first_one(name: /o/, child: String)
69
- match.id.must_equal 1
56
+ match.id.must_equal(1)
70
57
  end
71
58
 
72
59
  it "finds the first item violating at least one condition" do
73
60
  match = @store.first_not_all(name: /o/, child: String)
74
- match.id.must_equal 1
61
+ match.id.must_equal(1)
75
62
  end
76
63
 
77
64
  it "finds the first item violating all conditions" do
78
65
  match = @store.first_none(name: /o/, child: String)
79
- match.id.must_equal 3
66
+ match.id.must_equal(3)
80
67
  end
81
68
 
69
+ # count
70
+
82
71
  it "counts all items fulfilling all conditions" do
83
72
  count = @store.count_all(id: 3..7, child: String)
84
- count.must_equal [4, 6].length
73
+ count.must_equal([4, 6].length)
85
74
  end
86
75
 
87
76
  it "counts all items fulfilling at least one condition" do
88
77
  count = @store.count_any(id: 3..7, child: String)
89
- count.must_equal [0, 2, 3, 4, 5, 6, 7, 8].length
78
+ count.must_equal([0, 2, 3, 4, 5, 6, 7, 8].length)
90
79
  end
91
80
 
92
81
  it "counts all items fulfilling exactly one condition" do
93
82
  count = @store.count_one(name: /o/, child: String)
94
- count.must_equal [1, 4, 7, 9].length
83
+ count.must_equal([1, 4, 7, 9].length)
95
84
  end
96
85
 
97
86
  it "counts all items violating at least one condition" do
98
87
  count = @store.count_not_all(name: /o/, child: String)
99
- count.must_equal [1, 3, 4, 5, 7, 9].length
88
+ count.must_equal([1, 3, 4, 5, 7, 9].length)
100
89
  end
101
90
 
102
91
  it "counts all items violating all conditions" do
103
92
  count = @store.count_none(name: /o/, child: String)
104
- count.must_equal [3, 5].length
93
+ count.must_equal([3, 5].length)
94
+ end
95
+
96
+ # delete
97
+
98
+ it "deletes all items fulfilling all conditions" do
99
+ deleted = @store.delete_all(id: 3..7, child: String)
100
+ deleted.collect(&:id).must_equal([4, 6])
101
+ @store.all.collect(&:id).must_equal([0, 1, 2, 3, 5, 7, 8, 9])
102
+ end
103
+
104
+ it "deletes all items fulfilling at least one condition" do
105
+ deleted = @store.delete_any(id: 3..7, child: String)
106
+ deleted.collect(&:id).must_equal([0, 2, 3, 4, 5, 6, 7, 8])
107
+ @store.all.collect(&:id).must_equal([1, 9])
108
+ end
109
+
110
+ it "deletes all items fulfilling exactly one condition" do
111
+ deleted = @store.delete_one(name: /o/, child: String)
112
+ deleted.collect(&:id).must_equal([1, 4, 7, 9])
113
+ @store.all.collect(&:id).must_equal([0, 2, 3, 5, 6, 8])
114
+ end
115
+
116
+ it "deletes all items violating at least one condition" do
117
+ deleted = @store.delete_not_all(name: /o/, child: String)
118
+ deleted.collect(&:id).must_equal([1, 3, 4, 5, 7, 9])
119
+ @store.all.collect(&:id).must_equal([0, 2, 6, 8])
120
+ end
121
+
122
+ it "deletes all items violating all conditions" do
123
+ deleted = @store.delete_none(name: /o/, child: String)
124
+ deleted.collect(&:id).must_equal([3, 5])
125
+ @store.all.collect(&:id).must_equal([0, 1, 2, 4, 6, 7, 8, 9])
126
+ end
127
+
128
+ # Array refinement
129
+
130
+ it "finds items included in an Array" do
131
+ matches = @store.find(id: [1, 3, 5])
132
+ matches.collect(&:id).must_equal([1, 3, 5])
105
133
  end
106
134
 
107
135
  end
metadata CHANGED
@@ -1,72 +1,62 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: memstore
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
5
- prerelease:
4
+ version: 2.0.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - Sebastian Klepper
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-02-21 00:00:00.000000000 Z
11
+ date: 2013-12-18 00:00:00.000000000 Z
13
12
  dependencies: []
14
- description: MemStore is a simple in-memory data store that supports adding, retrieving
15
- and deleting items as well as complex search queries and easy serialization.
16
- email:
17
- - sk@sebastianklepper.com
13
+ description: MemStore is a simple in-memory data store that supports complex search
14
+ queries.
15
+ email:
18
16
  executables: []
19
17
  extensions: []
20
18
  extra_rdoc_files: []
21
19
  files:
22
- - .gitignore
20
+ - ".gitignore"
21
+ - ".rdoc_options"
23
22
  - Gemfile
24
23
  - LICENSE.txt
25
24
  - README.md
26
25
  - Rakefile
27
26
  - lib/memstore.rb
28
- - lib/memstore/hashstore.rb
29
- - lib/memstore/json.rb
30
- - lib/memstore/msgpack.rb
31
- - lib/memstore/objectstore.rb
27
+ - lib/memstore/core.rb
32
28
  - lib/memstore/queries.rb
29
+ - lib/memstore/refinements.rb
33
30
  - lib/memstore/version.rb
34
- - lib/memstore/yaml.rb
35
31
  - memstore.gemspec
36
- - spec/hashstore_spec.rb
37
- - spec/json_spec.rb
38
- - spec/msgpack_spec.rb
39
- - spec/objectstore_spec.rb
32
+ - spec/core_methods_spec.rb
33
+ - spec/core_setup_spec.rb
40
34
  - spec/queries_spec.rb
41
- - spec/yaml_spec.rb
42
35
  homepage: https://github.com/sklppr/memstore
43
- licenses: []
36
+ licenses:
37
+ - MIT
38
+ metadata: {}
44
39
  post_install_message:
45
40
  rdoc_options: []
46
41
  require_paths:
47
42
  - lib
48
43
  required_ruby_version: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - ">="
52
46
  - !ruby/object:Gem::Version
53
- version: '0'
47
+ version: 2.1.0dev
54
48
  required_rubygems_version: !ruby/object:Gem::Requirement
55
- none: false
56
49
  requirements:
57
- - - ! '>='
50
+ - - ">="
58
51
  - !ruby/object:Gem::Version
59
52
  version: '0'
60
53
  requirements: []
61
54
  rubyforge_project:
62
- rubygems_version: 1.8.25
55
+ rubygems_version: 2.2.0
63
56
  signing_key:
64
- specification_version: 3
65
- summary: A simple, in-memory data store.
57
+ specification_version: 4
58
+ summary: A simple in-memory data store.
66
59
  test_files:
67
- - spec/hashstore_spec.rb
68
- - spec/json_spec.rb
69
- - spec/msgpack_spec.rb
70
- - spec/objectstore_spec.rb
60
+ - spec/core_methods_spec.rb
61
+ - spec/core_setup_spec.rb
71
62
  - spec/queries_spec.rb
72
- - spec/yaml_spec.rb