lux-fw 0.2.3 → 0.5.32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (138) hide show
  1. checksums.yaml +4 -4
  2. data/.version +1 -1
  3. data/bin/README.md +33 -0
  4. data/bin/build_gem +76 -0
  5. data/bin/cli/config.rb +44 -0
  6. data/bin/cli/console.rb +61 -0
  7. data/bin/cli/dbconsole.rb +8 -0
  8. data/bin/cli/eval.rb +32 -0
  9. data/bin/cli/generate.rb +88 -0
  10. data/bin/cli/get.rb +12 -0
  11. data/bin/cli/new.rb +22 -0
  12. data/bin/cli/routes.rb +90 -0
  13. data/bin/cli/secrets.rb +40 -0
  14. data/bin/cli/server.rb +24 -0
  15. data/bin/cli/stats.rb +133 -0
  16. data/bin/lux +24 -65
  17. data/lib/common/class_attributes.rb +40 -64
  18. data/lib/common/class_callbacks.rb +34 -51
  19. data/lib/common/crypt.rb +10 -8
  20. data/lib/common/free_struct.rb +42 -0
  21. data/lib/common/hash_with_indifferent_access.rb +1 -1
  22. data/lib/common/html_tag_builder.rb +26 -2
  23. data/lib/common/url.rb +58 -40
  24. data/lib/lux/application/application.rb +290 -117
  25. data/lib/lux/application/lib/nav.rb +64 -38
  26. data/lib/lux/application/lib/render.rb +2 -1
  27. data/lib/lux/cache/cache.rb +87 -62
  28. data/lib/lux/cache/lib/{ram.rb → memory.rb} +5 -7
  29. data/lib/lux/cache/lib/null.rb +6 -8
  30. data/lib/lux/config/config.rb +109 -52
  31. data/lib/lux/config/lib/plugin.rb +65 -0
  32. data/lib/lux/config/lib/secrets.rb +48 -0
  33. data/lib/lux/controller/controller.rb +241 -0
  34. data/lib/lux/current/current.rb +33 -47
  35. data/lib/lux/current/lib/session.rb +72 -0
  36. data/lib/lux/delayed_job/delayed_job.rb +14 -7
  37. data/lib/lux/delayed_job/lib/memory.rb +7 -5
  38. data/lib/lux/delayed_job/lib/redis.rb +1 -1
  39. data/lib/lux/error/error.rb +164 -62
  40. data/lib/lux/event_bus/event_bus.rb +27 -0
  41. data/lib/lux/lux.rb +103 -66
  42. data/lib/lux/mailer/mailer.rb +23 -25
  43. data/lib/lux/response/lib/file.rb +81 -0
  44. data/lib/lux/response/lib/header.rb +14 -1
  45. data/lib/lux/response/response.rb +64 -56
  46. data/lib/lux/view/cell.rb +102 -0
  47. data/lib/lux/{helper → view}/helper.rb +39 -23
  48. data/lib/lux/view/lib/cell_helpers.rb +29 -0
  49. data/lib/lux/{helper/helpers/mailer_helper.rb → view/lib/helper_modules.rb} +7 -1
  50. data/lib/lux/{template/template.rb → view/view.rb} +21 -24
  51. data/lib/lux-fw.rb +4 -2
  52. data/lib/overload/array.rb +12 -6
  53. data/lib/overload/blank.rb +0 -1
  54. data/lib/overload/dir.rb +18 -0
  55. data/lib/overload/file.rb +1 -6
  56. data/lib/overload/hash.rb +56 -13
  57. data/lib/overload/integer.rb +2 -2
  58. data/lib/overload/it.rb +4 -4
  59. data/lib/overload/object.rb +37 -8
  60. data/lib/overload/{r.rb → raise_variants.rb} +23 -4
  61. data/lib/overload/string.rb +22 -6
  62. data/misc/demo/app/cells/demo_cell.rb +12 -0
  63. data/misc/demo/app/controllers/application_controller.rb +7 -0
  64. data/misc/demo/app/controllers/main/root_controller.rb +9 -0
  65. data/misc/demo/app/routes.rb +5 -0
  66. data/misc/demo/config/application.rb +14 -0
  67. data/misc/demo/config/assets.rb +6 -0
  68. data/misc/demo/config/environment.rb +7 -0
  69. data/misc/puma_auto_tune.rb +43 -0
  70. data/misc/unicorn.rb +37 -0
  71. data/{lib/lux → plugins}/api/api.rb +46 -29
  72. data/plugins/api/lib/attr.rb +31 -0
  73. data/{lib/lux → plugins}/api/lib/dsl.rb +3 -6
  74. data/{lib/lux → plugins}/api/lib/error.rb +0 -0
  75. data/{lib/lux → plugins}/api/lib/model_api.rb +51 -12
  76. data/{lib/lux → plugins}/api/lib/response.rb +31 -17
  77. data/{bin/cli/am → plugins/db/auto_migrate/auto_migrate.rb} +18 -35
  78. data/plugins/db/helpers/array_search.rb +27 -0
  79. data/plugins/db/helpers/before_save_filters.rb +32 -0
  80. data/plugins/db/helpers/composite_primary_keys.rb +36 -0
  81. data/plugins/db/helpers/core.rb +94 -0
  82. data/plugins/db/helpers/dataset_methods.rb +138 -0
  83. data/plugins/db/helpers/enums_plugin.rb +52 -0
  84. data/plugins/db/helpers/find_precache.rb +31 -0
  85. data/plugins/db/helpers/link_objects.rb +84 -0
  86. data/plugins/db/helpers/schema_checks.rb +83 -0
  87. data/plugins/db/helpers/typero_adapter.rb +71 -0
  88. data/plugins/db/logger/config.rb +22 -0
  89. data/plugins/db/logger/lux_response_adapter.rb +10 -0
  90. data/plugins/db/paginate/helper.rb +32 -0
  91. data/plugins/db/paginate/sequel_adapter.rb +23 -0
  92. data/plugins/exceptions/simple_exception.rb +64 -0
  93. data/plugins/favicon/favicon.rb +10 -0
  94. data/plugins/html/html_form.rb +118 -0
  95. data/plugins/html/html_input.rb +98 -0
  96. data/plugins/html/html_menu.rb +79 -0
  97. data/plugins/html/input_types.rb +346 -0
  98. data/plugins/js_widgets/js_widgets.rb +15 -0
  99. data/plugins/oauth/lib/facebook.rb +35 -0
  100. data/plugins/oauth/lib/github.rb +38 -0
  101. data/plugins/oauth/lib/google.rb +41 -0
  102. data/plugins/oauth/lib/linkedin.rb +41 -0
  103. data/plugins/oauth/lib/stackexchange.rb +41 -0
  104. data/plugins/oauth/lib/twitter.rb +38 -0
  105. data/plugins/oauth/oauth.rb +42 -0
  106. data/{lib/common → plugins/policy}/policy.rb +6 -7
  107. data/tasks/loader.rb +49 -0
  108. metadata +151 -49
  109. data/bin/cli/assets +0 -41
  110. data/bin/cli/console +0 -51
  111. data/bin/cli/dev +0 -1
  112. data/bin/cli/eval +0 -24
  113. data/bin/cli/exceptions +0 -62
  114. data/bin/cli/generate +0 -86
  115. data/bin/cli/get +0 -5
  116. data/bin/cli/nginx +0 -34
  117. data/bin/cli/production +0 -1
  118. data/bin/cli/render +0 -18
  119. data/bin/cli/routes +0 -14
  120. data/bin/cli/server +0 -4
  121. data/bin/cli/stat +0 -1
  122. data/bin/cli/systemd +0 -36
  123. data/bin/txt/nginx.conf +0 -46
  124. data/bin/txt/siege-and-puma.txt +0 -3
  125. data/lib/common/base32.rb +0 -47
  126. data/lib/common/dynamic_class.rb +0 -28
  127. data/lib/common/folder_model.rb +0 -50
  128. data/lib/common/generic_model.rb +0 -62
  129. data/lib/lux/application/lib/plugs.rb +0 -10
  130. data/lib/lux/application/lib/route_test.rb +0 -64
  131. data/lib/lux/cache/lib/memcached.rb +0 -3
  132. data/lib/lux/cell/cell.rb +0 -261
  133. data/lib/lux/current/lib/static_file.rb +0 -103
  134. data/lib/lux/helper/helpers/application_helper.rb +0 -3
  135. data/lib/lux/helper/helpers/html_helper.rb +0 -3
  136. data/lib/overload/auto_loader.rb +0 -27
  137. data/lib/overload/module.rb +0 -10
  138. data/lib/overload/string_inflections.rb +0 -8
