Blob of lib/strokedb/core_ext/string.rb (raw blob data)

1 # extracted and adapted from ActiveRecord (http://rubyforge.org/projects/activesupport/)
2
3 class String
4 def ends_with?(suffix)
5 suffix = suffix.to_s
6 self[-suffix.length, suffix.length] == suffix
7 end
8
9 def camel_case
10 split('_').map{|e| e.capitalize}.join
11 end
12 alias :camelize :camel_case
13
14 def snake_case
15 gsub(/\B[A-Z][^A-Z]/, '_\&').downcase.gsub(' ', '_')
16 end
17
18 def tableize
19 words = snake_case.split('_')
20 words.last.replace words.last.plural
21 words.join('_')
22 end
23
24 def demodulize
25 gsub(/^.*::/, '')
26 end
27
28 def modulize
29 return '' unless include?('::') && self[0,2] != '::'
30 self.gsub(/^(.+)::(#{demodulize})$/,'\\1')
31 end
32
33 def constantize
34 if /^meta:/ =~ self
35 return StrokeDB::META_CACHE[Meta.make_uuid_from_fullname(self)]
36 end
37 unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ self
38 raise NameError, "#{self.inspect} is not a valid constant name!"
39 end
40 Object.module_eval("::#{$1}", __FILE__, __LINE__)
41 end
42
43 def /(o)
44 File.join(self, o.to_s)
45 end
46
47 def unindent!
48 self.gsub!(/^\n/, '').gsub!(/^#{self.match(/^\s*/)[0]}/, '')
49 end
50
51 def underscore
52 self.to_s.gsub(/::/, '/').
53 gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
54 gsub(/([a-z\d])([A-Z])/,'\1_\2').
55 tr("-", "_").
56 downcase
57 end
58
59 def lines
60 self.split("\n").size
61 end
62 end