file_db 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +143 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/file_db.gemspec +25 -0
- data/lib/file_db/columns.rb +25 -0
- data/lib/file_db/configuration.rb +20 -0
- data/lib/file_db/convert.rb +11 -0
- data/lib/file_db/data.rb +25 -0
- data/lib/file_db/database.rb +20 -0
- data/lib/file_db/model.rb +7 -0
- data/lib/file_db/query.rb +36 -0
- data/lib/file_db/table.rb +45 -0
- data/lib/file_db/version.rb +3 -0
- data/lib/file_db.rb +14 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 60f8985b783b22dce4f2d0bfd8869bbef4e42aa8
|
4
|
+
data.tar.gz: fa31bf722121dd5b289970891e4e61bbaf3a5b5c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0d068f1075e82294529a363cb25f4d93795c6995955c86fca7b84da0edd055d552e7f8b7f2abd93907defab9efc916379524d30a7c8907e62b2d7d6dd6d8da8d
|
7
|
+
data.tar.gz: cf4f679eb57937297a7f7ca0f4ba3974993acfc5d9240b6400f67e06c576c3b7fca0dcff353aa0ec2e0daee6d7c7692ee6e78efc0e11dd23fe9af1ffd880ee04
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 robst
|
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,143 @@
|
|
1
|
+
# FileDb
|
2
|
+
|
3
|
+
You need to store data into a small type of database, like CSV and want a better way to handle it? So you can use `FileDb` for this. It stores all data for a model into CSV files and create accessor for you.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
hm, yeah. just add this to your Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'file_db', '~> 0.1.0'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
```
|
15
|
+
$ bundle
|
16
|
+
```
|
17
|
+
Or install it yourself as:
|
18
|
+
```
|
19
|
+
$ gem install file_db
|
20
|
+
```
|
21
|
+
|
22
|
+
Huh, ready to use!
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
### Configuration
|
27
|
+
|
28
|
+
First configure the storage directory:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
FileDb::Configuration.configure data_directory: 'data'
|
32
|
+
```
|
33
|
+
|
34
|
+
Make sure, the directory exists or use the build in check for checking and creating the data directory:
|
35
|
+
```ruby
|
36
|
+
FileDb::Database.database_check!
|
37
|
+
```
|
38
|
+
|
39
|
+
ok, lets go to create a model.
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
class User < FileDb::Model
|
43
|
+
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
thats it. now we got a user model which actually just storing an id. lets add `columns` for this.
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
class User < FileDb::Model
|
51
|
+
columns :name, :email
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
Now lets create some entries.
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
my_user = User.new name: 'rob'
|
59
|
+
my_user.save
|
60
|
+
```
|
61
|
+
now the `User` is stored under `data/user.csv`. You should find an entry like this one:
|
62
|
+
|
63
|
+
```
|
64
|
+
1466311874,rob,
|
65
|
+
```
|
66
|
+
|
67
|
+
Let's get them user back:
|
68
|
+
```ruby
|
69
|
+
User.find 1466311874
|
70
|
+
-> #<User:0x00000004651798 @name="rob", @id="1466311874", @email=nil>
|
71
|
+
```
|
72
|
+
|
73
|
+
you can access all attributes like `attr_accessor`
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
User.find(1466311874).name
|
77
|
+
-> rob
|
78
|
+
```
|
79
|
+
|
80
|
+
The accessors will be created automaticly for you.
|
81
|
+
Let's find all users named with rob. I think you know how get this to work:
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
User.where(name: 'rob')
|
85
|
+
-> [#<User:0x00000004651798 @name="rob", @id="1466311874", @email=nil>]
|
86
|
+
```
|
87
|
+
|
88
|
+
rename the user:
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
user = User.find(1466311874)
|
92
|
+
-> #<User:0x00000004651798 @name="rob", @id="1466311874", @email=nil>
|
93
|
+
user.name = 'bob'
|
94
|
+
user.email = 'test@example.com'
|
95
|
+
user.save
|
96
|
+
User.find(1466311874)
|
97
|
+
-> #<User:0x00000004651798 @name="bob", @id="1466311874", @email=test@example.com>
|
98
|
+
```
|
99
|
+
|
100
|
+
You want to use another table than `user`? So just configure it:
|
101
|
+
|
102
|
+
```ruby
|
103
|
+
class User < FileDb::Model
|
104
|
+
columns :name, :email
|
105
|
+
set_table_name 'crazy'
|
106
|
+
end
|
107
|
+
```
|
108
|
+
|
109
|
+
## The MIT License (MIT)
|
110
|
+
|
111
|
+
Copyright (c) 2015 [Robert Starke](robertst81+github@gmail.com)
|
112
|
+
|
113
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
114
|
+
of this software and associated documentation files (the "Software"), to deal
|
115
|
+
in the Software without restriction, including without limitation the rights
|
116
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
117
|
+
copies of the Software, and to permit persons to whom the Software is
|
118
|
+
furnished to do so, subject to the following conditions:
|
119
|
+
|
120
|
+
The above copyright notice and this permission notice shall be included in
|
121
|
+
all copies or substantial portions of the Software.
|
122
|
+
|
123
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
124
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
125
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
126
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
127
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
128
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
129
|
+
THE SOFTWARE.
|
130
|
+
|
131
|
+
## Questions?
|
132
|
+
|
133
|
+
If you have further questions, code smells, hints or a beer, just contact me :)
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "file_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
|
data/bin/setup
ADDED
data/file_db.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'file_db/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "file_db"
|
8
|
+
spec.version = FileDb::VERSION
|
9
|
+
spec.authors = ["Robert Starke"]
|
10
|
+
spec.email = ["robertst81@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Use CSV Files like a Database with ActiveRecord Feeling.}
|
13
|
+
spec.description = %q{You need to store informationen in a CSV File, because there is no need for a database? You can use FileDb to store all informationen in a CSV File and search in it like ActiveRecord (User.find(1212). See detailed Information at the github Page.}
|
14
|
+
spec.homepage = "https://github.com/robst/file_db"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
25
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module FileDb
|
2
|
+
module Columns
|
3
|
+
|
4
|
+
def columns *names
|
5
|
+
return @columns if names.empty?
|
6
|
+
names.delete(:id)
|
7
|
+
@columns = [:id] + names
|
8
|
+
|
9
|
+
@columns.each do |name|
|
10
|
+
define_method name do
|
11
|
+
instance_variable_get "@#{name}"
|
12
|
+
end
|
13
|
+
define_method "#{name}=" do |new_val|
|
14
|
+
instance_variable_set "@#{name}", new_val
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
def column_index column
|
21
|
+
@columns.index column
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module FileDb
|
2
|
+
module Configuration
|
3
|
+
def self.configure options = {}
|
4
|
+
@configuration = default_options.merge(options)
|
5
|
+
FileDb::Database.database_check!
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.configured option
|
9
|
+
configuration[option]
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.configuration
|
13
|
+
@configuration ||= default_options
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.default_options
|
17
|
+
{ data_directory: 'data' }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/file_db/data.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
module FileDb
|
2
|
+
module Data
|
3
|
+
def initialize params= {}
|
4
|
+
params.each do |key, value|
|
5
|
+
next unless self.class.columns.include?(key)
|
6
|
+
send("#{key}=", value)
|
7
|
+
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def save
|
12
|
+
if persisted?
|
13
|
+
self.class.update_database self
|
14
|
+
else
|
15
|
+
self.id = Time.now.to_i
|
16
|
+
self.class.append_to_database self
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
def persisted?
|
22
|
+
!id.nil?
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module FileDb
|
2
|
+
module Database
|
3
|
+
def self.database_check!
|
4
|
+
create_data_directory! unless exist_data_directory?
|
5
|
+
end
|
6
|
+
|
7
|
+
private
|
8
|
+
def self.data_directory
|
9
|
+
Configuration.configured(:data_directory)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.exist_data_directory?
|
13
|
+
File.exist? data_directory
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.create_data_directory!
|
17
|
+
Dir.mkdir data_directory
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module FileDb
|
2
|
+
module Query
|
3
|
+
include FileDb::Columns
|
4
|
+
include FileDb::Table
|
5
|
+
|
6
|
+
def find id
|
7
|
+
where(id: id).first
|
8
|
+
end
|
9
|
+
|
10
|
+
def where conditions
|
11
|
+
results = []
|
12
|
+
searching_database do |data_row|
|
13
|
+
if conditions_match?(conditions,data_row)
|
14
|
+
results << create_object(data_row)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
results
|
18
|
+
end
|
19
|
+
|
20
|
+
def create_object values
|
21
|
+
hash = {}
|
22
|
+
columns.each do |column|
|
23
|
+
hash[column] = values[column_index(column)]
|
24
|
+
end
|
25
|
+
new hash
|
26
|
+
end
|
27
|
+
|
28
|
+
def conditions_match? conditions, data
|
29
|
+
conditions.each do |key, value|
|
30
|
+
return false unless data[column_index(key)].eql?(value.to_s)
|
31
|
+
end
|
32
|
+
true
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module FileDb
|
2
|
+
module Table
|
3
|
+
def table_name
|
4
|
+
@table_name ||= self.new.class.to_s.gsub(/(.)([A-Z])/,'\1_\2').downcase
|
5
|
+
end
|
6
|
+
|
7
|
+
def set_table_name name
|
8
|
+
@table_name = name
|
9
|
+
end
|
10
|
+
|
11
|
+
def searching_database
|
12
|
+
return unless File.exist?(table_file_location)
|
13
|
+
::CSV.foreach(table_file_location) do |row|
|
14
|
+
yield row
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def append_to_database entry_object
|
19
|
+
::CSV.open(table_file_location, "a") do |csv|
|
20
|
+
csv << entry_object.to_csv
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def update_database entry_object
|
25
|
+
records = []
|
26
|
+
::CSV.foreach(table_file_location) do |row|
|
27
|
+
if row[0]==entry_object.id.to_s
|
28
|
+
records << entry_object.to_csv
|
29
|
+
else
|
30
|
+
records << row
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
::CSV.open(table_file_location, "w") do |csv|
|
35
|
+
records.each do |record|
|
36
|
+
csv << record
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def table_file_location
|
42
|
+
File.join(Configuration.configured(:data_directory), "#{table_name}.csv")
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/file_db.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require "csv"
|
2
|
+
require "file_db/version"
|
3
|
+
require "file_db/configuration"
|
4
|
+
require "file_db/database"
|
5
|
+
require "file_db/columns"
|
6
|
+
require "file_db/table"
|
7
|
+
require "file_db/data"
|
8
|
+
require "file_db/query"
|
9
|
+
require "file_db/convert"
|
10
|
+
require "file_db/model"
|
11
|
+
|
12
|
+
module FileDb
|
13
|
+
# Your code goes here...
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: file_db
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Robert Starke
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-06-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: You need to store informationen in a CSV File, because there is no need
|
56
|
+
for a database? You can use FileDb to store all informationen in a CSV File and
|
57
|
+
search in it like ActiveRecord (User.find(1212). See detailed Information at the
|
58
|
+
github Page.
|
59
|
+
email:
|
60
|
+
- robertst81@gmail.com
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- ".gitignore"
|
66
|
+
- ".rspec"
|
67
|
+
- ".travis.yml"
|
68
|
+
- Gemfile
|
69
|
+
- LICENSE.txt
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- bin/console
|
73
|
+
- bin/setup
|
74
|
+
- file_db.gemspec
|
75
|
+
- lib/file_db.rb
|
76
|
+
- lib/file_db/columns.rb
|
77
|
+
- lib/file_db/configuration.rb
|
78
|
+
- lib/file_db/convert.rb
|
79
|
+
- lib/file_db/data.rb
|
80
|
+
- lib/file_db/database.rb
|
81
|
+
- lib/file_db/model.rb
|
82
|
+
- lib/file_db/query.rb
|
83
|
+
- lib/file_db/table.rb
|
84
|
+
- lib/file_db/version.rb
|
85
|
+
homepage: https://github.com/robst/file_db
|
86
|
+
licenses:
|
87
|
+
- MIT
|
88
|
+
metadata: {}
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 2.5.1
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: Use CSV Files like a Database with ActiveRecord Feeling.
|
109
|
+
test_files: []
|