deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@apache.org
Subject git commit: Core: re-use core_ext methods in String if they exists (DTACLOUD-506)
Date Tue, 12 Mar 2013 10:08:48 GMT
Updated Branches:
  refs/heads/master f5f2ee0b0 -> ea132e764


Core: re-use core_ext methods in String if they exists (DTACLOUD-506)


Project: http://git-wip-us.apache.org/repos/asf/deltacloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltacloud/commit/ea132e76
Tree: http://git-wip-us.apache.org/repos/asf/deltacloud/tree/ea132e76
Diff: http://git-wip-us.apache.org/repos/asf/deltacloud/diff/ea132e76

Branch: refs/heads/master
Commit: ea132e764d5c81c34bd160ad7c747985006640f8
Parents: f5f2ee0
Author: Michal Fojtik <mfojtik@redhat.com>
Authored: Thu Mar 7 12:29:53 2013 +0100
Committer: Michal fojtik <mfojtik@redhat.com>
Committed: Tue Mar 12 11:08:33 2013 +0100

----------------------------------------------------------------------
 server/lib/deltacloud/core_ext/array.rb   |    1 +
 server/lib/deltacloud/core_ext/integer.rb |   22 ++++---
 server/lib/deltacloud/core_ext/string.rb  |   73 +++++++++++++++---------
 3 files changed, 59 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/ea132e76/server/lib/deltacloud/core_ext/array.rb
----------------------------------------------------------------------
diff --git a/server/lib/deltacloud/core_ext/array.rb b/server/lib/deltacloud/core_ext/array.rb
index 232b216..8931b68 100644
--- a/server/lib/deltacloud/core_ext/array.rb
+++ b/server/lib/deltacloud/core_ext/array.rb
@@ -14,6 +14,7 @@
 # under the License.
 
 class Array
+
   def expand_opts!(more_opts)
     self << {} unless last.is_a?(Hash)
     last.update(more_opts)

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/ea132e76/server/lib/deltacloud/core_ext/integer.rb
----------------------------------------------------------------------
diff --git a/server/lib/deltacloud/core_ext/integer.rb b/server/lib/deltacloud/core_ext/integer.rb
index dba4d56..d73fcf9 100644
--- a/server/lib/deltacloud/core_ext/integer.rb
+++ b/server/lib/deltacloud/core_ext/integer.rb
@@ -15,17 +15,21 @@
 # under the License.
 
 class Integer
+
   # Turn integers into strings +1st+, +2nd+, +3rd+ etc.
-  def ordinalize
-    if (11..13).include?(self % 100)
-      "#{self}th"
-    else
-      case self % 10
-      when 1; "#{self}st"
-      when 2; "#{self}nd"
-      when 3; "#{self}rd"
-      else    "#{self}th"
+  unless method_defined? 'ordinalize'
+    def ordinalize
+      if (11..13).include?(self % 100)
+        "#{self}th"
+      else
+        case self % 10
+        when 1; "#{self}st"
+        when 2; "#{self}nd"
+        when 3; "#{self}rd"
+        else    "#{self}th"
+        end
       end
     end
   end
+
 end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/ea132e76/server/lib/deltacloud/core_ext/string.rb
----------------------------------------------------------------------
diff --git a/server/lib/deltacloud/core_ext/string.rb b/server/lib/deltacloud/core_ext/string.rb
index 6838d6d..f4c45e5 100644
--- a/server/lib/deltacloud/core_ext/string.rb
+++ b/server/lib/deltacloud/core_ext/string.rb
@@ -15,10 +15,13 @@
 # under the License.
 
 class String
+
   # Rails defines this for a number of other classes, including Object
   # see activesupport/lib/active_support/core_ext/object/blank.rb
-  def blank?
+  unless method_defined? 'blank?'
+    def blank?
       self !~ /\S/
+    end
   end
 
   # Title case.
@@ -28,49 +31,63 @@ class String
   #
   # CREDIT: Eliazar Parra
   # Copied from facets
-  def titlecase
-    gsub(/\b\w/){ $`[-1,1] == "'" ? $& : $&.upcase }
+  unless method_defined? 'titlecase'
+    def titlecase
+      gsub(/\b\w/){ $`[-1,1] == "'" ? $& : $&.upcase }
+    end
   end
 
-  def pluralize
-    return self + 'es' if self =~ /ess$/
-    return self[0, self.length-1] + "ies" if self =~ /ty$/
-    return self if self =~ /data$/
-    self + "s"
+  unless method_defined? 'pluralize'
+    def pluralize
+      return self + 'es' if self =~ /ess$/
+      return self[0, self.length-1] + "ies" if self =~ /ty$/
+      return self if self =~ /data$/
+      self + "s"
+    end
   end
 
-  def singularize
-    return self.gsub(/ies$/, 'y') if self =~ /ies$/
-    return self.gsub(/es$/, '') if self =~ /sses$/
-    self.gsub(/s$/, '')
+  unless method_defined? 'singularize'
+    def singularize
+      return self.gsub(/ies$/, 'y') if self =~ /ies$/
+      return self.gsub(/es$/, '') if self =~ /sses$/
+      self.gsub(/s$/, '')
+    end
   end
 
-  def underscore
-    return self.downcase if self =~ /VSPs$/i
-    gsub(/::/, '/').
-    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
-    gsub(/([a-z\d])([A-Z])/,'\1_\2').
-    tr("-", "_").
-    downcase
+  unless method_defined? 'underscore'
+    def underscore
+      return self.downcase if self =~ /VSPs$/i
+      gsub(/::/, '/').
+        gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
+        gsub(/([a-z\d])([A-Z])/,'\1_\2').
+        tr("-", "_").
+        downcase
+    end
   end
 
-  def camelize(lowercase_first_letter=nil)
-    s = split('_').map { |w| w.capitalize }.join
-    lowercase_first_letter ? s.uncapitalize : s
+  unless method_defined? 'camelize'
+    def camelize(lowercase_first_letter=nil)
+      s = split('_').map { |w| w.capitalize }.join
+      lowercase_first_letter ? s.uncapitalize : s
+    end
   end
 
-  def uncapitalize
-    self[0, 1].downcase + self[1..-1]
+  unless method_defined? 'uncapitalize'
+    def uncapitalize
+      self[0, 1].downcase + self[1..-1]
+    end
   end
 
   def upcase_first
     self[0, 1].upcase + self[1..-1]
   end
 
-  def truncate(length = 10)
-    return self if self.length <= length
-    end_string = "...#{self[(self.length-(length/2))..self.length]}"
-    "#{self[0..(length/2)]}#{end_string}"
+  unless method_defined? 'truncate'
+    def truncate(length = 10)
+      return self if self.length <= length
+      end_string = "...#{self[(self.length-(length/2))..self.length]}"
+      "#{self[0..(length/2)]}#{end_string}"
+    end
   end
 
   def remove_matrix_params


Mime
View raw message