# # https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb # class String # def pluralize(locale = :en) # word = self # apply_inflections(word, inflections(locale).plurals) # end # def singularize(locale = :en) # word = self # apply_inflections(word, inflections(locale).singulars) # end # def camelize(uppercase_first_letter = true) # term = self # string = term.to_s # if uppercase_first_letter # string = string.sub(/^[a-z\d]*/) { |match| inflections.acronyms[match] || match.capitalize } # else # string = string.sub(/^(?:#{inflections.acronym_regex}(?=\b|[A-Z_])|\w)/) { |match| match.downcase } # end # string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{inflections.acronyms[$2] || $2.capitalize}" } # string.gsub!("/".freeze, "::".freeze) # string # end # def underscore # camel_cased_word = self # return camel_cased_word unless /[A-Z-]|::/.match?(camel_cased_word) # word = camel_cased_word.to_s.gsub("::".freeze, "/".freeze) # word.gsub!(/(?:(?<=([A-Za-z\d]))|\b)(#{inflections.acronym_regex})(?=\b|[^a-z])/) { "#{$1 && '_'.freeze }#{$2.downcase}" } # word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2'.freeze) # word.gsub!(/([a-z\d])([A-Z])/, '\1_\2'.freeze) # word.tr!("-".freeze, "_".freeze) # word.downcase! # word # end # def humanize(options = {}) # result = self.to_s.dup # inflections.humans.each { |(rule, replacement)| break if result.sub!(rule, replacement) } # result.sub!(/\A_+/, "".freeze) # result.sub!(/_id\z/, "".freeze) # result.tr!("_".freeze, " ".freeze) # result.gsub!(/([a-z\d]*)/i) do |match| # "#{inflections.acronyms[match] || match.downcase}" # end # if options.fetch(:capitalize, true) # result.sub!(/\A\w/) { |match| match.upcase } # end # result # end # def upcase_first # string = self # string.length > 0 ? string[0].upcase.concat(string[1..-1]) : "" # end # def titleize # word = self # humanize(underscore(word)).gsub(/\b(? 1 && names.first.blank? # names.inject(Object) do |constant, name| # if constant == Object # constant.const_get(name) # else # candidate = constant.const_get(name) # next candidate if constant.const_defined?(name, false) # next candidate unless Object.const_defined?(name) # # Go down the ancestors to check if it is owned directly. The check # # stops when we reach Object or the end of ancestors tree. # constant = constant.ancestors.inject do |const, ancestor| # break const if ancestor == Object # break ancestor if ancestor.const_defined?(name, false) # const # end # # owner is in Object, so raise # constant.const_get(name, false) # end # end # end # def safe_constantize # camel_cased_word = self # constantize(camel_cased_word) # rescue NameError => e # raise if e.name && !(camel_cased_word.to_s.split("::").include?(e.name.to_s) || # e.name.to_s == camel_cased_word.to_s) # rescue ArgumentError => e # raise unless /not missing constant #{const_regexp(camel_cased_word)}!$/.match?(e.message) # end # def ordinal # number = self # abs_number = number.to_i.abs # if (11..13).include?(abs_number % 100) # "th" # else # case abs_number % 10 # when 1; "st" # when 2; "nd" # when 3; "rd" # else "th" # end # end # end # def ordinalize # number = self # "#{number}#{ordinal(number)}" # end # private # # Mounts a regular expression, returned as a string to ease interpolation, # # that will match part by part the given constant. # # # # const_regexp("Foo::Bar::Baz") # => "Foo(::Bar(::Baz)?)?" # # const_regexp("::") # => "::" # def const_regexp(camel_cased_word) #:nodoc: # parts = camel_cased_word.split("::".freeze) # return Regexp.escape(camel_cased_word) if parts.blank? # last = parts.pop # parts.reverse.inject(last) do |acc, part| # part.blank? ? acc : "#{part}(::#{acc})?" # end # end # # Applies inflection rules for +singularize+ and +pluralize+. # # # # apply_inflections('post', inflections.plurals) # => "posts" # # apply_inflections('posts', inflections.singulars) # => "post" # def apply_inflections(word, rules) # result = word.to_s.dup # if word.blank? || inflections.uncountables.uncountable?(result) # result # else # rules.each { |(rule, replacement)| break if result.sub!(rule, replacement) } # result # end # end # end