class Pettanr # Foo.class -> Pettanr[@constructor.name] @is_blank: (str) -> !str?.trim() @is_a_string: (str) -> if (str instanceof String) or (typeof str == "string") true else false @is_number: (str) -> if typeof(str) != 'number' && typeof(str) != 'string' return false else n = if typeof(str) == 'string' parseInt(str) else str isFinite(n) @to_i: (str) -> if @is_a_string(str) parseInt(str) else if @is_number(str) Math.floor(str) else 0 @to_s: (str) -> if @is_a_string(str) str else if @is_number(str) str.toString() else '' @truncate: (str, length = null) -> s = Pettanr.to_s(str) if length and s.length > 3 # 3 = '...' r = s.substr(0, length - 4) r = r.concat( '...') if s.length > length - 3 r else Pettanr.to_s(str) @format_date: (date) -> y = ('000' + (date.getFullYear())).slice(-4) m = ('0' + (date.getMonth() + 1)).slice(-2) d = ('0' + date.getDate()).slice(-2) h = ('0' + date.getHours()).slice(-2) n = ('0' + date.getMinutes()).slice(-2) s = ('0' + date.getSeconds()).slice(-2) y + '/' + m + '/' + d + ' ' + h + ':' + n + ':' + s @to_style: (hash) -> a = _.map hash, (n, k) -> Pettanr.to_s(k) + ': ' + Pettanr.to_s(n) a.join('; ') @url: (controller_name, action_name, params) -> if controller = Manifest.manifest().controllers[controller_name] # convert from manifest action = controller.actions[action_name] action.url(params) else # has no manifest controller_name + '/' + action_name @params_to_url: (params) -> return '' if params['controller'] == 'folders' and params['action'] == 'root' @url(params['controller'], params['action'], params) @is_sns: () -> Manifest.manifest().magic_numbers.run_mode != 0 class Pettanr.AppHelper @manifest: () -> Manifest.manifest @full_url: (filename) -> request.protocol + request.host_with_port + filename @t_m: (label) -> l = label.split('.') if l.length > 2 label else if l.length == 1 I18n.t('activerecord.models.' + Pettanr[label].item_name()) else Pettanr.AppHelper.t_a(Pettanr[l[0]].item_name(), l[1]) @t_a: (item_name, attr_name) -> I18n.t('activerecord.attributes.' + item_name + '.' + attr_name) @t_select_items: (items) -> _.map items, (i) -> [I18n.t(i[0]), i[1]] @t_selected_item: (name, index) -> I18n.t(Manifest.manifest().system_resources.select_items[name][index][0]) @distance_of_time_in_words_to_now: (datetime) -> if Pettanr.is_blank(datetime) '' else $.timeago(datetime) @cache = null @credits = {} #>> https://gist.github.com/davidjbeveridge/3813724 @xeach: (arr, func, index=0) -> if index < arr.length then [ func(arr[index], index), @xeach(arr, func, index + 1)... ] else [] @camelize: (input) -> pieces = input.split(/[\W_-]/) @xeach(pieces, @capitalize).join("") @capitalize: (input) -> input.charAt(0).toUpperCase() + input.slice(1) @lowercase: (input) -> input.toLowerCase() @underscore: (input) -> pieces = input.replace(/([A-Z])/g, '_$1').split(/[\W_-]/).filter (n) -> !!n @xeach(pieces, @lowercase ).join("_") # << @Pettanr = Pettanr