tzispa 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +12 -0
  3. data/lib/tzispa/api/handler.rb +54 -23
  4. data/lib/tzispa/app.rb +60 -68
  5. data/lib/tzispa/cli.rb +42 -3
  6. data/lib/tzispa/commands/api.rb +55 -0
  7. data/lib/tzispa/commands/app.rb +83 -0
  8. data/lib/tzispa/commands/cli/generate.rb +60 -0
  9. data/lib/tzispa/commands/command.rb +28 -0
  10. data/lib/tzispa/commands/console.rb +62 -0
  11. data/lib/tzispa/commands/helpers/i18n.rb +67 -0
  12. data/lib/tzispa/commands/helpers/project.rb +69 -0
  13. data/lib/tzispa/commands/helpers/repository.rb +46 -0
  14. data/lib/tzispa/commands/project.rb +104 -0
  15. data/lib/tzispa/commands/repository.rb +66 -0
  16. data/lib/tzispa/commands/rig.rb +28 -0
  17. data/lib/tzispa/commands/server.rb +26 -0
  18. data/lib/tzispa/config/{appconfig.rb → app_config.rb} +12 -32
  19. data/lib/tzispa/config/base.rb +7 -5
  20. data/lib/tzispa/config/db_config.rb +67 -0
  21. data/lib/tzispa/config/yaml.rb +9 -10
  22. data/lib/tzispa/context.rb +3 -2
  23. data/lib/tzispa/controller/api.rb +66 -60
  24. data/lib/tzispa/controller/auth_layout.rb +4 -28
  25. data/lib/tzispa/controller/base.rb +61 -24
  26. data/lib/tzispa/controller/exceptions.rb +3 -4
  27. data/lib/tzispa/controller/http_error.rb +0 -3
  28. data/lib/tzispa/controller/layout.rb +4 -4
  29. data/lib/tzispa/domain.rb +27 -23
  30. data/lib/tzispa/env.rb +34 -0
  31. data/lib/tzispa/environment.rb +231 -0
  32. data/lib/tzispa/http/context.rb +65 -80
  33. data/lib/tzispa/http/request.rb +29 -17
  34. data/lib/tzispa/http/response.rb +45 -12
  35. data/lib/tzispa/route_set.rb +100 -0
  36. data/lib/tzispa/server.rb +61 -0
  37. data/lib/tzispa/tzisparc.rb +80 -0
  38. data/lib/tzispa/version.rb +1 -1
  39. data/lib/tzispa.rb +3 -1
  40. data/tzispa.gemspec +12 -6
  41. metadata +68 -17
  42. data/lib/tzispa/command/api.rb +0 -24
  43. data/lib/tzispa/command/app.rb +0 -95
  44. data/lib/tzispa/command/cli/generate.rb +0 -51
  45. data/lib/tzispa/command/project.rb +0 -258
  46. data/lib/tzispa/command/rig.rb +0 -26
  47. data/lib/tzispa/controller/signed_api.rb +0 -13
  48. data/lib/tzispa/http/session_flash_bag.rb +0 -62
  49. data/lib/tzispa/middleware.rb +0 -48
  50. data/lib/tzispa/routes.rb +0 -69
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tzispa/commands/command'
4
+
5
+ module Tzispa
6
+ module Commands
7
+
8
+ class Console < Command
9
+ ENGINES = {
10
+ 'pry' => 'Pry',
11
+ 'irb' => 'IRB'
12
+ }.freeze
13
+
14
+ DEFAULT_ENGINE = ['irb'].freeze
15
+
16
+ attr_reader :options
17
+
18
+ def initialize(options)
19
+ super(options)
20
+
21
+ @options = Tzispa::Environment.instance.to_options
22
+ end
23
+
24
+ def start
25
+ prepare
26
+ engine.start
27
+ end
28
+
29
+ def engine
30
+ load_engine options.fetch(:engine) { engine_lookup }
31
+ end
32
+
33
+ private
34
+
35
+ def prepare
36
+ # Clear out ARGV so Pry/IRB don't attempt to parse the rest
37
+ ARGV.shift until ARGV.empty?
38
+ # Add convenience methods to the main:Object binding
39
+ # TOPLEVEL_BINDING.eval('self').__send__(:include, CodeReloading)
40
+ end
41
+
42
+ def engine_lookup
43
+ (ENGINES.find { |_, klass| Object.const_defined?(klass) } || DEFAULT_ENGINE).first
44
+ end
45
+
46
+ # rubocop:disable Lint/HandleExceptions
47
+ # rubocop:disable Lint/EnsureReturn
48
+ def load_engine(engine)
49
+ require engine
50
+ rescue LoadError
51
+ ensure
52
+ return Object.const_get(
53
+ ENGINES.fetch(engine) do
54
+ raise ArgumentError("Unknown console engine: `#{engine}'")
55
+ end
56
+ )
57
+ end
58
+ # rubocop:enable Lint/EnsureReturn
59
+ # rubocop:enable Lint/HandleExceptions
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tzispa
4
+ module Commands
5
+ module Helpers
6
+ module I18n
7
+
8
+ # Zlib::Inflate.inflate(Base64.decode64(e)).force_encoding('UTF-8').encode
9
+
10
+ I18N_DEFAULTS_EN = <<-I18NDEFAULTSEN
11
+ eJydWFtv2zYUfu+vIIQJaYF6gLc3vS0ruqJDdkGzDXkSKIm2iFCkx0sSt8h/
12
+ 7+GdlqXUWx9q8lw/fjw8pLLZbF4R3rxCaMCa2F+EcNfJdsDHluOJKC/boE+G
13
+ h9GNiKNbQ8LoHzJE2WjC6L2k0RfrHHkSXI8nsYPVRxzjviddzIVlGP10kEl2
14
+ jB4J00fDop3Zx6zkEEa/9zqMfhMPYfSO9G60tNAhZYC15gksV+UZLJmXc1i4
15
+ LKaw+jwBAoyM052QE9YhHwAgO2yYblBV323qaVMPVdAwwfdWfI3q4S2q76Jc
16
+ jUI6+w5F45dINVgeM7GymAK7/Zj5pWyJYZIpPmaOjdKZZk2mjshMtsgzILxU
17
+ Aut5KuRAZKS9OZK0141bTpx43mx9ajqFGh2o0pj3pKW8fYQ4iU3cCaPbp3YU
18
+ sBtRCKk4abwObZHVZY0eAUTQ1V96Ybh+diZqFtKBWouZEa8H9QFmUe2y14Im
19
+ StZjOvcYkk1CrcZ0yrWgXrkYdcRs1+J2otxAh3BThJGfxkIlSrV6xNyy5BTz
20
+ 7NYCWYu5a4KQLTJfPtRCEkV6OJjrSbbIW1yQJIQKllCtcoVAq1qhz6kWyXuy
21
+ jXQeaYtiKyhiVMnfelTJf5nP7QqL1Zy8ItBS+a4UbjWr2Rxlmfk1vqs5zT7Q
22
+ QYrpUHRAfGzQu0SJPXsN+pAPaSy9m3LJDlhjG3SC73M00JHKLbXb0aA7v21E
23
+ ShF31vdhBxJrLWkHwZ9hG6GHKrwn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V2bePGw/kJo4Xp8oMJ6ybe0kGmBsHq1qcr0anV1yRE
60
+ mqniZ2s449Q2OSyJJxcI6aKhu62wJpph4wpncGyiJUJGU0x0RUQsW4zO4Kcm
61
+ muMEV1g6elTFFfq0FEW3krWxRfix92sDPVo04nHd/8Y/VCdZrA==
62
+ I18NDEFAULTSES
63
+
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tzispa
4
+ module Commands
5
+ module Helpers
6
+ module Project
7
+
8
+ PROJECT_STRUCTURE = [
9
+ 'apps', 'config', 'config/locales', 'config/routes', 'data', 'logs', 'public',
10
+ 'public/css', 'public/vendors', 'public/css/fonts', 'public/css/less', 'public/img',
11
+ 'public/js', 'repository', 'scripts', 'tmp'
12
+ ].freeze
13
+
14
+ PUMA_CONFIG_FILE = 'puma.rb'
15
+
16
+ BOOT_FILE = 'boot.rb'
17
+
18
+ DEFAULT_MOUNT_PATH = '/'
19
+
20
+ GIT_IGNORE = [
21
+ '*.gem', '*.rbc', '.bundle', '.config', 'Gemfile.lock', 'test/tmp',
22
+ 'tmp', '*.bundle', ' .DS_Store', '.tzisparc', '.rubocop.yml', '.rubocop_todo.yml',
23
+ 'logs/', 'data/', 'tmp/', 'config/*.yml', 'puma.pid', 'puma.state', '.directory',
24
+ '*.lock', '.env.*'
25
+ ].freeze
26
+
27
+ PUMA_CONFIG = <<-PUMACONFIG
28
+ #!/usr/bin/env puma
29
+ env = Tzispa::Environment.instance
30
+ app_dir = env.root.to_s
31
+ tmp_dir = "\#{app_dir}/tmp"
32
+ logs_dir = "\#{app_dir}/logs"
33
+ environment env.environment
34
+ daemonize env.daemonize?
35
+ pidfile "\#{tmp_dir}/puma.pid"
36
+ state_path "\#{tmp_dir}/puma.state"
37
+ if env.daemonize?
38
+ stdout_redirect "\#{logs_dir}/puma.stdout", "\#{logs_dir}/puma.stderr"
39
+ end
40
+ workers 0
41
+ # threads 0, 16
42
+ if env.ssl?
43
+ path_to_key = "\#{app_dir}/\#{env['TZISPA_SSL_KEY']}"
44
+ path_to_cert = "\#{app_dir}/\#{env['TZISPA_SSL_CERT']}"
45
+ bind "ssl://\#{env.server_host}:\#{env.server_port}?key=\#{path_to_key}&cert=\#{path_to_cert}"
46
+ else
47
+ bind "tcp://\#{env.server_host}:\#{env.server_port}"
48
+ end
49
+ tag '%s'
50
+ worker_timeout 90
51
+ PUMACONFIG
52
+
53
+ ENVC_DEFAULTS = <<-ENVCDEFAULTS
54
+ # Define ENV variables
55
+ WEB_SESSIONS_SECRET="%s"
56
+ WEB_SESSIONS_TIMEOUT=2400
57
+ TZISPA_HOST=localhost
58
+ TZISPA_SERVER_HOST=0.0.0.0
59
+ # TZISPA_PORT = 9412
60
+ # TZISPA_SERVER_PORT = 9412
61
+ TZISPA_SSL=no
62
+ # TZISPA_SSL_KEY=.ssl.key
63
+ # TZISPA_SSL_CERT=.ssl.cer
64
+ ENVCDEFAULTS
65
+
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Tzispa
4
+ module Commands
5
+ module Helpers
6
+ module Repository
7
+
8
+ REPO_STRUCTURE = %w(helpers model entity command).freeze
9
+
10
+ MODEL_TEMPLATE = <<-MODTPL
11
+ # frozen_string_literal: true
12
+
13
+ require 'tzispa/data/entity'
14
+
15
+ module %s
16
+ module Model
17
+ class %s < Sequel::Model(:%s)
18
+ include Tzispa::Data::Entity
19
+ plugin :validation_helpers
20
+
21
+ def validate
22
+ true
23
+ end
24
+ end
25
+ end
26
+ end
27
+ MODTPL
28
+
29
+ ENTITY_TEMPLATE = <<-ENTTTPL
30
+ # frozen_string_literal: true
31
+
32
+ require 'tzispa/utils/decorator'
33
+
34
+ module %s
35
+ module Entity
36
+ class %s < Tzispa::Utils::Decorator
37
+
38
+ end
39
+ end
40
+ end
41
+ ENTTTPL
42
+
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'base64'
5
+ require 'zlib'
6
+ require 'pathname'
7
+ require 'tzispa/tzisparc'
8
+ require 'tzispa/environment'
9
+ require 'tzispa/helpers/security'
10
+ require 'tzispa/commands/helpers/project'
11
+ require 'tzispa/commands/helpers/i18n'
12
+ require 'tzispa/config/db_config'
13
+
14
+ module Tzispa
15
+ module Commands
16
+
17
+ class Project
18
+ include Tzispa::Helpers::Security
19
+ include Tzispa::Commands::Helpers::Project
20
+ include Tzispa::Commands::Helpers::I18n
21
+
22
+ attr_accessor :name, :apps, :created
23
+
24
+ def initialize(name)
25
+ @name = name
26
+ @apps = []
27
+ end
28
+
29
+ def generate
30
+ return unless generate_structure
31
+ generate_projectrc
32
+ generate_environment
33
+ generate_rackup
34
+ generate_puma_config
35
+ generate_gitignore
36
+ generate_i18n(%w(en es))
37
+ generate_database_config
38
+ end
39
+
40
+ private
41
+
42
+ def generate_structure
43
+ return if File.exist? name
44
+ Dir.mkdir name.to_s
45
+ PROJECT_STRUCTURE.each do |psdir|
46
+ Dir.mkdir "#{name}/#{psdir}"
47
+ File.open("#{name}/#{psdir}/.gitkeep", 'w')
48
+ end
49
+ end
50
+
51
+ def generate_projectrc
52
+ rc = Tzisparc.new Pathname.new(Dir.pwd).join(name)
53
+ rc.generate
54
+ end
55
+
56
+ def generate_environment
57
+ File.open("#{name}/.env.production", 'w') { |file| file.puts ENVC_DEFAULTS % secret(64) }
58
+ File.open("#{name}/.env.deployment", 'w') { |file| file.puts ENVC_DEFAULTS % secret(64) }
59
+ File.open("#{name}/.env.test", 'w') { |file| file.puts ENVC_DEFAULTS % secret(64) }
60
+ end
61
+
62
+ def generate_rackup
63
+ File.open("#{name}/#{Tzispa::Environment::DEFAULT_RACKUP}", 'w') do |file|
64
+ file.puts "\# project #{name} started on #{Time.now}"
65
+ end
66
+ end
67
+
68
+ def generate_gitignore
69
+ File.open("#{name}/.gitignore", 'w') do |file|
70
+ GIT_IGNORE.each { |sig| file.puts sig }
71
+ end
72
+ end
73
+
74
+ def generate_puma_config
75
+ File.open("#{name}/config/#{PUMA_CONFIG_FILE}", 'w') do |f|
76
+ f.puts PUMA_CONFIG % name
77
+ end
78
+ end
79
+
80
+ def generate_database_config
81
+ Tzispa::Config::DbConfig.create_default name
82
+ end
83
+
84
+ def generate_boot
85
+ File.open("#{name}/config/#{BOOT_FILE}", 'w') do |file|
86
+ file.puts 'require \'bundler/setup\' # Set up gems listed in the Gemfile'
87
+ file.puts
88
+ file.puts 'Bundler.require(*Tzispa::Environment.instance.bundler_groups)'
89
+ end
90
+ end
91
+
92
+ def generate_i18n(langs)
93
+ langs.each do |lang|
94
+ File.open("#{name}/config/locales/#{lang}.yml", 'w') do |f|
95
+ content = Base64.decode64(self.class.const_get("I18N_DEFAULTS_#{lang.upcase}"))
96
+ content = Zlib::Inflate.inflate(content)
97
+ f.puts content.force_encoding('UTF-8').encode
98
+ end
99
+ end
100
+ end
101
+ end
102
+
103
+ end
104
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tzispa/config/db_config'
4
+ require 'tzispa/commands/helpers/repository'
5
+
6
+ module Tzispa
7
+ module Commands
8
+
9
+ class Repository
10
+ include Tzispa::Commands::Helpers::Repository
11
+
12
+ attr_reader :name, :adapter, :database
13
+
14
+ def initialize(name, adapter, dbconn)
15
+ @name = name
16
+ @adapter = adapter
17
+ @database = dbconn
18
+ end
19
+
20
+ def generate
21
+ return unless generate_structure
22
+ Tzispa::Config::DbConfig.add_repository(name, adapter, database)
23
+ return unless (db = Sequel.connect "#{adapter}://#{database}")
24
+ tables = db.tables
25
+ generate_models tables
26
+ generate_entities tables
27
+ end
28
+
29
+ def generate_structure
30
+ return if File.exist? repo_root
31
+ Dir.mkdir repo_root
32
+ REPO_STRUCTURE.each do |psdir|
33
+ Dir.mkdir "#{repo_root}/#{psdir}"
34
+ File.open("#{repo_root}/#{psdir}/.gitkeep", 'w')
35
+ end
36
+ end
37
+
38
+ def generate_models(tables)
39
+ tables.each do |tc|
40
+ model_src = format(MODEL_TEMPLATE, name.capitalize, tc.capitalize, tc)
41
+ File.open(model_file(tc), 'w') { |f| f << model_src }
42
+ end
43
+ end
44
+
45
+ def generate_entities(tables)
46
+ tables.each do |tc|
47
+ entity_src = format(ENTITY_TEMPLATE, name.capitalize, tc.capitalize)
48
+ File.open(entity_file(tc), 'w') { |f| f << entity_src }
49
+ end
50
+ end
51
+
52
+ def repo_root
53
+ @repo_root ||= File.join('repository', name)
54
+ end
55
+
56
+ def model_file(name)
57
+ "#{repo_root}/model/#{name}.rb"
58
+ end
59
+
60
+ def entity_file(name)
61
+ "#{repo_root}/entity/#{name}.rb"
62
+ end
63
+ end
64
+
65
+ end
66
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tzispa/rig/template'
4
+
5
+ module Tzispa
6
+ module Commands
7
+
8
+ class Rig < Command
9
+ attr_reader :name, :domain, :type, :mime_format
10
+
11
+ def initialize(name, app, type, mime_format = nil, options = nil)
12
+ super(options)
13
+ @domain = Tzispa::Domain.new app
14
+ @type = type.to_sym
15
+ @name = name
16
+ @mime_format = mime_format
17
+ end
18
+
19
+ def generate
20
+ Tzispa::Rig::Template.new(name: name,
21
+ type: type,
22
+ domain: domain,
23
+ format: mime_format).create
24
+ end
25
+ end
26
+
27
+ end
28
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tzispa/commands/command'
4
+
5
+ module Tzispa
6
+ module Commands
7
+
8
+ class Server < Command
9
+ def initialize(options)
10
+ super(options)
11
+
12
+ require 'tzispa/server'
13
+ @server = Tzispa::Server.new
14
+ end
15
+
16
+ def start
17
+ server.start
18
+ end
19
+
20
+ protected
21
+
22
+ attr_reader :server
23
+ end
24
+
25
+ end
26
+ end
@@ -6,7 +6,6 @@ require 'tzispa/helpers/security'
6
6
  module Tzispa