@@ -1,89 +1,114 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Lux::Cache
4
- extend self
3
+ module Lux
4
+ class Cache
5
+ def initialize
6
+ @server = Lux::Cache::MemoryCache.new
7
+ end
5
8
 
6
- # Lux::Cache.sever = :ram
7
- def server= obj
8
- obj = "Lux::Cache::#{obj.to_s.classify}Cache".constantize if obj.class == Symbol
9
- @@server = obj
10
- end
9
+ # sert cache server
10
+ # Lux.cache.server = :memory
11
+ # Lux.cache.server = :memcached
12
+ # Lux.cache.server = Dalli::Client.new('localhost:11211', { :namespace=>Digest::MD5.hexdigest(__FILE__)[0,4], :compress => true, :expires_in => 1.hour })
13
+ def server= name
14
+ @server = if name.is_a?(Symbol)
15
+ if name == :memcached
16
+ require 'dalli'
17
+ Dalli::Client.new('127.0.0.1:11211', { :namespace=>Digest::MD5.hexdigest(__FILE__)[0,4], :compress => true, :expires_in => 1.hour })
18
+ else
19
+ "Lux::Cache::#{name.to_s.classify}Cache".constantize.new
20
+ end
21
+ else
22
+ name
23
+ end
11
24
 
12
- def read key
13
- return nil if (Lux.current.no_cache? rescue false)
14
- @@server.get(key)
15
- end
16
- alias :get :read
25
+ fetch('cache-test') { true }
26
+ end
17
27
 
