Module: Lux
Defined Under Namespace
Modules: Config, DelayedJob, EventBus Classes: Application, Cache, Controller, Current, Error, Mailer, Response, View
Constant Summary collapse
- ENV_PROD =
ENV['RACK_ENV'] == 'production'
Instance Method Summary collapse
- #app(&block) ⇒ Object
-
#call(env = nil) ⇒ Object
main rack response.
- #config(key = nil) ⇒ Object
- #current ⇒ Object
- #current=(what) ⇒ Object
-
#delay(*args, &block) ⇒ Object
if block given, simple new thread bg job if string given, eval it in bg if object given, instance it and run it.
- #env(key = nil) ⇒ Object
- #error(data = nil) ⇒ Object
-
#load_tasks ⇒ Object
load rake tasks + including ones in plugins.
-
#log(what = nil) ⇒ Object
simple log to stdout.
-
#logger(name = nil) ⇒ Object
Lux.logger(:foo).warn ‘bar’.
-
#plugin(*args) ⇒ Object
simple interface to plugins Lux.plugin :foo Lux.plugin.
-
#ram_cache(key) ⇒ Object
in memory cache, used on app init, no need for Mutex.
-
#serve(rack_handler) ⇒ Object
must be called when serving web pages from rackup.
-
#speed ⇒ Object
simple block to calc block execution speed.
-
#start ⇒ Object
initialize the Lux application.
Instance Method Details
#app(&block) ⇒ Object
76 77 78 |
# File 'lib/lux/lux.rb', line 76 def app &block block ? Lux::Application.class_eval(&block) : Lux::Application end |
#call(env = nil) ⇒ Object
main rack response
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/lux/lux.rb', line 35 def call env=nil state = Lux::Current.new env app = Lux::Application.new state app.render rescue => error if Lux.config(:dump_errors) raise error else log error.backtrace [500, {}, ['Server error: %s' % error.]] end end |
#config(key = nil) ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/lux/lux.rb', line 58 def config key=nil if key value = CONFIG[key] die 'Lux.config.%s not found' % key if value.nil? value.kind_of?(Proc) ? value.call() : value else CONFIG end end |
#current ⇒ Object
68 69 70 |
# File 'lib/lux/lux.rb', line 68 def current Thread.current[:lux][:page] ||= Lux::Current.new('/mock') end |
#current=(what) ⇒ Object
72 73 74 |
# File 'lib/lux/lux.rb', line 72 def current=(what) Thread.current[:lux][:page] = what end |
#delay(*args, &block) ⇒ Object
if block given, simple new thread bg job if string given, eval it in bg if object given, instance it and run it
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/lux/lux.rb', line 100 def delay *args, &block if block_given? puts 'add' t = Thread.new do begin block.call rescue => e Lux.logger(:delay_errors).error [e., e.backtrace] end end BACKGROUND_THREADS.push t elsif args[0] # Lux.delay(mail_object, :deliver) Lux::DelayedJob.push(*args) else Lux::DelayedJob end end |
#env(key = nil) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/lux/lux.rb', line 48 def env key=nil if key value = ENV[key] die "ENV['#{key}'] not found" if value.nil? value else ENV['RACK_ENV'] end end |
#error(data = nil) ⇒ Object
80 81 82 |
# File 'lib/lux/lux.rb', line 80 def error data=nil data ? Lux::Error.render(data) : Lux::Error end |
#load_tasks ⇒ Object
load rake tasks + including ones in plugins
122 123 124 |
# File 'lib/lux/lux.rb', line 122 def load_tasks require_relative '../../tasks/loader.rb' end |
#log(what = nil) ⇒ Object
simple log to stdout
85 86 87 88 |
# File 'lib/lux/lux.rb', line 85 def log what=nil return unless Lux.config(:log_to_stdout) puts what || yield end |
#logger(name = nil) ⇒ Object
Lux.logger(:foo).warn ‘bar’
157 158 159 160 161 162 163 164 |
# File 'lib/lux/lux.rb', line 157 def logger name=nil name ||= ENV.fetch('RACK_ENV').downcase MCACHE['lux-logger-%s' % name] ||= Logger.new('./log/%s.log' % name).tap do |it| it.formatter = Lux.config.logger_formater end end |
#plugin(*args) ⇒ Object
simple interface to plugins Lux.plugin :foo Lux.plugin
93 94 95 |
# File 'lib/lux/lux.rb', line 93 def plugin *args args.first ? Lux::Config::Plugin.load(*args) : Lux::Config::Plugin end |
#ram_cache(key) ⇒ Object
in memory cache, used on app init, no need for Mutex
127 128 129 130 |
# File 'lib/lux/lux.rb', line 127 def ram_cache key MCACHE[key] = nil if Lux.config(:compile_assets) MCACHE[key] ||= yield end |
#serve(rack_handler) ⇒ Object
must be called when serving web pages from rackup
140 141 142 143 144 145 146 |
# File 'lib/lux/lux.rb', line 140 def serve rack_handler @rackup_start = true # Boot Lux Object.class_callback :rack_boot, Lux::Application, rack_handler rack_handler.run self end |