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
@@ -14,8 +14,21 @@ class Lux::Response::Header
14
14
  @data[key.downcase] = value
15
15
  end
16
16
 
17
+ def merge hash
18
+ for key, value in hash
19
+ @data[key.downcase] = value
20
+ end
21
+
22
+ @data
23
+ end
24
+
25
+ def delete name
26
+ @data.delete name.downcase
27
+ end
28
+
17
29
  def to_h
18
- @data.to_h.sort.to_h
30
+ # data['Set-Cookie'] = data.delete('set-cookie') if data['set-cookie']
31
+ @data#.to_h.sort.to_h
19
32
  end
20
33
 
21
34
  end
@@ -7,8 +7,7 @@ class Lux::Response
7
7
  # if defined, cache becomes public and can be cache by proxies
8
8
  # use with care.only for static resources and
9
9
  attr_reader :max_age
10
-
11
- attr_accessor :body, :headers, :cookies, :content_type, :status, :cookie_multidomain, :cookie_domain
10
+ attr_accessor :headers, :cookies, :content_type, :status
12
11
 
13
12
  def initialize
14
13
  @render_start = Time.monotonic
@@ -29,6 +28,13 @@ class Lux::Response
29
28
  @max_age = age.to_i
30
29
  end
31
30
 
31
+ # http 103
32
+ def early_hints link=nil, type=nil
33
+ @early_hints ||= []
34
+ @early_hints.push [link, type] if type && !@early_hints.include?(link)
35
+ @early_hints
36
+ end
37
+
32
38
  def etag *args
33
39
  unless @headers['etag']
34
40
  args.push current.request.url
@@ -39,12 +45,9 @@ class Lux::Response
39
45
  @headers['etag'] = key
40
46
  end
41
47
 
42
- if current.request.env['HTTP_IF_NONE_MATCH'] == @headers['etag']
43
- @status = 304
44
- @body = 'not-modified'
45
- true
46
- else
47
- false
48
+ if !@body && current.request.env['HTTP_IF_NONE_MATCH'] == @headers['etag']
49
+ status 304
50
+ body 'not-modified'
48
51
  end
49
52
  end
50
53
 
@@ -64,33 +67,30 @@ class Lux::Response
64
67
  throw :done
65
68
  end
66
69
 
67
- def body what=nil
68
- @body ||= what
69
-
70
- if @body && block_given?
71
- @body = yield @body
72
- Lux.error 'Lux.current.response.body is not a string (bad current.response.body filter)' unless @body.is_a?(String)
73
- end
74
-
70
+ def body body_data=nil, status=nil
71
+ @status = status if status
72
+ @body = body_data if body_data
73
+ @body = yield @body if block_given?
75
74
  @body
76
75
  end
77
- alias :body= :body
78
76
 
79
- def body! what
80
- @body = what
77
+ def body?
78
+ !!@body
81
79
  end
82
80
 
83
81
  def content_type type=nil
84
82
  return @content_type unless type
85
83
 
86
- # can be set only once
87
- return @content_type if @content_type
88
-
89
- type = 'application/json' if type == :json
90
- type = 'text/plain' if type == :text
91
- type = 'text/html' if type == :html
92
-
93
- raise 'Invalid page content-type %s' % type if type === Symbol
84
+ if type.is_a?(Symbol)
85
+ case type
86
+ when :javascript then 'application/javascript'
87
+ when :json then 'application/json'
88
+ when :text then 'text/plain'
89
+ when :html then 'text/html'
90
+ else
91
+ raise ArgumentError.new('Bad content type')
92
+ end
93
+ end
94
94
 
95
95
  @content_type = type
96
96
  end
@@ -105,16 +105,40 @@ class Lux::Response
105
105
  message ? @flash.error(message) : @flash
106
106
  end
107
107
 
