json_store 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -2
- data/VERSION +1 -1
- data/json_store.gemspec +3 -3
- data/lib/json_store.rb +12 -1
- data/spec/json_store_spec.rb +18 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9f830215d37fd4db60d5e6234a11fa3770d95f56
|
4
|
+
data.tar.gz: 483473da12bd432db0e67c29ce716194d3ceba49
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 06826fc502d8e81fe12b6e1aafd56eacf0a1838bd65a06d719cffb27a197c8bf4f1dcdf53dc5d74acbd7923e149b474315ffb1b20a78d289e1c7ec2169b98eb7
|
7
|
+
data.tar.gz: b55748e94bbf546da1529be90cbfb9f701b2d8762cbabf140938e1e40a52421f60620d532ca538f8085de24c00f56ec33a9e1dd3ed1cd045f09f48a038c406d8
|
data/README.md
CHANGED
@@ -24,6 +24,11 @@ db.set(:name,'Kingsley')
|
|
24
24
|
p db.get(:name # Kingsley)
|
25
25
|
db.merge
|
26
26
|
db.push
|
27
|
+
|
28
|
+
# if removing items use write instead of merge and push
|
29
|
+
db.pull
|
30
|
+
db.remove(:name)
|
31
|
+
db.write # write replaces file with in memory data
|
27
32
|
```
|
28
33
|
|
29
34
|
* Peristing more complex things like custom objects
|
@@ -105,7 +110,8 @@ Get just finds a value by key e.g.
|
|
105
110
|
db.get(:name) # Kingsley
|
106
111
|
```
|
107
112
|
|
108
|
-
Search uses the json select gem (https://github.com/fd/json_select) and (http://jsonselect.org/#overview) to provide advanced searching capability.
|
113
|
+
Search uses the json select gem (https://github.com/fd/json_select) and (http://jsonselect.org/#overview) to provide advanced searching capability. If you persist custom ruby objects however this
|
114
|
+
searching method will not work as it will only work with the standard json types. But then you can just use regular ruby to find things e.g. take, select, reject, find. sort_by etc
|
109
115
|
|
110
116
|
Read the documentation in the links above for a better idea how to use it - here is a simple example:
|
111
117
|
|
@@ -128,7 +134,9 @@ When persisting objects - such as the Person class mentioned earlier - the defau
|
|
128
134
|
containg an O to denote it's an object. Oj has several other notations for Array etc.
|
129
135
|
|
130
136
|
But if you want to have nicer json so you can use it elsewhere as a feed for example - you might prefer to use the :compat mode which looks for a to_json method on the object and uses that
|
131
|
-
to serialize it. So you can add your own to_json method.
|
137
|
+
to serialize it. So you can add your own to_json method. Please note that when retrieving data again it will be as hash/array data not the ruby custom object initially created -
|
138
|
+
because the :compat mode will force serialization to be the standard supported json - e.g. hash/arrays. If you want to persist objects and retrieve them then stick with the default
|
139
|
+
serialization. Here is an example of using compat:
|
132
140
|
|
133
141
|
```ruby
|
134
142
|
class Car
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.5
|
data/json_store.gemspec
CHANGED
@@ -2,16 +2,16 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: json_store 0.1.
|
5
|
+
# stub: json_store 0.1.5 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "json_store"
|
9
|
-
s.version = "0.1.
|
9
|
+
s.version = "0.1.5"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib"]
|
13
13
|
s.authors = ["Kingsley Hendrickse"]
|
14
|
-
s.date = "2014-
|
14
|
+
s.date = "2014-09-21"
|
15
15
|
s.description = "A simple in memory key/value database using json that writes to file"
|
16
16
|
s.email = "kingsleyhendrickse@me.com"
|
17
17
|
s.extra_rdoc_files = [
|
data/lib/json_store.rb
CHANGED
@@ -29,6 +29,17 @@ class JsonStore
|
|
29
29
|
@map[key]
|
30
30
|
end
|
31
31
|
|
32
|
+
# Removes the data from memory map by key
|
33
|
+
# e.g. db.remove('name')
|
34
|
+
def remove(key)
|
35
|
+
@map.delete(key)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Writes the in memory data to file by replacing file
|
39
|
+
def write
|
40
|
+
write_data
|
41
|
+
end
|
42
|
+
|
32
43
|
# Returns the whole memory map
|
33
44
|
def all
|
34
45
|
@map
|
@@ -49,7 +60,7 @@ class JsonStore
|
|
49
60
|
@json_opts
|
50
61
|
end
|
51
62
|
|
52
|
-
#�Clears the memory map
|
63
|
+
#�Clears the memory map
|
53
64
|
def clear
|
54
65
|
@map = {}
|
55
66
|
end
|
data/spec/json_store_spec.rb
CHANGED
@@ -34,6 +34,24 @@ describe JsonStore do
|
|
34
34
|
expect(db.all).to eq({name: 'Kostas', slot: 'PM'})
|
35
35
|
end
|
36
36
|
|
37
|
+
it 'should remove data' do
|
38
|
+
db = create_new_db('test2.json')
|
39
|
+
expect(db.all).to eq({name: 'Kingsley'})
|
40
|
+
db.remove(:name)
|
41
|
+
db.write
|
42
|
+
expect(db.all).to eq({})
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should write data' do
|
46
|
+
db = create_new_db('test2.json')
|
47
|
+
expect(db.all).to eq({name: 'Kingsley'})
|
48
|
+
db.set(:name,'kings')
|
49
|
+
db.set(:slot,'PM')
|
50
|
+
db.write
|
51
|
+
db.pull
|
52
|
+
expect(db.all).to eq({name:'kings',slot:'PM'})
|
53
|
+
end
|
54
|
+
|
37
55
|
it 'should merge file changes into memory map on merge into_local' do
|
38
56
|
db = create_new_db('test2.json')
|
39
57
|
expect(db.all).to eq({name: 'Kingsley'})
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_store
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kingsley Hendrickse
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-09-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oj
|