Class: Tml::Config
- Inherits:
-
Object
- Object
- Tml::Config
- Defined in:
- lib/tml/config.rb
Overview
Acts as a global singleton that holds all Tml configuration The class can be extended with a different implementation, as long as the interface is supported
Instance Attribute Summary collapse
-
#agent ⇒ Object
Returns the value of attribute agent.
-
#api_client ⇒ Object
Returns the value of attribute api_client.
-
#application ⇒ Object
Configuration Attributes.
-
#auto_init ⇒ Object
Returns the value of attribute auto_init.
-
#cache ⇒ Object
Configuration Attributes.
-
#context_rules ⇒ Object
Configuration Attributes.
-
#current_locale_method ⇒ Object
Used by Rails and Sinatra extensions.
-
#current_user_method ⇒ Object
Used by Rails and Sinatra extensions.
-
#default_level ⇒ Object
Configuration Attributes.
-
#default_tokens ⇒ Object
Configuration Attributes.
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#enabled ⇒ Object
Configuration Attributes.
-
#format ⇒ Object
Configuration Attributes.
-
#i18n_backend ⇒ Object
Used by Rails and Sinatra extensions.
-
#invalidator ⇒ Object
Returns the value of attribute invalidator.
-
#locale ⇒ Object
Configuration Attributes.
-
#localization ⇒ Object
Configuration Attributes.
-
#logger ⇒ Object
Configuration Attributes.
-
#postoffice ⇒ Object
Configuration Attributes.
-
#source_separator ⇒ Object
Returns the value of attribute source_separator.
-
#submit_missing_keys_realtime ⇒ Object
Used for IRB only.
-
#translator_options ⇒ Object
Used by Rails and Sinatra extensions.
Instance Method Summary collapse
- #access_token ⇒ Object
- #agent_locale_strategy ⇒ Object
- #cache_enabled? ⇒ Boolean
-
#decorator_class ⇒ Object
Decorations.
- #default_abbr_day_name(index) ⇒ Object
- #default_abbr_day_names ⇒ Object
- #default_abbr_month_name(index) ⇒ Object
- #default_abbr_month_names ⇒ Object
- #default_application ⇒ Object
- #default_date_formats ⇒ Object
- #default_day_name(index) ⇒ Object
- #default_day_names ⇒ Object
- #default_language ⇒ Object
-
#default_locale ⇒ Object
def default_level return Tml.session.application.default_level if Tml.session.application @default_level end.
- #default_month_name(index) ⇒ Object
- #default_month_names ⇒ Object
- #default_token_value(token_name, type = :data, format = :html) ⇒ Object
- #disabled? ⇒ Boolean
- #enabled? ⇒ Boolean
-
#initialize ⇒ Config
constructor
A new instance of Config.
- #locale_browser_enabled? ⇒ Boolean
- #locale_cookie_enabled? ⇒ Boolean
- #locale_expression ⇒ Object
- #locale_param ⇒ Object
- #locale_redirect_enabled? ⇒ Boolean
- #locale_strategy ⇒ Object
- #nested_value(hash, key, default_value = nil) ⇒ Object
- #set_default_token(token_name, value, type = :data, format = :html) ⇒ Object
-
#strftime_symbol_to_token(symbol) ⇒ Object
Localization.
- #translator_option(key) ⇒ Object
Constructor Details
#initialize ⇒ Config
Returns a new instance of Config.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/tml/config.rb', line 88 def initialize @enabled = true @default_level = 0 @format = :html @auto_init = true @source_separator = '@:@' @api_client = { class: Tml::Api::Client, timeout: 5, open_timeout: 2 } @locale = { default: 'en', # default locale method: 'current_locale', # method to use for user selected locale, if nil, the rest will be a fallback param: 'locale', # the way to name the param strategy: 'param', # approach param, pre-path, pre-domain, custom-domain redirect: true, # if TML should handle locale logic redirects skip_default: false, # if the default locales should not be visible browser: true, # if you want to use a browser header to determine the locale cookie: true, # if you want to store user selected locale in the cookie # default_host: '', # if user wants to not display default locale, we need to know the default host # mapping: { # domain to locale mapping # en: '', # ru: '', # } } @agent = { enabled: true, type: 'agent', cache: 86400 # timeout every 24 hours } # if running from IRB, make it default to TRUE @submit_missing_keys_realtime = (%w(irb pry).include?($0 || '')) @current_locale_method = :current_locale @current_user_method = :current_user @application = nil @postoffice = nil = { debug: false, debug_format_html: "<span style='font-size:20px;color:red;'>{</span> {$0} <span style='font-size:20px;color:red;'>}</span>", debug_format: '{{{{$0}}}}', split_sentences: false, nodes: { ignored: %w(), scripts: %w(style script code pre), inline: %w(a span i b img strong s em u sub sup), short: %w(i b), splitters: %w(br hr) }, attributes: { labels: %w(title alt) }, name_mapping: { b: 'bold', i: 'italic', a: 'link', img: 'picture' }, data_tokens: { special: { enabled: true, regex: /(&[^;]*;)/ }, date: { enabled: true, formats: [ [/((Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s+\d+,\s+\d+)/, "{month} {day}, {year}"], [/((January|February|March|April|May|June|July|August|September|October|November|December)\s+\d+,\s+\d+)/, "{month} {day}, {year}"], [/(\d+\s+(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec),\s+\d+)/, "{day} {month}, {year}"], [/(\d+\s+(January|February|March|April|May|June|July|August|September|October|November|December),\s+\d+)/, "{day} {month}, {year}"] ], name: 'date' }, rules: [ {enabled: true, name: 'time', regex: /(\d{1,2}:\d{1,2}\s+([A-Z]{2,3}|am|pm|AM|PM)?)/}, {enabled: true, name: 'phone', regex: /((\d{1}-)?\d{3}-\d{3}-\d{4}|\d?\(\d{3}\)\s*\d{3}-\d{4}|(\d.)?\d{3}.\d{3}.\d{4})/}, {enabled: true, name: 'email', regex: /([-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|io|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?)/}, {enabled: true, name: 'price', regex: /(\$\d*(,\d*)*(\.\d*)?)/}, {enabled: true, name: 'fraction', regex: /(\d+\/\d+)/}, {enabled: true, name: 'num', regex: /(\b\d*(,\d*)*(\.\d*)?%?\b)/} ] } } @context_rules = { :number => { :variables => { } }, :gender => { :variables => { '@gender' => 'gender', } }, :genders => { :variables => { '@genders' => lambda{|list| list.collect do |u| u.is_a?(Hash) ? (u['gender'] || u[:gender]) : u.gender end }, '@size' => lambda{ |list| list.size } } }, :date => { :variables => { } }, :time => { :variables => { } }, :list => { :variables => { '@count' => lambda{|list| list.size} } }, } @cache = nil @logger = { :enabled => false, :path => './log/tml.log', :level => 'debug', :secure => true } @default_tokens = { :html => { :data => { :ndash => '–', # – :mdash => '—', # — :iexcl => '¡', # ¡ :iquest => '¿', # ¿ :quot => '"', # ' :ldquo => '“', # “ :rdquo => '”', # ” :lsquo => '‘', # ‘ :rsquo => '’', # ’ :laquo => '«', # « :raquo => '»', # » :nbsp => ' ', # space :lsaquo => '‹', # ‹ :rsaquo => '›', # › :br => '<br/>', # line break :lbrace => '{', :rbrace => '}', :trade => '™', # TM }, :decoration => { :strong => '<strong>{$0}</strong>', :bold => '<strong>{$0}</strong>', :b => '<strong>{$0}</strong>', :em => '<em>{$0}</em>', :italic => '<i>{$0}</i>', :i => '<i>{$0}</i>', :link => "<a href='{$href}' class='{$class}' style='{$style}' title='{$title}'>{$0}</a>", :br => '<br>{$0}', :strike => '<strike>{$0}</strike>', :div => "<div id='{$id}' class='{$class}' style='{$style}'>{$0}</div>", :span => "<span id='{$id}' class='{$class}' style='{$style}'>{$0}</span>", :h1 => '<h1>{$0}</h1>', :h2 => '<h2>{$0}</h2>', :h3 => '<h3>{$0}</h3>', } }, :text => { :data => { :ndash => '–', :mdash => '-', :iexcl => '¡', :iquest => '¿', :quot => "'", :ldquo => '“', :rdquo => '”', :lsquo => '‘', :rsquo => '’', :laquo => '«', :raquo => '»', :nbsp => ' ', :lsaquo => '‹', :rsaquo => '›', :br => '\n', :lbrace => '{', :rbrace => '}', :trade => '™', }, :decoration => { :strong => '{$0}', :bold => '{$0}', :b => '{$0}', :em => '{$0}', :italic => '{$0}', :i => '{$0}', :link => '{$0}:{$1}', :br => '\n{$0}', :strike => '{$0}', :div => '{$0}', :span => '{$0}', :h1 => '{$0}', :h2 => '{$0}', :h3 => '{$0}', } } } @localization = { :default_day_names => %w(Sunday Monday Tuesday Wednesday Thursday Friday Saturday), :default_abbr_day_names => %w(Sun Mon Tue Wed Thu Fri Sat), :default_month_names => %w(January February March April May June July August September October November December), :default_abbr_month_names => %w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec), :custom_date_formats => { :default => '%m/%d/%Y', # 07/4/2008 :short_numeric => '%m/%d', # 07/4 :short_numeric_year => '%m/%d/%y', # 07/4/08 :long_numeric => '%m/%d/%Y', # 07/4/2008 :verbose => '%A, %B %d, %Y', # Friday, July 4, 2008 :monthname => '%B %d', # July 4 :monthname_year => '%B %d, %Y', # July 4, 2008 :monthname_abbr => '%b %d', # Jul 4 :monthname_abbr_year => '%b %d, %Y', # Jul 4, 2008 :date_time => '%m/%d/%Y at %H:%M', # 01/03/1010 at 5:30 }, :token_mapping => { '%a' => '{short_week_day_name}', '%A' => '{week_day_name}', '%b' => '{short_month_name}', '%B' => '{month_name}', '%p' => '{am_pm}', '%d' => '{days}', '%e' => '{day_of_month}', '%j' => '{year_days}', '%m' => '{months}', '%W' => '{week_num}', '%w' => '{week_days}', '%y' => '{short_years}', '%Y' => '{years}', '%l' => '{trimed_hour}', '%H' => '{full_hours}', '%I' => '{short_hours}', '%M' => '{minutes}', '%S' => '{seconds}', '%s' => '{since_epoch}' } } end |
Instance Attribute Details
#agent ⇒ Object
Returns the value of attribute agent.
83 84 85 |
# File 'lib/tml/config.rb', line 83 def agent @agent end |
#api_client ⇒ Object
Returns the value of attribute api_client.
83 84 85 |
# File 'lib/tml/config.rb', line 83 def api_client @api_client end |
#application ⇒ Object
Configuration Attributes
78 79 80 |
# File 'lib/tml/config.rb', line 78 def application @application end |
#auto_init ⇒ Object
Returns the value of attribute auto_init.
79 80 81 |
# File 'lib/tml/config.rb', line 79 def auto_init @auto_init end |
#cache ⇒ Object
Configuration Attributes
78 79 80 |
# File 'lib/tml/config.rb', line 78 def cache @cache end |
#context_rules ⇒ Object
Configuration Attributes
78 79 80 |
# File 'lib/tml/config.rb', line 78 def context_rules @context_rules end |
#current_locale_method ⇒ Object
Used by Rails and Sinatra extensions
82 83 84 |
# File 'lib/tml/config.rb', line 82 def current_locale_method @current_locale_method end |
#current_user_method ⇒ Object
Used by Rails and Sinatra extensions
82 83 84 |
# File 'lib/tml/config.rb', line 82 def current_user_method @current_user_method end |
#default_level ⇒ Object
Configuration Attributes
78 79 80 |
# File 'lib/tml/config.rb', line 78 def default_level @default_level end |
#default_tokens ⇒ Object
Configuration Attributes
78 79 80 |
# File 'lib/tml/config.rb', line 78 def default_tokens @default_tokens end |
#domain ⇒ Object
Returns the value of attribute domain.
79 80 81 |
# File 'lib/tml/config.rb', line 79 def domain @domain end |
#enabled ⇒ Object
Configuration Attributes
78 79 80 |
# File 'lib/tml/config.rb', line 78 def enabled @enabled end |
#format ⇒ Object
Configuration Attributes
78 79 80 |
# File 'lib/tml/config.rb', line 78 def format @format end |
#i18n_backend ⇒ Object
Used by Rails and Sinatra extensions
82 83 84 |
# File 'lib/tml/config.rb', line 82 def i18n_backend @i18n_backend end |
#invalidator ⇒ Object
Returns the value of attribute invalidator.
83 84 85 |
# File 'lib/tml/config.rb', line 83 def invalidator @invalidator end |
#locale ⇒ Object
Configuration Attributes
78 79 80 |
# File 'lib/tml/config.rb', line 78 def locale @locale end |
#localization ⇒ Object
Configuration Attributes
78 79 80 |
# File 'lib/tml/config.rb', line 78 def localization @localization end |
#logger ⇒ Object
Configuration Attributes
78 79 80 |
# File 'lib/tml/config.rb', line 78 def logger @logger end |
#postoffice ⇒ Object
Configuration Attributes
78 79 80 |
# File 'lib/tml/config.rb', line 78 def postoffice @postoffice end |
#source_separator ⇒ Object
Returns the value of attribute source_separator.
79 80 81 |
# File 'lib/tml/config.rb', line 79 def source_separator @source_separator end |
#submit_missing_keys_realtime ⇒ Object
Used for IRB only
86 87 88 |
# File 'lib/tml/config.rb', line 86 def submit_missing_keys_realtime @submit_missing_keys_realtime end |
#translator_options ⇒ Object
Used by Rails and Sinatra extensions
82 83 84 |
# File 'lib/tml/config.rb', line 82 def end |
Instance Method Details
#access_token ⇒ Object
427 428 429 |
# File 'lib/tml/config.rb', line 427 def access_token @application[:token] || @application[:access_token] || '' end |
#agent_locale_strategy ⇒ Object
358 359 360 |
# File 'lib/tml/config.rb', line 358 def agent_locale_strategy locale.merge(param: locale_param) end |
#cache_enabled? ⇒ Boolean
398 399 400 |
# File 'lib/tml/config.rb', line 398 def cache_enabled? not cache.nil? end |
#decorator_class ⇒ Object
Decorations
435 436 437 438 |
# File 'lib/tml/config.rb', line 435 def decorator_class return Tml::Decorators::Html if @format == :html Tml::Decorators::Default end |
#default_abbr_day_name(index) ⇒ Object
470 471 472 |
# File 'lib/tml/config.rb', line 470 def default_abbr_day_name(index) '' + default_abbr_day_names[index] end |
#default_abbr_day_names ⇒ Object
466 467 468 |
# File 'lib/tml/config.rb', line 466 def default_abbr_day_names localization[:default_abbr_day_names] end |
#default_abbr_month_name(index) ⇒ Object
486 487 488 |
# File 'lib/tml/config.rb', line 486 def default_abbr_month_name(index) '' + default_abbr_month_names[index] end |
#default_abbr_month_names ⇒ Object
482 483 484 |
# File 'lib/tml/config.rb', line 482 def default_abbr_month_names localization[:default_abbr_month_names] end |
#default_application ⇒ Object
423 424 425 |
# File 'lib/tml/config.rb', line 423 def default_application @default_application ||= Tml::Application.new(:host => Tml::Api::Client::API_HOST) end |
#default_date_formats ⇒ Object
490 491 492 |
# File 'lib/tml/config.rb', line 490 def default_date_formats localization[:custom_date_formats] end |
#default_day_name(index) ⇒ Object
462 463 464 |
# File 'lib/tml/config.rb', line 462 def default_day_name(index) '' + default_day_names[index] end |
#default_day_names ⇒ Object
458 459 460 |
# File 'lib/tml/config.rb', line 458 def default_day_names localization[:default_day_names] end |
#default_language ⇒ Object
416 417 418 419 420 421 |
# File 'lib/tml/config.rb', line 416 def default_language @default_language ||= begin file = File.(File.join(File.dirname(__FILE__), '..', 'tml', 'languages', "#{Tml.config.default_locale}.json")) Tml::Language.new(JSON.parse(File.read(file))) end end |
#default_locale ⇒ Object
def default_level
return Tml.session.application.default_level if Tml.session.application
@default_level
end
412 413 414 |
# File 'lib/tml/config.rb', line 412 def default_locale @locale[:default] || 'en' end |
#default_month_name(index) ⇒ Object
478 479 480 |
# File 'lib/tml/config.rb', line 478 def default_month_name(index) '' + default_month_names[index] end |
#default_month_names ⇒ Object
474 475 476 |
# File 'lib/tml/config.rb', line 474 def default_month_names localization[:default_month_names] end |
#default_token_value(token_name, type = :data, format = :html) ⇒ Object
440 441 442 |
# File 'lib/tml/config.rb', line 440 def default_token_value(token_name, type = :data, format = :html) default_tokens[format.to_sym][type.to_sym][token_name.to_sym] end |
#disabled? ⇒ Boolean
346 347 348 |
# File 'lib/tml/config.rb', line 346 def disabled? not enabled? end |
#enabled? ⇒ Boolean
342 343 344 |
# File 'lib/tml/config.rb', line 342 def enabled? enabled end |
#locale_browser_enabled? ⇒ Boolean
377 378 379 |
# File 'lib/tml/config.rb', line 377 def locale_browser_enabled? locale[:browser].nil? || locale[:browser] end |
#locale_cookie_enabled? ⇒ Boolean
370 371 372 373 374 375 |
# File 'lib/tml/config.rb', line 370 def if %w(pre-domain custom-domain).include?(locale[:strategy]) return false end locale[:cookie].nil? || locale[:cookie] end |
#locale_expression ⇒ Object
350 351 352 |
# File 'lib/tml/config.rb', line 350 def locale_expression /^[a-z]{2}(-[A-Z]{2,3})?$/ end |
#locale_param ⇒ Object
362 363 364 |
# File 'lib/tml/config.rb', line 362 def locale_param locale[:param] || 'locale' end |
#locale_redirect_enabled? ⇒ Boolean
381 382 383 |
# File 'lib/tml/config.rb', line 381 def locale_redirect_enabled? locale[:redirect].nil? || locale[:redirect] end |
#locale_strategy ⇒ Object
354 355 356 |
# File 'lib/tml/config.rb', line 354 def locale_strategy locale[:strategy] || 'param' end |
#nested_value(hash, key, default_value = nil) ⇒ Object
385 386 387 388 389 390 391 392 |
# File 'lib/tml/config.rb', line 385 def nested_value(hash, key, default_value = nil) parts = key.split('.') parts.each do |part| return default_value unless hash[part.to_sym] hash = hash[part.to_sym] end hash end |
#set_default_token(token_name, value, type = :data, format = :html) ⇒ Object
444 445 446 447 448 |
# File 'lib/tml/config.rb', line 444 def set_default_token(token_name, value, type = :data, format = :html) default_tokens[format.to_sym] ||= {} default_tokens[format.to_sym][type.to_sym] ||= {} default_tokens[format.to_sym][type.to_sym][token_name.to_sym] = value end |
#strftime_symbol_to_token(symbol) ⇒ Object
Localization
454 455 456 |
# File 'lib/tml/config.rb', line 454 def strftime_symbol_to_token(symbol) localization[:token_mapping][symbol] end |
#translator_option(key) ⇒ Object
394 395 396 |
# File 'lib/tml/config.rb', line 394 def translator_option(key) nested_value(self., key) end |