18
- def read_multi *args
19
- @@server.get_multi(*args)
20
- end
21
- alias :get_multi :read_multi
28
+ def server
29
+ @server
30
+ end
22
31
 
23
- def write key, data, ttl=nil
24
- ttl = ttl.to_i if ttl
25
- @@server.set(key, data, ttl)
26
- end
27
- alias :set :write
32
+ def read key
33
+ return nil if (Lux.current.no_cache? rescue false)
34
+ @server.get(key)
35
+ end
36
+ alias :get :read
28
37
 
29
- def delete key, data=nil
30
- @@server.delete(key)
31
- end
38
+ def read_multi *args
39
+ @server.get_multi(*args)
40
+ end
41
+ alias :get_multi :read_multi
42
+
43
+ def write key, data, ttl=nil
44
+ ttl = ttl.to_i if ttl
45
+ @server.set(key, data, ttl)
46
+ end
47
+ alias :set :write
48
+
49
+ def delete key, data=nil
50
+ @server.delete(key)
51
+ end
32
52
 
33
- def fetch key, opts={}
34
- opts = { ttl: opts } unless opts.is_a?(Hash)
35
- opts = opts.to_opts!(:ttl, :force, :log)
53
+ def fetch key, opts={}
54
+ opts = { ttl: opts } unless opts.is_a?(Hash)
55
+ opts = opts.to_opts(:ttl, :force, :log, :if)
36
56
 
37
- opts.ttl = opts.ttl.to_i if opts.ttl
38
- opts.log ||= Lux.config(:log_to_stdout) unless opts.log.class == FalseClass
39
- opts.force ||= Lux.current.try(:no_cache?) unless opts.force.class == FalseClass
57
+ return yield if opts.if.is_a?(FalseClass)
40
58
 
41
- @@server.delete key if opts.force
59
+ opts.ttl = opts.ttl.to_i if opts.ttl
60
+ opts.log ||= Lux.config(:log_to_stdout) unless opts.log.class == FalseClass
61
+ opts.force ||= Lux.current.try(:no_cache?) unless opts.force.class == FalseClass
42
62
 
43
- Lux.log " Cache.fetch.get #{key} (ttl: #{opts.ttl.or(:nil)})".green if opts.log
63
+ @server.delete key if opts.force
44
64
 
45
- data = @@server.fetch key, opts.ttl do
46
- speed = Lux.speed { data = yield }
65
+ Lux.log " Cache.fetch.get #{key} (ttl: #{opts.ttl.or(:nil)})".green if opts.log
47
66
 
