Class: Tml::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/tml/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#applicationObject

Returns the value of attribute application.



40
41
42
# File 'lib/tml/session.rb', line 40

def application
  @application
end

#block_optionsObject

Returns the value of attribute block_options.



40
41
42
# File 'lib/tml/session.rb', line 40

def block_options
  @block_options
end

#current_languageObject

Returns the value of attribute current_language.



41
42
43
# File 'lib/tml/session.rb', line 41

def current_language
  @current_language
end

#current_localeObject

Returns the value of attribute current_locale.



41
42
43
# File 'lib/tml/session.rb', line 41

def current_locale
  @current_locale
end

#current_sourceObject

Returns the value of attribute current_source.



41
42
43
# File 'lib/tml/session.rb', line 41

def current_source
  @current_source
end

#current_translatorObject

Returns the value of attribute current_translator.



41
42
43
# File 'lib/tml/session.rb', line 41

def current_translator
  @current_translator
end

#current_userObject

Returns the value of attribute current_user.



41
42
43
# File 'lib/tml/session.rb', line 41

def current_user
  @current_user
end

Instance Method Details

#block_option(key, lookup = true) ⇒ Object

Block Options



116
117
118
119
120
121
122
123
124
125
# File 'lib/tml/session.rb', line 116

def block_option(key, lookup = true)
  if lookup
    block_options_queue.reverse.each do |options|
      value = options[key.to_s] || options[key.to_sym]
      return value if value
    end
    return nil
  end
  block_options[key]
end

#block_options_queueObject



136
137
138
# File 'lib/tml/session.rb', line 136

def block_options_queue
  @block_options ||= []
end

#init(opts = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/tml/session.rb', line 43

def init(opts = {})
  return if Tml.config.disabled?

  key       = opts[:key]        || Tml.config.application[:key]
  host      = opts[:host]       || Tml.config.application[:host]
  cdn_host  = opts[:cdn_host]   || Tml.config.application[:cdn_host]
  token     = opts[:access_token]

  Tml.cache.reset_version

  self.current_user = opts[:user]
  self.current_source = opts[:source] || 'index'
  self.current_locale = opts[:locale]
  self.current_translator = opts[:translator]

  # Tml.logger.debug(opts.inspect)

  self.application = Tml::Application.new(:key => key, :access_token => token, :host => host, :cdn_host => cdn_host).fetch

  if self.current_translator
    self.current_translator.application = self.application
  end

  self.current_locale = preferred_locale(opts[:locale])
  self.current_language = self.application.current_language(self.current_locale)

  self
end

#inline_mode?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/tml/session.rb', line 108

def inline_mode?
  current_translator and current_translator.inline?
end

#pop_block_optionsObject



131
132
133
134
# File 'lib/tml/session.rb', line 131

def pop_block_options
  return unless @block_options
  @block_options.pop
end

#preferred_locale(locales) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/tml/session.rb', line 72

def preferred_locale(locales)
  return application.default_locale unless locales
  locales = locales.is_a?(String) ? locales.split(',') : locales
  locales.each do |locale|
    return locale if application.locales.include?(locale)
  end
  application.default_locale
end

#push_block_options(opts) ⇒ Object



127
128
129
# File 'lib/tml/session.rb', line 127

def push_block_options(opts)
  block_options_queue.push(opts)
end

#resetObject



81
82
83
84
85
86
87
88
# File 'lib/tml/session.rb', line 81

def reset
  self.application= nil
  self.current_user= nil
  self.current_language= nil
  self.current_translator= nil
  self.current_source= nil
  self.block_options= nil
end

#source_languageObject



98
99
100
101
# File 'lib/tml/session.rb', line 98

def source_language
  locale = block_option(:locale)
  locale ? application.language(locale) : application.language
end

#target_languageObject



103
104
105
106
# File 'lib/tml/session.rb', line 103

def target_language
  target_locale = block_option(:target_locale)
  target_locale ? application.language(target_locale) : current_language
end

#with_block_options(opts) ⇒ Object



144
145
146
147
148
149
150
151
# File 'lib/tml/session.rb', line 144

def with_block_options(opts)
  push_block_options(opts)
  if block_given?
    ret = yield
  end
  pop_block_options
  ret
end