Permalink
Browse files

moving string extenions into their own file.

  • Loading branch information...
1 parent 04dd44c commit 3730ca3c8d47c27c66cfa203e84293b5672127e1 Jeremy committed Nov 12, 2011
Showing with 29 additions and 16 deletions.
  1. +25 −0 lib/extensions/string.rb
  2. +4 −16 lib/gild.rb
View
25 lib/extensions/string.rb
@@ -0,0 +1,25 @@
+class String
+ unless method_defined?(:underscore)
+ def underscore
+ word = self.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
+
+ unless method_defined?(:pluralize)
+ def pluralize
+ self.dup + "s"
+ end
+ end
+
+ unless method_defined?(:demodulize)
+ def demodulize
+ self.dup.gsub(/^.*::/, '')
+ end
+ end
+end
View
20 lib/gild.rb
@@ -2,29 +2,17 @@
require 'gild/builder'
require 'gild/render_context'
+require 'extensions/string.rb'
+
module Gild
def self.gilded_name(object)
if object.is_a?(Symbol) or object.is_a?(String)
object.to_s
else
thing_that_responds_to_name = object.is_a?(Array) ? object.first.class : object.class
- name = thing_that_responds_to_name.name.split('::').last.underscore.downcase
- name += "s" if object.is_a?(Array)
+ name = thing_that_responds_to_name.name.demodulize.underscore.downcase
+ name = name.pluralize if object.is_a?(Array)
name
end
end
end
-
-unless String.method_defined?(:underscore)
- class String
- 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

0 comments on commit 3730ca3

Please sign in to comment.