Sha256: 12267bfea3c82a437610d8f2d93417a427376e37666612e7c5bbfb1f467be246

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

module ActiveSupport
  module Inflector
    extend self

    def inflections
      if block_given?
        yield Inflections.instance
      else
        Inflections.instance
      end
    end

    def pluralize(word)
      apply_inflections(word, inflections.plurals)
    end

    def singularize(word)
      apply_inflections(word, inflections.singulars)
    end

    def apply_inflections(word, rules)
      result = word.to_s

      if inflections.uncountables.include?(result.downcase)
        result
      else
        rules.each do |rule, replacement|
          changed = result.sub(rule, replacement)
          unless changed == result
            result = changed
            break
          end
        end

        result
      end
    end

    class Inflections
      def self.instance
        @__instance__ ||= new
      end

      attr_reader :plurals, :singulars, :uncountables

      def initialize
        @plurals, @singulars, @uncountables = [], [], Set.new
      end

      def plural(rule, replacement)
        @plurals.unshift([rule, replacement])
      end

      def singular(rule, replacement)
        @singulars.unshift([rule, replacement])
      end

      def uncountable(words)
        words.each { |w| @uncountables << w.downcase }
      end

      def irregular()

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
opal-activesupport-0.1.0 opal/active_support/inflector/inflections.rb