Class: Tr8n::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/tr8n/config.rb

Overview

Acts as a global singleton that holds all Tr8n configuration The class can be extended with a different implementation, as long as the interface is supported

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig



78
79
80
81
82
83
84
85
86
87
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
# File 'lib/tr8n/config.rb', line 78

def initialize
  @enabled = true
  @default_locale = 'en-US'
  @default_level  = 0
  @format = :html

  # if running from IRB, make it default to TRUE
  @submit_missing_keys_realtime = (['irb', 'pry'].include?($0 || ''))

  @current_locale_method = :current_locale
  @current_user_method = :current_user

  #@application = {
  #  :host     => 'https://localhost:3000',
  #  :key      => 'default',
  #  :secret   => '12345',
  #}

  @application = nil

  @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}
      }
    },
  }

  @logger  = {
    :enabled  => false,
    :path     => './log/tr8n.log',
    :level    => 'debug'
  }

  @cache = {
    :enabled  => false,
    :host     => 'localhost:11211',
    :adapter  => 'memcache',
    :version  => 1,
    :timeout  => 3600
  }

  @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  =>  "&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}'>{$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        =>  ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"],
    :default_abbr_day_names   =>  ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
    :default_month_names      =>  ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
    :default_abbr_month_names =>  ["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

#applicationObject

Configuration Attributes



70
71
72
# File 'lib/tr8n/config.rb', line 70

def application
  @application
end

#cacheObject

Configuration Attributes



70
71
72
# File 'lib/tr8n/config.rb', line 70

def cache
  @cache
end

#context_rulesObject

Configuration Attributes



70
71
72
# File 'lib/tr8n/config.rb', line 70

def context_rules
  @context_rules
end

#current_locale_methodObject

Used by Rails and Sinatra extensions



73
74
75
# File 'lib/tr8n/config.rb', line 73

def current_locale_method
  @current_locale_method
end

#current_user_methodObject

Used by Rails and Sinatra extensions



73
74
75
# File 'lib/tr8n/config.rb', line 73

def current_user_method
  @current_user_method
end

#default_levelObject

Configuration Attributes



70
71
72
# File 'lib/tr8n/config.rb', line 70

def default_level
  @default_level
end

#default_localeObject

Configuration Attributes



70
71
72
# File 'lib/tr8n/config.rb', line 70

def default_locale
  @default_locale
end

#default_tokensObject

Configuration Attributes



70
71
72
# File 'lib/tr8n/config.rb', line 70

def default_tokens
  @default_tokens
end

#enabledObject

Configuration Attributes



70
71
72
# File 'lib/tr8n/config.rb', line 70

def enabled
  @enabled
end

#formatObject

Configuration Attributes



70
71
72
# File 'lib/tr8n/config.rb', line 70

def format
  @format
end

#localizationObject

Configuration Attributes



70
71
72
# File 'lib/tr8n/config.rb', line 70

def localization
  @localization
end

#loggerObject

Configuration Attributes



70
71
72
# File 'lib/tr8n/config.rb', line 70

def logger
  @logger
end

#submit_missing_keys_realtimeObject

Used for IRB only



76
77
78
# File 'lib/tr8n/config.rb', line 76

def submit_missing_keys_realtime
  @submit_missing_keys_realtime
end

Instance Method Details

#decorator_classObject

Decorations



303
304
305
306
# File 'lib/tr8n/config.rb', line 303

def decorator_class
  return Tr8n::Decorators::Html if @format == :html
  Tr8n::Decorators::Default
end

#default_abbr_day_name(index) ⇒ Object



332
333
334
# File 'lib/tr8n/config.rb', line 332

def default_abbr_day_name(index)
  "" + default_abbr_day_names[index]
end

#default_abbr_day_namesObject



328
329
330
# File 'lib/tr8n/config.rb', line 328

def default_abbr_day_names
  localization[:default_abbr_day_names]
end

#default_abbr_month_name(index) ⇒ Object



348
349
350
# File 'lib/tr8n/config.rb', line 348

def default_abbr_month_name(index)
  "" + default_abbr_month_names[index]
end

#default_abbr_month_namesObject



344
345
346
# File 'lib/tr8n/config.rb', line 344

def default_abbr_month_names
  localization[:default_abbr_month_names]
end

#default_applicationObject



295
296
297
# File 'lib/tr8n/config.rb', line 295

def default_application
  @default_application ||= Tr8n::Application.new(:host => Tr8n::ApiClient::API_HOST)
end

#default_date_formatsObject



352
353
354
# File 'lib/tr8n/config.rb', line 352

def default_date_formats
  localization[:custom_date_formats]
end

#default_day_name(index) ⇒ Object



324
325
326
# File 'lib/tr8n/config.rb', line 324

def default_day_name(index)
  "" + default_day_names[index]
end

#default_day_namesObject



320
321
322
# File 'lib/tr8n/config.rb', line 320

def default_day_names
  localization[:default_day_names]
end

#default_languageObject

def default_locale

return Tr8n.session.application.default_locale if Tr8n.session.application
@default_locale

end

def default_level

return Tr8n.session.application.default_level if Tr8n.session.application
@default_level

end



288
289
290
291
292
293
# File 'lib/tr8n/config.rb', line 288

def default_language
  @default_language ||= begin
    file = File.expand_path(File.join(File.dirname(__FILE__), '..', 'tr8n_core', 'languages', "#{Tr8n.config.default_locale}.json"))
    Tr8n::Language.new(JSON.parse(File.read(file)))
  end
end

#default_month_name(index) ⇒ Object



340
341
342
# File 'lib/tr8n/config.rb', line 340

def default_month_name(index)
  "" + default_month_names[index]
end

#default_month_namesObject



336
337
338
# File 'lib/tr8n/config.rb', line 336

def default_month_names
  localization[:default_month_names]
end

#default_token_value(token_name, type = :data, format = :html) ⇒ Object



308
309
310
# File 'lib/tr8n/config.rb', line 308

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



270
271
272
# File 'lib/tr8n/config.rb', line 270

def disabled?
  not enabled?
end

#enabled?Boolean



266
267
268
# File 'lib/tr8n/config.rb', line 266

def enabled?
  enabled
end

#strftime_symbol_to_token(symbol) ⇒ Object

Localization



316
317
318
# File 'lib/tr8n/config.rb', line 316

def strftime_symbol_to_token(symbol)
  localization[:token_mapping][symbol]
end