Module: Lux

Extended by:
Lux
Included in:
Lux
Defined in:
lib/lux/lux.rb,
lib/lux/cache/cache.rb

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

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.message]]
  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

#currentObject



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.message, 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_tasksObject

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

#speedObject

simple block to calc block execution speed



149
150
151
152
153
154
# File 'lib/lux/lux.rb', line 149

def speed
  render_start = Time.monotonic
  yield
  num = (Time.monotonic - render_start) * 1000
  '%s ms' % num.round(1)
end

#startObject

initialize the Lux application



133
134
135
136
137
# File 'lib/lux/lux.rb', line 133

def start
  Lux.config.lux_config_loaded = true

  Config.start!
end