lux-fw 0.5.36 → 0.6.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (181) hide show
  1. checksums.yaml +4 -4
  2. data/.version +1 -1
  3. data/bin/README.md +26 -13
  4. data/bin/cli/benchmark.rb +18 -0
  5. data/bin/cli/cerb.rb +90 -0
  6. data/bin/cli/config.rb +4 -11
  7. data/bin/cli/console.rb +72 -30
  8. data/bin/cli/eval.rb +5 -27
  9. data/bin/cli/generate.rb +9 -11
  10. data/bin/cli/get.rb +14 -6
  11. data/bin/cli/memory.rb +12 -0
  12. data/bin/cli/new.rb +2 -0
  13. data/bin/cli/secrets.rb +3 -31
  14. data/bin/cli/server.rb +28 -9
  15. data/bin/cli/stats.rb +26 -20
  16. data/bin/cli/template.rb +10 -0
  17. data/bin/generate_doc +168 -0
  18. data/bin/lux +73 -10
  19. data/lib/README.md +18 -8
  20. data/lib/common/crypt.rb +60 -9
  21. data/lib/common/string_base.rb +54 -15
  22. data/lib/common/struct_opts.rb +30 -0
  23. data/lib/common/time_difference.rb +75 -0
  24. data/lib/loader.rb +41 -0
  25. data/lib/lux/application/README.md +67 -105
  26. data/lib/lux/application/application.rb +87 -329
  27. data/lib/lux/application/lib/magic_routes.rb +22 -0
  28. data/lib/lux/application/lib/nav.rb +218 -99
  29. data/lib/lux/application/lib/render.rb +47 -67
  30. data/lib/lux/application/lib/routes.rb +242 -0
  31. data/lib/lux/application/lib/shared.rb +47 -0
  32. data/lib/lux/application/lux_adapter.rb +6 -0
  33. data/lib/lux/application/lux_config.rb +26 -0
  34. data/lib/lux/cache/Find Results +24 -0
  35. data/lib/lux/cache/README.md +28 -35
  36. data/lib/lux/cache/cache.rb +78 -26
  37. data/lib/lux/cache/lib/memcached_server.rb +36 -0
  38. data/lib/lux/cache/lib/memory_server.rb +44 -0
  39. data/lib/lux/cache/lib/null_server.rb +33 -0
  40. data/lib/lux/cache/lib/sqlite_server.rb +62 -0
  41. data/lib/lux/cache/lux_adapter.rb +19 -0
  42. data/lib/lux/config/config.rb +93 -144
  43. data/lib/lux/config/lux_adapter.rb +27 -0
  44. data/lib/lux/controller/README.md +67 -12
  45. data/lib/lux/controller/controller.rb +237 -187
  46. data/lib/lux/current/README.md +29 -10
  47. data/lib/lux/current/current.rb +166 -77
  48. data/lib/lux/current/lib/current.rb +6 -0
  49. data/lib/lux/current/lib/encrypt_params.rb +38 -36
  50. data/lib/lux/current/lib/session.rb +86 -65
  51. data/lib/lux/current/lux_adapter.rb +5 -0
  52. data/lib/lux/environment/README.md +24 -0
  53. data/lib/lux/environment/environment.rb +91 -0
  54. data/lib/lux/environment/lux_adapter.rb +13 -0
  55. data/lib/lux/error/README.md +18 -19
  56. data/lib/lux/error/error.rb +207 -152
  57. data/lib/lux/error/lux_adapter.rb +9 -0
  58. data/lib/lux/logger/README.md +45 -0
  59. data/lib/lux/logger/lux_adapter.rb +27 -0
  60. data/lib/lux/lux.rb +72 -129
  61. data/lib/lux/mailer/README.md +12 -5
  62. data/lib/lux/mailer/lux_config.rb +4 -0
  63. data/lib/lux/mailer/mailer.rb +81 -59
  64. data/lib/lux/plugin/README.md +19 -0
  65. data/lib/lux/plugin/lux_adapter.rb +8 -0
  66. data/lib/lux/plugin/plugin.rb +65 -0
  67. data/lib/lux/render/README.md +100 -0
  68. data/lib/lux/render/render.rb +12 -0
  69. data/lib/lux/response/README.md +57 -1
  70. data/lib/lux/response/lib/file.rb +87 -65
  71. data/lib/lux/response/lib/flash.rb +49 -34
  72. data/lib/lux/response/lib/header.rb +34 -33
  73. data/lib/lux/response/response.rb +252 -164
  74. data/lib/lux/template/helper.rb +130 -0
  75. data/lib/lux/template/inline_render_proxy.rb +31 -0
  76. data/lib/lux/template/lux_adapter.rb +5 -0
  77. data/lib/lux/template/template.rb +127 -0
  78. data/lib/lux-fw.rb +15 -33
  79. data/lib/overload/array.rb +33 -7
  80. data/lib/overload/blank.rb +8 -3
  81. data/lib/overload/boolean.rb +46 -0
  82. data/lib/overload/class.rb +28 -0
  83. data/lib/overload/dir.rb +117 -7
  84. data/lib/overload/file.rb +40 -5
  85. data/lib/overload/float.rb +38 -3
  86. data/lib/overload/hash.rb +81 -91
  87. data/lib/overload/integer.rb +15 -0
  88. data/lib/overload/json.rb +26 -0
  89. data/lib/overload/nil.rb +4 -8
  90. data/lib/overload/object.rb +91 -24
  91. data/lib/overload/pathname.rb +11 -0
  92. data/lib/overload/raise_variants.rb +26 -36
  93. data/lib/overload/string.rb +100 -27
  94. data/lib/overload/thread_simple.rb +100 -0
  95. data/lib/overload/time.rb +60 -38
  96. data/tasks/loader.rb +20 -7
  97. metadata +170 -169
  98. data/bin/.DS_Store +0 -0
  99. data/bin/build_gem +0 -77
  100. data/bin/cli/dbconsole.rb +0 -8
  101. data/bin/cli/routes.rb +0 -90
  102. data/lib/.DS_Store +0 -0
  103. data/lib/common/.DS_Store +0 -0
  104. data/lib/common/class_attributes.rb +0 -51
  105. data/lib/common/class_callbacks.rb +0 -47
  106. data/lib/common/free_struct.rb +0 -42
  107. data/lib/common/hash_with_indifferent_access.rb +0 -74
  108. data/lib/common/html_tag_builder.rb +0 -91
  109. data/lib/common/method_attr.rb +0 -69
  110. data/lib/common/url.rb +0 -191
  111. data/lib/lux/.DS_Store +0 -0
  112. data/lib/lux/README.md +0 -10
  113. data/lib/lux/cache/.DS_Store +0 -0
  114. data/lib/lux/cache/lib/memory.rb +0 -36
  115. data/lib/lux/cache/lib/null.rb +0 -21
  116. data/lib/lux/config/README.md +0 -63
  117. data/lib/lux/config/lib/plugin.rb +0 -65
  118. data/lib/lux/config/lib/secrets.rb +0 -48
  119. data/lib/lux/current/.DS_Store +0 -0
  120. data/lib/lux/delayed_job/.DS_Store +0 -0
  121. data/lib/lux/delayed_job/README.md +0 -3
  122. data/lib/lux/delayed_job/delayed_job.rb +0 -51
  123. data/lib/lux/delayed_job/lib/memory.rb +0 -16
  124. data/lib/lux/delayed_job/lib/nsq.rb +0 -3
  125. data/lib/lux/delayed_job/lib/postgre.rb +0 -6
  126. data/lib/lux/delayed_job/lib/redis.rb +0 -19
  127. data/lib/lux/event_bus/README.md +0 -36
  128. data/lib/lux/event_bus/event_bus.rb +0 -27
  129. data/lib/lux/view/README.md +0 -85
  130. data/lib/lux/view/cell.rb +0 -102
  131. data/lib/lux/view/helper.rb +0 -120
  132. data/lib/lux/view/lib/cell_helpers.rb +0 -29
  133. data/lib/lux/view/view.rb +0 -95
  134. data/lib/overload/it.rb +0 -29
  135. data/plugins/api/README.md +0 -49
  136. data/plugins/api/api.rb +0 -153
  137. data/plugins/api/lib/attr.rb +0 -31
  138. data/plugins/api/lib/dsl.rb +0 -52
  139. data/plugins/api/lib/error.rb +0 -3
  140. data/plugins/api/lib/model_api.rb +0 -203
  141. data/plugins/api/lib/response.rb +0 -85
  142. data/plugins/db/.DS_Store +0 -0
  143. data/plugins/db/README.md +0 -29
  144. data/plugins/db/auto_migrate/auto_migrate.rb +0 -268
  145. data/plugins/db/auto_migrate/db.rake +0 -15
  146. data/plugins/db/helpers/array_search.rb +0 -27
  147. data/plugins/db/helpers/before_save_filters.rb +0 -32
  148. data/plugins/db/helpers/composite_primary_keys.rb +0 -36
  149. data/plugins/db/helpers/core.rb +0 -94
  150. data/plugins/db/helpers/dataset_methods.rb +0 -138
  151. data/plugins/db/helpers/enums_plugin.rb +0 -52
  152. data/plugins/db/helpers/find_precache.rb +0 -31
  153. data/plugins/db/helpers/link_objects.rb +0 -84
  154. data/plugins/db/helpers/schema_checks.rb +0 -83
  155. data/plugins/db/helpers/typero_adapter.rb +0 -71
  156. data/plugins/db/logger/config.rb +0 -22
  157. data/plugins/db/logger/lux_response_adapter.rb +0 -10
  158. data/plugins/db/paginate/helper.rb +0 -32
  159. data/plugins/db/paginate/sequel_adapter.rb +0 -23
  160. data/plugins/exceptions/exceptions.rake +0 -43
  161. data/plugins/exceptions/simple_exception.rb +0 -64
  162. data/plugins/favicon/favicon.rb +0 -10
  163. data/plugins/html/README.md +0 -3
  164. data/plugins/html/html_form.rb +0 -118
  165. data/plugins/html/html_input.rb +0 -98
  166. data/plugins/html/html_menu.rb +0 -79
  167. data/plugins/html/input_types.rb +0 -346
  168. data/plugins/js_widgets/README.md +0 -5
  169. data/plugins/js_widgets/js/html_tag.coffee +0 -42
  170. data/plugins/js_widgets/js/widgets.coffee +0 -161
  171. data/plugins/js_widgets/js_widgets.rb +0 -15
  172. data/plugins/oauth/lib/facebook.rb +0 -35
  173. data/plugins/oauth/lib/github.rb +0 -38
  174. data/plugins/oauth/lib/google.rb +0 -41
  175. data/plugins/oauth/lib/linkedin.rb +0 -41
  176. data/plugins/oauth/lib/stackexchange.rb +0 -41
  177. data/plugins/oauth/lib/twitter.rb +0 -38
  178. data/plugins/oauth/oauth.rb +0 -42
  179. data/plugins/policy/policy.rb +0 -53
  180. data/tasks/nginx.rake +0 -23
  181. /data/lib/lux/{view/lib → mailer}/helper_modules.rb +0 -0