48
- Lux.log " Cache.fetch.SET #{key} len:#{data.to_s.length} (#{speed})".red if opts.log
67
+ data = @server.fetch key, opts.ttl do
68
+ speed = Lux.speed { data = yield }
69
+
70
+ Lux.log " Cache.fetch.SET #{key} len:#{data.to_s.length} (#{speed})".red if opts.log
71
+
72
+ data
73
+ end
49
74
 
50
75
  data
51
76
  end
52
77
 
53
- data
54
- end
55
-
56
- def is_available?
57
- set('lux-test', 9)
58
- get('lux-test') == 9
59
- end
78
+ def is_available?
79
+ set('lux-test', 9)
80
+ get('lux-test') == 9
81
+ end
60
82
 
61
- def generate_key *data
62
- keys = []
83
+ def generate_key *data
84
+ keys = []
63
85
 
64
- for el in [data].flatten
65
- keys.push el.id if el.respond_to?(:id)
86
+ for el in [data].flatten
87
+ keys.push el.id if el.respond_to?(:id)
66
88
 
67
- if el.respond_to?(:updated_at)
68
- keys.push el.updated_at
69
- elsif el.respond_to?(:created_at)
70
- keys.push el.created_at
71
- else
72
- keys.push el.to_s
89
+ if el.respond_to?(:updated_at)
90
+ keys.push el.updated_at
91
+ elsif el.respond_to?(:created_at)
92
+ keys.push el.created_at
93
+ else
94
+ keys.push el.to_s
95
+ end
73
96
  end
97
+
98
+ key = keys.join('-')
99
+ Crypt.sha1(key)
74
100
  end
75
101
 
76
- key = keys.join('-')
77
- Crypt.sha1(key)
78
- end
102
+ def []= key, value
103
+ @server.set key.to_s, value
104
+ value
105
+ end
79
106
 
80
- def []= key, value
81
- @@server.set key.to_s, value
82
- value
83
- end
107
+ def [] key
108
+ @server.get key.to_s
109
+ end
84
110
 
85
- def [] key
86
- @@server.get key.to_s
87
111
  end
88
-
89
112
  end
113
+
114
+ require_relative 'lib/memory'
@@ -1,18 +1,16 @@
1
- module Lux::Cache::RamCache
2
- extend self
3
-
1
+ class Lux::Cache::MemoryCache
4
2
  @@lock = Mutex.new
5
3
  @@ram_cache = {}
6
4
  @@ttl_cache = {}
7
5
 
8
- def set(key, data, ttl=nil)
6
+ def set key, data, ttl=nil
9
7
  @@lock.synchronize do
10
8
  @@ttl_cache[key] = Time.now.to_i + ttl if ttl
11
9
  @@ram_cache[key] = data
12
10
  end
13
11
  end
14
12
 
15
- def get(key)
13
+ def get key
16
14
  if ttl_check = @@ttl_cache[key]
17
15
  return nil if ttl_check < Time.now.to_i
18
16
  end
@@ -20,13 +18,13 @@ module Lux::Cache::RamCache
20
18
  @@ram_cache[key]
21
19
  end
22
20
 
23
- def fetch(key, ttl=nil)
21
+ def fetch key, ttl=nil
24
22
  data = get key
25
23
  return data if data
26
24
  set(key, yield, ttl)
27
25
  end
28
26
 
29
- def delete(key)
27
+ def delete key
30
28
  @@lock.synchronize do
31
29
  @@ram_cache.delete(key)
32
30
  end
@@ -1,23 +1,21 @@
1
- module Lux::Cache::NullCache
2
- extend self
3
-
4
- def set(key, data, ttl=nil)
1
+ class Lux::Cache::NullCache
2
+ def set key, data, ttl=nil
5
3
  data
6
4
  end
7
5
 
8
- def get(key)
6
+ def get key
9
7
  nil
10
8
  end
11
9
 
12
- def fetch(key, ttl=nil)
10
+ def fetch key, ttl=nil
13
11
  yield
14
12
  end
15
13
 
16
- def delete(key)
14
+ def delete key
17
15
  nil
18
16
  end
19
17
 
20
- def get_multi(*args)
18
+ def get_multi *args
21
19
  {}
22
20
  end
23
21
  end
@@ -2,23 +2,18 @@
2
2
 
