Module: Mobility::Util

Extended by:
Util
Included in:
Backends::Sequel::KeyValue, Util
Defined in:
lib/mobility/util.rb

Overview

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

Examples:

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"

With methods on String

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

class A
  include Mobility::Util
end

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

Constant Summary collapse

VALID_CONSTANT_NAME_REGEXP =
/\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



36
37
38
# File 'lib/mobility/util.rb', line 36

def self.included(klass)
  klass.extend(self)
end

Instance Method Details

#blank?(object) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
106
# 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) ⇒ String

Converts strings to UpperCamelCase.

Parameters:

  • str (String)

Returns:

  • (String)


43
44
45
46
47
# 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) ⇒ Object

Tries to find a constant with the name specified in the argument string.

Parameters:

  • str (String)

Returns:

  • (Object)


52
53
54
55
56
57
58
# 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) ⇒ String

Removes the module part from the expression in the string.

Parameters:

  • str (String)

Returns:

  • (String)


74
75
76
77
78
# File 'lib/mobility/util.rb', line 74

def demodulize(str)
  call_or_yield str do
    str.to_s.gsub(/^.*::/, '')
  end
end

#foreign_key(str) ⇒ String

Creates a foreign key name from a class name

Parameters:

  • str (String)

Returns:

  • (String)


83
84
85
86
87
# File 'lib/mobility/util.rb', line 83

def foreign_key(str)
  call_or_yield str do
    "#{underscore(demodulize(str))}_id"
  end
end

#presence(object) ⇒ Object



108
109
110
# File 'lib/mobility/util.rb', line 108

def presence(object)
  object if present?(object)
end

#present?(object) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/mobility/util.rb', line 99

def present?(object)
  !blank?(object)
end

#singularize(str) ⇒ String

Note:

If singularize is not defined on String, falls back to simply stripping the trailing ā€˜sā€™ from the string.

Returns the singular form of a word in a string.

Parameters:

  • str (String)

Returns:

  • (String)


65
66
67
68
69
# File 'lib/mobility/util.rb', line 65

def singularize(str)
  call_or_yield str do
    str.to_s.gsub(/s$/, '')
  end
end

#underscore(str) ⇒ String

Makes an underscored, lowercase form from the expression in the string.

Parameters:

  • str (String)

Returns:

  • (String)


92
93
94
95
96
97
# 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