@@ -0,0 +1,75 @@
1
+ class TimeDifference
2
+ TIMES ||= [
3
+ [:year, 60 * 60 * 24 * 365],
4
+ [:month, 60 * 60 * 24 * 30],
5
+ [:day, 60 * 60 * 24],
6
+ [:hour, 60 * 60],
7
+ [:minute, 60]
8
+ ]
9
+
10
+ LOCALE ||= {
11
+ today: {
12
+ en: 'today',
13
+ hr: 'danas'
14
+ },
15
+ in: {
16
+ en: 'in',
17
+ hr: 'za'
18
+ },
19
+ before: {
20
+ en: 'before',
21
+ hr: 'prije'
22
+ },
23
+ in_few_econds: {
24
+ en: 'in few seconds',
25
+ hr: 'za par sekundi'
26
+ },
27
+ just_happend: {
28
+ en: 'just happend',
29
+ hr: 'upravo sada'
30
+ }
31
+ }
32
+
33
+ def initialize start_date, end_date = nil, klass = nil
34
+ unless end_date
35
+ @end_date = start_date
36
+ @start_date = Time.now
37
+ end
38
+
39
+ @klass = klass
40
+ end
41
+
42
+ def humanize
43
+ diff = (@start_date.to_i - @end_date.to_i).abs
44
+
45
+ if @klass == Date && diff < TIMES[2][1]
46
+ return locale(:today)
47
+ # @start_date < @end_date ? locale(:in_few_econds) : locale(:just_happend)
48
+ end
49
+
50
+ TIMES.each do |(key, ref)|
51
+ value = diff / ref
52
+ return part(key, value) if value > 0
53
+ end
54
+
55
+ @start_date < @end_date ? locale(:in_few_econds) : locale(:just_happend)
56
+ end
57
+
58
+ # def set_locale key, value
59
+ # raise ArgumentError.new('Key not found') unless locale(key)
60
+
61
+ # LOCALE[key] = value
62
+ # end
63
+
64
+ private
65
+
66
+ def part key, value
67
+ kind = @start_date < @end_date ? locale(:in) : locale(:before)
68
+ text = [value, value > 1 ? '%ss' % key : key].join(' ')
69
+ [kind, text].join(' ')
70
+ end
71
+
72
+ def locale name
73
+ LOCALE[name][:en]
74
+ end
75
+ end
data/lib/loader.rb ADDED
@@ -0,0 +1,41 @@
1
+ Encoding.default_internal = Encoding.default_external = 'utf-8'
2
+
3
+ Sequel.extension :inflector
4
+ Sequel::Model.plugin :after_initialize
5
+ Sequel::Model.plugin :def_dataset_method
6
+
7
+ Sequel.database_timezone = :utc
8
+ # Sequel.default_timezone = +2
9
+
10
+ # load basic lux libs
11
+ require_relative './overload/dir'
12
+ require_relative './lux/lux'
13
+
14
+ Lux::Config.set_defaults
15
+
16
+ # load all lux libs
17
+ [:overload, :common, :lux].each do |f|
18
+ Dir.require_all Lux.fw_root.join('./lib/%s' % f)
19
+ end
20
+
21
+ String.inflections do |inflect|
22
+ # inflect.plural /^(ox)$/i, '\1\2en'
23
+ # inflect.singular /^(ox)en/i, '\1'
24
+ # inflect.irregular 'octopus', 'octopi'
25
+ inflect.plural 'bonus', 'bonuses'
26
+ inflect.plural 'clothing', 'clothes'
27
+ inflect.plural 'people', 'people'
28
+ inflect.singular /news$/, 'news'
29
+ inflect.singular /Data$/i, 'Data'
30
+ end
31
+
32
+ # load Tilt parsers
33
+ Haml::Template.options[:escape_html] = false
34
+ # Tilt.register Tilt::ERBTemplate, 'erb'
35
+ # Tilt.register Haml::Template, 'haml'
36
+
37
+ # ensure we are not loading lux in lux folder
38
+ if Lux.root != Lux.fw_root
39
+ # create folders if needed
40
+ ['./log', './tmp'].each { |d| `mkdir #{d}` unless Dir.exist?(d) }
41
+ end
@@ -1,57 +1,15 @@
1
- ## Lux::Application - main application controller and router
1
+ ## Lux.app (Lux::Application)
2
2
 