3
3
  # $LOADED_FEATURES.select{ |f| f.index('/app/') || f.index('/lux/') }
4
4
 
5
+ require 'yaml'
6
+
5
7
  module Lux::Config
6
8
  extend self
7
9
 
8
- LIVE_RELOAD ||= {}
9
-
10
- # if we have errors in module loading, try to load them one more time
11
- @module_error = []
12
-
13
10
  # requires all files recrusive in, with spart sort
14
- def require_all files
15
- files = files.to_s
16
- files += '/*' unless files.include?('*')
17
-
18
- file_errors = []
19
- glob = `echo #{files} #{files}/* #{files}/*/* #{files}/*/*/* #{files}/*/*/*/* |tr ' ' '\n' | grep .rb`.split("\n")
11
+ def require_all dir_path
12
+ dir_path = dir_path.to_s.sub(/\/$/,'')
13
+ raise '* is not allowed' if dir_path.include?('*')
20
14
 
21
- glob.select{ |o| o.index('.rb') }.map{ |o| o.split('.rb')[0]}.each do |ruby_file|
15
+ glob = `echo #{dir_path}/* #{dir_path}/*/* #{dir_path}/*/*/* #{dir_path}/*/*/*/* #{dir_path}/*/*/*/*/* #{dir_path}/*/*/*/*/*/* |tr ' ' '\n' | grep .rb`.split("\n")
16
+ glob.select{ |o| o.index('.rb') }.each do |ruby_file|
22
17
  require ruby_file
23
18
  end
24
19
  end
@@ -31,42 +26,39 @@ module Lux::Config
31
26
  end
32
27
  end
33
28
 
34
- # check all files and reload if needed
35
29
  def live_require_check!
36
- if LIVE_RELOAD.blank?
37
- root = Lux.root.to_s
38
- for file in $LOADED_FEATURES.select{ |f| f.index(root) || f.index('/lux/') }
39
- LIVE_RELOAD[file] = File.mtime(file).to_i
40
- end
41
- end
30
+ $live_require_check ||= Time.now
42
31
 
43
- for file, mtime in LIVE_RELOAD
44
- new_file_mtime = File.mtime(file).to_i
32
+ changed_files = $LOADED_FEATURES
33
+ .select{ |f| f.include?('/app/') || f.include?('lux') }
34
+ .select {|f| File.mtime(f) > $live_require_check }
45
35
 
46
- next if mtime == new_file_mtime
47
- LIVE_RELOAD[file] = new_file_mtime
48
- Lux.log " Reloaded: .#{file.split(Lux.root.to_s).last.red}"
36
+ for file in changed_files
37
+ Lux.log ' Reloaded: %s' % file.split(Lux.root.to_s).last.red
49
38
  load file
50
39
  end
51
- true
40
+
41
+ $live_require_check = Time.now
52
42
  end
53
43
 
54
44
  def ram
55
45
  `ps -o rss -p #{$$}`.chomp.split("\n").last.to_i / 1000
56
46
  end
57
47
 
58
- def show_load_speed load_start=nil
59
- return @@load_info || 'No lux load info' unless load_start
48
+ def start!
49
+ Object.class_callback :config, Lux::Application
50
+ start_info $lux_start_time
51
+ end
60
52
 
61
- speed = ((Time.now - load_start)*1000).round.to_s.sub(/(\d)(\d{3})$/,'\1s \2')+'ms'
53
+ def start_info start=nil
54
+ return @load_info if @load_info
62
55
 
63
56
  production_mode = true
64
-
65
57
  production_opts = [
66
- [:compile_assets, false],
67
- [:auto_code_reload, false],
68
- [:show_server_errors, false],
69
- [:log_to_stdout, false],
58
+ [:compile_assets, false],
59
+ [:auto_code_reload, false],
60
+ [:dump_errors, false],
61
+ [:log_to_stdout, false],
70
62
  ]
71
63
 
72
64
  opts = production_opts.map do |key, production_value|
@@ -78,36 +70,101 @@ module Lux::Config
78
70
  config_ok ? data : data.yellow
79
71
  end
80
72
 