7
7
  module Config
8
8
  class AppConfig
9
-
10
9
  include Tzispa::Helpers::Security
11
10
 
12
11
  CONFIG_FILENAME = :appconfig
@@ -25,54 +24,35 @@ module Tzispa
25
24
  def load!
26
25
  if @cftime.nil?
27
26
  @cftime = File.ctime(filename)
28
- else
29
- if @cftime != File.ctime(filename)
30
- @config = nil
31
- @cftime = File.ctime(filename)
32
- end
27
+ elsif @cftime != File.ctime(filename)
28
+ @config = nil
29
+ @cftime = File.ctime(filename)
33
30
  end
34
31
  @config ||= Tzispa::Config::Yaml.load(filename)
35
32
  end
36
33
 
37
- def create_default(host:, default_layout: 'index', locale: 'en')
38
- hcfg = Hash.new.tap { |cfg|
39
- cfg['developing'] = true
40
- cfg['id'] = domain.name
34
+ def create_default(default_layout, locale)
35
+ hcfg = {}.tap do |cfg|
41
36
  cfg['default_layout'] = default_layout
42
37
  cfg['default_format'] = 'htm'
43
- cfg['host_name'] = host
44
- cfg['canonical_url'] = "http://#{host}"
45
38
  cfg['default_encoding'] = 'utf-8'
46
- cfg['auth_required'] = false
47
39
  cfg['absolute_redirects'] = true
48
40
  cfg['salt'] = secret(24)
49
41
  cfg['secret'] = secret(36)
50
- cfg['temp_dir'] = 'tmp'
51
- cfg['temp_dir'] = true
52
- cfg['template_cache'] = Hash.new.tap { |tpc|
53
- tpc['enabled'] = false
54
- tpc['size'] = 0
55
- }
56
- cfg['locales'] = Hash.new.tap { |loc|
42
+ cfg['locales'] = {}.tap do |loc|
57
43
  loc['preload'] = true
58
44
  loc['default'] = locale
59
- }
60
- cfg['logging'] = Hash.new.tap { |log|
45
+ end
46
+ cfg['logging'] = {}.tap do |log|
61
47
  log['enabled'] = true
62
48
  log['shift_age'] = 'daily'
63
- }
64
- cfg['sessions'] = Hash.new.tap { |ses|
65
- ses['enabled'] = true
66
- ses['timeout'] = 3600
67
- ses['store_path'] = 'data/session'
68
- ses['secret'] = secret(32)
69
- }
70
- }
49
+ end
50
+ cfg['sessions'] = {}.tap { |ses| ses['enabled'] = true }
51
+ end
71
52
  Tzispa::Config::Yaml.save(hcfg, filename)