3
- * can capture errors with `on_error` instance method
4
- * calls `before`, `routes` and `after` class filters on every request
5
- * routes requests to controllers via `map`, `root` and `call` methods
6
-
7
- ### Instance methods
8
-
9
- #### root
10
-
11
- executes if nav.root is empty
12
-
13
- #### map
14
-
15
- map specific nav root to Controller and calls if root mathes
16
-
17
- for example if path is /blogs
18
-
19
- `map blogs: Main::BlogController`
20
-
21
- will call instance method call with @path expanded
22
-
23
- `Main::BlogController.new.call(*@path)`
24
-
25
- more examples
26
-
27
- * `map blog: BlogController` will call `BlogController.action(:blog)`
28
- * `map blog: 'blog#single'` will call `BlogController.action(:single)`
29
- * `map blog: -> { BlogController.custom(:whatever) }`
30
-
31
- ### call
32
-
33
- Calls specific controller action inside call.
34
-
35
- ```ruby
36
- call 'main/links#index'
37
- call [Main::LinksController, :index]
38
- call -> { [200, {}, ['OK']]}
39
- ```
40
-
41
- ### Router example
3
+ Main application controller and router
42
4
 
43
- For Lux routing you need to know only few things
44
-
45
- * taget can be 5 object variants, look at root example
46
- * "root" method calls object if nav.root is blank?
47
- * "map" method calls object if nav.first == match
48
- * "namespace" method accepts block that wraps map calls.
5
+ * can capture errors with `rescue_from` class method
6
+ * calls `before`, `routes` and `after` class filters on every request
49
7
 
