Module: Lux

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

Defined Under Namespace

Modules: Cache, Config, DelayedJob, Error Classes: Api, Application, Cell, Current, Helper, Mailer, Response, Template

Constant Summary collapse

ENV_PROD =
ENV['RACK_ENV']    == 'production'
ENV_DEV =
ENV['RACK_ENV']    == 'development'
ENV_TEST =
ENV['RACK_ENV']    == 'test'
LUX_CLI =
$0 == 'pry' || $0.index('/run.rb') || ENV['RACK_ENV'] == 'test'
VERSION =
File.read File.expand_path('../../../.version', __FILE__).chomp

Instance Method Summary collapse

Instance Method Details

#app(&block) ⇒ Object



57
58
59
# File 'lib/lux/lux.rb', line 57

def app &block
  block ? Lux::Application.class_eval(&block) : Lux::Application
end

#call(env = nil) ⇒ Object

main rack response



28
29
30
31
32
33
34
35
# File 'lib/lux/lux.rb', line 28

def call  env=nil
  req = Lux::Current.new env
  req.response.render
rescue
  raise $! if Lux.config(:show_server_errors)

  [500, {}, ['Lux server error']]
end

#config(key = nil) ⇒ Object



43
44
45
46
47
# File 'lib/lux/lux.rb', line 43

def config key=nil
  return CONFIG unless key
  die 'Lux.config.%s not found' % key if CONFIG[key].nil?
  CONFIG[key].kind_of?(Proc) ? CONFIG[key].call() : CONFIG[key]
end

#currentObject



49
50
51
# File 'lib/lux/lux.rb', line 49

def current
  Thread.current[:lux][:page] ||= Lux::Current.new('/mock')
end

#current=(what) ⇒ Object



53
54
55
# File 'lib/lux/lux.rb', line 53

def current=(what)
  Thread.current[:lux][:page] = what
end

#delay(*args) ⇒ Object

if block given, simple new thread bg job if string given, eval it in bg if object given, instance it and run it



95
96
97
98
99
100
101
102
103
104
# File 'lib/lux/lux.rb', line 95

def delay *args
  if block_given?
    BACKGROUND_THREADS.push Thread.new { yield }
  elsif args[0]
    # Lux.delay(mail_object, :deliver)
    Lux::DelayedJob.push(*args)
  else
    Lux::DelayedJob
  end
end

#env(key = nil) ⇒ Object



37
38
39
40
41
# File 'lib/lux/lux.rb', line 37

def env key=nil
  return ENV['RACK_ENV'] unless key
  die "ENV['#{key}'] not found" if ENV[key].nil?
  ENV[key]
end

#error(data) ⇒ Object



69
70
71
72
# File 'lib/lux/lux.rb', line 69

def error data
  current.response.status 500
  Lux::Error.show(data)
end

#fw_rootObject



65
66
67
# File 'lib/lux/lux.rb', line 65

def fw_root
  @@lux_fw_root ||= Pathname.new(File.expand_path('../../', File.dirname(__FILE__))).freeze
end

#log(what) ⇒ Object



74
75
76
77
# File 'lib/lux/lux.rb', line 74

def log what
  return unless Lux.config(:log_to_stdout)
  puts what
end

#on(name, ref = nil, &proc) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/lux/lux.rb', line 79

def on name, ref=nil, &proc
  EVENTS[name] ||= []

  if block_given?
    puts "* event: #{name} defined".white
    EVENTS[name].push(proc)
  else
    for func in EVENTS[name]
      ref.instance_eval(&func)
    end
  end
end

#plugin(name) ⇒ Object

load specific plugin



115
116
117
118
119
120
121
122
123
124
# File 'lib/lux/lux.rb', line 115

def plugin name
  dir  = '%s/plugins/%s' % [Lux.fw_root, name]
  base = '%s/base.rb' % dir

  if File.exist?(base)
    load base
  else
    Lux::Config.require_all('%s/*' % dir)
  end
end

#rootObject



61
62
63
# File 'lib/lux/lux.rb', line 61

def root
  @@lux_app_root ||= Pathname.new(Dir.pwd).freeze
end

#speed(loops = 1) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/lux/lux.rb', line 106

def speed loops=1
  render_start = Time.monotonic
  loops.times { yield }
  num = (Time.monotonic - render_start) * 1000
  num = "#{num.round(1)} ms"
  loops == 1 ? num : "Done #{loops.to_s.sub(/(\d)(\d{3})$/,'\1s \2')} loops in #{num}"
end