lizarb 0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -0
- data/CHANGELOG.md +13 -2
- data/Gemfile +21 -2
- data/Gemfile.lock +44 -0
- data/README.md +88 -12
- data/app/dev/commands/app_command.rb +5 -0
- data/app/dev/commands/app_command_test.rb +7 -0
- data/app/dev/commands/example_command.rb +57 -0
- data/app/dev/commands/example_command_test.rb +43 -0
- data/app/dev/commands/macro_child_command.rb +32 -0
- data/app/dev/commands/macro_child_command_test.rb +7 -0
- data/app/dev/commands/macro_parent_command.rb +70 -0
- data/app/dev/commands/macro_parent_command_test.rb +7 -0
- data/app/dev/commands/my_command.rb +7 -0
- data/app/dev/commands/my_command_test.rb +7 -0
- data/app/dev/commands/new_command.rb +69 -0
- data/app/dev/commands/new_command_test.rb +7 -0
- data/app/dev_box.rb +13 -0
- data/app/happy/axos/axo.rb +50 -0
- data/app/happy_box.rb +8 -0
- data/app/net/databases/redis_db.rb +5 -0
- data/app/net/databases/redis_db_test.rb +12 -0
- data/app/net/databases/sqlite_db.rb +11 -0
- data/app/net/databases/sqlite_db_test.rb +12 -0
- data/app/net/models/app_model.rb +20 -0
- data/app/net/models/app_model_test.rb +8 -0
- data/app/net/models/post_model.rb +4 -0
- data/app/net/models/post_model_test.rb +8 -0
- data/app/net/models/user_model.rb +4 -0
- data/app/net/models/user_model_test.rb +8 -0
- data/app/net_box.rb +21 -0
- data/app/web/requests/api_request.rb +61 -0
- data/app/web/requests/api_request_test.rb +31 -0
- data/app/web/requests/app_request.rb +5 -0
- data/app/web/requests/app_request_test.rb +7 -0
- data/app/web/requests/assets_request.rb +71 -0
- data/app/web/requests/assets_request_test.rb +33 -0
- data/app/web/requests/root_request.rb +55 -0
- data/app/web/requests/root_request_test.rb +31 -0
- data/app/web_box.rb +8 -0
- data/app.code.env +11 -0
- data/app.env +11 -0
- data/app.rb +17 -0
- data/app_new/.gitignore +4 -0
- data/app_new/Gemfile +31 -0
- data/app_new/Procfile +3 -0
- data/exe/Gemfile +3 -0
- data/exe/Gemfile.lock +13 -0
- data/exe/liza +3 -0
- data/exe/lizarb +11 -0
- data/lib/app.rb +200 -0
- data/lib/dev_system/dev/controllers/command/dev_command.rb +19 -0
- data/lib/dev_system/dev/controllers/command/dev_command_test.rb +14 -0
- data/lib/dev_system/dev/controllers/command/echo_command.rb +11 -0
- data/lib/dev_system/dev/controllers/command/echo_command_test.rb +14 -0
- data/lib/dev_system/dev/controllers/command/test_command.rb +70 -0
- data/lib/dev_system/dev/controllers/command/test_command_test.rb +14 -0
- data/lib/dev_system/dev/controllers/command/version_command.rb +9 -0
- data/lib/dev_system/dev/controllers/command/version_command_test.rb +14 -0
- data/lib/dev_system/dev/controllers/command.rb +19 -0
- data/lib/dev_system/dev/controllers/command_test.rb +14 -0
- data/lib/dev_system/dev/dev_box.rb +19 -0
- data/lib/dev_system/dev/dev_box_test.rb +19 -0
- data/lib/dev_system/dev/panels/command_panel.rb +10 -0
- data/lib/dev_system/dev/panels/command_panel_test.rb +23 -0
- data/lib/dev_system/dev/panels/log_panel.rb +10 -0
- data/lib/dev_system/dev/panels/log_panel_test.rb +18 -0
- data/lib/dev_system.rb +8 -0
- data/lib/happy_system/dev/happy_command.rb +11 -0
- data/lib/happy_system/happy/controllers/axo.rb +8 -0
- data/lib/happy_system/happy/happy_box.rb +12 -0
- data/lib/happy_system/happy/happy_box_test.rb +18 -0
- data/lib/happy_system/happy/panels/axo_panel.rb +5 -0
- data/lib/{viral.rb → happy_system.rb} +7 -3
- data/lib/liza/base/box.rb +40 -0
- data/lib/liza/base/box_test.rb +12 -0
- data/lib/liza/base/controller.rb +27 -0
- data/lib/liza/base/controller_test.rb +12 -0
- data/lib/liza/base/panel.rb +38 -0
- data/lib/liza/base/panel_test.rb +16 -0
- data/lib/liza/meta/part.rb +19 -0
- data/lib/liza/meta/part_extension.rb +13 -0
- data/lib/liza/meta/part_test.rb +8 -0
- data/lib/liza/meta/system.rb +20 -0
- data/lib/liza/meta/system_test.rb +13 -0
- data/lib/liza/test/test.rb +11 -0
- data/lib/liza/test/test_test.rb +108 -0
- data/lib/liza/test_parts/test_assertions_part.rb +110 -0
- data/lib/liza/test_parts/test_context_part.rb +81 -0
- data/lib/liza/test_parts/test_dsl_part.rb +122 -0
- data/lib/liza/test_parts/test_subject_part.rb +19 -0
- data/lib/liza/unit.rb +149 -0
- data/lib/liza/unit_test.rb +79 -0
- data/lib/liza.rb +34 -0
- data/lib/lizarb/ruby/class.rb +15 -0
- data/lib/lizarb/ruby/string.rb +9 -0
- data/lib/lizarb/ruby/time.rb +9 -0
- data/lib/{viral → lizarb}/version.rb +2 -2
- data/lib/lizarb.rb +68 -0
- data/lib/net_system/dev/net_command.rb +24 -0
- data/lib/net_system/net/controllers/adapter/redis_adapter.rb +28 -0
- data/lib/net_system/net/controllers/adapter/redis_adapter_test.rb +29 -0
- data/lib/net_system/net/controllers/adapter/sqlite_adapter.rb +28 -0
- data/lib/net_system/net/controllers/adapter/sqlite_adapter_test.rb +29 -0
- data/lib/net_system/net/controllers/adapter.rb +5 -0
- data/lib/net_system/net/controllers/adapter_test.rb +14 -0
- data/lib/net_system/net/controllers/database/redis_db.rb +11 -0
- data/lib/net_system/net/controllers/database/redis_db_test.rb +23 -0
- data/lib/net_system/net/controllers/database/sqlite_db.rb +11 -0
- data/lib/net_system/net/controllers/database/sqlite_db_test.rb +23 -0
- data/lib/net_system/net/controllers/database.rb +33 -0
- data/lib/net_system/net/controllers/database_test.rb +14 -0
- data/lib/net_system/net/controllers/model.rb +31 -0
- data/lib/net_system/net/controllers/model_test.rb +14 -0
- data/lib/net_system/net/net_box.rb +20 -0
- data/lib/net_system/net/net_box_test.rb +19 -0
- data/lib/net_system/net/panels/adapter_panel.rb +5 -0
- data/lib/net_system/net/panels/adapter_panel_test.rb +18 -0
- data/lib/net_system/net/panels/database_panel.rb +12 -0
- data/lib/net_system/net/panels/database_panel_test.rb +18 -0
- data/lib/net_system.rb +3 -0
- data/lib/web_system/dev/web_command.rb +42 -0
- data/lib/web_system/web/controllers/request/client_error_request.rb +18 -0
- data/lib/web_system/web/controllers/request/not_found_request.rb +18 -0
- data/lib/web_system/web/controllers/request/server_error_request.rb +20 -0
- data/lib/web_system/web/controllers/request.rb +5 -0
- data/lib/web_system/web/controllers/request_test.rb +18 -0
- data/lib/web_system/web/panels/request_panel.rb +55 -0
- data/lib/web_system/web/panels/request_panel_test.rb +14 -0
- data/lib/web_system/web/web_box.rb +12 -0
- data/lib/web_system/web/web_box_test.rb +18 -0
- data/lib/web_system.rb +5 -0
- data/lizarb.gemspec +44 -0
- data/sig/{viral.rbs → lizarb.rbs} +1 -1
- data/web_files/favicon.ico +0 -0
- data/web_files/favicon.svg +948 -0
- metadata +190 -13
- data/viral.gemspec +0 -45
data/lib/liza/unit.rb
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
module Liza
|
2
|
+
class Unit
|
3
|
+
|
4
|
+
# PARTS
|
5
|
+
|
6
|
+
def self.part key, system: nil
|
7
|
+
App.connect_part self, key, system
|
8
|
+
end
|
9
|
+
|
10
|
+
# SETTINGS
|
11
|
+
|
12
|
+
def self.settings
|
13
|
+
@settings ||= {}
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.get key
|
17
|
+
return settings[key] if settings.has_key? key
|
18
|
+
|
19
|
+
found = nil
|
20
|
+
|
21
|
+
for klass in ancestors
|
22
|
+
break unless klass.respond_to? :settings
|
23
|
+
|
24
|
+
if klass.settings.has_key? key
|
25
|
+
found = klass.settings[key]
|
26
|
+
|
27
|
+
break
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
found = settings[key] = found.dup if found.is_a? Enumerable
|
32
|
+
|
33
|
+
found
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.set key, value
|
37
|
+
settings[key] = value
|
38
|
+
value
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.add list, key = nil, value
|
42
|
+
if key
|
43
|
+
fetch(list) { Hash.new }[key] = value
|
44
|
+
else
|
45
|
+
fetch(list) { Set.new } << value
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.fetch key, &block
|
50
|
+
x = get key
|
51
|
+
x ||= set key, instance_eval(&block)
|
52
|
+
x
|
53
|
+
end
|
54
|
+
|
55
|
+
def settings
|
56
|
+
@settings ||= {}
|
57
|
+
end
|
58
|
+
|
59
|
+
def get key
|
60
|
+
return settings[key] if settings.has_key? key
|
61
|
+
|
62
|
+
self.class.get key
|
63
|
+
end
|
64
|
+
|
65
|
+
def set key, value
|
66
|
+
settings[key] = value
|
67
|
+
end
|
68
|
+
|
69
|
+
def add list, key = nil, value
|
70
|
+
if key
|
71
|
+
fetch(list) { Hash.new }[key] = value
|
72
|
+
else
|
73
|
+
fetch(list) { Set.new } << value
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def fetch key, &block
|
78
|
+
x = get key
|
79
|
+
x ||= set key, eval(&block)
|
80
|
+
x
|
81
|
+
end
|
82
|
+
|
83
|
+
# LOG
|
84
|
+
|
85
|
+
LOG_LEVELS = {
|
86
|
+
:higher => 2,
|
87
|
+
:high => 1,
|
88
|
+
:normal => 0,
|
89
|
+
:low => -1,
|
90
|
+
:lower => -2,
|
91
|
+
}
|
92
|
+
|
93
|
+
set :log_level, :normal
|
94
|
+
set :log_color, :white
|
95
|
+
|
96
|
+
#
|
97
|
+
|
98
|
+
LOG_JUST = 40
|
99
|
+
|
100
|
+
def self.log log_level = :normal, string
|
101
|
+
raise "invalid log_level `#{log_level}`" unless LOG_LEVELS.keys.include? log_level
|
102
|
+
return unless log_level? log_level
|
103
|
+
|
104
|
+
source = (self.is_a? Class) ? self : self.class
|
105
|
+
source = source.to_s.ljust(LOG_JUST).bold.colorize(source.log_color)
|
106
|
+
|
107
|
+
string = "#{source} #{string}"
|
108
|
+
|
109
|
+
DevBox.logs.call string
|
110
|
+
end
|
111
|
+
|
112
|
+
def self.log_level
|
113
|
+
get(:log_level) || :normal
|
114
|
+
end
|
115
|
+
|
116
|
+
def self.log_level? log_level = :normal
|
117
|
+
# TODO
|
118
|
+
true
|
119
|
+
end
|
120
|
+
|
121
|
+
def self.log_color
|
122
|
+
(get(:system) || self).get :log_color
|
123
|
+
end
|
124
|
+
|
125
|
+
def self.log?(log_level = :normal)= log_level? log_level
|
126
|
+
def log(...)= self.class.log(...)
|
127
|
+
def log_level(...)= self.class.log_level(...)
|
128
|
+
def log?(...)= self.class.log?(...)
|
129
|
+
def log_level?(...)= self.class.log_level?(...)
|
130
|
+
def log_color(...)= self.class.log_color(...)
|
131
|
+
|
132
|
+
# SYSTEM
|
133
|
+
|
134
|
+
def self.inherited_explicitly_sets_system
|
135
|
+
|
136
|
+
def self.inherited sub
|
137
|
+
super
|
138
|
+
|
139
|
+
return unless sub.name.to_s.include? "::"
|
140
|
+
|
141
|
+
system = Object.const_get sub.first_namespace
|
142
|
+
|
143
|
+
sub.set :system, system
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
147
|
+
|
148
|
+
end
|
149
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module Liza
|
2
|
+
class UnitTest < Test
|
3
|
+
test :subject_class do
|
4
|
+
assert subject_class == Liza::Unit
|
5
|
+
end
|
6
|
+
|
7
|
+
test :settings do
|
8
|
+
assert subject_class.get(:log_level) == :normal
|
9
|
+
assert subject_class.get(:log_color) == :white
|
10
|
+
assert subject_class.log_level == :normal
|
11
|
+
assert subject_class.log_color == :white
|
12
|
+
|
13
|
+
assert subject_class.settings == {log_level: :normal, log_color: :white}
|
14
|
+
end
|
15
|
+
|
16
|
+
test :settings_inheritance do
|
17
|
+
class_a = Class.new(subject_class) do
|
18
|
+
set :number, 1
|
19
|
+
|
20
|
+
add :default, 10
|
21
|
+
add :default, 20
|
22
|
+
|
23
|
+
set :array, []
|
24
|
+
add :array, 10
|
25
|
+
add :array, 20
|
26
|
+
|
27
|
+
add :hash, :a, 10
|
28
|
+
add :hash, :b, 20
|
29
|
+
end
|
30
|
+
|
31
|
+
assert class_a.settings == {
|
32
|
+
number: 1,
|
33
|
+
default: Set[10, 20],
|
34
|
+
array: [10, 20],
|
35
|
+
hash: {a: 10, b: 20}
|
36
|
+
}
|
37
|
+
|
38
|
+
assert class_a.get(:default) == Set[10, 20]
|
39
|
+
assert class_a.get(:array) == [10, 20]
|
40
|
+
assert class_a.get(:hash) == {a: 10, b: 20}
|
41
|
+
|
42
|
+
class_b = Class.new(class_a) do
|
43
|
+
set :string, "a"
|
44
|
+
|
45
|
+
add :default, 30
|
46
|
+
add :default, 40
|
47
|
+
|
48
|
+
add :array, 30
|
49
|
+
add :array, 40
|
50
|
+
|
51
|
+
add :hash, :c, 30
|
52
|
+
add :hash, :d, 40
|
53
|
+
end
|
54
|
+
|
55
|
+
assert class_a.settings == {
|
56
|
+
number: 1,
|
57
|
+
default: Set[10, 20],
|
58
|
+
array: [10, 20],
|
59
|
+
hash: {a: 10, b: 20}
|
60
|
+
}
|
61
|
+
|
62
|
+
assert class_a.get(:default) == Set[10, 20]
|
63
|
+
assert class_a.get(:array) == [10, 20]
|
64
|
+
assert class_a.get(:hash) == {a: 10, b: 20}
|
65
|
+
|
66
|
+
assert class_b.settings == {
|
67
|
+
string: "a",
|
68
|
+
default: Set[10, 20, 30, 40],
|
69
|
+
array: [10, 20, 30, 40],
|
70
|
+
hash: {a: 10, b: 20, c: 30, d: 40}
|
71
|
+
}
|
72
|
+
|
73
|
+
assert class_b.get(:default) == Set[10, 20, 30, 40]
|
74
|
+
assert class_b.get(:array) == [10, 20, 30, 40]
|
75
|
+
assert class_b.get(:hash) == {a: 10, b: 20, c: 30, d: 40}
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
end
|
data/lib/liza.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Liza
|
4
|
+
class Error < StandardError; end
|
5
|
+
|
6
|
+
#
|
7
|
+
|
8
|
+
module_function
|
9
|
+
|
10
|
+
def log s
|
11
|
+
puts s.bold
|
12
|
+
end
|
13
|
+
|
14
|
+
#
|
15
|
+
|
16
|
+
# After checking the top-level namespace, looks up the Liza namespace
|
17
|
+
def const name
|
18
|
+
name = name.to_s.camelize
|
19
|
+
|
20
|
+
return Object.const_get name if Object.const_defined? name
|
21
|
+
|
22
|
+
const_get name
|
23
|
+
end
|
24
|
+
|
25
|
+
# constants missing from Liza will be looked up in all systems
|
26
|
+
def const_missing name
|
27
|
+
for k in App.systems.values.reverse
|
28
|
+
return k.const_get(name) if k.const_defined? name
|
29
|
+
end
|
30
|
+
|
31
|
+
super
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Class
|
4
|
+
def descendants
|
5
|
+
ObjectSpace.each_object(Class).select { |klass| klass < self }
|
6
|
+
end
|
7
|
+
|
8
|
+
def first_namespace
|
9
|
+
name.rpartition('::')[0]
|
10
|
+
end
|
11
|
+
|
12
|
+
def last_namespace
|
13
|
+
name.rpartition('::')[-1]
|
14
|
+
end
|
15
|
+
end
|
data/lib/lizarb.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "colorize"
|
4
|
+
require "pathname"
|
5
|
+
require "zeitwerk"
|
6
|
+
|
7
|
+
require_relative "lizarb/version"
|
8
|
+
|
9
|
+
module Lizarb
|
10
|
+
class Error < StandardError; end
|
11
|
+
|
12
|
+
#
|
13
|
+
|
14
|
+
GEM_DIR = Gem::Specification.find_by_name("lizarb").gem_dir
|
15
|
+
CUR_DIR = Dir.pwd
|
16
|
+
|
17
|
+
IS_APP_DIR = File.file? "#{CUR_DIR}/app.rb"
|
18
|
+
IS_LIZ_DIR = File.file? "#{CUR_DIR}/lib/lizarb.rb"
|
19
|
+
|
20
|
+
APP_DIR = IS_APP_DIR ? CUR_DIR : GEM_DIR
|
21
|
+
|
22
|
+
#
|
23
|
+
|
24
|
+
module_function
|
25
|
+
|
26
|
+
def log s
|
27
|
+
puts s.bold
|
28
|
+
end
|
29
|
+
|
30
|
+
# called from exe/lizarb
|
31
|
+
def call
|
32
|
+
require "app"
|
33
|
+
|
34
|
+
setup_core_ext
|
35
|
+
setup_gemfile
|
36
|
+
|
37
|
+
require "#{APP_DIR}/app"
|
38
|
+
|
39
|
+
VERSION
|
40
|
+
end
|
41
|
+
|
42
|
+
# called from "#{APP_DIR}/app"
|
43
|
+
def bundle
|
44
|
+
require "bundler/setup"
|
45
|
+
Bundler.require :default
|
46
|
+
|
47
|
+
bundle_liza
|
48
|
+
|
49
|
+
check_mode!
|
50
|
+
end
|
51
|
+
|
52
|
+
# setup
|
53
|
+
|
54
|
+
def setup_core_ext
|
55
|
+
pattern =
|
56
|
+
IS_LIZ_DIR ? "lib/lizarb/ruby/*.rb"
|
57
|
+
: "#{GEM_DIR}/lib/lizarb/ruby/*.rb"
|
58
|
+
|
59
|
+
Dir[pattern].each &method(:load)
|
60
|
+
end
|
61
|
+
|
62
|
+
def setup_gemfile
|
63
|
+
ENV["BUNDLE_GEMFILE"] =
|
64
|
+
IS_APP_DIR ? "#{CUR_DIR}/Gemfile"
|
65
|
+
: "#{GEM_DIR}/exe/Gemfile"
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class NetSystem
|
2
|
+
class NetCommand < Liza::Command
|
3
|
+
|
4
|
+
def self.call args
|
5
|
+
log :higher, "Called #{self} with args #{args}"
|
6
|
+
|
7
|
+
ruby_time = Time.now
|
8
|
+
redis_time = ::NetBox.databases.redis.now
|
9
|
+
sqlite_time = ::NetBox.databases.sqlite.now
|
10
|
+
# redis_time = ::RedisDb.current.now
|
11
|
+
# sqlite_time = ::SqliteDb.current.now
|
12
|
+
|
13
|
+
puts <<-OUTPUT.bold
|
14
|
+
|
15
|
+
Time for Ruby: #{ruby_time.to_s.light_red}
|
16
|
+
Time for Redis: #{redis_time.to_s.light_red}
|
17
|
+
Time for Sqlite: #{sqlite_time.to_s.light_red}
|
18
|
+
|
19
|
+
OUTPUT
|
20
|
+
Liza::DevCommand.call []
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class NetSystem
|
2
|
+
class RedisAdapter < Adapter
|
3
|
+
|
4
|
+
# https://redis.io/
|
5
|
+
# https://github.com/redis/redis-rb
|
6
|
+
def initialize *args
|
7
|
+
args = [url: Liza.const(:net_box).adapters.get(:redis_url)] if args.empty?
|
8
|
+
log "Connecting to #{args}"
|
9
|
+
@conn = Redis.new *args
|
10
|
+
end
|
11
|
+
|
12
|
+
attr_reader :conn
|
13
|
+
|
14
|
+
def call cmd_name, *args
|
15
|
+
t = Time.now
|
16
|
+
result = @conn.send cmd_name, *args
|
17
|
+
|
18
|
+
result
|
19
|
+
ensure
|
20
|
+
log "#{t.diff}s | #{cmd_name} | #{args}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def now
|
24
|
+
call :time
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class NetSystem
|
2
|
+
class RedisAdapterTest < AdapterTest
|
3
|
+
|
4
|
+
def subject
|
5
|
+
@subject ||= subject_class.new
|
6
|
+
end
|
7
|
+
|
8
|
+
test :subject_class do
|
9
|
+
assert subject_class == Liza::RedisAdapter
|
10
|
+
end
|
11
|
+
|
12
|
+
test :subject do
|
13
|
+
assert subject.conn.class == Redis
|
14
|
+
end
|
15
|
+
|
16
|
+
test :call do
|
17
|
+
result = subject.call :keys
|
18
|
+
assert result.class == Array
|
19
|
+
assert result.count == 0
|
20
|
+
end
|
21
|
+
|
22
|
+
test :now do
|
23
|
+
result = subject.now
|
24
|
+
assert result.class == Array
|
25
|
+
assert result.map(&:class) == [Integer, Integer]
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class NetSystem
|
2
|
+
class SqliteAdapter < Adapter
|
3
|
+
|
4
|
+
# https://www.sqlite.org/
|
5
|
+
# https://github.com/sparklemotion/sqlite3-ruby
|
6
|
+
def initialize *args
|
7
|
+
args = [Liza.const(:net_box).adapters.get(:sqlite)] if args.empty?
|
8
|
+
log "Connecting to #{args}"
|
9
|
+
@conn = SQLite3::Database.new *args
|
10
|
+
end
|
11
|
+
|
12
|
+
attr_reader :conn
|
13
|
+
|
14
|
+
def call sql, *args
|
15
|
+
t = Time.now
|
16
|
+
result = @conn.execute2 sql, *args
|
17
|
+
|
18
|
+
result
|
19
|
+
ensure
|
20
|
+
log "#{t.diff}s | #{sql} | #{args}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def now
|
24
|
+
call "SELECT strftime('%Y-%m-%dT%H:%M:%S.%f', 'now', 'localtime');"
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class NetSystem
|
2
|
+
class SqliteAdapterTest < AdapterTest
|
3
|
+
|
4
|
+
def subject
|
5
|
+
@subject ||= subject_class.new ":memory:"
|
6
|
+
end
|
7
|
+
|
8
|
+
test :subject_class do
|
9
|
+
assert subject_class == Liza::SqliteAdapter
|
10
|
+
end
|
11
|
+
|
12
|
+
test :subject do
|
13
|
+
assert subject.conn.class == SQLite3::Database
|
14
|
+
end
|
15
|
+
|
16
|
+
test :call do
|
17
|
+
result = subject.call "SELECT name, sql FROM sqlite_master WHERE type = 'table';"
|
18
|
+
assert result == [["name", "sql"]]
|
19
|
+
end
|
20
|
+
|
21
|
+
test :now do
|
22
|
+
result = subject.now
|
23
|
+
assert result.class == Array
|
24
|
+
assert result[0] == ["strftime('%Y-%m-%dT%H:%M:%S.%f', 'now', 'localtime')"]
|
25
|
+
assert result[1][0].class == String
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class NetSystem
|
2
|
+
class AdapterTest < Liza::ControllerTest
|
3
|
+
|
4
|
+
test :subject_class do
|
5
|
+
assert subject_class == NetSystem::Adapter
|
6
|
+
end
|
7
|
+
|
8
|
+
test :settings do
|
9
|
+
assert subject_class.log_level == :normal
|
10
|
+
assert subject_class.log_color == :red
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class NetSystem
|
2
|
+
class RedisDbTest < DatabaseTest
|
3
|
+
|
4
|
+
test :subject_class do
|
5
|
+
assert subject_class == NetSystem::RedisDb
|
6
|
+
end
|
7
|
+
|
8
|
+
test :subject do
|
9
|
+
assert subject.adapter.class == NetSystem::RedisAdapter
|
10
|
+
end
|
11
|
+
|
12
|
+
test :now do
|
13
|
+
t = subject.now
|
14
|
+
assert! t.is_a? Time
|
15
|
+
assert t.yday == Time.now.yday
|
16
|
+
end
|
17
|
+
|
18
|
+
# test :call do
|
19
|
+
# todo "write this"
|
20
|
+
# end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class NetSystem
|
2
|
+
class SqliteDbTest < DatabaseTest
|
3
|
+
|
4
|
+
test :subject_class do
|
5
|
+
assert subject_class == NetSystem::SqliteDb
|
6
|
+
end
|
7
|
+
|
8
|
+
test :subject do
|
9
|
+
assert subject.adapter.class == NetSystem::SqliteAdapter
|
10
|
+
end
|
11
|
+
|
12
|
+
test :now do
|
13
|
+
t = subject.now
|
14
|
+
assert! t.is_a? Time
|
15
|
+
assert t.yday == Time.now.yday
|
16
|
+
end
|
17
|
+
|
18
|
+
# test :call do
|
19
|
+
# todo "write this"
|
20
|
+
# end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
class NetSystem
|
2
|
+
class Database < Liza::Controller
|
3
|
+
|
4
|
+
def self.inherited sub
|
5
|
+
super
|
6
|
+
|
7
|
+
return if sub.name.nil?
|
8
|
+
return if sub.name.end_with? "Db"
|
9
|
+
raise "please rename #{sub.name} to #{sub.name}Db"
|
10
|
+
end
|
11
|
+
|
12
|
+
attr_reader :adapter
|
13
|
+
|
14
|
+
def initialize adapter: get(:adapter).new
|
15
|
+
@adapter = adapter
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.set_adapter adapter_id
|
19
|
+
set :adapter, Liza.const("#{adapter_id}_adapter")
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.current
|
23
|
+
Thread.current[last_namespace] ||= begin
|
24
|
+
log "Connecting to #{last_namespace}"
|
25
|
+
new
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.call(...); current.call(...); end
|
30
|
+
def call(...); @adapter.call(...); end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class NetSystem
|
2
|
+
class DatabaseTest < Liza::ControllerTest
|
3
|
+
|
4
|
+
test :subject_class do
|
5
|
+
assert subject_class == NetSystem::Database
|
6
|
+
end
|
7
|
+
|
8
|
+
test :settings do
|
9
|
+
assert subject_class.log_level == :normal
|
10
|
+
assert subject_class.log_color == :red
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class NetSystem
|
2
|
+
class Model < Liza::Controller
|
3
|
+
|
4
|
+
def self.inherited sub
|
5
|
+
super
|
6
|
+
|
7
|
+
return if sub.name.nil?
|
8
|
+
return if sub.name.end_with? "Model"
|
9
|
+
raise "please rename #{sub.name} to #{sub.name}Model"
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.db database_id = nil
|
13
|
+
if database_id.nil?
|
14
|
+
db = get :db
|
15
|
+
if db
|
16
|
+
NetBox.databases.get db
|
17
|
+
else
|
18
|
+
raise "please set a db to model #{self}"
|
19
|
+
end
|
20
|
+
else
|
21
|
+
valid = NetBox.databases.settings.keys
|
22
|
+
if valid.include? database_id
|
23
|
+
set :db, database_id
|
24
|
+
else
|
25
|
+
raise "invalid db, valid options are #{valid}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|