yaml-file-db 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/yaml-file-db/database.rb +65 -0
- data/lib/yaml-file-db/exception.rb +4 -0
- data/lib/yaml-file-db/row.rb +66 -0
- data/lib/yaml-file-db.rb +7 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: dcccc03ffdf919d10a071489113df40c30a6bdc7895648b333ce3fc892a95322
|
4
|
+
data.tar.gz: 9a5b1b311ed1cf15c73ecdc5fe2e33bd2e3471973d7909569d25881f579081b8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c8569acad2ff389a8827dfb4203aa56525de1465849f01be2fa3f1f04742015d8fc615bf7c778f463ad8e752ceee1254928d268e3e3ee7568e704a5f4f2cd340
|
7
|
+
data.tar.gz: c4189b21b1b971969a0f2dfe3d2bb48ca181689f827b8e4bedefdd1033bb263f531c7248da2a364ef7d23d195ef802a58f2bb1c8f4ebd6ac4e668212e96684a8
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module YDB
|
2
|
+
class Database
|
3
|
+
|
4
|
+
INTERNAL_VARS = [:@errors, :@schemas, :@source].freeze
|
5
|
+
|
6
|
+
attr_reader :errors, :schemas, :source
|
7
|
+
|
8
|
+
def initialize(source, schemas)
|
9
|
+
@errors = []
|
10
|
+
@schemas = schemas
|
11
|
+
@source = source
|
12
|
+
end
|
13
|
+
|
14
|
+
def build()
|
15
|
+
build_tables()
|
16
|
+
build_relationships()
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def build_tables()
|
23
|
+
Dir["#{@source}/*"].each do |table_path|
|
24
|
+
table = {}
|
25
|
+
|
26
|
+
table_name = File.basename(table_path)
|
27
|
+
klass_name = table_name.singularize.capitalize
|
28
|
+
Object.const_set(klass_name, Class.new(Row))
|
29
|
+
schema_path = "#{@schemas}/#{table_name.singularize}.yml"
|
30
|
+
|
31
|
+
Dir["#{table_path}/*.yml"].each do |source|
|
32
|
+
begin
|
33
|
+
row = Object.const_get(klass_name).new(source, schema_path)
|
34
|
+
table[row.id] = row
|
35
|
+
rescue ValidationError => error
|
36
|
+
@errors << "#{source}: #{error.to_s}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
instance_variable_set("@#{table_name}", table)
|
41
|
+
self.class.send("attr_reader", table_name.to_sym)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def build_relationships()
|
46
|
+
keywords = []
|
47
|
+
(self.instance_variables - INTERNAL_VARS).each do |var|
|
48
|
+
keywords << var.to_s[1..-1]
|
49
|
+
keywords << var.to_s[1..-1].singularize
|
50
|
+
end
|
51
|
+
self.instance_variables.each do |var|
|
52
|
+
next if INTERNAL_VARS.include? var
|
53
|
+
table = instance_variable_get var
|
54
|
+
table.each do |id, row|
|
55
|
+
begin
|
56
|
+
row.build_relationships(self, keywords)
|
57
|
+
rescue ValidationError => error
|
58
|
+
@errors << "#{row.source}: #{error.to_s}"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module YDB
|
2
|
+
class Row
|
3
|
+
INTERNAL_VARS = [:@id, :@source].freeze
|
4
|
+
|
5
|
+
attr_reader :id, :source
|
6
|
+
|
7
|
+
def initialize(source, schema_path)
|
8
|
+
@id = File.basename(source, ".yml")
|
9
|
+
@source = source
|
10
|
+
|
11
|
+
validate_filename()
|
12
|
+
build(source, schema_path)
|
13
|
+
end
|
14
|
+
|
15
|
+
def build_relationships(db, keywords)
|
16
|
+
self.instance_variables.each do |var|
|
17
|
+
next if INTERNAL_VARS.include? var
|
18
|
+
keyword = var.to_s[1..-1]
|
19
|
+
|
20
|
+
if keywords.include? keyword
|
21
|
+
if keyword.pluralize == keyword
|
22
|
+
array = instance_variable_get var
|
23
|
+
entities = db.public_send(keyword.to_sym)
|
24
|
+
value = []
|
25
|
+
array.each do |primary_key|
|
26
|
+
entity = entities[primary_key]
|
27
|
+
raise ValidationError.new("invalid primary_key #{primary_key}") if entity.nil?
|
28
|
+
value << entity
|
29
|
+
end
|
30
|
+
instance_variable_set("@#{keyword}", value)
|
31
|
+
else
|
32
|
+
primary_key = instance_variable_get var
|
33
|
+
entity = db.public_send(keyword.pluralize.to_sym)[primary_key]
|
34
|
+
raise ValidationError.new("invalid primary_key #{primary_key}") if entity.nil?
|
35
|
+
instance_variable_set("@#{keyword}", entity)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
def build(source, schema_path)
|
44
|
+
doc = YAML.load(File.read(source))
|
45
|
+
raise ValidationError.new("invalid YAML") if doc == false
|
46
|
+
|
47
|
+
schema = YAML.load(File.read(schema_path))
|
48
|
+
begin
|
49
|
+
JSON::Validator.validate!(schema, doc, :parse_data => false)
|
50
|
+
rescue JSON::Schema::ValidationError => error
|
51
|
+
raise ValidationError.new("invalid data (#{error.message})")
|
52
|
+
end
|
53
|
+
|
54
|
+
doc.each do |name, value|
|
55
|
+
instance_variable_set("@#{name}", value)
|
56
|
+
next if self.respond_to? name.to_sym
|
57
|
+
self.class.send("attr_reader", name.to_sym)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def validate_filename()
|
62
|
+
raise ValidationError.new("invalid filename") unless self.id =~ /^[\w-]+$/
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
end
|
data/lib/yaml-file-db.rb
ADDED
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yaml-file-db
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Louis-Philippe Gauthier
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-02-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 6.0.0
|
20
|
+
- - "~>"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '6.0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 6.0.0
|
30
|
+
- - "~>"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '6.0'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: json-schema
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '2.8'
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 2.8.1
|
43
|
+
type: :runtime
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '2.8'
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 2.8.1
|
53
|
+
description: YAML file database
|
54
|
+
email: lpgauth@gmail.com
|
55
|
+
executables: []
|
56
|
+
extensions: []
|
57
|
+
extra_rdoc_files: []
|
58
|
+
files:
|
59
|
+
- lib/yaml-file-db.rb
|
60
|
+
- lib/yaml-file-db/database.rb
|
61
|
+
- lib/yaml-file-db/exception.rb
|
62
|
+
- lib/yaml-file-db/row.rb
|
63
|
+
homepage: https://rubygems.org/gems/yaml-file-db
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
metadata: {}
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubygems_version: 3.0.3
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: database
|
86
|
+
test_files: []
|