ruby_extensions 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/m_class.rb CHANGED
@@ -4,7 +4,16 @@ class Class
4
4
  mod = Module
5
5
  mod = mod.const_get(klass_name)
6
6
  mod.new
7
- end
7
+ end
8
+
9
+ def class_is_a?(klass_name)
10
+ self.ancestors.each do |an|
11
+ if an == klass_name
12
+ return true
13
+ end
14
+ end
15
+ return false
16
+ end
8
17
 
9
18
  end
10
19
 
data/lib/m_logger.rb CHANGED
@@ -9,12 +9,29 @@ class Logger
9
9
  end
10
10
  }
11
11
  end
12
+
13
+ j = 5
14
+ 5.times do |i|
15
+ lbs = ""
16
+ ast = ""
17
+ j.times { lbs << "\n"}
18
+ (j * 5).times { ast << "*"}
19
+ class_eval %{
20
+ def debug_#{i+1}(progname = nil, &block)
21
+ progname = format_progname(progname)
22
+ mess = "\n#{ast}#{lbs}\#{progname}#{lbs}#{ast}"
23
+ debug(mess, &block)
24
+ end
25
+ }
26
+ j = j - 1
27
+ end
12
28
 
13
29
  def format_message(severity, timestamp, progname, msg)
14
30
  # Check for characters at the start of the message string indicating special formatting
15
31
  # Make the typical case as efficient as possible
16
32
  if not msg[0..1] == '$@'
17
- return "[#{severity}]\t#{timestamp}\t#{msg}\n"
33
+ pid = (defined?(ENABLE_PIDS_IN_LOGS) and ENABLE_PIDS_IN_LOGS) ? "(#{$$}) " : ''
34
+ return "#{pid}[#{severity}]\t#{timestamp}\t#{msg}\n"
18
35
  end
19
36
  format = msg[2..2]
20
37
  msg = msg[3..-1]
@@ -35,4 +52,4 @@ class Logger
35
52
  progname
36
53
  end
37
54
 
38
- end
55
+ end
data/lib/m_string.rb CHANGED
@@ -1,5 +1,81 @@
1
+ require 'active_support'
1
2
  class String
2
3
 
4
+ include ActiveSupport::CoreExtensions::String::Inflections
5
+
6
+ def methodize
7
+ x = self
8
+
9
+ # if we get down to a nil or an empty string raise an exception!
10
+ raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
11
+
12
+ # get rid of the big stuff in the front/back
13
+ x.strip!
14
+
15
+ # if we get down to a nil or an empty string raise an exception!
16
+ raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
17
+
18
+ x = x.underscore
19
+
20
+ # get rid of spaces and make the _
21
+ x.gsub!(' ', '_')
22
+ # get rid of everything that isn't 'safe' a-z, 0-9, ?, !, =, _
23
+ x.gsub!(/([^ a-zA-Z0-9\_\?\!\=]+)/n, '_')
24
+
25
+ # if we get down to a nil or an empty string raise an exception!
26
+ raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
27
+
28
+ # condense multiple 'safe' non a-z chars to just one.
29
+ # ie. ___ becomes _ !!!! becomes ! etc...
30
+ [' ', '_', '?', '!', "="].each do |c|
31
+ x.squeeze!(c)
32
+ end
33
+
34
+ # if we get down to a nil or an empty string raise an exception!
35
+ raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
36
+
37
+ #down case the whole thing
38
+ x.downcase!
39
+
40
+ # get rid of any characters at the beginning that aren't a-z
41
+ while !x.match(/^[a-z]/)
42
+ x.slice!(0)
43
+
44
+ # if we get down to a nil or an empty string raise an exception!
45
+ raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
46
+ end
47
+
48
+ # let's trim this bad boy down a bit now that we've cleaned it up, somewhat.
49
+ # we should do this before cleaning up the end character, because it's possible to end up with a
50
+ # bad char at the end if you trim too late.
51
+ x = x[0..100] if x.length > 100
52
+
53
+ # get rid of any characters at the end that aren't safe
54
+ while !x.match(/[a-z0-9\?\!\=]$/)
55
+ x.slice!(x.length - 1)
56
+ # if we get down to a nil or an empty string raise an exception!
57
+ raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
58
+ end
59
+
60
+ # if we get down to a nil or an empty string raise an exception!
61
+ raise NameError.new("#{self} cannot be converted to a valid method name!") if x.nil? || x == ''
62
+
63
+ # let's get rid of characters that don't belong in the 'middle' of the method.
64
+ orig_middle = x[1..(x.length - 2)]
65
+ n_middle = orig_middle.dup
66
+
67
+ ['?', '!', "="].each do |c|
68
+ n_middle.gsub!(c, "_")
69
+ end
70
+
71
+ # the previous gsub can leave us with multiple underscores that need cleaning up.
72
+ n_middle.squeeze!("_")
73
+
74
+ x.gsub!(orig_middle, n_middle)
75
+ x.gsub!("_=", "=")
76
+ x
77
+ end
78
+
3
79
  def starts_with?(x)
