Class: Lux::Current

Inherits:
Object show all
Defined in:
lib/lux/current/current.rb

Defined Under Namespace

Modules: EncryptParams Classes: Session

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env = nil) ⇒ Current

Returns a new instance of Current.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/lux/current/current.rb', line 13

def initialize env=nil
  env   ||= '/mock'
  env     = ::Rack::MockRequest.env_for(env) if env.is_a?(String)
  request = ::Rack::Request.new env

  # reset page cache
  Thread.current[:lux] = { cache:{}, page: self }

  @files_in_use = []
  @response     = Lux::Response.new
  @request      = request
  @session      = Lux::Current::Session.new request

  # remove empty paramsters in GET request
  if request.request_method == 'GET'
    for el in request.params.keys
      request.params.delete(el) if request.params[el].blank?
    end
  end

  # indiferent access
  request.instance_variable_set(:@params, request.params.h_wia) if request.params.keys.length > 0

  Lux::Current::EncryptParams.decrypt request.params
  ap request.params if request.post? && Lux.config(:log_to_stdout)

  @nav = Lux::Application::Nav.new request
end

Instance Attribute Details

#can_clear_cacheObject

set to true if user is admin and you want him to be able to clear caches in production



8
9
10
# File 'lib/lux/current/current.rb', line 8

def can_clear_cache
  @can_clear_cache
end

#localeObject

Returns the value of attribute locale.



10
11
12
# File 'lib/lux/current/current.rb', line 10

def locale
  @locale
end

Returns the value of attribute nav.



11
12
13
# File 'lib/lux/current/current.rb', line 11

def nav
  @nav
end

#requestObject (readonly)

Returns the value of attribute request.



11
12
13
# File 'lib/lux/current/current.rb', line 11

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



11
12
13
# File 'lib/lux/current/current.rb', line 11

def response
  @response
end

#sessionObject

Returns the value of attribute session.



10
11
12
# File 'lib/lux/current/current.rb', line 10

def session
  @session
end

Instance Method Details

#cache(key) ⇒ Object

cache data in current page



58
59
60
61
62
# File 'lib/lux/current/current.rb', line 58

def cache key
  data = Thread.current[:lux][:cache][key]
  return data if data
  Thread.current[:lux][:cache][key] = yield
end

#domainObject



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

def domain
  host = Lux.current.request.host.split('.')
  host_country = host.pop
  host_name    = host.pop
  host_name ? "#{host_name}.#{host_country}" : host_country
end

#files_in_use(file = nil) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/lux/current/current.rb', line 90

def files_in_use file=nil
  if block_given?
    return yield(file) unless @files_in_use.include?(file)
  end

  return @files_in_use unless file
  return unless Lux.config(:log_to_stdout)

  file = file.sub './', ''

  if @files_in_use.include?(file)
    true
  else
    @files_in_use.push file
    false
  end
end

#hostObject



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

def host
  "#{request.env['rack.url_scheme']}://#{request.host}:#{request.port}".sub(':80','')# rescue 'http://locahost:3000'
end

#no_cache?Boolean

set current.can_clear_cache = true in production for admins

Returns:

  • (Boolean)


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

def no_cache?
  @can_clear_cache = true if Lux.dev?
  @can_clear_cache && @request.env['HTTP_CACHE_CONTROL'].to_s.downcase == 'no-cache' ? true : false
end

#once(id = nil, data = nil, &block) ⇒ Object

execute action once per page



75
76
77
78
79
80
81
82
83
# File 'lib/lux/current/current.rb', line 75

def once id=nil, data=nil, &block
  id ||= Digest::SHA1.hexdigest caller[0] if block

  @once_hash ||= {}
  return if @once_hash[id]
  @once_hash[id] = true

  block_given? ? yield : data
end

#redirect(*args) ⇒ Object



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

def redirect *args
  response.redirect *args
end

#uidObject



85
86
87
88
# File 'lib/lux/current/current.rb', line 85

def uid
  Thread.current[:uid_cnt] ||= 0
  "uid-#{Thread.current[:uid_cnt]+=1}"
end

#varObject



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

def var
  Thread.current[:lux][:var] ||= Hashie::Mash.new
end