easy-record 0.1.1.alpha1 → 0.1.1.alpha2
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 +4 -4
- data/lib/easy_record/association.rb +2 -2
- data/lib/easy_record/global_storage/store_csv.rb +114 -0
- data/lib/easy_record/record.rb +2 -2
- data/lib/easy_record/storage/store_csv.rb +21 -18
- data/lib/easy_record.rb +5 -3
- metadata +3 -3
- data/lib/easy_record/index.rb +0 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 86e233ddc404be4c889991f597ce1f0aaf966144aa18bda8b8c6d06c5956a276
|
4
|
+
data.tar.gz: 0305543e5e97ba0484dc4d94e3638f0d82628d9fa76ef44cb6cb97597ca47460
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: def94a971890ac383f23ce5bc01c4f889fd7ac18ef9d452f62007c309683541e0a2b26cf15847d673327c8ed035a7b2c31d0eab62c2c7ca196fb47b5430d4baa
|
7
|
+
data.tar.gz: 34ba14737a7cff55caa1ca4d6ad7f70b1c01044373aa32bd5c8ec83e7afcfe05fce57ac1d53da36e2a26d24072afceacd6510cac63913e02c1aeff3d93be3e80
|
@@ -8,9 +8,9 @@ module Association
|
|
8
8
|
end
|
9
9
|
end
|
10
10
|
|
11
|
-
def belongs_to(name,
|
11
|
+
def belongs_to(name, model, target_id)
|
12
12
|
self.define_method(name) do
|
13
|
-
Object.const_get(
|
13
|
+
Object.const_get(model[:class_name]).all.find { |m| m.id == self.send(target_id.to_s) }
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
@@ -0,0 +1,114 @@
|
|
1
|
+
module GlobalStorage
|
2
|
+
module StoreCSV
|
3
|
+
require 'csv'
|
4
|
+
require 'snake_camel'
|
5
|
+
require_relative '../record'
|
6
|
+
|
7
|
+
def write_to_csv(headers, records)
|
8
|
+
CSV.open(csv_filename, 'wb') do |csv|
|
9
|
+
csv << headers
|
10
|
+
records.each do |record|
|
11
|
+
record.is_a?(Array) ? csv << record : csv << headers.map do |attr|
|
12
|
+
record.send(attr)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def save_as_csv
|
19
|
+
write_to_csv(instance_headers, self.all)
|
20
|
+
end
|
21
|
+
|
22
|
+
def load_from_csv
|
23
|
+
p csv_structs
|
24
|
+
read_from_csv.each do |record|
|
25
|
+
self.new(record)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def read_from_csv
|
30
|
+
csv_body.map { |record| Hash[csv_headers.zip(record)] }
|
31
|
+
end
|
32
|
+
|
33
|
+
def save_record(record)
|
34
|
+
return first_save(record) unless csv_exist?
|
35
|
+
records = csv_structs
|
36
|
+
to_replace = records.find { |row| row.id == record.id }
|
37
|
+
unless to_replace.nil?
|
38
|
+
records[records.index(to_replace)] = record
|
39
|
+
else
|
40
|
+
records << record
|
41
|
+
end
|
42
|
+
write_to_csv(csv_headers, records)
|
43
|
+
end
|
44
|
+
|
45
|
+
def first_save(record)
|
46
|
+
write_to_csv(instance_headers, [record])
|
47
|
+
end
|
48
|
+
|
49
|
+
def destroy_record(record)
|
50
|
+
records = csv_structs
|
51
|
+
to_delete = records.find { |row| row.id == record.id }
|
52
|
+
if records.delete_at(records.index(to_delete))
|
53
|
+
write_to_csv(csv_headers, records)
|
54
|
+
tracked = Record.of(class_name).find { |row| row.id == record.id }
|
55
|
+
Record.untrack(tracked)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def csv_index_of(record)
|
60
|
+
csv_body.index(csv_body.find do |row|
|
61
|
+
row[csv_headers.index('id')].to_i == record.id.to_i
|
62
|
+
end) unless csv_body.nil?
|
63
|
+
end
|
64
|
+
|
65
|
+
def csv_exist?
|
66
|
+
File.exist?(csv_filename)
|
67
|
+
end
|
68
|
+
|
69
|
+
def csv_contains?(record)
|
70
|
+
csv_contains_id?(record.id)
|
71
|
+
end
|
72
|
+
|
73
|
+
def csv_cantains_id?(id)
|
74
|
+
csv_body.any? { |row| row[csv_headers.index('id')] == id }
|
75
|
+
end
|
76
|
+
|
77
|
+
def csv_filename
|
78
|
+
"./#{class_name.snakecase}.csv"
|
79
|
+
end
|
80
|
+
|
81
|
+
def csv_raw
|
82
|
+
{ headers: csv_headers, body: csv_body }
|
83
|
+
end
|
84
|
+
|
85
|
+
private
|
86
|
+
|
87
|
+
def csv_structs
|
88
|
+
struct = Struct.new(class_name, *csv_headers)
|
89
|
+
csv_body.each_with_index.map do |row, index|
|
90
|
+
struct.new(*row)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def csv_headers
|
95
|
+
csv_exist? ? csv_data.to_a.shift : instance_headers
|
96
|
+
end
|
97
|
+
|
98
|
+
def csv_body
|
99
|
+
csv_data.to_a[1..-1] unless csv_data.nil?
|
100
|
+
end
|
101
|
+
|
102
|
+
def class_name
|
103
|
+
self.name
|
104
|
+
end
|
105
|
+
|
106
|
+
def csv_data
|
107
|
+
CSV.read(csv_filename, headers: true) if csv_exist?
|
108
|
+
end
|
109
|
+
|
110
|
+
def instance_headers
|
111
|
+
self.first.instance_variables.map { |var| var.to_s[1..-1] }
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
data/lib/easy_record/record.rb
CHANGED
@@ -18,8 +18,8 @@ module Record
|
|
18
18
|
|
19
19
|
def self.untrack(instance)
|
20
20
|
# Delete the record from tracked records
|
21
|
-
index = $records.index(instance)
|
22
|
-
$records.delete_at(index) unless index.nil?
|
21
|
+
index = $records[instance.class.name].index(instance)
|
22
|
+
$records[instance.class.name].delete_at(index) unless index.nil?
|
23
23
|
$records
|
24
24
|
end
|
25
25
|
|
@@ -3,35 +3,38 @@ module Storage
|
|
3
3
|
require 'csv'
|
4
4
|
require 'snake_camel'
|
5
5
|
|
6
|
-
def
|
7
|
-
|
8
|
-
# Headers
|
9
|
-
csv << attrs
|
10
|
-
self.all.each do |record|
|
11
|
-
csv << attrs.map { |attr| record.send(attr) }
|
12
|
-
end
|
13
|
-
end
|
6
|
+
def save_to_csv
|
7
|
+
self.class.save_record(self)
|
14
8
|
end
|
15
9
|
|
16
|
-
def
|
17
|
-
|
18
|
-
|
10
|
+
def destroy_from_csv
|
11
|
+
self.class.destroy_record(self)
|
12
|
+
end
|
19
13
|
|
20
|
-
|
14
|
+
private
|
21
15
|
|
22
|
-
|
23
|
-
|
24
|
-
end
|
16
|
+
def csv_filename
|
17
|
+
"./#{class_name.snakecase}.csv"
|
25
18
|
end
|
26
19
|
|
27
|
-
|
20
|
+
def class_name
|
21
|
+
self.class.name
|
22
|
+
end
|
23
|
+
|
24
|
+
def headers
|
25
|
+
csv_data.to_a.shift
|
26
|
+
end
|
27
|
+
|
28
|
+
def body
|
29
|
+
csv_data.to_a[1..-1]
|
30
|
+
end
|
28
31
|
|
29
32
|
def csv_data
|
30
|
-
CSV.read(
|
33
|
+
CSV.read(csv_filename, headers: true)
|
31
34
|
end
|
32
35
|
|
33
36
|
def attrs
|
34
|
-
self.
|
37
|
+
self.instance_variables.map { |var| var.to_s[1..-1] }
|
35
38
|
end
|
36
39
|
end
|
37
40
|
end
|
data/lib/easy_record.rb
CHANGED
@@ -1,14 +1,16 @@
|
|
1
1
|
require_relative './easy_record/record'
|
2
|
-
require_relative './easy_record/index'
|
3
2
|
require_relative './easy_record/association'
|
3
|
+
require_relative './easy_record/global_storage'
|
4
4
|
require_relative './easy_record/storage'
|
5
5
|
|
6
6
|
class EasyRecord
|
7
7
|
require 'snake_camel'
|
8
8
|
require 'pry'
|
9
|
+
require 'UUID'
|
9
10
|
|
10
11
|
extend Association
|
11
|
-
extend
|
12
|
+
extend GlobalStorage
|
13
|
+
include Storage
|
12
14
|
|
13
15
|
attr_accessor :id
|
14
16
|
|
@@ -26,9 +28,9 @@ class EasyRecord
|
|
26
28
|
|
27
29
|
def initialize(attributes = nil)
|
28
30
|
Record.track(self)
|
29
|
-
@id = Index.next_id(self)
|
30
31
|
|
31
32
|
set_attributes(attributes)
|
33
|
+
set(:id, UUID.generate) unless attributes.keys.include?('id')
|
32
34
|
end
|
33
35
|
|
34
36
|
private
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easy-record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.1.
|
4
|
+
version: 0.1.1.alpha2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ricardo Rafael Villagrana Larios
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-02-
|
11
|
+
date: 2019-02-11 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Easy Record allows you to create associations between models and store
|
14
14
|
them in disk
|
@@ -19,7 +19,7 @@ extra_rdoc_files: []
|
|
19
19
|
files:
|
20
20
|
- lib/easy_record.rb
|
21
21
|
- lib/easy_record/association.rb
|
22
|
-
- lib/easy_record/
|
22
|
+
- lib/easy_record/global_storage/store_csv.rb
|
23
23
|
- lib/easy_record/record.rb
|
24
24
|
- lib/easy_record/storage/store_csv.rb
|
25
25
|
homepage: http://github.com/ricvillagrana/easy-record
|
data/lib/easy_record/index.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
module Index
|
2
|
-
# Initilialize $indexes
|
3
|
-
$indexes = {} unless defined? $indexes
|
4
|
-
|
5
|
-
def self.next_id(model = nil)
|
6
|
-
# If model was not privided just exit
|
7
|
-
return if model.nil?
|
8
|
-
|
9
|
-
# map indexes of $indexes to know if the model is alredy in it.
|
10
|
-
if $indexes.map(&:first).include?(model.class.name)
|
11
|
-
# If so, increment
|
12
|
-
$indexes[model.class.name] += 1
|
13
|
-
else
|
14
|
-
# Otherwise initialize with 1
|
15
|
-
$indexes[model.class.name] = 1
|
16
|
-
end
|
17
|
-
# return the current index
|
18
|
-
$indexes[model.class.name]
|
19
|
-
end
|
20
|
-
end
|