108
- def redirect where=nil, opts={}
109
- return @headers['location'] unless where
108
+ def send_file file, opts={}
109
+ ::Lux::Response::File.new(file, opts).send
110
+ end
111
+
112
+ # redirect '/foo'
113
+ # redirect :back, info: 'bar ...'
114
+ def redirect where, opts={}
115
+ where = current.request.env['HTTP_REFERER'].or('/') if where == :back
116
+ where = "#{current.request.path}#{where}" if where[0,1] == '?'
117
+ where = current.host + where unless where.include?('://')
118
+
119
+ # local redirect
120
+ if where.include?(current.host)
121
+ redirect_var = Lux.config.redirect_var || :_r
122
+
123
+ url = Url.new where
124
+ url[redirect_var] = current.request.params[redirect_var].to_i + 1
125
+
126
+ where =
127
+ if opts.delete(:silent)
128
+ url.delete redirect_var
129
+ url.to_s
130
+ else
131
+ url[redirect_var] > 3 ? '/' : url.to_s
132
+ end
133
+ end
110
134
 
111
135
  @status = opts.delete(:status) || 302
112
136
  opts.map { |k,v| flash.send(k, v) }
113
137
 
114
- @headers['location'] = where.index('//') ? where : "#{current.host}#{where}"
115
- @headers['access-control-expose-headers'] ||= 'Location'
138
+ @body = %[redirecting to #{where}\n\n#{opts.values.join("\n")}]
116
139
 
117
- @body = %[redirecting to #{@headers['location']}\n\n#{opts.values.join("\n")}]
140
+ @headers['location'] = where
141
+ @headers['access-control-expose-headers'] ||= 'Location'
118
142
 
119
143
  throw :done
120
144
  end
@@ -132,15 +156,14 @@ class Lux::Response
132
156
 
133
157
  status 401
134
158
  header('WWW-Authenticate', 'Basic realm="%s"' % relam.or('default'))
135
- body = ErrorCell.unauthorized('HTTP 401 Authorization needed')
136
-
137
- false
159
+ body 'HTTP 401 Authorization needed'
160
+ throw :done
138
161
  end
139
162
 
140
163
  def write_response_body
141
164
  unless @body
142
- @status = 404
143
- @body = Lux.error 'Document not found'
165
+ @status = 204
166
+ @body = 'Lux HTTP ERROR 204: NO CONTENT'
144
167
  end
145
168
 
146
169
  # respond as JSON if we recive hash
@@ -164,13 +187,6 @@ class Lux::Response
164
187
  end
165
188
 
166
189
  def write_response_header
167
- domain =
168
- if cookie_multidomain && current.domain.index('.')
169
- ".#{current.domain}"
170
- else
171
- cookie_domain || current.request.host
172
- end
173
-
174
190
  # cache-control
175
191
  @headers['cache-control'] ||= Proc.new do
176
192
  cc = ['max-age=%d' % max_age]
@@ -182,18 +198,16 @@ class Lux::Response
182
198
 
183
199
  # dont annd cookies to public pages (images, etc..)
184
200
  unless @headers['cache-control'].index('public')
185
- encrypted = Crypt.encrypt(current.session.to_json)
186
-
187
- if current.cookies[Lux.config.session_cookie_name] != encrypted
188
- @headers['set-cookie'] = "#{Lux.config.session_cookie_name}=#{encrypted}; Expires=#{(Time.now+1.month).utc}; Path=/; Domain=#{domain};"
189
- end
201
+ cookie = current.session.generate_cookie
202
+ @headers['set-cookie'] = cookie if cookie
190
203
  end
191
204
 
192
205
  etag(@body) if current.request.request_method == 'GET'
193
206
 
194
207
  @headers['x-lux-speed'] = "#{((Time.monotonic - @render_start)*1000).round(1)} ms"
195
208
  @headers['content-type'] ||= "#{@content_type}; charset=utf-8"
196
- @headers['content-length'] = @body.bytesize
209
+ @headers['content-length'] = @body.bytesize.to_s
210
+
197
211
  # if "no-store" is present then HTTP_IF_NONE_MATCH is not sent from browser
198
212
  end
199
213
 
@@ -204,12 +218,6 @@ class Lux::Response
204
218
  @status ||= 200
205
219
  Lux.log " #{@status}, #{@headers['x-lux-speed']}"
206
220
 
207
- if ENV['LUX_PRINT_ROUTES']
208
- print '* Finished route print '
209
- puts @status == 404 ? 'without a match'.red : 'with a match'.green
210
- exit
211
- end
212
-
213
221
  [@status, @headers.to_h, [@body]]
214
222
  end
215
223
 
@@ -0,0 +1,102 @@
1
+ require_relative 'view'
2
+
3
+ class Lux::View::Cell
4
+ class Loader
5
+ def initialize parent
6
+ @parent = parent
7
+ end
8
+
9
+ def method_missing m, vars={}
10
+ ViewCell.get(m, @parent, vars)
11
+ end
12
+ end
13
+
14
+ class_callback :before
15
+
16
+ @@cache = {}
17
+
18
+ class << self
19
+ # CityCell.folder -> "./app/cells/city"
20
+ def base_folder
21
+ name = instance_methods(false).first || dir('Can not find method')
22
+ file = instance_method(name).source_location
23
+ File.dirname file.first
24
+ end
25
+
26
+ def get name, parent, vars={}
27
+ w = ('%sCell' % name.to_s.classify).constantize
28
+ w = w.new parent, vars
29
+ w
30
+ end
31
+ end
32
+
33
+ ###
34
+
35
+ define_method(:current) { Lux.current }
36
+ define_method(:request) { Lux.current.request }
37
+ define_method(:params) { Lux.current.request.params }
38
+
39
+ def initialize parent, vars={}
40
+ @_parent = parent
41
+
42
+ Object.class_callback :before, self
43
+
44
+ vars.each { |k,v| instance_variable_set "@#{k}", v}
45
+
46
+ # add runtime file reference
47
+ if m = self.class.instance_methods(false).first
48
+ src = method(m).source_location[0].split(':').first
49
+ src = src.sub(Lux.root.to_s+'/', '')
50
+ Lux.log " #{src}" unless Lux.current.files_in_use.include?(src)
51
+ Lux.current.files_in_use src
52
+ end
53
+ end
54
+
55
+ def parent &block
56
+ if block_given?
57
+ @_parent.instance_exec &block
58
+ else
59
+ @_parent
60
+ end
61
+ end
62
+
63
+ # if block is passed, template render will be passed as an argument
64
+ def template name=:cellm, &block
65
+ tpl = 'cell-tpl-%s-%s' % [self.class, name]
66
+
67
+ tpl = Lux.ram_cache(tpl) do
68
+ file = '%s/%s.haml' % [self.class.base_folder, name]
69
+ file = file.sub(Lux.root.to_s+'/', '')
70
+
71
+ Lux.log ' ' + file unless Lux.current.files_in_use(file)
72
+
73
+ Tilt[:haml].new { File.read(file) }
74
+ end
75
+
76
+ data = tpl.render(self)
77
+ data = block.call(data) if block
78
+ data
79
+ end
80
+
81
+ # tag :div, { 'class'=>'iform' } do
82
+ def tag name=nil, opts={}, data=nil
83
+ return HtmlTagBuilder unless name
84
+
85
+ data = yield(opts) if block_given?
86
+ HtmlTagBuilder.tag name, opts, data
87
+ end
88
+
89
+ # execute block only once per page
90
+ def once id=nil
91
+ id ||= self.class
92
+ Lux.current.once('cell-once-%s' % id) { yield }
93
+ end
94
+
95
+ def cell name=nil
96
+ return parent.cell unless name
97
+
98
+ w = ('%sCell' % name.to_s.classify).constantize
99
+ w = w.new @_parent
100
+ w
101
+ end
102
+ end
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Lux::Helper
3
+ require_relative 'view'
4
+
5
+ class Lux::View::Helper
4
6
 
5
7
  # create helper object that cah be used in template render
6
8
  def initialize instance, *list
@@ -21,24 +23,36 @@ class Lux::Helper
21
23
  # helper.instance_exec &block if block
22
24
  end
23
25
 
26
+ define_method(:current) { Lux.current }
27
+ define_method(:request) { Lux.current.request }
28
+ define_method(:params) { Lux.current.request.params }
29
+ define_method(:nav) { Lux.current.nav }
30
+ define_method(:get) { |name| instance_variable_get('@%s' % name) }
31
+
24
32
  def no_white_space
25
33
  yield.gsub(/>\s+</,'><')
26
34
  end
27
35
 
28
- def current
29
- Lux.current
30
- end
31
-
32
- def request
33
- Lux.current.request
34
- end
36
+ # - @foo = content do ...
37
+ # = @foo
38
+ # - content :foo do ...
39
+ # = content :foo
40
+ def content name=nil
41
+ ivar = '@content_%s' % name
35
42
 
36
- def params
37
- Lux.current.params
43
+ if block_given?
44
+ yield.tap do |data|
45
+ instance_variable_set(ivar, data) if name
46
+ end
47
+ else
48
+ name ? instance_variable_get(ivar) : nil
49
+ end
38
50
  end
39
51
 
40
- def nav
41
- Lux.current.nav
52
+ # foo = function do |list| ...
53
+ # foo.call @list
54
+ def function &block
55
+ block
42
56
  end
43
57
 
44
58
  # renders just template but it is called
@@ -48,7 +62,7 @@ class Lux::Helper
48
62
  if name.is_array?
49
63
  return name.map { |b| render(b) }.join("\n")
50
64
  elsif name.respond_to?(:db_schema)
51
- path = Lux.thread[:last_template_path].split('/')[1]
65
+ path = Thread.current[:lux][:last_template_path].split('/')[1]
52
66
  table_name = name.class.name.tableize
53
67
  locals[table_name.singularize.to_sym] = name
54
68
  eval "@_#{table_name.singularize} = name"
@@ -58,6 +72,8 @@ class Lux::Helper
58
72
  name = "#{Thread.current[:lux][:last_template_path]}/#{name}" unless name.index('/')
59
73
  end
60
74
 
75
+ Lux.current.files_in_use name
76
+
61
77
  for k, v in locals
62
78
  instance_variable_set("@_#{k}", v)
63
79
  end
@@ -65,9 +81,9 @@ class Lux::Helper
65
81
  if block_given?
66
82
  name = "#{name}/layout" unless name.index('/')
67
83
 
68
- Lux::Template.new(name, self).render_part { yield }
84
+ Lux::View.new(name, self).render_part { yield }
69
85
  else
70
- Lux::Template.new(name, self).render_part
86
+ Lux::View.new(name, self).render_part
71
87
  end
72
88
  end
73
89
 
@@ -81,13 +97,6 @@ class Lux::Helper
81
97
  %[<pre style="color:red; background:#eee; padding:10px; font-family:'Lucida Console'; line-height:14pt; font-size:10pt;">#{msg}</pre>]
82
98
  end
83
99
 
84
- # = render 'main/tag_list', klass:Link
85
- # = cell Main::PartsCell, :tag_filter, klass
86
- def cell name, action, *args
87
- klass = name.is_string? ? "#{name}_cell".classify.constantize : name
88
- klass.cell(action, *args) rescue error("#{klass}.#{action}()\n\n#{$!.message}")
89
- end
90
-
91
100
  # tag :div, { 'class'=>'iform' } do
92
101
  def tag name=nil, opts={}, data=nil
93
102
  return HtmlTagBuilder unless name
@@ -98,7 +107,14 @@ class Lux::Helper
98
107
 
99
108
  # helper(:main).method
100
109
  def helper *names
101
- Lux::Helper.new(self, *names)
110
+ Lux::View::Helper.new(self, *names)
111
+ end
112
+
113
+ def once id=nil
114
+ Lux.current.once("template-#{id || caller[0]}") do
115
+ block_given? ? yield : true
116
+ end
102
117
  end
103
118
 
104
119
  end
120
+
@@ -0,0 +1,29 @@
1
+ # make cell available in helpers
2
+
3
+ require_relative 'helper_modules'
4
+
5
+ class ViewCell < Lux::View::Cell
6
+ end
7
+
8
+ HtmlHelper.class_eval do
9
+ def cell name=nil, vars={}
10
+ if name
11
+ ViewCell.get(name, self, vars)
12
+ else
13
+ return @cell_base ||= ViewCell::Loader.new(self)
14
+ end
15
+ end
16
+ end
17
+
18
+ # make cell available in controllers
19
+ Lux::Controller.class_eval do
20
+ def cell name=nil
21
+ name = if name
22
+ name.to_s.classify
23
+ else
24
+ self.class.to_s.split('::').last.sub('Controller')
25
+ end
26
+
27
+ name.constantize.new(self, vars)
28
+ end
29
+ end
@@ -6,6 +6,12 @@ module MailerHelper
6
6
  end
7
7
 
8
8
  def host
9
- ENV.fetch('HOST')
9
+ Lux.config.host
10
10
  end
11
+ end
12
+
13
+ module ApplicationHelper
14
+ end
15
+
16
+ module HtmlHelper
11
17
  end
@@ -1,12 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
  require 'thread'
3
3
 
4
- class Lux::Template
4
+ class Lux::View
5
5
  @@template_cache = {}
6
6
 
7
7
  class << self
8
- def render_with_layout template, helper={}
9
- new(template, helper).render_with_layout
8
+ def render_with_layout layout, template, helper={}
9
+ part_data = new(template, helper).render_part
10
+ new(layout, helper).render_part { part_data }
10
11
  end
11
12
 
12
13
  def render_part template, helper={}
@@ -20,15 +21,16 @@ class Lux::Template
20
21
 
21
22
  @helper = if context.class == Hash
22
23
  # create helper class if only hash given
23
- Lux::Helper.new(context)
24
+ Lux::View::Helper.new(context)
24
25
  else
25
26
  context
26
27
  end
27
28
 
28
29
  mutex = Mutex.new
29
30
 
30
- if Lux.config(:auto_code_reload) && !Lux.thread[:template_cache]
31
- Lux.thread[:template_cache] = true
31
+ # if auto_code_reload is on then clear only once per request
32
+ if Lux.config(:auto_code_reload) && !Thread.current[:lux][:template_cache]
33
+ Thread.current[:lux][:template_cache] = true
32
34
  mutex.synchronize { @@template_cache = {} }
33
35
  end
34
36
 
@@ -46,9 +48,11 @@ class Lux::Template
46
48
  end
47
49
 
48
50
  unless @template
49
- err = caller.reject{ |l| l =~ %r{(/lux/|/gems/)} }.map{ |l| el=l.to_s.split(Lux.root.to_s); el[1] || l }.join("\n")
50
- Lux.error %[Lux::Template "#{template}.{erb,haml}" not found\n\n#{err}]
51
- raise
51
+ err = caller.reject{ |l| l =~ %r{(/lux/|/gems/)} }.map{ |l| el=l.to_s.split(Lux.root.to_s); el[1] || l }.join("\n")
52
+ msg = %[Lux::View "#{template}.{erb,haml}" not found]
53
+ msg += %[\n\n#{err}] if Lux.config(:dump_errors)
54
+
55
+ Lux.error msg
52
56
  end
53
57
 
54
58
  begin
@@ -65,34 +69,27 @@ class Lux::Template
65
69
  def render_part
66
70
  # global thread safe reference pointer to last temaplte rendered
67
71
  # we nned this for inline template render
68
- Lux.thread[:last_template_path] = @template.sub('/app/views','').sub(/\/[^\/]+$/,'').sub(/^\./,'')
69
72
 
73
+ Thread.current[:lux][:last_template_path] = @template.sub('/app/views','').sub(/\/[^\/]+$/,'').sub(/^\./,'')
70
74
  Lux.current.files_in_use @template
71
75
 
72
76
  data = nil
77
+
73
78
  speed = Lux.speed do
74
- data = Lux::Error.try %[Lux::Template "#{@template}" render error] do
75
- @tilt.render(@helper) do
79
+ begin
80
+ data = @tilt.render(@helper) do
76
81
  yield if block_given?
77
82
  end
78
- end rescue Lux::Error.inline
83
+ rescue => e
84
+ data = Lux::Error.inline %[Lux::View #{@template} render error], e
85
+ end
79
86
  end
80
87
 
81
- Lux.log " #{@template.split('app/views/').last}, #{speed}"
88
+ Lux.log " app/views/#{@template.split('app/views/').last}, #{speed}"
82
89
 
83
90
  data
84
91
  end
85
92
 
86
- def render_with_layout
87
- @part_data = render_part
88
-
89
- layout_path = "#{@original_template.split('/')[0]}/layout"
90
-
91
- Lux::Template.new(layout_path, @helper).render_part do
92
- @part_data
93
- end
94
- end
95
-
96
93
  end
97
94
 
98
95
 
data/lib/lux-fw.rb CHANGED
@@ -13,7 +13,8 @@ require_relative './overload/object'
13
13
  Encoding.default_internal = Encoding.default_external = 'utf-8'
14
14
 
15
15
  Sequel.extension :inflector, :string_date_time
16
- Sequel::Model.plugin :after_initialize, :def_dataset_method
16
+ Sequel::Model.plugin :after_initialize
17
+ Sequel::Model.plugin :def_dataset_method
17
18
 
18
19
  Sequel.database_timezone = :utc
19
20
  Sequel.default_timezone = +2
@@ -36,4 +37,5 @@ if Lux.root != Lux.fw_root
36
37
  ['./log', './tmp'].each { |d| `mkdir #{d}` unless Dir.exist?(d) }
37
38
  end
38
39
 
39
- Lux::Config.set_default_vars
40
+ Lux::Config.init!
41
+
@@ -1,5 +1,5 @@
1
1
  class Array
2
-
2
+ # convert list of lists to CSV
3
3
  def to_csv
4
4
  ret = []
5
5
  for row in self
@@ -12,15 +12,18 @@ class Array
12
12
  ret.join("\n")
13
13
  end
14
14
 
15
- def wrap(name, opts={})
15
+ # wrap all list elements with a tag
16
+ def wrap name, opts={}
16
17
  map{ |el| opts.tag(name, opts) }
17
18
  end
18
19
 
19
- def last=(what)
20
+ # set last element of an array
21
+ def last= what
20
22
  self[self.length-1] = what
21
23
  end
22
24
 
23
- def to_sentence(opts={})
25
+ # convert list to sentence, Rails like
26
+ def to_sentence opts={}
24
27
  opts[:words_connector] ||= ', '
25
28
  opts[:two_words_connector] ||= ' and '
26
29
  opts[:last_word_connector] ||= ', and '
@@ -36,10 +39,11 @@ class Array
36
39
  self.join(opts[:words_connector]) + opts[:last_word_connector] + last_word
37
40
  end
38
41
 
39
- # toggle element in array and return true when added
40
- def toggle(element)
42
+ # toggle existance of an element in array and return true when one added
43
+ def toggle element
41
44
  self.uniq!
42
45
  self.compact!
46
+
43
47
  if self.include?(element)
44
48
  self.delete(element)
45
49
  false
@@ -49,6 +53,7 @@ class Array
49
53
  end
50
54
  end
51
55
 
56
+ # for easier Sequel query
52
57
  def all
53
58
  self
54
59
  end
@@ -58,4 +63,5 @@ class Array
58
63
  i = string.split('').map{ |_| _.ord }.sum
59
64
  self[i % length]
60
65
  end
66
+
61
67
  end
@@ -61,7 +61,6 @@ class String
61
61
  def blank?
62
62
  return true if self.length == 0
63
63
 
64
- # test = !!(self =~ /^\s*$/)
65
64
  !(self =~ /[^\s]/)
66
65
  end
67
66
  end
@@ -0,0 +1,18 @@
1
+ class Dir
2
+ # get only folder list form a folder
3
+ def self.folders dir
4
+ dir = dir.to_s
5
+
6
+ Dir
7
+ .entries(dir)
8
+ .reject { |el| ['.', '..'].include?(el) }
9
+ .select { |el| File.directory?([dir, el].join('/')) }
10
+ end
11
+ end
12
+
13
+ class Pathname
14
+ # Lux.fw_root.join('plugins').folders do |folder| ...
15
+ def folders &block
16
+ Dir.folders(to_s, &block)
17
+ end
18
+ end