lizarb 0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (139) hide show
  1. checksums.yaml +4 -4
  2. data/.ruby-version +1 -0
  3. data/CHANGELOG.md +13 -2
  4. data/Gemfile +21 -2
  5. data/Gemfile.lock +44 -0
  6. data/README.md +88 -12
  7. data/app/dev/commands/app_command.rb +5 -0
  8. data/app/dev/commands/app_command_test.rb +7 -0
  9. data/app/dev/commands/example_command.rb +57 -0
  10. data/app/dev/commands/example_command_test.rb +43 -0
  11. data/app/dev/commands/macro_child_command.rb +32 -0
  12. data/app/dev/commands/macro_child_command_test.rb +7 -0
  13. data/app/dev/commands/macro_parent_command.rb +70 -0
  14. data/app/dev/commands/macro_parent_command_test.rb +7 -0
  15. data/app/dev/commands/my_command.rb +7 -0
  16. data/app/dev/commands/my_command_test.rb +7 -0
  17. data/app/dev/commands/new_command.rb +69 -0
  18. data/app/dev/commands/new_command_test.rb +7 -0
  19. data/app/dev_box.rb +13 -0
  20. data/app/happy/axos/axo.rb +50 -0
  21. data/app/happy_box.rb +8 -0
  22. data/app/net/databases/redis_db.rb +5 -0
  23. data/app/net/databases/redis_db_test.rb +12 -0
  24. data/app/net/databases/sqlite_db.rb +11 -0
  25. data/app/net/databases/sqlite_db_test.rb +12 -0
  26. data/app/net/models/app_model.rb +20 -0
  27. data/app/net/models/app_model_test.rb +8 -0
  28. data/app/net/models/post_model.rb +4 -0
  29. data/app/net/models/post_model_test.rb +8 -0
  30. data/app/net/models/user_model.rb +4 -0
  31. data/app/net/models/user_model_test.rb +8 -0
  32. data/app/net_box.rb +21 -0
  33. data/app/web/requests/api_request.rb +61 -0
  34. data/app/web/requests/api_request_test.rb +31 -0
  35. data/app/web/requests/app_request.rb +5 -0
  36. data/app/web/requests/app_request_test.rb +7 -0
  37. data/app/web/requests/assets_request.rb +71 -0
  38. data/app/web/requests/assets_request_test.rb +33 -0
  39. data/app/web/requests/root_request.rb +55 -0
  40. data/app/web/requests/root_request_test.rb +31 -0
  41. data/app/web_box.rb +8 -0
  42. data/app.code.env +11 -0
  43. data/app.env +11 -0
  44. data/app.rb +17 -0
  45. data/app_new/.gitignore +4 -0
  46. data/app_new/Gemfile +31 -0
  47. data/app_new/Procfile +3 -0
  48. data/exe/Gemfile +3 -0
  49. data/exe/Gemfile.lock +13 -0
  50. data/exe/liza +3 -0
  51. data/exe/lizarb +11 -0
  52. data/lib/app.rb +200 -0
  53. data/lib/dev_system/dev/controllers/command/dev_command.rb +19 -0
  54. data/lib/dev_system/dev/controllers/command/dev_command_test.rb +14 -0
  55. data/lib/dev_system/dev/controllers/command/echo_command.rb +11 -0
  56. data/lib/dev_system/dev/controllers/command/echo_command_test.rb +14 -0
  57. data/lib/dev_system/dev/controllers/command/test_command.rb +70 -0
  58. data/lib/dev_system/dev/controllers/command/test_command_test.rb +14 -0
  59. data/lib/dev_system/dev/controllers/command/version_command.rb +9 -0
  60. data/lib/dev_system/dev/controllers/command/version_command_test.rb +14 -0
  61. data/lib/dev_system/dev/controllers/command.rb +19 -0
  62. data/lib/dev_system/dev/controllers/command_test.rb +14 -0
  63. data/lib/dev_system/dev/dev_box.rb +19 -0
  64. data/lib/dev_system/dev/dev_box_test.rb +19 -0
  65. data/lib/dev_system/dev/panels/command_panel.rb +10 -0
  66. data/lib/dev_system/dev/panels/command_panel_test.rb +23 -0
  67. data/lib/dev_system/dev/panels/log_panel.rb +10 -0
  68. data/lib/dev_system/dev/panels/log_panel_test.rb +18 -0
  69. data/lib/dev_system.rb +8 -0
  70. data/lib/happy_system/dev/happy_command.rb +11 -0
  71. data/lib/happy_system/happy/controllers/axo.rb +8 -0
  72. data/lib/happy_system/happy/happy_box.rb +12 -0
  73. data/lib/happy_system/happy/happy_box_test.rb +18 -0
  74. data/lib/happy_system/happy/panels/axo_panel.rb +5 -0
  75. data/lib/{viral.rb → happy_system.rb} +7 -3
  76. data/lib/liza/base/box.rb +40 -0
  77. data/lib/liza/base/box_test.rb +12 -0
  78. data/lib/liza/base/controller.rb +27 -0
  79. data/lib/liza/base/controller_test.rb +12 -0
  80. data/lib/liza/base/panel.rb +38 -0
  81. data/lib/liza/base/panel_test.rb +16 -0
  82. data/lib/liza/meta/part.rb +19 -0
  83. data/lib/liza/meta/part_extension.rb +13 -0
  84. data/lib/liza/meta/part_test.rb +8 -0
  85. data/lib/liza/meta/system.rb +20 -0
  86. data/lib/liza/meta/system_test.rb +13 -0
  87. data/lib/liza/test/test.rb +11 -0
  88. data/lib/liza/test/test_test.rb +108 -0
  89. data/lib/liza/test_parts/test_assertions_part.rb +110 -0
  90. data/lib/liza/test_parts/test_context_part.rb +81 -0
  91. data/lib/liza/test_parts/test_dsl_part.rb +122 -0
  92. data/lib/liza/test_parts/test_subject_part.rb +19 -0
  93. data/lib/liza/unit.rb +149 -0
  94. data/lib/liza/unit_test.rb +79 -0
  95. data/lib/liza.rb +34 -0
  96. data/lib/lizarb/ruby/class.rb +15 -0
  97. data/lib/lizarb/ruby/string.rb +9 -0
  98. data/lib/lizarb/ruby/time.rb +9 -0
  99. data/lib/{viral → lizarb}/version.rb +2 -2
  100. data/lib/lizarb.rb +68 -0
  101. data/lib/net_system/dev/net_command.rb +24 -0
  102. data/lib/net_system/net/controllers/adapter/redis_adapter.rb +28 -0
  103. data/lib/net_system/net/controllers/adapter/redis_adapter_test.rb +29 -0
  104. data/lib/net_system/net/controllers/adapter/sqlite_adapter.rb +28 -0
  105. data/lib/net_system/net/controllers/adapter/sqlite_adapter_test.rb +29 -0
  106. data/lib/net_system/net/controllers/adapter.rb +5 -0
  107. data/lib/net_system/net/controllers/adapter_test.rb +14 -0
  108. data/lib/net_system/net/controllers/database/redis_db.rb +11 -0
  109. data/lib/net_system/net/controllers/database/redis_db_test.rb +23 -0
  110. data/lib/net_system/net/controllers/database/sqlite_db.rb +11 -0
  111. data/lib/net_system/net/controllers/database/sqlite_db_test.rb +23 -0
  112. data/lib/net_system/net/controllers/database.rb +33 -0
  113. data/lib/net_system/net/controllers/database_test.rb +14 -0
  114. data/lib/net_system/net/controllers/model.rb +31 -0
  115. data/lib/net_system/net/controllers/model_test.rb +14 -0
  116. data/lib/net_system/net/net_box.rb +20 -0
  117. data/lib/net_system/net/net_box_test.rb +19 -0
  118. data/lib/net_system/net/panels/adapter_panel.rb +5 -0
  119. data/lib/net_system/net/panels/adapter_panel_test.rb +18 -0
  120. data/lib/net_system/net/panels/database_panel.rb +12 -0
  121. data/lib/net_system/net/panels/database_panel_test.rb +18 -0
  122. data/lib/net_system.rb +3 -0
  123. data/lib/web_system/dev/web_command.rb +42 -0
  124. data/lib/web_system/web/controllers/request/client_error_request.rb +18 -0
  125. data/lib/web_system/web/controllers/request/not_found_request.rb +18 -0
  126. data/lib/web_system/web/controllers/request/server_error_request.rb +20 -0
  127. data/lib/web_system/web/controllers/request.rb +5 -0
  128. data/lib/web_system/web/controllers/request_test.rb +18 -0
  129. data/lib/web_system/web/panels/request_panel.rb +55 -0
  130. data/lib/web_system/web/panels/request_panel_test.rb +14 -0
  131. data/lib/web_system/web/web_box.rb +12 -0
  132. data/lib/web_system/web/web_box_test.rb +18 -0
  133. data/lib/web_system.rb +5 -0
  134. data/lizarb.gemspec +44 -0
  135. data/sig/{viral.rbs → lizarb.rbs} +1 -1
  136. data/web_files/favicon.ico +0 -0
  137. data/web_files/favicon.svg +948 -0
  138. metadata +190 -13
  139. 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
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class String
4
+ alias lpartition partition
5
+
6
+ def camelize
7
+ split("_").map { |s| "#{s[0].upcase}#{s[1..-1]}" }.join("")
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Time
4
+ def diff digits = 4
5
+ f = (self.class.now.to_f - to_f).floor(digits)
6
+ u, d = f.to_s.split "."
7
+ "#{u}.#{d.ljust digits, "0"}"
8
+ end
9
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Viral
4
- VERSION = "0"
3
+ module Lizarb
4
+ VERSION = "1.0.0"
5
5
  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,5 @@
1
+ class NetSystem
2
+ class Adapter < Liza::Controller
3
+
4
+ end
5
+ 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,11 @@
1
+ class NetSystem
2
+ class RedisDb < Database
3
+ set_adapter :redis
4
+
5
+ def now
6
+ array = adapter.now
7
+ Time.at array[0]
8
+ end
9
+
10
+ end
11
+ 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,11 @@
1
+ class NetSystem
2
+ class SqliteDb < Database
3
+ set_adapter :sqlite
4
+
5
+ def now
6
+ array = adapter.now
7
+ Time.parse array[1][0]
8
+ end
9
+
10
+ end
11
+ 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