Sha256: 2efed9d7ee633cc93583c578f0734ac96cfe4c3b74c0305f92cd0fefeb43e16a

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

module Rich
  module Pluralization  
    module Inflector
      extend self
    
      def inflections(locale = nil)
        Inflections.instance.locale = locale
        if block_given?
          yield Inflections.instance
        else
          Inflections.instance
        end
      end
    
      def singularize(word)
        inflect :singulars, word
      end
    
      def pluralize(word, count = nil)
        count == 1 ? singularize(word) : inflect(:plurals, word)
      end
    
    private
    
      [:singulars, :plurals, :irregulars, :uncountables].each do |type|
        define_method type do
          (Inflections.instance.send(type)[I18n.locale] || (type == :uncountables ? [] : {}))
        end
      end
      
      def inflect(type, word)
        return word if uncountable?(word) or irregular_counterpart?(type, word)
      
        if irregular = irregulars[word.downcase]
          return irregular.cp_case(word)
        end
      
        send(type).each do |inflection|
          if result = inflection.inflect!(word)
            return result.cp_case(word)
          end
        end
      
        word
      end
    
      def uncountable?(word)
        uncountables.include?(word.downcase)
      end
    
      def irregular_counterpart?(type, word)
        irregulars.send({:singulars => :keys, :plurals => :values}[type]).include?(word.downcase)
      end
    
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rich_pluralization-1.0.3 lib/rich/pluralization/inflector.rb
rich_pluralization-1.0.2 lib/rich/pluralization/inflector.rb