module Taketo::Support::Inflections

Public Instance Methods

to_class(thing, nesting = Module.nesting.first || Object) click to toggle source
# File lib/taketo/support/inflections.rb, line 19
def to_class(thing, nesting = Module.nesting.first || Object)
  nesting.const_get(class_name_from_string(to_singular(thing)))
end
to_plural(thing) click to toggle source
# File lib/taketo/support/inflections.rb, line 13
def to_plural(thing)
  str = name(thing)
  str << "s" unless plural?(str)
  str.to_sym
end
to_singular(thing) click to toggle source
# File lib/taketo/support/inflections.rb, line 7
def to_singular(thing)
  str = name(thing)
  str.chop! if plural?(str)
  str.to_sym
end

Private Instance Methods

class_name_from_string(singular) click to toggle source
# File lib/taketo/support/inflections.rb, line 33
def class_name_from_string(singular)
  singular.to_s.gsub(/(^|_)\w/) { |s| s.gsub(/_/, "").capitalize }
end
name(thing) click to toggle source
# File lib/taketo/support/inflections.rb, line 25
def name(thing)
  thing.is_a?(Class) ? name_from_class(thing) : thing.to_s
end
name_from_class(klass) click to toggle source
# File lib/taketo/support/inflections.rb, line 29
def name_from_class(klass)
  klass.name.gsub(/[A-Za-z0-9]+::/, "").gsub(/[A-Z][^A-Z]*/) { |s| s.gsub("::", "").downcase + "_" }.chop
end
plural?(str) click to toggle source
# File lib/taketo/support/inflections.rb, line 37
def plural?(str)
  str =~ /s$/
end