72
53
  load!
73
54
  end
74
-
75
-
76
55
  end
56
+
77
57
  end
78
58
  end
@@ -1,15 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'ostruct'
2
4
 
3
5
  module Tzispa
4
6
  module Config
5
- class Base < Struct
6
7
 
8
+ class Base < Struct
7
9
  def self.parametrize(params)
8
- self.new( *(params.keys.map { |k| k.to_sym })).new(*(params.values.map { |v|
9
- v.is_a?(Hash) ? self.parametrize(v) : v
10
- }))
10
+ new(*params.keys.map(&:to_sym)).new(*(params.values.map do |v|
11
+ v.is_a?(Hash) ? parametrize(v) : v
12
+ end))
11
13
  end
12
-
13
14
  end
15
+
14
16
  end
15
17
  end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'tzispa/config/yaml'
4
+
5
+ module Tzispa
6
+ module Config
7
+
8
+ class DbConfig
9
+ attr_reader :env, :config
10
+
11
+ CONFIG_FILENAME = 'database'
12
+
13
+ def initialize(env)
14
+ @cftime = nil
15
+ @env = env.to_sym
16
+ end
17
+
18
+ def to_h
19
+ load!
20
+ config.to_h[env]&.to_h
21
+ end
22
+
23
+ def load!
24
+ if @cftime.nil?
25
+ @cftime = File.ctime(filename)
26
+ elsif @cftime != File.ctime(filename)
27
+ @config = nil
28
+ @cftime = File.ctime(filename)
29
+ end
30
+ @config ||= Tzispa::Config::Yaml.load(filename)
31
+ end
32
+
33
+ def filename
34
+ DbConfig.filename
35
+ end
36
+
37
+ class << self
38
+ def filename
39
+ @filename ||= "config/#{CONFIG_FILENAME}.yml"
40
+ end
41
+
42
+ def create_default(path)
43
+ hcfg = {}.tap do |cfg|
44
+ cfg['development'] = {}
45
+ cfg['deployment'] = {}
46
+ cfg['test'] = {}
47
+ end
48
+ Yaml.save(hcfg, File.join(path, filename))
49
+ end
50
+
51
+ def add_repository(name, adapter, dbconn)
52
+ hs = YAML.safe_load(File.open(filename))
53
+ hs.each do |_, v|
54
+ v[name] = {
55
+ 'adapter' => adapter,
56
+ 'database' => dbconn,
57
+ 'connection_validation' => 'No',
58
+ 'local' => 'Yes'
59
+ }
60
+ end
61
+ Yaml.save(hs, filename)
62
+ end
63
+ end
64
+ end
65
+
66
+ end
67
+ end
@@ -1,24 +1,23 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'yaml'
2
4
  require 'tzispa/config/base'
3
5
 
4
6
  module Tzispa
5
7
  module Config
6
- class Yaml < Tzispa::Config::Base
7
-
8
8
 
9
+ class Yaml < Tzispa::Config::Base
9
10
  def self.load(filename)
10
- params = YAML.load(File.open(filename))
11
- self.parametrize params
11
+ params = YAML.safe_load(File.open(filename))
12
+ parametrize params
12
13
  end
13
14
 
14
15
  def self.save(cfg, filename)
15
- File.open(filename, 'w') { |f|
16
- f.write cfg.to_yaml
17
- }
16
+ File.open(filename, 'w') do |f|
17
+ f.write cfg.to_yaml
18
+ end
18
19
  end
19
-
20
-
21
-
22
20
  end
21
+
23
22
  end
24
23
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'forwardable'
2
4
  require 'i18n'
3
5
 
@@ -13,10 +15,9 @@ module Tzispa
13
15
  def initialize(app, environment)
14
16
  @env = environment
15
17
  @app = app
16
- @cache = Hash.new
18
+ @cache = {}
17
19
  I18n.locale = app.config.locales.default.to_sym if app.config&.respond_to?(:locales)
18
20
  end
19
-
20
21
  end
21
22
 
22
23
  end