File: ordinal.rb

package info (click to toggle)
ruby-stamp 0.6.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 220 kB
  • sloc: ruby: 371; makefile: 4
file content (31 lines) | stat: -rw-r--r-- 731 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
module Stamp
  module Emitters
    class Ordinal
      attr_reader :field

      # @param [field] the field to be formatted (e.g. +:month+, +:year+)
      def initialize(field)
        @field = field
      end

      def format(target)
        ordinalize(target.send(field))
      end

      # Cribbed from ActiveSupport::Inflector
      # https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
      def ordinalize(number)
        number.to_s + if (11..13).include?(number % 100)
          'th'
        else
          case number % 10
            when 1; 'st'
            when 2; 'nd'
            when 3; 'rd'
            else    'th'
          end
        end
      end
    end
  end
end