class SigtermExtensions::Inflector
inflector
@since 0.1.0
Constants
- DEFAULT_SEPARATOR
@since 0.1.2 @api private
- ORDINALIZE_TH
@since 0.1.0 @api private
- VERSION
@since 0.1.0
Attributes
Public Class Methods
Instantiate the inflector
@param blk [Proc] an optional block to specify custom inflection rules @yieldparam [SigtermExtensions::Inflector::Inflections] the inflection rules
@return [SigtermExtensions::Inflector] the inflector
@since 0.1.0
@example Basic usage
require "sigterm_extensions/inflector" inflector = SigtermExtensions::Inflector.new
@example Custom inflection rules
require "sigterm_extensions/inflector" inflector = SigtermExtensions::Inflector.new do |inflections| inflections.plural "virus", "viruses" # specify a rule for #pluralize inflections.singular "thieves", "thief" # specify a rule for #singularize inflections.uncountable "inflector" # add an exception for an uncountable word end
# File lib/sigterm_extensions/inflector.rb, line 33 def initialize(&blk) @inflections = Inflections.build(&blk) end
Public Instance Methods
Lower camelize a string
@param input [String,Symbol] the input @return [String] the lower camelized string
@since 0.1.3
@example
require "sigterm_extensions/inflector" inflector = SigtermExtensions::Inflector.new inflector.camelize_lower("data_mapper") # => "dataMapper"
# File lib/sigterm_extensions/inflector.rb, line 49 def camelize_lower(input) internal_camelize(input, false) end
Upper camelize a string
@param input [String,Symbol] the input @return [String] the upper camelized string
@since 0.1.3
@example
require "sigterm_extensions/inflector" inflector = SigtermExtensions::Inflector.new inflector.camelize_upper("data_mapper") # => "DataMapper" inflector.camelize_upper("sigterm_extensions/inflector") # => "SigtermExtensions::Inflector"
# File lib/sigterm_extensions/inflector.rb, line 66 def camelize_upper(input) internal_camelize(input, true) end
Classify a string
@param input [String,Symbol] the input @return [String] the classified string
@since 0.1.0
@example
require "sigterm_extensions/inflector" inflector = SigtermExtensions::Inflector.new inflector.classify("books") # => "Book"
# File lib/sigterm_extensions/inflector.rb, line 105 def classify(input) camelize(singularize(input.to_s.sub(/.*\./, ""))) end
Find a constant with the name specified in the argument string
The name is assumed to be the one of a top-level constant, constant scope of caller is ignored
@param input [String,Symbol] the input @return [Class, Module] the class or module
@since 0.1.0
@example
require "sigterm_extensions/inflector" inflector = SigtermExtensions::Inflector.new inflector.constantize("Module") # => Module inflector.constantize("SigtermExtensions::Inflector") # => SigtermExtensions::Inflector
# File lib/sigterm_extensions/inflector.rb, line 89 def constantize(input) Object.const_get(input) end
Dasherize a string
@param input [String,Symbol] the input @return [String] the dasherized string
@since 0.1.0
@example
require "sigterm_extensions/inflector" inflector = SigtermExtensions::Inflector.new inflector.dasherize("sigterm_extensions_inflector") # => "sigterm-extensions-inflector"
# File lib/sigterm_extensions/inflector.rb, line 121 def dasherize(input) input.to_s.tr("_", "-") end
Demodulize a string
@param input [String,Symbol] the input @return [String] the demodulized string
@since 0.1.0
@example
require "sigterm_extensions/inflector" inflector = SigtermExtensions::Inflector.new inflector.demodulize("SigtermExtensions::Inflector") # => "Inflector"
# File lib/sigterm_extensions/inflector.rb, line 137 def demodulize(input) input.to_s.split("::").last end
Creates a foreign key name
@param input [String, Symbol] the input @return [String] foreign key
@example
require "sigterm_extensions/inflector" inflector = SigtermExtensions::Inflector.new inflector.foreign_key("Message") => "message_id"
# File lib/sigterm_extensions/inflector.rb, line 176 def foreign_key(input) "#{underscore(demodulize(input))}_id" end
Humanize a string
@param input [String,Symbol] the input @return [String] the humanized string
@since 0.1.0
@example
require "sigterm_extensions/inflector" inflector = SigtermExtensions::Inflector.new inflector.humanize("dry_inflector") # => "SigtermExtensions inflector" inflector.humanize("author_id") # => "Author"
# File lib/sigterm_extensions/inflector.rb, line 154 def humanize(input) input = input.to_s result = inflections.humans.apply_to(input) result.chomp!("_id") result.tr!("_", " ") match = /(?<separator>\W)/.match(result) separator = match ? match[:separator] : DEFAULT_SEPARATOR result.split(separator).map.with_index { |word, index| inflections.acronyms.apply_to(word, index.zero?) }.join(separator) end
Ordinalize a number
@param number [Integer] the input @return [String] the ordinalized number
@since 0.1.0
@example
require "sigterm_extensions/inflector" inflector = SigtermExtensions::Inflector.new inflector.ordinalize(1) # => "1st" inflector.ordinalize(2) # => "2nd" inflector.ordinalize(3) # => "3rd" inflector.ordinalize(10) # => "10th" inflector.ordinalize(23) # => "23rd"
# File lib/sigterm_extensions/inflector.rb, line 196 def ordinalize(number) # rubocop:disable Metrics/MethodLength abs_value = number.abs if ORDINALIZE_TH.key?(abs_value % 100) "#{number}th" else case abs_value % 10 when 1 then "#{number}st" when 2 then "#{number}nd" when 3 then "#{number}rd" else "#{number}th" end end end
Pluralize a string
@param input [String,Symbol] the input @return [String] the pluralized string
@since 0.1.0
@example
require "sigterm_extensions/inflector" inflector = SigtermExtensions::Inflector.new inflector.pluralize("book") # => "books" inflector.pluralize("money") # => "money"
# File lib/sigterm_extensions/inflector.rb, line 224 def pluralize(input) input = input.to_s return input if uncountable?(input) inflections.plurals.apply_to(input) end
Singularize a string
@param input [String] the input @return [String] the singularized string
@since 0.1.0
@example
require "sigterm_extensions/inflector" inflector = SigtermExtensions::Inflector.new inflector.singularize("books") # => "book" inflector.singularize("money") # => "money"
# File lib/sigterm_extensions/inflector.rb, line 243 def singularize(input) input = input.to_s return input if uncountable?(input) inflections.singulars.apply_to(input) end
Tableize a string
@param input [String,Symbol] the input @return [String] the tableized string
@since 0.1.0
@example
require "sigterm_extensions/inflector" inflector = SigtermExtensions::Inflector.new inflector.tableize("Book") # => "books"
# File lib/sigterm_extensions/inflector.rb, line 261 def tableize(input) input = input.to_s.gsub(/::/, "_") pluralize(underscore(input)) end
@return [String]
@since 0.2.0 @api public
# File lib/sigterm_extensions/inflector.rb, line 307 def to_s '#<SigtermExtensions::Inflector>' end
Check if the input is an uncountable word
@param input [String] the input @return [TrueClass,FalseClass] the result of the check
@since 0.1.0 @api private
# File lib/sigterm_extensions/inflector.rb, line 299 def uncountable?(input) !(input =~ /\A[[:space:]]*\z/).nil? || inflections.uncountables.include?(input.downcase) end
Underscore a string
@param input [String,Symbol] the input @return [String] the underscored string
@since 0.1.0
@example
require "sigterm_extensions/inflector" inflector = SigtermExtensions::Inflector.new inflector.underscore("sigterm-extensions-inflector") # => "sigterm_extensions_inflector"
# File lib/sigterm_extensions/inflector.rb, line 278 def underscore(input) input = input.to_s.gsub("::", "/") input.gsub!(inflections.acronyms.regex) do m1 = Regexp.last_match(1) m2 = Regexp.last_match(2) "#{m1 ? '_' : '' }#{m2.downcase}" end input.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2') input.gsub!(/([a-z\d])([A-Z])/, '\1_\2') input.tr!("-", "_") input.downcase! input end
Private Instance Methods
@since 0.1.3 @api private
# File lib/sigterm_extensions/inflector.rb, line 326 def internal_camelize(input, upper) input = input.to_s.dup input.sub!(/^[a-z\d]*/) { |match| inflections.acronyms.apply_to(match, upper) } input.gsub!(%r{(?:_|(/))([a-z\d]*)}i) do m1 = Regexp.last_match(1) m2 = Regexp.last_match(2) "#{m1}#{inflections.acronyms.apply_to(m2)}" end input.gsub!("/", "::") input end