lib/shapewear.rb in shapewear-0.1.4 vs lib/shapewear.rb in shapewear-0.1.5
- old
+ new
@@ -22,13 +22,39 @@
end
end
end
end
-# defines String.camelize if it is not defined by, e.g. Rails
-unless ''.respond_to? :camelize
- class String
- def camelize
- self.split('_').map(&:capitalize).join
+# defines String.camelize and String.underscore, if it is not defined by, e.g. Rails
+class String
+ unless ''.respond_to? :camelize
+ def camelize(first_letter_in_uppercase = true)
+ if first_letter_in_uppercase
+ self.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
+ else
+ self.to_s[0].chr.downcase + self.camelize[1..-1]
+ end
+ end
+ end
+
+ unless ''.respond_to? :underscore
+ def underscore
+ word = self.to_s.dup
+ word.gsub!(/::/, '/')
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
+ word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
+ word.tr!("-", "_")
+ word.downcase!
+ word
+ end
+ end
+end
+
+class Object
+ def camelize_if_symbol(first_letter_in_uppercase = true)
+ if is_a?(Symbol) then
+ to_s.camelize(first_letter_in_uppercase)
+ else
+ to_s
end
end
end
\ No newline at end of file