### Home Classes Methods

### In Files

- lib/ramaze/helper/formatting.rb

### Methods

- #auto_link
- #autolink
- #nl2br
- #number_counter
- #number_format
- #obfuscate_email
- #ordinal
- #tagcloud
- #time_diff

### Class/Module Index

Quicksearch

- Ramaze
- Ramaze::App
- Ramaze::AppGraph
- Ramaze::Bin
- Ramaze::Bin::Console
- Ramaze::Bin::Create
- Ramaze::Bin::Helper
- Ramaze::Bin::Restart
- Ramaze::Bin::Runner
- Ramaze::Bin::Start
- Ramaze::Bin::Status
- Ramaze::Bin::Stop
- Ramaze::Cache
- Ramaze::Cache::LRU
- Ramaze::Cache::LocalMemCache
- Ramaze::Cache::MemCache
- Ramaze::Cache::Redis
- Ramaze::Cache::Sequel
- Ramaze::Controller
- Ramaze::CoreExtensions
- Ramaze::CoreExtensions::Array
- Ramaze::CoreExtensions::Binding
- Ramaze::CoreExtensions::Kernel
- Ramaze::CoreExtensions::Numeric
- Ramaze::CoreExtensions::Object
- Ramaze::CoreExtensions::Proc
- Ramaze::CoreExtensions::String
- Ramaze::Current
- Ramaze::DefaultController
- Ramaze::Dictionary
- Ramaze::Fiber
- Ramaze::Files
- Ramaze::GemSetup
- Ramaze::Gestalt
- Ramaze::Helper
- Ramaze::Helper::Auth
- Ramaze::Helper::Bench
- Ramaze::Helper::BlueForm
- Ramaze::Helper::BlueForm::Form
- Ramaze::Helper::CSRF
- Ramaze::Helper::Cache
- Ramaze::Helper::Cache::SingletonMethods
- Ramaze::Helper::Disqus
- Ramaze::Helper::Email
- Ramaze::Helper::Erector
- Ramaze::Helper::Flash
- Ramaze::Helper::Formatting
- Ramaze::Helper::Gestalt
- Ramaze::Helper::Gravatar
- Ramaze::Helper::Identity
- Ramaze::Helper::Layout
- Ramaze::Helper::Layout::SingletonMethods
- Ramaze::Helper::Link
- Ramaze::Helper::Localize
- Ramaze::Helper::Localize::Dictionary
- Ramaze::Helper::Localize::Parser
- Ramaze::Helper::Markaby
- Ramaze::Helper::Maruku
- Ramaze::Helper::Paginate
- Ramaze::Helper::Paginate::Paginator
- Ramaze::Helper::Paginate::Paginator::ArrayPager
- Ramaze::Helper::Paginate::Paginator::DataMapperPager
- Ramaze::Helper::Remarkably
- Ramaze::Helper::RequestAccessor
- Ramaze::Helper::SendFile
- Ramaze::Helper::SimpleCaptcha
- Ramaze::Helper::Stack
- Ramaze::Helper::Thread
- Ramaze::Helper::Ultraviolet
- Ramaze::Helper::Upload
- Ramaze::Helper::Upload::ClassMethods
- Ramaze::Helper::Upload::UploadedFile
- Ramaze::Helper::UserHelper
- Ramaze::Helper::UserHelper::Wrapper
- Ramaze::Helper::XHTML
- Ramaze::LRUHash
- Ramaze::Logger
- Ramaze::Logger::Analogger
- Ramaze::Logger::Growl
- Ramaze::Logger::Informer
- Ramaze::Logger::Knotify
- Ramaze::Logger::LogHub
- Ramaze::Logger::Logger
- Ramaze::Logger::RotatingInformer
- Ramaze::Logger::Syslog
- Ramaze::Logger::Xosd
- Ramaze::Logging
- Ramaze::MiddlewareCompiler
- Ramaze::Plugin
- Ramaze::Reloader
- Ramaze::Reloader::Hooks
- Ramaze::Reloader::WatchInotify
- Ramaze::Reloader::WatchStat
- Ramaze::Reloader::Watcher
- Ramaze::Request
- Ramaze::Response
- Ramaze::SpecLogger
- Ramaze::Struct
- Ramaze::View
- Ramaze::View::Erector
- Ramaze::View::Erubis
- Ramaze::View::Ezamar
- Ramaze::View::Gestalt
- Ramaze::View::Haml
- Ramaze::View::Liquid
- Ramaze::View::Liquid::Tag
- Ramaze::View::Lokar
- Ramaze::View::Mustache
- Ramaze::View::Mustache::RamazeContext
- Ramaze::View::Nagoro
- Ramaze::View::Remarkably
- Ramaze::View::Sass
- Ramaze::View::Slippers
- Ramaze::View::Tagz
- Ramaze::View::Tagz::Methods
- Ramaze::View::Tenjin
- Nagoro
- Nagoro::Pipe
- Nagoro::Pipe::RenderPartial
- Bacon
- Bacon::PrettyOutput
- Erector
- Erector::Widget
- Rack
- Rack::RouteExceptions
- Tenjin
- Tenjin::Template
- BlankSlate
- Controller
- Fiber
- FiberError
- MainController
- Object
- OrderedSet
- Thread