50
8
  ```ruby
51
9
  Lux.app do
52
10
 
53
11
  def api_router
54
- error :forbiden, 'Only POST requests are allowed' if Lux.prod? && !post?
12
+ error :forbiden, 'Only POST requests are allowed' if Lux.env.prod? && !post?
55
13
  Lux::Api.call nav.path
56
14
  end
57
15
 
@@ -63,40 +21,48 @@ Lux.app do
63
21
  error 404
64
22
  end
65
23
 
24
+ rescue_from :all do |error|
25
+ case error
26
+ when PG::ConnectionBad
27
+ # ...
28
+ when Lux::Error
29
+ # ...
30
+ else
31
+ end
32
+
66
33
  ###
67
34
 
68
35
  routes do
69
36
  # we show on root method, that target can be multiple object types, 5 variants
37
+ # this target is valid target for any of the follwing methods: get, post, map, call, root
70
38
  root [RootController, :index] # calls RootController#index
71
- root 'root#call' # calls RootController#call
72
- root :call_root # calls "call_root" method in current scope
73
- root 'root' # calls RootController#index
74
- root 'root#foo' # calls RootController#foo
75
-
76
- # we can route based on the user status
77
- root User.current ? 'main/root' : 'guest'
78
-
79
- # map "/api" to "api_router" method
80
- map api: :api_router
81
-
82
- # with MainController
83
- # map MainController do
84
- map 'main' do
85
- map :search # map "/search" to MainController#search
86
- map '/login' # map "/login" to MainController#login
39
+ root 'root#index' # calls RootController#index
40
+ root 'root' # calls RootController#call
41
+
42
+ root 'main/root'
43
+
44
+ # simple route, only for GET
45
+ map.about 'static#about' if get?
46
+
47
+ # execute blok if request_type is POST
48
+ post? do
49
+ # map "/api" to "api_router" method
50
+ map.api :api_router
51
+ # or
52
+ map api: :api_router
87
53
  end
88
54
 
89
55
  # map "/foo/dux/baz" route to MainController#foo with params[:bar] == 'dux'
90
56
  map '/foo/:bar/baz' => 'main#foo'
91
57
 
92
- # if method "city" in current scope returns true
93
- namespace :city do
58
+ # call method "city_map", if it returns true, proceed
59
+ map :city do
94
60
  # call MainController#city if request.method == 'GET'
95
- map 'main#city' if get?
61
+ map 'main#city'
96
62
  end
97
63
 
98
64
  # if we match '/foo' route
99
- namespace 'foo' do
65
+ map 'foo' do
100
66
  # call MainController#foo with params[:bar] == '...'
101
67
  map '/baz/:bar' => 'main#foo'
102
68
  end
@@ -104,44 +70,40 @@ Lux.app do
104
70
  end
105
71
  ```
