From 27731a18b93fcea61fbcbc017c5087679224406b Mon Sep 17 00:00:00 2001 From: Mark Maglana Date: Fri, 6 Jun 2014 10:30:26 -0700 Subject: [PATCH] Define inflections only when they're not already defined MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is so that Aviator doesn’t step on ActiveSupport’s toes in case it’s already loaded into memory. This is not the best solution since Aviator’s implementation may diverge from ActiveSupport’s implementation at some point. For now, this will have to do. Change-Id: Iaf99d9499394c4a5ce1c0141f1a68492bb2f3191 --- lib/aviator/string.rb | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/aviator/string.rb b/lib/aviator/string.rb index 7efa2e1..25d24b6 100644 --- a/lib/aviator/string.rb +++ b/lib/aviator/string.rb @@ -1,18 +1,24 @@ class String - def camelize - word = self.slice(0,1).capitalize + self.slice(1..-1) - word.gsub(/_([a-zA-Z\d])/) { "#{$1.capitalize}" } - end - - def constantize - self.split("::").inject(Object) do |namespace, sym| - namespace.const_get(sym.to_s.camelize, false) + unless instance_methods.include? 'camelize' + define_method :camelize do + word = self.slice(0,1).capitalize + self.slice(1..-1) + word.gsub(/_([a-zA-Z\d])/) { "#{$1.capitalize}" } end end - def underscore - self.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase + unless instance_methods.include? 'constantize' + define_method :constantize do + self.split("::").inject(Object) do |namespace, sym| + namespace.const_get(sym.to_s.camelize, false) + end + end + end + + unless instance_methods.include? 'underscore' + define_method :underscore do + self.gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase + end end end