No matching classes.

# Ramaze::Helper::Formatting

### Constants

AUTO_LINK_RE

  Copied from actionpack, and revised by insane-dreamer to fix a bug (original fails on some URLs)

FORMATTING_NUMBER_COUNTER

### Public Instance Methods

auto_link(text, opts = {}) click to toggle source

Turns all urls into clickable links. If a block is given, each url is yielded and the result is used as the link text.

    # File lib/ramaze/helper/formatting.rb, line 138
    def auto_link(text, opts = {})
      html_options = ' ' + opts.map{|k,v| "#{k}='#{v}'"}.join(' ') if opts.any?
      text.gsub(AUTO_LINK_RE) do
        all, a, b, c, d = $&, $1, $2, $3, $4
        if a =~ /<a\s/ # don't replace URL's that are already linked
          all
        else
          text = b + c
          text = yield(text) if block_given?
          %(#{a}<a href="#{b=="www."?"http://www.":b}#{c}"#{html_options}>#{text}</a>#{d})
        end
      end
    end

Also aliased as: autolink
autolink(text, opts = {}) click to toggle source
Alias for: auto_link
nl2br(string, xhtml = true) click to toggle source

takes a string and optional argument for outputting compliance HTML instead of XHTML.

@example

    nl2br "a\nb\n\c" #=> 'a<br />b<br />c'

    # File lib/ramaze/helper/formatting.rb, line 159
    def nl2br(string, xhtml = true)
      br = xhtml ? '<br />' : '<br>'
      string.gsub(/\n/, br)
    end

number_counter(count, item, items = nil) click to toggle source

Answers with a representation of given `count` with correct grammar. If no `items` argument is given, and the `count` argument is not 1, then we first check whether the `item` argument responds to "pluralize" (for example if you are using Sequel). If this doesn't work we append 's' to the `item` argument.

@example usage

    number_counter(0, 'comment') # => 'no comments'
    number_counter(1, 'comment') # => 'one comment'
    number_counter(2, 'comment') # => '2 comments'

    # File lib/ramaze/helper/formatting.rb, line 33
    def number_counter(count, item, items = nil)
      count, item = count.to_i, item.to_s

      if count == 1
        "one #{item}"
      else
        items ||= item.respond_to?(:pluralize) ? item.pluralize : "#{item}s"
        prefix = FORMATTING_NUMBER_COUNTER[count] || count
        "#{prefix} #{items}"
      end
    end

number_format(n, delimiter = ',') click to toggle source

Format a floating number nicely for display.

@example

    number_format(123.123)           # => '123.123'
    number_format(123456.12345)      # => '123,456.12345'
    number_format(123456.12345, '.') # => '123.456,12345'

    # File lib/ramaze/helper/formatting.rb, line 52
    def number_format(n, delimiter = ',')
      delim_l, delim_r = delimiter == ',' ? ], .] : ]. ,]
      h, r = n.to_s.split('.')
      [h.reverse.scan(/\d{1,3}/).join(delim_l).reverse, r].compact.join(delim_r)
    end