106
72
 
107
- ### Router rescues example
73
+ #### Instance methods
108
74
 
109
- ```ruby
110
- Lux.app do
111
- def on_error error
112
-
113
- message = case error
114
- when PG::ConnectionBad
115
- msg = error.message || 'DB connection error, please refresh page.'
116
- msg = "PG: #{msg}"
117
- Lux.logger(:db_error).error msg
118
- msg
119
-
120
- when Lux::Error
121
- # for handled errors
122
- # show HTTP errors in a browser without a log
123
- Main::RootController.action(:error, error)
124
-
125
- else
126
- # raise errors in developmet
127
- raise error if Lux.dev?
128
-
129
- key = Lux.error.log error
130
- "#{error.class}: #{error.message} \n\nkey: #{key}"
131
- end
75
+ * routes requests to controllers via `map`, `root` and `call` methods
76
+ * taget can be 3 object variants, look at the `call` example
77
+ * `map` maps requests to controller actions
78
+ * `map.about => 'main#about' if get?` -> map '/about' to `MainControler#about` if request is `GET`
79
+ * `map about: 'main#about'` -> map '/about' to `MainControler#about`
80
+ * `root` will call only for root
81
+ * `map.about => 'main#about' if get?` -> map '/about' to `MainControler#about` if request is `GET`
82
+ * `map about: 'main#about'` -> map '/about' to `MainControler#about`
83
+ * `call` calls specific controller action inside call - stops routing parsing
84
+ * `call 'main/links#index'` - call `Main::LinksController#index`
85
+ * `call [Main::LinksController, :index]` - call `Main::LinksController#index`
86
+ * `call -> { [200, {}, ['OK']]}` - return HTTP 200 - OK
87
+
88
+ #### Class filters
89
+
90
+ There are a few route filtes
91
+ * `config` # pre boot app config
92
+ * `boot` # after rack app boot (web only)
93
+ * `info` # called by "lux config" cli
94
+ * `before` # before any page load
95
+ * `routes` # routes resolve
96
+ * `after` # after any page load
97
+ * `rescue_from` # on routing error
98
+
99
+
100
+ #### Router example
132
101
 
133
- # use default error formater
134
- Lux.error message
135
- end
136
- end
102
+ For Lux routing you need to know only few things
103
+
104
+ * `get?`, `post?`, `delete?`, ... will be true of false based HTTP_REQUEST type
105
+ * `get? { @exec_if_true }` works as well
106
+ * `map"`method accepts block that wraps map calls.
107
+ * `map :city do ...` will call `city_map` method. it has to return falsey if no match
108
+ * `map 'city' do ...` will check if we are under `/city/*` nav namespace
137
109
 
138
- if Lux.prod?
139
- Lux.config.error_logger = proc do |error|
140
- # log and show error page in a production
141
- key = SimpleException.log error
142
- Lux.cache.fetch('error-mail-%s' % key) { Mailer.error(error, key).deliver }
143
- Lux.logger(:exceptions).error [key, User.current.try(:email).or('guest'), error.message].join(' - ')
144
- key
145
- end
146
- end
147
- ```