tiny_db 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +34 -0
- data/LICENSE.txt +21 -0
- data/README.md +82 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/tiny_db/configuration.rb +11 -0
- data/lib/tiny_db/db_interface.rb +40 -0
- data/lib/tiny_db/db_setup.rb +0 -0
- data/lib/tiny_db/file_model.rb +101 -0
- data/lib/tiny_db/version.rb +3 -0
- data/lib/tiny_db.rb +25 -0
- data/tiny_db.gemspec +26 -0
- metadata +64 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ca7805aeadd098311d11d331a7b6426569887f611ef75ca50b64757da82c8d34
|
4
|
+
data.tar.gz: bc0b7661a4b6c1aebb50207be7936df5845cdf37d7232ccf68e90e60f21252be
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 453bf618971940e1939462f633b0ccc458102cd29a56f42c649f09348a8067f52c6963a4bb3d771cb568e6a5c3b03abe05af2aa1afb5a73826a12de90ba54f0f
|
7
|
+
data.tar.gz: dadd128780c94ddb50613c367c6a390b4bad5f9bd0e7e0b0f5dd4dacb81686c8e8f8f54fb446bda1921ef30d82dfca7c6a58057d9eb6bf083a222da9a8ca2877
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
tiny_db (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.5.0)
|
10
|
+
rake (12.3.3)
|
11
|
+
rspec (3.10.0)
|
12
|
+
rspec-core (~> 3.10.0)
|
13
|
+
rspec-expectations (~> 3.10.0)
|
14
|
+
rspec-mocks (~> 3.10.0)
|
15
|
+
rspec-core (3.10.1)
|
16
|
+
rspec-support (~> 3.10.0)
|
17
|
+
rspec-expectations (3.10.1)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.10.0)
|
20
|
+
rspec-mocks (3.10.2)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.10.0)
|
23
|
+
rspec-support (3.10.3)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
rake (~> 12.0)
|
30
|
+
rspec (~> 3.0)
|
31
|
+
tiny_db!
|
32
|
+
|
33
|
+
BUNDLED WITH
|
34
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 timothyjb
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
# TinyDb
|
2
|
+
|
3
|
+
The purpose of this gem is provide a very simple solution for storing records locally in a text file as json and providing a basic ActivateRecord like interface to access those records.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'tiny_db'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install tiny_db
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Initialize:
|
24
|
+
```ruby
|
25
|
+
TinyDb.configure do |config|
|
26
|
+
config.db_file_path = "your_file_name.json"
|
27
|
+
end
|
28
|
+
```
|
29
|
+
|
30
|
+
### Create file:
|
31
|
+
Create a file that matches the `db_file_path` provided above.
|
32
|
+
`your_file_name.json`
|
33
|
+
```
|
34
|
+
{}
|
35
|
+
```
|
36
|
+
|
37
|
+
###
|
38
|
+
Example usage:
|
39
|
+
```ruby
|
40
|
+
class MyModel < TinyDb::FileModel
|
41
|
+
TABLE_NAME = "my_model"
|
42
|
+
ATTRIBUTES = [:id, :name]
|
43
|
+
attr_accessor *ATTRIBUTES
|
44
|
+
end
|
45
|
+
|
46
|
+
```
|
47
|
+
|
48
|
+
|
49
|
+
This snippet:
|
50
|
+
```ruby
|
51
|
+
record = MyModel.new
|
52
|
+
record.name = "Timbo"
|
53
|
+
record.save
|
54
|
+
```
|
55
|
+
|
56
|
+
Would result in the json file being updated to:
|
57
|
+
```
|
58
|
+
{
|
59
|
+
"my_model": [
|
60
|
+
{
|
61
|
+
"id": 1,
|
62
|
+
"name": "Timbo"
|
63
|
+
}
|
64
|
+
]
|
65
|
+
}
|
66
|
+
```
|
67
|
+
|
68
|
+
### Methods provided:
|
69
|
+
- `.new`
|
70
|
+
- `.create(key: value)`
|
71
|
+
- `.update(key: value)`
|
72
|
+
- `.find_by(key: value)`
|
73
|
+
- `.destroy`
|
74
|
+
|
75
|
+
## Contributing
|
76
|
+
|
77
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Timothyjb/tiny_db.
|
78
|
+
|
79
|
+
|
80
|
+
## License
|
81
|
+
|
82
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "tiny_db"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
module TinyDb
|
2
|
+
class DbInterface
|
3
|
+
class << self
|
4
|
+
|
5
|
+
def write(file)
|
6
|
+
check_setup_completion
|
7
|
+
File.write("#{db_file_path}", file.to_json)
|
8
|
+
end
|
9
|
+
|
10
|
+
def read
|
11
|
+
check_setup_completion
|
12
|
+
JSON.load File.open("#{db_file_path}", "r")
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def db_file_path
|
18
|
+
@db_file_path ||= TinyDb.configuration.db_file_path
|
19
|
+
end
|
20
|
+
|
21
|
+
def check_setup_completion
|
22
|
+
raise_missing_config_error if db_file_path.nil?
|
23
|
+
raise_missing_file_error unless file_exists?
|
24
|
+
end
|
25
|
+
|
26
|
+
def raise_missing_config_error
|
27
|
+
raise ConfigError.new("required config 'db_file_path' is missing")
|
28
|
+
end
|
29
|
+
|
30
|
+
def file_exists?
|
31
|
+
File.file?("#{db_file_path}")
|
32
|
+
end
|
33
|
+
|
34
|
+
def raise_missing_file_error
|
35
|
+
raise ConfigError.new("the db file '#{db_file_path}' is missing")
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
File without changes
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require 'json'
|
2
|
+
require './lib/tiny_db/db_interface.rb'
|
3
|
+
module TinyDb
|
4
|
+
class FileModel
|
5
|
+
|
6
|
+
def save
|
7
|
+
id.nil? ? create_record : update_record
|
8
|
+
|
9
|
+
self.class.write_to_file
|
10
|
+
end
|
11
|
+
|
12
|
+
def update(hash)
|
13
|
+
self.class::ATTRIBUTES.each do |attr|
|
14
|
+
self.instance_variable_set("@#{attr.to_s}", hash[attr]) if hash.keys.include?(attr)
|
15
|
+
end
|
16
|
+
|
17
|
+
self.save
|
18
|
+
end
|
19
|
+
|
20
|
+
def destroy
|
21
|
+
self.class.table.delete_if { |x| x[:id] == id }
|
22
|
+
|
23
|
+
self.class.write_to_file
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def create_record
|
29
|
+
@id = self.class.last_index + 1
|
30
|
+
data = {"id": @id, "name": @name}
|
31
|
+
|
32
|
+
self.class.table << data
|
33
|
+
end
|
34
|
+
|
35
|
+
def update_record
|
36
|
+
data = self.class.table.select{|x| x[:id] == id}.first
|
37
|
+
data[:name] = name
|
38
|
+
end
|
39
|
+
|
40
|
+
class << self
|
41
|
+
def all
|
42
|
+
file[table_name]
|
43
|
+
end
|
44
|
+
|
45
|
+
def find_by(arg)
|
46
|
+
klass = self.new
|
47
|
+
|
48
|
+
key = arg.keys.first
|
49
|
+
value = arg.values.first
|
50
|
+
|
51
|
+
data = table.select{|x| x[key] == value}.first
|
52
|
+
|
53
|
+
self::ATTRIBUTES.each do |attr|
|
54
|
+
klass.instance_variable_set("@#{attr.to_s}", data[attr])
|
55
|
+
end
|
56
|
+
|
57
|
+
klass
|
58
|
+
end
|
59
|
+
|
60
|
+
def create(params)
|
61
|
+
klass = self.new
|
62
|
+
|
63
|
+
self::ATTRIBUTES.each do |attr|
|
64
|
+
klass.instance_variable_set("@#{attr.to_s}", params[attr])
|
65
|
+
end
|
66
|
+
|
67
|
+
klass.save
|
68
|
+
end
|
69
|
+
|
70
|
+
def file
|
71
|
+
@file ||= load_file
|
72
|
+
end
|
73
|
+
|
74
|
+
def table_name
|
75
|
+
self::TABLE_NAME
|
76
|
+
end
|
77
|
+
|
78
|
+
def table
|
79
|
+
file[table_name] = [] if file[table_name].nil?
|
80
|
+
|
81
|
+
file[table_name]
|
82
|
+
end
|
83
|
+
|
84
|
+
def last_index
|
85
|
+
latest_record = table.sort_by{|x| x[:id]}.last
|
86
|
+
|
87
|
+
latest_record.nil? ? 0 : latest_record[:id]
|
88
|
+
end
|
89
|
+
|
90
|
+
def write_to_file
|
91
|
+
TinyDb::DbInterface.write(file)
|
92
|
+
end
|
93
|
+
|
94
|
+
def load_file
|
95
|
+
TinyDb::DbInterface.read
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
end
|
data/lib/tiny_db.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require "tiny_db/version"
|
2
|
+
require "tiny_db/file_model"
|
3
|
+
require 'tiny_db/configuration'
|
4
|
+
|
5
|
+
module TinyDb
|
6
|
+
class ConfigError < StandardError; end
|
7
|
+
|
8
|
+
class << self
|
9
|
+
attr_accessor :configuration
|
10
|
+
|
11
|
+
def configuration
|
12
|
+
@configuration ||= Configuration.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def reset
|
16
|
+
@configuration = Configuration.new
|
17
|
+
end
|
18
|
+
|
19
|
+
def configure
|
20
|
+
yield(configuration)
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
data/tiny_db.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require_relative 'lib/tiny_db/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "tiny_db"
|
5
|
+
spec.version = TinyDb::VERSION
|
6
|
+
spec.authors = ["timothyjb"]
|
7
|
+
spec.email = ["timothyjbarkley@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = "Save and access records in json file"
|
10
|
+
spec.description = "The purpose of this gem is provide a very simple solution for storing records locally in a text file as json and providing a basic ActivateRecord like interface to access those records"
|
11
|
+
spec.homepage = "https://rubygems.org/gems/tiny_db"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["source_code_uri"] = "https://github.com/Timothyjb/tiny_db"
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
end
|
23
|
+
spec.bindir = "exe"
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ["lib"]
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tiny_db
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- timothyjb
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-01-02 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: The purpose of this gem is provide a very simple solution for storing
|
14
|
+
records locally in a text file as json and providing a basic ActivateRecord like
|
15
|
+
interface to access those records
|
16
|
+
email:
|
17
|
+
- timothyjbarkley@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- ".gitignore"
|
23
|
+
- ".rspec"
|
24
|
+
- ".travis.yml"
|
25
|
+
- Gemfile
|
26
|
+
- Gemfile.lock
|
27
|
+
- LICENSE.txt
|
28
|
+
- README.md
|
29
|
+
- Rakefile
|
30
|
+
- bin/console
|
31
|
+
- bin/setup
|
32
|
+
- lib/tiny_db.rb
|
33
|
+
- lib/tiny_db/configuration.rb
|
34
|
+
- lib/tiny_db/db_interface.rb
|
35
|
+
- lib/tiny_db/db_setup.rb
|
36
|
+
- lib/tiny_db/file_model.rb
|
37
|
+
- lib/tiny_db/version.rb
|
38
|
+
- tiny_db.gemspec
|
39
|
+
homepage: https://rubygems.org/gems/tiny_db
|
40
|
+
licenses:
|
41
|
+
- MIT
|
42
|
+
metadata:
|
43
|
+
homepage_uri: https://rubygems.org/gems/tiny_db
|
44
|
+
source_code_uri: https://github.com/Timothyjb/tiny_db
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 2.3.0
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubygems_version: 3.1.2
|
61
|
+
signing_key:
|
62
|
+
specification_version: 4
|
63
|
+
summary: Save and access records in json file
|
64
|
+
test_files: []
|