obfuscate_email(email, text = nil) click to toggle source

    # File lib/ramaze/helper/formatting.rb, line 164
    def obfuscate_email(email, text = nil)
      obfuscated = []
      email.to_s.each_byte{|c| obfuscated << "&#%03d" % c }
      joined = obfuscated.join

      %(<a href="mailto:#{joined}">#{text || joined}</a>)
    end

ordinal(number) click to toggle source

Answer with the ordinal version of a number.

@example

    ordinal(1)   # => "1st"
    ordinal(2)   # => "2nd"
    ordinal(3)   # => "3rd"
    ordinal(13)  # => "13th"
    ordinal(33)  # => "33rd"
    ordinal(100) # => "100th"
    ordinal(133) # => "133rd"

    # File lib/ramaze/helper/formatting.rb, line 69
    def ordinal(number)
      number = number.to_i

      case number % 100
      when 11..13; "#{number}th"
      else
        case number % 10
        when 1; "#{number}st"
        when 2; "#{number}nd"
        when 3; "#{number}rd"
        else    "#{number}th"
        end
      end
    end

tagcloud(tags, min = 0.5, max = 1.5) click to toggle source

Returns Hash with tags as keys and their weight as value.

Example:

    tags = %w[ruby ruby code ramaze]
    tagcloud(tags)
    # => {"code"=>0.75, "ramaze"=>0.75, "ruby"=>1.0}

The weight can be influenced by adjusting the `min` and `max` parameters, please make sure that `max` is larger than `min` to get meaningful output.

This is not thought as immediate output to your template but rather to help either implementing your own algorithm or using the result as input for your tagcloud.

@example

    tagcloud(tags).each do |tag, weight|
      style = "font-size: %0.2fem" % weight
      %a{:style => style, :href => Rs(tag)}= h(tag)
    end

    # File lib/ramaze/helper/formatting.rb, line 193
    def tagcloud(tags, min = 0.5, max = 1.5)
      result = {}
      total = tags.size.to_f
      diff = max - min

      tags.uniq.each do |tag|
        count = tags.respond_to?(:count) ? tags.count(tag) : tags.select{|t| t==tag }.size
        result[tag] = ((count / total) * diff) + min
      end

      result
    end

time_diff(from_time, to_time = Time.now, include_seconds = false) click to toggle source

stolen and adapted from rails

    # File lib/ramaze/helper/formatting.rb, line 85
    def time_diff(from_time, to_time = Time.now, include_seconds = false)
      distance_in_minutes = (((to_time - from_time).abs)/60).round
      distance_in_seconds = ((to_time - from_time).abs).round if include_seconds

      case distance_in_minutes
        when 0..1
          return (distance_in_minutes == 0) ? 'less than a minute' : '1 minute' unless include_seconds
          case distance_in_seconds
            when 0..4   then 'less than 5 seconds'
            when 5..9   then 'less than 10 seconds'
            when 10..19 then 'less than 20 seconds'
            when 20..39 then 'half a minute'
            when 40..59 then 'less than a minute'
            else             '1 minute'
          end

        when 2..44           then "#{distance_in_minutes} minutes"
        when 45..89          then 'about 1 hour'
        when 90..1439        then "about #{(distance_in_minutes.to_f / 60.0).round} hours"
        when 1440..2879      then '1 day'
        when 2880..43199     then "#{(distance_in_minutes / 1440).round} days"
        when 43200..86399    then 'about 1 month'
        when 86400..525959   then "#{(distance_in_minutes / 43200).round} months"
        when 525960..1051919 then 'about 1 year'
        else                      "over #{(distance_in_minutes / 525960).round} years"
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.