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



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

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

#domainObject

Domain part of the host



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

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

Add to list of files in use



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/lux/current/current.rb', line 96

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

Full host with port



51
52
53
# File 'lib/lux/current/current.rb', line 51

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)


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

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



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

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

Redirect from current page



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

def redirect *args
  response.redirect *args
end

#uidObject

Generete unique ID par page render



90
91
92
93
# File 'lib/lux/current/current.rb', line 90

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

#varObject

Current scope variables hash



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

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