json_store 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/VERSION +1 -1
- data/json_store.gemspec +3 -3
- data/lib/json_store.rb +38 -2
- data/spec/json_store_spec.rb +6 -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: 5aff085a13c5db3917ffb78d77faecd80f31e7f8
|
4
|
+
data.tar.gz: 462c05300901e67a8fc86a9fc0c798c460a28631
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae1b6f3ad3f482a6e0fb078bdf8fe689c5217ac83dd57dee780fff25d9df6234db5ba072b9b2ca7224c182b8c4333219993f15f04df7ae612cf8c6fa6c1256c7
|
7
|
+
data.tar.gz: 088095aa91ad6508563ba5f8342a42820c8320a71b383ce040691a6ce76b0b5ef4370b0c811d4cf34b2460269a047d0c93d0540bc41346cd4297d22ac9dcd8d1
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/kingsleyh/json_store.svg?branch=master)](https://travis-ci.org/kingsleyh/json_store)
|
4
4
|
[![Gem Version](https://badge.fury.io/rb/json_store.svg)](http://badge.fury.io/rb/json_store)
|
5
|
-
[![json_store
|
5
|
+
[![json_store downloads](http://www.gemetric.me/images/json_store.gif?1)](https://rubygems.org/gems/json_store)
|
6
6
|
|
7
7
|
|
8
8
|
Very simple key/value in memory database using json
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.4
|
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.4 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.4"
|
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-06-
|
14
|
+
s.date = "2014-06-26"
|
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
@@ -2,50 +2,75 @@ require 'oj'
|
|
2
2
|
require 'lock_method'
|
3
3
|
require 'json_select'
|
4
4
|
|
5
|
+
|
6
|
+
# JsonStore is a simple key/value database in which key/values are stored in json format
|
7
|
+
# The basic concept is to pull data into an in memory map and then set or get data and push the data to file.
|
8
|
+
# You can also merge in 2 different ways to decide how you want to use the data coming from file or the map
|
9
|
+
# file locking is handled by the lock_method gem to prevent concurrent access when reading and writing
|
5
10
|
class JsonStore
|
6
11
|
|
12
|
+
# Creates a new store at the path/filename provided to the constructor
|
13
|
+
# e.g. db = JsonStore.new('path/to/some/file.json')
|
7
14
|
def initialize(db)
|
8
15
|
@db = db
|
9
16
|
@map = {}
|
10
17
|
@json_opts = Oj.default_options
|
11
18
|
end
|
12
19
|
|
20
|
+
# Sets data into the memory map by key/value
|
21
|
+
# e.g db.set('name','Kingsley')
|
13
22
|
def set(key, value)
|
14
23
|
@map[key] = value
|
15
24
|
end
|
16
25
|
|
26
|
+
# Gets data from the memory map by key
|
27
|
+
# e.g. db.get('name')
|
17
28
|
def get(key)
|
18
29
|
@map[key]
|
19
30
|
end
|
20
31
|
|
32
|
+
# Returns the whole memory map
|
21
33
|
def all
|
22
34
|
@map
|
23
35
|
end
|
24
36
|
|
37
|
+
# Replaces the memory map with some json directly
|
38
|
+
def set_json(json)
|
39
|
+
@map = Oj.load(json)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Sets the Oj json parser options by overriding defaults. See Oj josn parser docs for more details.
|
25
43
|
def set_json_opts(options)
|
26
44
|
@json_opts.merge!(options)
|
27
45
|
end
|
28
46
|
|
47
|
+
# Lists the Oj json parser options that have been set
|
29
48
|
def get_json_opts
|
30
49
|
@json_opts
|
31
50
|
end
|
32
51
|
|
52
|
+
#�Clears the memory map
|
33
53
|
def clear
|
34
54
|
@map = {}
|
35
55
|
end
|
36
56
|
|
57
|
+
# Returns the whole memory map as a json string
|
37
58
|
def all_as_json
|
38
59
|
Oj.dump(@map,@json_opts)
|
39
60
|
end
|
40
61
|
|
62
|
+
# Uses the ruby gem for JSONSelect to search basic json. (doesn't work on ruby objects stored as json by Oj :object mode)
|
63
|
+
# See the JSONSelect website / json_select ruby gem docs for more details
|
41
64
|
def search(selector, kind=:matches)
|
42
65
|
JSONSelect(selector).send(kind, @map)
|
43
66
|
end
|
44
67
|
|
68
|
+
# Gets data from the memory map by key but as a json string
|
45
69
|
def get_as_json(key)
|
46
70
|
Oj.dump(@map[key],@json_opts)
|
47
71
|
end
|
48
72
|
|
73
|
+
# Pulls data from file into the memory map - replacing the memory map with the data from file.
|
49
74
|
def pull
|
50
75
|
begin
|
51
76
|
@map = read_data
|
@@ -55,6 +80,7 @@ class JsonStore
|
|
55
80
|
end
|
56
81
|
end
|
57
82
|
|
83
|
+
# Pushes the data in the memory map to file
|
58
84
|
def push
|
59
85
|
begin
|
60
86
|
write_data
|
@@ -64,6 +90,11 @@ class JsonStore
|
|
64
90
|
end
|
65
91
|
end
|
66
92
|
|
93
|
+
# Merges the data in 2 ways
|
94
|
+
# 1. db.merge(:into_remote) is the default and will merge data from the memory map into data from file and assign the result to the memory map
|
95
|
+
# 2. db.merge(:into_local) will merge data from file into the memory map and assign the result to the memory map.
|
96
|
+
# into_remote will essentially load the memory map data into the file data thus overwriting the file data with the memory map data where the keys match
|
97
|
+
# into_local will essentially merge the file data into the memory map thus overwriting the memory map data with the file data where the keys match
|
67
98
|
def merge(direction=:into_remote)
|
68
99
|
begin
|
69
100
|
@map = direction == :into_local ? @map.merge(read_data) : read_data.merge(@map)
|
@@ -73,6 +104,7 @@ class JsonStore
|
|
73
104
|
end
|
74
105
|
end
|
75
106
|
|
107
|
+
# Get the path to the datastore json file
|
76
108
|
def db_path
|
77
109
|
@db
|
78
110
|
end
|
@@ -86,8 +118,12 @@ class JsonStore
|
|
86
118
|
def read_data
|
87
119
|
data = {}
|
88
120
|
if File.exists?(@db)
|
89
|
-
f = File.
|
90
|
-
|
121
|
+
f = File.open(@db)
|
122
|
+
begin
|
123
|
+
data = Oj.load(f)
|
124
|
+
rescue => e
|
125
|
+
puts "WARNING: #{e}"
|
126
|
+
end
|
91
127
|
end
|
92
128
|
data
|
93
129
|
end
|
data/spec/json_store_spec.rb
CHANGED
@@ -132,6 +132,12 @@ describe JsonStore do
|
|
132
132
|
expect(db.get_json_opts[:mode]).to eq(:compat)
|
133
133
|
end
|
134
134
|
|
135
|
+
it 'should set the map directly with json' do
|
136
|
+
db = JsonStore.new('test')
|
137
|
+
db.set_json('{"name":{"first":"Kingsley"}}')
|
138
|
+
expect(db.get('name')).to eq({'first' =>'Kingsley'})
|
139
|
+
end
|
140
|
+
|
135
141
|
private
|
136
142
|
|
137
143
|
def create_new_db(path, content={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.4
|
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-06-
|
11
|
+
date: 2014-06-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oj
|