module Mobility::Util

Some useful methods on strings, borrowed in parts from Sequel and ActiveSupport.

@example With no methods defined on String

"foos".respond_to?(:singularize)
#=> false

class A
  include Mobility::Util
end

A.new.singularize("foos")
#=> "foo"
A.new.singularize("bunnies")
#=> "bunnie"

@example With methods on String

require "active_support"
"foos".respond_to?(:singularize)
#=> true

class A
  include Mobility::Util
end

A.new.singularize("bunnies")
#=> "bunny"

Constants

VALID_CONSTANT_NAME_REGEXP

Public Class Methods

included(klass) click to toggle source
# File lib/mobility/util.rb, line 36
def self.included(klass)
  klass.extend(self)
end

Public Instance Methods

blank?(object) click to toggle source
# File lib/mobility/util.rb, line 103
def blank?(object)
  return true if object.nil?
  object.respond_to?(:empty?) ? !!object.empty? : !object
end
camelize(str) click to toggle source

Converts strings to UpperCamelCase. @param [String] str @return [String]

# File lib/mobility/util.rb, line 43
def camelize(str)
  call_or_yield str do
    str.to_s.sub(/^[a-z\d]*/) { $&.capitalize }.gsub(/(?:_|(\/))([a-z\d]*)/) { "#{$1}#{$2.capitalize}" }.gsub('/', '::')
  end
end
constantize(str) click to toggle source

Tries to find a constant with the name specified in the argument string. @param [String] str @return [Object]

# File lib/mobility/util.rb, line 52
def constantize(str)
  str = str.to_s
  call_or_yield str do
    raise(NameError, "#{s.inspect} is not a valid constant name!") unless m = VALID_CONSTANT_NAME_REGEXP.match(str)
    Object.module_eval("::#{m[1]}", __FILE__, __LINE__)
  end
end
demodulize(str) click to toggle source

Removes the module part from the expression in the string. @param [String] str @return [String]

# File lib/mobility/util.rb, line 74
def demodulize(str)
  call_or_yield str do
    str.to_s.gsub(/^.*::/, '')
  end
end
foreign_key(str) click to toggle source

Creates a foreign key name from a class name @param [String] str @return [String]

# File lib/mobility/util.rb, line 83
def foreign_key(str)
  call_or_yield str do
    "#{underscore(demodulize(str))}_id"
  end
end
presence(object) click to toggle source
# File lib/mobility/util.rb, line 108
def presence(object)
  object if present?(object)
end
present?(object) click to toggle source
# File lib/mobility/util.rb, line 99
def present?(object)
  !blank?(object)
end
singularize(str) click to toggle source

Returns the singular form of a word in a string. @param [String] str @return [String] @note If singularize is not defined on String, falls back to simply

stripping the trailing 's' from the string.
# File lib/mobility/util.rb, line 65
def singularize(str)
  call_or_yield str do
    str.to_s.gsub(/s$/, '')
  end
end
underscore(str) click to toggle source

Makes an underscored, lowercase form from the expression in the string. @param [String] str @return [String]

# File lib/mobility/util.rb, line 92
def underscore(str)
  call_or_yield str do
    str.to_s.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
      gsub(/([a-z\d])([A-Z])/,'\1_\2').tr("-", "_").downcase
  end
end

Private Instance Methods

call_or_yield(object) { || ... } click to toggle source

Calls caller method on object if defined, otherwise yields to block

# File lib/mobility/util.rb, line 115
def call_or_yield(object)
  caller_method = caller_locations(1,1)[0].label
  if object.respond_to?(caller_method)
    object.public_send(caller_method)
  else
    yield
  end
end