json_store 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 49e14dd153040a235c67133f3df7faeb1357ae9f
4
+ data.tar.gz: b7f1b9de726f8f9da032f01260ee3f4365f94635
5
+ SHA512:
6
+ metadata.gz: a85d3a673bbce87fc59532f65895852e76afe1c83c899a4777a72223ceb9ec9dbb072816b2f3c95cde2090a1110ba090751f490f1c1418fc5338c5f904ebdf33
7
+ data.tar.gz: 3246102a9104e11127f467ee7a542789c9e6812c64527827a9b700469d9e9b00da135243a329f0daeca2a40650a7eca2c0fd00fba1db138f43a45d849e008f1f
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'oj'
4
+ gem 'lock_method'
5
+ gem 'json_select'
6
+
7
+ group :development do
8
+ gem 'rspec','~> 3.0.0.rc1'
9
+ gem 'bundler', '~> 1.0'
10
+ gem 'jeweler', '~> 2.0.1'
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,92 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (4.1.1)
5
+ i18n (~> 0.6, >= 0.6.9)
6
+ json (~> 1.7, >= 1.7.7)
7
+ minitest (~> 5.1)
8
+ thread_safe (~> 0.1)
9
+ tzinfo (~> 1.1)
10
+ addressable (2.3.6)
11
+ builder (3.2.2)
12
+ cache (0.4.0)
13
+ descendants_tracker (0.0.4)
14
+ thread_safe (~> 0.3, >= 0.3.1)
15
+ diff-lcs (1.2.5)
16
+ faraday (0.9.0)
17
+ multipart-post (>= 1.2, < 3)
18
+ git (1.2.7)
19
+ github_api (0.11.3)
20
+ addressable (~> 2.3)
21
+ descendants_tracker (~> 0.0.1)
22
+ faraday (~> 0.8, < 0.10)
23
+ hashie (>= 1.2)
24
+ multi_json (>= 1.7.5, < 2.0)
25
+ nokogiri (~> 1.6.0)
26
+ oauth2
27
+ hashie (3.0.0)
28
+ highline (1.6.21)
29
+ i18n (0.6.9)
30
+ jeweler (2.0.1)
31
+ builder
32
+ bundler (>= 1.0)
33
+ git (>= 1.2.5)
34
+ github_api
35
+ highline (>= 1.6.15)
36
+ nokogiri (>= 1.5.10)
37
+ rake
38
+ rdoc
39
+ json (1.8.1)
40
+ json_select (0.1.4)
41
+ treetop
42
+ jwt (1.0.0)
43
+ lock_method (0.5.5)
44
+ activesupport
45
+ cache (>= 0.2.1)
46
+ mini_portile (0.6.0)
47
+ minitest (5.3.5)
48
+ multi_json (1.10.1)
49
+ multi_xml (0.5.5)
50
+ multipart-post (2.0.0)
51
+ nokogiri (1.6.2.1)
52
+ mini_portile (= 0.6.0)
53
+ oauth2 (0.9.4)
54
+ faraday (>= 0.8, < 0.10)
55
+ jwt (~> 1.0)
56
+ multi_json (~> 1.3)
57
+ multi_xml (~> 0.5)
58
+ rack (~> 1.2)
59
+ oj (2.9.6)
60
+ polyglot (0.3.5)
61
+ rack (1.5.2)
62
+ rake (10.3.2)
63
+ rdoc (4.1.1)
64
+ json (~> 1.4)
65
+ rspec (3.0.0)
66
+ rspec-core (~> 3.0.0)
67
+ rspec-expectations (~> 3.0.0)
68
+ rspec-mocks (~> 3.0.0)
69
+ rspec-core (3.0.1)
70
+ rspec-support (~> 3.0.0)
71
+ rspec-expectations (3.0.1)
72
+ diff-lcs (>= 1.2.0, < 2.0)
73
+ rspec-support (~> 3.0.0)
74
+ rspec-mocks (3.0.1)
75
+ rspec-support (~> 3.0.0)
76
+ rspec-support (3.0.0)
77
+ thread_safe (0.3.4)
78
+ treetop (1.5.3)
79
+ polyglot (~> 0.3)
80
+ tzinfo (1.2.1)
81
+ thread_safe (~> 0.1)
82
+
83
+ PLATFORMS
84
+ ruby
85
+
86
+ DEPENDENCIES
87
+ bundler (~> 1.0)
88
+ jeweler (~> 2.0.1)
89
+ json_select
90
+ lock_method
91
+ oj
92
+ rspec (~> 3.0.0.rc1)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2014 Kingsley Hendrickse
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,103 @@
1
+ # json_store
2
+
3
+ Very simple key/value in memory database using json
4
+
5
+ ## Install
6
+
7
+ ```ruby
8
+ gem install json_store
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ruby
14
+ require 'json_store'
15
+
16
+ db = JsonStore.new('names.db')
17
+ db.pull
18
+ db.set(:name,'Kingsley')
19
+ p db.get(:name)
20
+ db.merge
21
+ db.push
22
+ ```
23
+
24
+ There are only a handful of commands:
25
+
26
+ * pull
27
+ * push
28
+ * merge
29
+ * set
30
+ * get
31
+ * get_as_json
32
+ * search
33
+ * all
34
+ * all_as_json
35
+
36
+ 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
37
+ one will be created.
38
+
39
+ * When you want to add to the db - you use set(key,value)
40
+ * When you want to push your data to file there are a couple of options - push or merge then push
41
+
42
+ ## Merge and Push
43
+
44
+ calling push will replace the db on file with the current content of the local map. Calling merge will merge the file db with the current in memory map after which you can call push
45
+ to store the data. If you are happy to replace the db file with the content of your local map just use push. If you think there might be changes to the db file that you want before
46
+ overwriting the file - then call merge to get them and then ovewrite with push.
47
+
48
+ Merge has 2 possibilities - :into_local and :into_remote - the default is :into_remote e.g.
49
+
50
+ ```ruby
51
+ db.merge(:into_local)
52
+ ```
53
+
54
+ * :into_local will read the file content and then merge it into the local map - overwriting local changes with ones from file where the key is the same
55
+ * :into_remote will read the local map into the data from file - thus keeping any local changes and overwriting file items where the key is the same
56
+
57
+ ## Get and Search
58
+
59
+ There are 2 ways to find data:
60
+
61
+ * Get
62
+ * Search
63
+
64
+ Get just finds a value by key e.g.
65
+
66
+ ```ruby
67
+ db.set(:name,'Kingsley')
68
+ db.get(:name) # Kingsley
69
+ ```
70
+
71
+ Search uses the json select gem (https://github.com/fd/json_select) and (http://jsonselect.org/#overview) to provide advanced searching capability.
72
+
73
+ Read the documentation in the links above for a better idea how to use it - here is a simple example:
74
+
75
+ ```ruby
76
+ db.set(:names,[{id:1,name:'Kingsley'},{id:2,name:'Kostas'}])
77
+ db.search('.names .name') # ['Kingsley','Kostas']
78
+ db.search('.names .name',:match) # "Kingsley"
79
+ db.search('.names object:last-child .name',:match) # "Kostas"
80
+ ```
81
+
82
+ Search takes 2 parameters: the selector and a match kind - which is set to :matches by default. The options are:
83
+
84
+ * :matches - returns an array of data - is the default
85
+ * :match - returns only the first match
86
+ * :test - returns true or false if there is a match
87
+
88
+
89
+ ## Contributing to json_store
90
+
91
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
92
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
93
+ * Fork the project.
94
+ * Start a feature/bugfix branch.
95
+ * Commit and push until you are happy with your contribution.
96
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
97
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
98
+
99
+ ## Copyright
100
+
101
+ Copyright (c) 2014 Kingsley Hendrickse. See LICENSE.txt for
102
+ further details.
103
+
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
17
+ gem.name = "json_store"
18
+ gem.homepage = "http://github.com/kingsleyh/json_store"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{A simple in memory key/value database using json}
21
+ gem.description = %Q{A simple in memory key/value database using json that writes to file}
22
+ gem.email = "kingsleyhendrickse@me.com"
23
+ gem.authors = ["Kingsley Hendrickse"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+ # stub: json_store 0.1.0 ruby lib
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "json_store"
9
+ s.version = "0.1.0"
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.require_paths = ["lib"]
13
+ s.authors = ["Kingsley Hendrickse"]
14
+ s.date = "2014-06-19"
15
+ s.description = "A simple in memory key/value database using json that writes to file"
16
+ s.email = "kingsleyhendrickse@me.com"
17
+ s.extra_rdoc_files = [
18
+ "LICENSE.txt",
19
+ "README.md"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE.txt",
26
+ "README.md",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "json_store.gemspec",
30
+ "lib/json_store.rb"
31
+ ]
32
+ s.homepage = "http://github.com/kingsleyh/json_store"
33
+ s.licenses = ["MIT"]
34
+ s.rubygems_version = "2.2.2"
35
+ s.summary = "A simple in memory key/value database using json"
36
+
37
+ if s.respond_to? :specification_version then
38
+ s.specification_version = 4
39
+
40
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
41
+ s.add_runtime_dependency(%q<oj>, [">= 0"])
42
+ s.add_runtime_dependency(%q<lock_method>, [">= 0"])
43
+ s.add_runtime_dependency(%q<json_select>, [">= 0"])
44
+ s.add_development_dependency(%q<rspec>, ["~> 3.0.0.rc1"])
45
+ s.add_development_dependency(%q<bundler>, ["~> 1.0"])
46
+ s.add_development_dependency(%q<jeweler>, ["~> 2.0.1"])
47
+ else
48
+ s.add_dependency(%q<oj>, [">= 0"])
49
+ s.add_dependency(%q<lock_method>, [">= 0"])
50
+ s.add_dependency(%q<json_select>, [">= 0"])
51
+ s.add_dependency(%q<rspec>, ["~> 3.0.0.rc1"])
52
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
53
+ s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<oj>, [">= 0"])
57
+ s.add_dependency(%q<lock_method>, [">= 0"])
58
+ s.add_dependency(%q<json_select>, [">= 0"])
59
+ s.add_dependency(%q<rspec>, ["~> 3.0.0.rc1"])
60
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
61
+ s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
62
+ end
63
+ end
64
+
data/lib/json_store.rb ADDED
@@ -0,0 +1,90 @@
1
+ require 'oj'
2
+ require 'lock_method'
3
+ require 'json_select'
4
+
5
+ class JsonStore
6
+
7
+ def initialize(db)
8
+ @db = db
9
+ @map = {}
10
+ end
11
+
12
+ def set(key, value)
13
+ @map[key] = value
14
+ end
15
+
16
+ def get(key)
17
+ @map[key]
18
+ end
19
+
20
+ def all
21
+ @map
22
+ end
23
+
24
+ def all_as_json
25
+ Oj.dump(@map)
26
+ end
27
+
28
+ def search(selector,kind=:matches)
29
+ JSONSelect(selector).send(kind,@map)
30
+ end
31
+
32
+ def get_as_json(key)
33
+ Oj.dump(@map[key])
34
+ end
35
+
36
+ def pull
37
+ begin
38
+ @map = read
39
+ rescue LockMethod::Locked
40
+ sleep 0.5
41
+ pull
42
+ end
43
+ end
44
+
45
+ def push
46
+ begin
47
+ write
48
+ rescue LockMethod::Locked
49
+ sleep 0.5
50
+ push
51
+ end
52
+ end
53
+
54
+ def merge(direction=:into_remote)
55
+ begin
56
+ direction == :into_local ? @map.merge!(read) : read.merge!(@map)
57
+ rescue LockMethod::Locked
58
+ sleep 0.5
59
+ merge(direction)
60
+ end
61
+ end
62
+
63
+ def as_lock
64
+ 'json_store'
65
+ end
66
+
67
+ private
68
+
69
+ def read
70
+ data = {}
71
+ if File.exists?(@db)
72
+ f = File.read(@db)
73
+ data = Oj.load(f) unless f.empty?
74
+ end
75
+ data
76
+ end
77
+
78
+ lock_method :read
79
+
80
+ def write
81
+ File.open(@db, 'w') { |f| f.write(Oj.dump(@map)) }
82
+ end
83
+
84
+ lock_method :write
85
+
86
+ def keep_trying
87
+
88
+ end
89
+
90
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_store
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kingsley Hendrickse
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: oj
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: lock_method
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: json_select
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.0.0.rc1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.0.0.rc1
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: jeweler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 2.0.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 2.0.1
97
+ description: A simple in memory key/value database using json that writes to file
98
+ email: kingsleyhendrickse@me.com
99
+ executables: []
100
+ extensions: []
101
+ extra_rdoc_files:
102
+ - LICENSE.txt
103
+ - README.md
104
+ files:
105
+ - ".document"
106
+ - Gemfile
107
+ - Gemfile.lock
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - VERSION
112
+ - json_store.gemspec
113
+ - lib/json_store.rb
114
+ homepage: http://github.com/kingsleyh/json_store
115
+ licenses:
116
+ - MIT
117
+ metadata: {}
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.2.2
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: A simple in memory key/value database using json
138
+ test_files: []