Sha256: a595c5e1a622c1a1776f89acede47dd0110dcecfa520b6f7f260f712db3430c7

Contents?: true

Size: 1.88 KB

Versions: 15

Compression:

Stored size: 1.88 KB

Contents

# used both in app and mailer, so we put them here

module Snails

  module RelativeTime

    def self.minutes_in_words(min)
      case min.to_i
        when 0..1            then 'un minuto'
        when 2..4            then '5 minutos'
        when 5..14           then '15 minutos'
        when 15..29          then "media hora"
        when 30..59          then "#{min} min"
        when 60..1439        then "#{(min/60).floor} horas"
        when 1440..11519     then "#{(min/1440).floor} días"
        when 11520..43199    then "#{(min/11520).floor} semanas"
        when 43200..525599   then "#{(min/43200).floor} meses"
        else                      "#{(min/525600).floor} años"
      end
    end

    def in_words
      minutes = (((Time.now - self).abs)/60).round
      return nil if minutes < 0
      RelativeTime.minutes_in_words(minutes)
    end

    def relative
      if str = in_words
        if Time.now < self
          # "#{str} más"
          "en #{str}"
        else
          "hace #{str}"
        end
      end
    end

  end

  module SimpleFormat

    def tag(name, options = nil, open = false)
      attributes = tag_attributes(options)
      "<#{name}#{attributes}#{open ? '>' : ' />'}"
    end

    def tag_attributes(options)
      return '' unless options
      options.inject('') do |all,(key,value)|
        next all unless value
        all << ' ' if all.empty?
        all << %(#{key}="#{value}" )
      end.chomp!(' ')
    end

    def simple_format(text, options = {})
      t = options.delete(:tag) || :p
      start_tag = tag(t, options, true)
      text = text.to_s.dup
      text.gsub!(/\r\n?/, "\n")                      # \r\n and \r -> \n
      text.gsub!(/\n\n+/, "</#{t}>\n\n#{start_tag}") # 2+ newline  -> paragraph
      text.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />')   # 1 newline   -> br
      text.insert 0, start_tag
      text << "</#{t}>"
      text
    end

  end

end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
snails-0.9.0 lib/snails/util.rb
snails-0.8.1 lib/snails/util.rb
snails-0.8.0 lib/snails/util.rb
snails-0.7.0 lib/snails/util.rb
snails-0.6.1 lib/snails/util.rb
snails-0.6.0 lib/snails/util.rb
snails-0.5.6 lib/snails/util.rb
snails-0.5.5 lib/snails/util.rb
snails-0.5.4 lib/snails/util.rb
snails-0.5.3 lib/snails/util.rb
snails-0.5.2 lib/snails/util.rb
snails-0.5.1 lib/snails/util.rb
snails-0.5.0 lib/snails/util.rb
snails-0.4.3 lib/snails/util.rb
snails-0.4.2 lib/snails/util.rb