Sha256: 7dc75b87f130f1b77a59060327652abdc7fa6ac334a6051e3109e7a0ef61caa0

Contents?: true

Size: 1 KB

Versions: 6

Compression:

Stored size: 1 KB

Contents

module SinatraMore
  module FormatHelpers
    def relative_time_ago(date)
      date = date.to_date
      date = Date.parse(date, true) unless /Date.*/ =~ date.class.to_s
      days = (date - Date.today).to_i

      return 'today'     if days >= 0 and days < 1
      return 'tomorrow'  if days >= 1 and days < 2
      return 'yesterday' if days >= -1 and days < 0

      return "in #{days} days"      if days.abs < 60 and days > 0
      return "#{days.abs} days ago" if days.abs < 60 and days < 0

      return date.strftime('%A, %B %e') if days.abs < 182
      return date.strftime('%A, %B %e, %Y')
    end

    def escape_javascript(javascript)
      return '' unless javascript
      javascript_mapping = { '\\' => '\\\\', '</' => '<\/', "\r\n" => '\n', "\n" => '\n' }
      javascript_mapping.merge("\r" => '\n', '"' => '\\"', "'" => "\\'")
      escaped_string = javascript.gsub(/(\\|<\/|\r\n|[\n\r"'])/) { javascript_mapping[$1] }
      "\"#{escaped_string}\""
    end

    alias js_escape escape_javascript

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
sinatra_more-0.0.7 lib/sinatra_more/markup_plugin/format_helpers.rb
sinatra_more-0.0.6 lib/sinatra_more/markup_plugin/format_helpers.rb
sinatra_more-0.0.5 lib/sinatra_more/markup_plugin/format_helpers.rb
sinatra_more-0.0.4 lib/sinatra_more/view_helpers/format_helpers.rb
sinatra_more-0.0.3 lib/sinatra_more/view_helpers/format_helpers.rb
sinatra_more-0.0.2 lib/sinatra_more/view_helpers/format_helpers.rb