Return-Path: Delivered-To: apmail-incubator-deltacloud-dev-archive@minotaur.apache.org Received: (qmail 76552 invoked from network); 19 Mar 2011 13:18:04 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 19 Mar 2011 13:18:04 -0000 Received: (qmail 35144 invoked by uid 500); 19 Mar 2011 13:18:03 -0000 Delivered-To: apmail-incubator-deltacloud-dev-archive@incubator.apache.org Received: (qmail 35125 invoked by uid 500); 19 Mar 2011 13:18:03 -0000 Mailing-List: contact deltacloud-dev-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: deltacloud-dev@incubator.apache.org Delivered-To: mailing list deltacloud-dev@incubator.apache.org Received: (qmail 35112 invoked by uid 99); 19 Mar 2011 13:18:03 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 19 Mar 2011 13:18:03 +0000 X-ASF-Spam-Status: No, hits=0.3 required=5.0 tests=RCVD_IN_DNSWL_LOW,SPF_SOFTFAIL,T_FRT_BELOW2 X-Spam-Check-By: apache.org Received-SPF: softfail (nike.apache.org: transitioning domain of mi@mifo.sk does not designate 209.85.214.47 as permitted sender) Received: from [209.85.214.47] (HELO mail-bw0-f47.google.com) (209.85.214.47) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 19 Mar 2011 13:17:53 +0000 Received: by bwz10 with SMTP id 10so3782097bwz.6 for ; Sat, 19 Mar 2011 06:17:33 -0700 (PDT) Received: by 10.204.19.14 with SMTP id y14mr1908100bka.187.1300540652748; Sat, 19 Mar 2011 06:17:32 -0700 (PDT) Received: from [192.168.1.125] (141.51.broadband13.iol.cz [90.180.51.141]) by mx.google.com with ESMTPS id t1sm2873883bkx.19.2011.03.19.06.17.30 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 19 Mar 2011 06:17:31 -0700 (PDT) Subject: Re: [PATCH 1/4] core_ext: move String enhancements out of rabbit; add Integer.ordinalize Mime-Version: 1.0 (Apple Message framework v1082) Content-Type: text/plain; charset=us-ascii From: Michal Fojtik In-Reply-To: <1300492994-16194-2-git-send-email-lutter@redhat.com> Date: Sat, 19 Mar 2011 14:17:28 +0100 Content-Transfer-Encoding: quoted-printable Message-Id: <069BB439-05FD-479D-B829-6F1E63856587@mifo.sk> References: <1300492994-16194-1-git-send-email-lutter@redhat.com> <1300492994-16194-2-git-send-email-lutter@redhat.com> To: deltacloud-dev@incubator.apache.org X-Mailer: Apple Mail (2.1082) X-Virus-Checked: Checked by ClamAV on apache.org On Mar 19, 2011, at 1:03 AM, lutter@redhat.com wrote: ACK, with small inline comment bellow. -- Michal > From: David Lutterkort >=20 > It is much cleaner to keep monkey patches to stock Ruby classes in a > separate place. That place is lib/deltacloud/core_ext > --- > server/deltacloud.rb | 2 + > server/lib/deltacloud/core_ext.rb | 20 +++++++++++ > server/lib/deltacloud/core_ext/integer.rb | 33 ++++++++++++++++++ > server/lib/deltacloud/core_ext/string.rb | 52 = +++++++++++++++++++++++++++++ > server/lib/sinatra/rabbit.rb | 35 ------------------- > 5 files changed, 107 insertions(+), 35 deletions(-) > create mode 100644 server/lib/deltacloud/core_ext.rb > create mode 100644 server/lib/deltacloud/core_ext/integer.rb > create mode 100644 server/lib/deltacloud/core_ext/string.rb >=20 > diff --git a/server/deltacloud.rb b/server/deltacloud.rb > index 83f7cfb..cd310ef 100644 > --- a/server/deltacloud.rb > +++ b/server/deltacloud.rb > @@ -20,6 +20,8 @@ $:.unshift File.join(File.dirname(__FILE__), 'lib') >=20 > require 'drivers' >=20 > +require 'deltacloud/core_ext' > + > require 'deltacloud/base_driver' > require 'deltacloud/hardware_profile' > require 'deltacloud/state_machine' > diff --git a/server/lib/deltacloud/core_ext.rb = b/server/lib/deltacloud/core_ext.rb > new file mode 100644 > index 0000000..e3822f2 > --- /dev/null > +++ b/server/lib/deltacloud/core_ext.rb > @@ -0,0 +1,20 @@ > +# > +# Copyright (C) 2011 David Lutterkort > +# > +# Licensed to the Apache Software Foundation (ASF) under one or more > +# contributor license agreements. See the NOTICE file distributed = with > +# this work for additional information regarding copyright ownership. = The > +# ASF licenses this file to you under the Apache License, Version 2.0 = (the > +# "License"); you may not use this file except in compliance with the > +# License. You may obtain a copy of the License at > +# > +# http://www.apache.org/licenses/LICENSE-2.0 > +# > +# Unless required by applicable law or agreed to in writing, software > +# distributed under the License is distributed on an "AS IS" BASIS, = WITHOUT > +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. = See the > +# License for the specific language governing permissions and = limitations > +# under the License. > + > +require 'deltacloud/core_ext/string' > +require 'deltacloud/core_ext/integer' > diff --git a/server/lib/deltacloud/core_ext/integer.rb = b/server/lib/deltacloud/core_ext/integer.rb > new file mode 100644 > index 0000000..60b6f68 > --- /dev/null > +++ b/server/lib/deltacloud/core_ext/integer.rb > @@ -0,0 +1,33 @@ > +# > +# Copyright (C) 2011 David Lutterkort > +# > +# Licensed to the Apache Software Foundation (ASF) under one or more > +# contributor license agreements. See the NOTICE file distributed = with > +# this work for additional information regarding copyright ownership. = The > +# ASF licenses this file to you under the Apache License, Version 2.0 = (the > +# "License"); you may not use this file except in compliance with the > +# License. You may obtain a copy of the License at > +# > +# http://www.apache.org/licenses/LICENSE-2.0 > +# > +# Unless required by applicable law or agreed to in writing, software > +# distributed under the License is distributed on an "AS IS" BASIS, = WITHOUT > +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. = See the > +# License for the specific language governing permissions and = limitations > +# 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" > + end > + end > + end > +end > diff --git a/server/lib/deltacloud/core_ext/string.rb = b/server/lib/deltacloud/core_ext/string.rb > new file mode 100644 > index 0000000..2beba00 > --- /dev/null > +++ b/server/lib/deltacloud/core_ext/string.rb > @@ -0,0 +1,52 @@ > +# > +# Copyright (C) 2011 David Lutterkort > +# > +# Licensed to the Apache Software Foundation (ASF) under one or more > +# contributor license agreements. See the NOTICE file distributed = with > +# this work for additional information regarding copyright ownership. = The > +# ASF licenses this file to you under the Apache License, Version 2.0 = (the > +# "License"); you may not use this file except in compliance with the > +# License. You may obtain a copy of the License at > +# > +# http://www.apache.org/licenses/LICENSE-2.0 > +# > +# Unless required by applicable law or agreed to in writing, software > +# distributed under the License is distributed on an "AS IS" BASIS, = WITHOUT > +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. = See the > +# License for the specific language governing permissions and = limitations > +# 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? > + self !~ /\S/ > + end > + > + # Title case. > + # > + # "this is a string".titlecase > + # =3D> "This Is A String" > + # > + # CREDIT: Eliazar Parra > + # Copied from facets > + def titlecase > + gsub(/\b\w/){ $`[-1,1] =3D=3D "'" ? $& : $&.upcase } > + end > + > + def pluralize > + self + "s" > + end > + > + def singularize > + self.gsub(/s$/, '') > + end There was some special cases when this pluralization/singularization = methods doesn't work. Collections like 'addresses', etc. > + > + def underscore > + gsub(/::/, '/'). > + gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). > + gsub(/([a-z\d])([A-Z])/,'\1_\2'). > + tr("-", "_"). > + downcase > + end > +end > diff --git a/server/lib/sinatra/rabbit.rb = b/server/lib/sinatra/rabbit.rb > index b736b7a..f8d9e3d 100644 > --- a/server/lib/sinatra/rabbit.rb > +++ b/server/lib/sinatra/rabbit.rb > @@ -312,41 +312,6 @@ module Sinatra > helpers RabbitHelper > end >=20 > -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? > - self !~ /\S/ > - end > - > - # Title case. > - # > - # "this is a string".titlecase > - # =3D> "This Is A String" > - # > - # CREDIT: Eliazar Parra > - # Copied from facets > - def titlecase > - gsub(/\b\w/){ $`[-1,1] =3D=3D "'" ? $& : $&.upcase } > - end > - > - def pluralize > - self + "s" > - end > - > - def singularize > - self.gsub(/s$/, '') > - end > - > - def underscore > - gsub(/::/, '/'). > - gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). > - gsub(/([a-z\d])([A-Z])/,'\1_\2'). > - tr("-", "_"). > - downcase > - end > -end > - > configure do > class << Sinatra::Base > def options(path, opts=3D{}, &block) > --=20 > 1.7.4 >=20