json_store 0.1.1 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +39 -1
- data/Rakefile +12 -0
- data/VERSION +1 -1
- data/json_store.gemspec +3 -5
- data/lib/json_store.rb +12 -3
- data/spec/json_store_spec.rb +5 -0
- metadata +1 -3
- data/spec/test_data/test2.json +0 -1
- data/spec/test_data/test3.json +0 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b77ef3a7d0a66c2d77dd27125f3cdb2099f69835
|
4
|
+
data.tar.gz: d20ae02567719e9685dd1b85b7d600bdede5ef12
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 508a228e2c509a08906fcd5db9c8425200efa314a8967cc3ca8969ec748c2ea0d68f432cc0d6be9a9a7ff5a37e7a3f39ae0ce7975b133c5df52b230c36e98c77
|
7
|
+
data.tar.gz: a2e576228c8ee7df9023324f1e55556fdafea6bd45444b14413a8f883fdb83d78786d937a063ec96a21b5a7262a2a0f35877d5ebb3cd954a32464c7df67e3969
|
data/README.md
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
# json_store
|
2
2
|
|
3
|
+
[![Build Status](https://travis-ci.org/kingsleyh/json_store.svg?branch=master)](https://travis-ci.org/kingsleyh/json_store)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/json_store.svg)](http://badge.fury.io/rb/json_store)
|
5
|
+
[![json_store Downloads](http://www.gemetric.me/images/json_store.gif)](https://rubygems.org/gems/json_store)
|
6
|
+
|
7
|
+
|
3
8
|
Very simple key/value in memory database using json
|
4
9
|
|
5
10
|
## Install
|
@@ -16,7 +21,7 @@ require 'json_store'
|
|
16
21
|
db = JsonStore.new('names.db')
|
17
22
|
db.pull
|
18
23
|
db.set(:name,'Kingsley')
|
19
|
-
p db.get(:name)
|
24
|
+
p db.get(:name # Kingsley)
|
20
25
|
db.merge
|
21
26
|
db.push
|
22
27
|
```
|
@@ -61,6 +66,9 @@ There are only a handful of commands:
|
|
61
66
|
* search
|
62
67
|
* all
|
63
68
|
* all_as_json
|
69
|
+
* clear
|
70
|
+
* set_json_opts
|
71
|
+
* get_json_opts
|
64
72
|
|
65
73
|
The basic concept is that you create new db with the JsonStore.new and if the db is already a valid json file you can do pull to populate the in memory map. If no file exists then
|
66
74
|
one will be created.
|
@@ -114,6 +122,36 @@ Search takes 2 parameters: the selector and a match kind - which is set to :mat
|
|
114
122
|
* :match - returns only the first match
|
115
123
|
* :test - returns true or false if there is a match
|
116
124
|
|
125
|
+
## Persisting Objects
|
126
|
+
|
127
|
+
When persisting objects - such as the Person class mentioned earlier - the default method of serialization used by Oj is :object. Which means it will expand the object into json format with an entry
|
128
|
+
containg an O to denote it's an object. Oj has several other notations for Array etc.
|
129
|
+
|
130
|
+
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. Here is an example:
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
class Car
|
135
|
+
|
136
|
+
attr_reader :doors,:wheels
|
137
|
+
|
138
|
+
def initialize(doors,wheels)
|
139
|
+
@doors = doors
|
140
|
+
@wheels = wheels
|
141
|
+
end
|
142
|
+
|
143
|
+
def to_json
|
144
|
+
puts %Q{ { "doors":#{@doors},"wheels",#{@wheels}} }
|
145
|
+
end
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
db = JsonStore.new('cars')
|
150
|
+
db.set_json_opts(mode: :compat,indent: 2)
|
151
|
+
db.set(:cars,[Car.new(4,4),Car.new(2,3])
|
152
|
+
p db.all_as_json
|
153
|
+
|
154
|
+
```
|
117
155
|
|
118
156
|
## Contributing to json_store
|
119
157
|
|
data/Rakefile
CHANGED
@@ -25,4 +25,16 @@ Jeweler::Tasks.new do |gem|
|
|
25
25
|
end
|
26
26
|
Jeweler::RubygemsDotOrgTasks.new
|
27
27
|
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
28
40
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.3
|
data/json_store.gemspec
CHANGED
@@ -2,11 +2,11 @@
|
|
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.3 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.3"
|
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"]
|
@@ -29,9 +29,7 @@ Gem::Specification.new do |s|
|
|
29
29
|
"json_store.gemspec",
|
30
30
|
"lib/json_store.rb",
|
31
31
|
"spec/json_store_spec.rb",
|
32
|
-
"spec/test_data/test1.json"
|
33
|
-
"spec/test_data/test2.json",
|
34
|
-
"spec/test_data/test3.json"
|
32
|
+
"spec/test_data/test1.json"
|
35
33
|
]
|
36
34
|
s.homepage = "http://github.com/kingsleyh/json_store"
|
37
35
|
s.licenses = ["MIT"]
|
data/lib/json_store.rb
CHANGED
@@ -7,6 +7,7 @@ class JsonStore
|
|
7
7
|
def initialize(db)
|
8
8
|
@db = db
|
9
9
|
@map = {}
|
10
|
+
@json_opts = Oj.default_options
|
10
11
|
end
|
11
12
|
|
12
13
|
def set(key, value)
|
@@ -21,12 +22,20 @@ class JsonStore
|
|
21
22
|
@map
|
22
23
|
end
|
23
24
|
|
25
|
+
def set_json_opts(options)
|
26
|
+
@json_opts.merge!(options)
|
27
|
+
end
|
28
|
+
|
29
|
+
def get_json_opts
|
30
|
+
@json_opts
|
31
|
+
end
|
32
|
+
|
24
33
|
def clear
|
25
34
|
@map = {}
|
26
35
|
end
|
27
36
|
|
28
37
|
def all_as_json
|
29
|
-
Oj.dump(@map)
|
38
|
+
Oj.dump(@map,@json_opts)
|
30
39
|
end
|
31
40
|
|
32
41
|
def search(selector, kind=:matches)
|
@@ -34,7 +43,7 @@ class JsonStore
|
|
34
43
|
end
|
35
44
|
|
36
45
|
def get_as_json(key)
|
37
|
-
Oj.dump(@map[key])
|
46
|
+
Oj.dump(@map[key],@json_opts)
|
38
47
|
end
|
39
48
|
|
40
49
|
def pull
|
@@ -87,7 +96,7 @@ class JsonStore
|
|
87
96
|
|
88
97
|
def write_data
|
89
98
|
f = File.new(@db, 'w')
|
90
|
-
f.write(Oj.dump(@map))
|
99
|
+
f.write(Oj.dump(@map,@json_opts))
|
91
100
|
f.close
|
92
101
|
end
|
93
102
|
|
data/spec/json_store_spec.rb
CHANGED
@@ -124,7 +124,12 @@ describe JsonStore do
|
|
124
124
|
db2 = JsonStore.new(File.dirname(__FILE__) + '/test_data/test3.json')
|
125
125
|
db2.pull
|
126
126
|
expect(db2.get(:slot)).to match(/PM/)
|
127
|
+
end
|
127
128
|
|
129
|
+
it 'should set oj json parser default options' do
|
130
|
+
db = JsonStore.new('test')
|
131
|
+
db.set_json_opts(mode: :compat)
|
132
|
+
expect(db.get_json_opts[:mode]).to eq(:compat)
|
128
133
|
end
|
129
134
|
|
130
135
|
private
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kingsley Hendrickse
|
@@ -113,8 +113,6 @@ files:
|
|
113
113
|
- lib/json_store.rb
|
114
114
|
- spec/json_store_spec.rb
|
115
115
|
- spec/test_data/test1.json
|
116
|
-
- spec/test_data/test2.json
|
117
|
-
- spec/test_data/test3.json
|
118
116
|
homepage: http://github.com/kingsleyh/json_store
|
119
117
|
licenses:
|
120
118
|
- MIT
|
data/spec/test_data/test2.json
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{":name":"Kingsley",":date":{":day":"Mon",":year":"2014"},":slot":"PM"}
|
data/spec/test_data/test3.json
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{":name":"Kingsley",":slot":"1_PM"}
|