4
80
  self.match(/^#{x}/) ? true : false
5
81
  end
@@ -104,23 +180,23 @@ class String
104
180
  self.replace(self.capitalize_all_words)
105
181
  end
106
182
 
107
- # remove_bad_chars!: any characters not in the allowed set will be removed
108
- # squeeze: remove any repeated dash, underscore, period, comma characters
109
- def remove_bad_chars
110
- #ic = Iconv.new('UTF-8//IGNORE', 'UTF-8') # ignore bytes you can't convert
111
- #str = ic.iconv(self + ' ')[0..-2]
112
- if !self.nil?
113
- x = self.gsub(/[^\s0-9\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\_\~\`\^\|\\ \{\}\[\]\’\“\” A-Za-z]/, '').to_s.gsub(/[\’]/, "'").to_s.gsub(/[\“]/, '"').to_s.gsub(/[\”]/, '"').squeeze(",").squeeze("-").squeeze("!").squeeze("*").squeeze("?")
114
-
115
- # now use the WhiteListHelper plugin to clean out html encodings
116
- white_list(x)
117
- end
118
-
119
- end
120
-
121
- def remove_bad_chars!
122
- self.replace(self.remove_bad_chars)
123
- end
183
+ # # remove_bad_chars!: any characters not in the allowed set will be removed
184
+ # # squeeze: remove any repeated dash, underscore, period, comma characters
185
+ # def remove_bad_chars
186
+ # #ic = Iconv.new('UTF-8//IGNORE', 'UTF-8') # ignore bytes you can't convert
187
+ # #str = ic.iconv(self + ' ')[0..-2]
188
+ # if !self.nil?
189
+ # x = self.gsub(/[^\s0-9\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\_\~\`\^\|\\ \{\}\[\]\’\“\” A-Za-z]/, '').to_s.gsub(/[\’]/, "'").to_s.gsub(/[\“]/, '"').to_s.gsub(/[\”]/, '"').squeeze(",").squeeze("-").squeeze("!").squeeze("*").squeeze("?")
190
+ #
191
+ # # now use the WhiteListHelper plugin to clean out html encodings
192
+ # white_list(x)
193
+ # end
194
+ #
195
+ # end
196
+ #
197
+ # def remove_bad_chars!
198
+ # self.replace(self.remove_bad_chars)
199
+ # end
124
200
 
125
201
  # keep adding on to this whenever it becomes apparent that unsafe strings
126
202
  # could get passed through into the database
@@ -1,5 +1,6 @@
1
1
  require 'find'
2
- path = File.join(RAILS_ROOT, "vendor", "plugins", "ruby_extensions", "lib")
2
+ path = File.dirname(__FILE__)#File.join(RAILS_ROOT, "vendor", "plugins", "ruby_extensions", "lib")
3
+ # puts "path = #{path}"
3
4
  Find.find(path) do |f|
4
5
  if FileTest.directory?(f) and f =~ /\.svn/
5
6
  Find.prune
@@ -2,4 +2,4 @@
2
2
  gem_name: ruby_extensions
3
3
  package: ruby_extensions
4
4
  project: magrathea
5
- version: 1.0.0
5
+ version: 1.0.1
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: ruby_extensions
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.0.0
7
- date: 2007-09-20 00:00:00 -04:00
6
+ version: 1.0.1
7
+ date: 2007-10-19 00:00:00 -04:00
8
8
  summary: ruby_extensions
9
9
  require_paths:
10
10
  - lib
@@ -26,6 +26,7 @@ autorequire:
26
26
  - m_dir
27
27
  - m_class
28
28
  - m_array
29
+ - ruby_extensions
29
30
  - m_string
30
31
  - m_object
31
32
  - m_math