81
- puts @@load_info = '* Config: %s' % opts.join(', ')
82
-
83
- mode = production_mode ? 'production'.green : 'development'.yellow
84
-
85
- "* #{'Lux'.white} loaded #{mode} mode in #{speed.to_s.white}, uses #{ram.to_s.white} MB RAM with total of #{Gem.loaded_specs.keys.length.to_s.white} gems in spec".tap do |it|
86
- @@load_info += "\n#{it}"
73
+ mode = production_mode ? 'production'.green : 'development'.yellow
74
+ speed =
75
+ if start
76
+ text = ((Time.now - start)*1000).round.to_s.sub(/(\d)(\d{3})$/,'\1s \2')
77
+ ' in %s ms' % text.to_s.white
78
+ else
87
79
  end
88
- end
89
80
 
90
- def set_default_vars
91
- # how long will session last if BROWSER or IP change
92
- Lux.config.session_forced_validity = 5.minutes.to_i
81
+ info = []
82
+ info.push '* Config: %s' % opts.join(', ')
83
+ info.push "* Lux loaded #{mode} mode#{speed}, uses #{ram.to_s.white} MB RAM with total of #{Gem.loaded_specs.keys.length.to_s.white} gems in spec"
84
+
85
+ @@load_info = info.join($/)
93
86
 
94
- # name of the session cookie
95
- Lux.config.session_cookie_name = '__luxs'
87
+ puts @@load_info if start
88
+ end
96
89
 
90
+ def init!
97
91
  # Show server errors to a client
98
- Lux.config.show_server_errors = false
92
+ Lux.config.dump_errors = Lux.dev?
99
93
 
100
94
  # Log debug output to stdout
101
- Lux.config.log_to_stdout = false
95
+ Lux.config.log_to_stdout = Lux.dev?
102
96
 
103
97
  # Automatic code reloads in development
104
- Lux.config.auto_code_reload = false
105
- end
98
+ Lux.config.auto_code_reload = Lux.dev?
99
+
100
+ # Runtime compile js and css assets
101
+ Lux.config.compile_assets = Lux.dev?
102
+
103
+ Lux.config.session_cookie_domain = false
104
+ Lux.config.asset_root = false
106
105
 
