Module: Mobility::Util
Overview
Some useful methods on strings, borrowed in parts from Sequel and ActiveSupport.
Constant Summary collapse
- VALID_CONSTANT_NAME_REGEXP =
/\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #blank?(object) ⇒ Boolean
-
#camelize(str) ⇒ String
Converts strings to UpperCamelCase.
-
#constantize(str) ⇒ Object
Tries to find a constant with the name specified in the argument string.
-
#demodulize(str) ⇒ String
Removes the module part from the expression in the string.
-
#foreign_key(str) ⇒ String
Creates a foreign key name from a class name.
- #presence(object) ⇒ Object
- #present?(object) ⇒ Boolean
-
#singularize(str) ⇒ String
Returns the singular form of a word in a string.
-
#underscore(str) ⇒ String
Makes an underscored, lowercase form from the expression in the string.
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
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.
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.
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.
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
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
99 100 101 |
# File 'lib/mobility/util.rb', line 99 def present?(object) !blank?(object) end |
#singularize(str) ⇒ String
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.
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.
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 |