106
+ ###
107
+
108
+ if ENV['LUX_MODE'].to_s.downcase == 'log'
109
+ Lux.config.dump_errors = false
110
+ Lux.config.auto_code_reload = false
111
+ Lux.config.compile_assets = false
112
+ end
113
+
114
+ ###
115
+
116
+ # Default error logging
117
+ Lux.config.error_logger = proc do |error|
118
+ ap [error.class, error.message, Lux.error.split_backtrace(error)]
119
+
120
+ 'no-key'
121
+ end
122
+
123
+ # Default mail logging
124
+ Lux.config.on_mail = proc do |mail|
125
+ Lux.logger(:email).info "[#{self.class}.#{@_template} to #{mail.to}] #{mail.subject}"
126
+ end
127
+
128
+ # default event bus error handle
129
+ Lux.config.on_event_bus_error = proc do |error, name|
130
+ Lux.logger(:event_bus).error '[%s] %s' % [name, error.message]
131
+ end
132
+
133
+ # server static files
134
+ Lux.config.serve_static_files = true
135
+
136
+ # Template to show when displaying unhandled server side errors
137
+ Lux.config.server_error_template = proc do |text|
138
+ text = text.to_s.gsub('<', '&lt;')
139
+ text = text.to_s.gsub($/,'<br />')
140
+
141
+ %[<html>
142
+ <head>
143
+ <title>Server error (#{Lux.current.response.status})</title>
144
+ </head>
145
+ <body style="background:#fff; font-size:12pt; font-family: Arial; padding: 20px;">
146
+ <h3>HTTP error #{Lux.current.response.status} in #{Lux.config.app.name}</h3>
147
+ <pre style="color:red; padding:10px; background-color: #eee; border: 1px solid #ccc; font-family:'Lucida console'; line-height: 15pt;">#{text}</pre>
148
+ <br>
149
+ <a href="https://httpstatuses.com/#{Lux.current.response.status}" target="http_error">more info on http error</a>
150
+ </body>
151
+ </html>]
152
+ end
153
+
154
+ # inflector
155
+ String.inflections do |inflect|
156
+ inflect.plural 'bonus', 'bonuses'
157
+ inflect.plural 'clothing', 'clothes'
158
+ inflect.plural 'people', 'people'
159
+ inflect.singular /news$/, 'news'
160
+ end
161
+ end
107
162
  end
108
163
 
109
- class Object
110
- def reload!
111
- Lux::Config.live_require_check!
164
+ if Lux.cli?
165
+ class Object
166
+ def reload!
167
+ Lux::Config.live_require_check!
168
+ end
112
169
  end
113
170
  end
@@ -0,0 +1,65 @@
1
+ module Lux::Config::Plugin
2
+ extend self
3
+
4
+ @plugins = {}
5
+
6
+ # load specific plugin
7
+ # Lux.plugin :foo
8
+ # Lux.plugin 'foo/bar'
9
+ # Lux.plugin.folders
10
+ def load arg
11
+ arg = arg.to_s if arg.is_a?(Symbol)
12
+
13
+ if arg.is_a?(String)
14
+ arg = arg.include?('/') ? { folder: arg } : { name: arg }
15
+ end
16
+
17
+ opts = arg.to_opts name: String, folder: String, namespace: Symbol
18
+ opts.name ||= opts.folder.split('/').last
19
+ opts.name = opts.name.to_s
20
+ opts.folder ||= Lux.fw_root.join('plugins', opts.name).to_s
21
+ opts.namespace = [opts.namespace] unless opts.namespace.is_a?(Array)
22
+
23
+ return @plugins[opts.name] if @plugins[opts.name]
24
+
25
+ die(%{Plugin "#{opts.name}" not found in "#{opts.folder}"}) unless Dir.exist?(opts.folder)
26
+
27
+ @plugins[opts.name] ||= opts
28
+
29
+ base = Pathname.new(opts.folder).join(opts.name, '.rb')
30
+
31
+ if base.exist?
32
+ require base.to_s
33
+ else
34
+ Lux::Config.require_all(opts.folder)
35
+ end
36
+
37
+ @plugins[opts.name]
38
+ end
39
+
40
+ def get name
41
+ @plugins[name.to_s] || die('Plugin "%s" not loaded' % name)
42
+ end
43
+
44
+ def loaded
45
+ @plugins.values
46
+ end
47
+
48
+ def keys
49
+ @plugins.keys
50
+ end
51
+
52
+ def plugins
53
+ @plugins
54
+ end
55
+
56
+ # get all folders in a namespace
57
+ def folders namespace=:main
58
+ name = name.to_sym
59
+
60
+ list = @plugins.values
61
+ list.select { |it| it.namespace.include?(namespace) }
62
+ list.map { |it| it.folder }
63
+ end
64
+
65
+ end
@@ -0,0 +1,48 @@
1
+ # if you use encrypted secrets
2
+ # * edit ./tmp/secrets.yaml
3
+ # * increase version by 1
4
+ # * run "rake secrets" to encrypt secrets to ./config/secrets.enc
5
+
6
+ # if you use unprotected secrets in ./config/secrets.yaml
7
+ # * edit the file, no other actions needed
8
+
9
+ require 'yaml'
10
+
11
+ class Lux::Config::Secrets
12
+ attr_reader :read_file, :secret_file, :secret, :strength
13
+
14
+ def initialize
15
+ @read_file = Pathname.new './tmp/secrets.yaml'
16
+ @secret_file = Pathname.new './config/secrets.enc'
17
+ @common_file = Pathname.new './config/secrets.yaml'
18
+ @secret = Lux.config.secret_key_base || Lux.config.secret || ENV['SECRET'] || die('ENV SECRET not found')
19
+ @strength = 'HS512'
20
+ end
21
+
22
+ def write
23
+ encoded = JWT.encode @read_file.read, @secret, @strength
24
+ @secret_file.write encoded
25
+ end
26
+
27
+ def encoded_data
28
+ JWT.decode(@secret_file.read, @secret, true, { algorithm: @strength }).first
29
+ end
30
+
31
+ def to_h
32
+ it = if @common_file.exist?
33
+ @common_file.read
34
+ else
35
+ encoded_data
36
+ end
37
+
38
+ it = YAML.load it
39
+ data = it['shared'] || {}
40
+
41
+ data.merge(it[Lux.env] || {})
42
+ end
43
+
44
+ def load
45
+ to_h.to_readonly
46
+ end
47
+
48
+ end