buzzcore 0.5.1 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1,2 +1,2 @@
1
- 0.5.1
1
+ 0.6.1
2
2
 
data/buzzcore.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{buzzcore}
8
- s.version = "0.5.1"
8
+ s.version = "0.6.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["buzzware"]
12
- s.date = %q{2010-09-22}
12
+ s.date = %q{2011-06-03}
13
13
  s.description = %q{buzzcore is the ruby core library developed and used by Buzzware Solutions.}
14
14
  s.email = %q{contact@buzzware.com.au}
15
15
  s.extra_rdoc_files = [
@@ -41,13 +41,12 @@ Gem::Specification.new do |s|
41
41
  "lib/buzzcore/shell_extras.rb",
42
42
  "lib/buzzcore/string_utils.rb",
43
43
  "lib/buzzcore/text_doc.rb",
44
- "lib/buzzcore/tweaks.rb",
45
44
  "lib/buzzcore/xml_utils.rb",
46
45
  "lib/buzzcore_dev.rb",
47
- "rails/init.rb",
48
46
  "test/buzzcore_test.rb",
49
47
  "test/config_test.rb",
50
48
  "test/credentials_test.rb",
49
+ "test/extend_base_classes_test.rb",
51
50
  "test/misc_test.rb",
52
51
  "test/shell_test.rb",
53
52
  "test/test_helper.rb"
@@ -61,6 +60,7 @@ Gem::Specification.new do |s|
61
60
  "test/buzzcore_test.rb",
62
61
  "test/config_test.rb",
63
62
  "test/credentials_test.rb",
63
+ "test/extend_base_classes_test.rb",
64
64
  "test/misc_test.rb",
65
65
  "test/shell_test.rb",
66
66
  "test/test_helper.rb"
@@ -1,38 +1,38 @@
1
- String.class_eval do
2
- def pad_left(value)
3
- increase = value-self.length
4
- return self if increase==0
5
- if increase > 0
6
- return self + ' '*increase
7
- else
8
- return self[0,value]
9
- end
10
- end
11
-
12
- def pad_right(value)
13
- increase = value-self.length
14
- return self if increase==0
15
- if increase > 0
16
- return ' '*increase + self
17
- else
18
- return self[0,value]
19
- end
20
- end
21
-
22
- # Like chomp! but operates on the leading characters instead.
23
- # The aString parameter would not normally be used.
24
- def bite!(aValue=$/,aString=self)
25
- if aString[0,aValue.length] == aValue
26
- aString[0,aValue.length] = ''
27
- return aString
28
- else
29
- return aString
30
- end
31
- end
32
-
33
- def bite(aValue=$/)
34
- bite!(aValue,self.clone)
35
- end
1
+ String.class_eval do
2
+ def pad_left(value)
3
+ increase = value-self.length
4
+ return self if increase==0
5
+ if increase > 0
6
+ return self + ' '*increase
7
+ else
8
+ return self[0,value]
9
+ end
10
+ end
11
+
12
+ def pad_right(value)
13
+ increase = value-self.length
14
+ return self if increase==0
15
+ if increase > 0
16
+ return ' '*increase + self
17
+ else
18
+ return self[0,value]
19
+ end
20
+ end
21
+
22
+ # Like chomp! but operates on the leading characters instead.
23
+ # The aString parameter would not normally be used.
24
+ def bite!(aValue=$/,aString=self)
25
+ if aString[0,aValue.length] == aValue
26
+ aString[0,aValue.length] = ''
27
+ return aString
28
+ else
29
+ return aString
30
+ end
31
+ end
32
+
33
+ def bite(aValue=$/)
34
+ bite!(aValue,self.clone)
35
+ end
36
36
 
37
37
  def begins_with?(aString)
38
38
  self[0,aString.length]==aString
@@ -79,128 +79,128 @@ String.class_eval do
79
79
  def is_f?
80
80
  self.to_float(false) and true
81
81
  end
82
-
82
+
83
83
  # like scan but returns array of MatchData's.
84
84
  # doesn't yet support blocks
85
85
  def scan_md(aPattern)
86
86
  result = []
87
- self.scan(aPattern) {|s| result << $~ }
87
+ self.scan(aPattern) {|s| result << $~ }
88
88
  result
89
89
  end
90
-
90
+
91
91
  def to_nil(aPattern=nil)
92
92
  return nil if self.empty?
93
93
  if aPattern
94
94
  return nil if (aPattern.is_a? Regexp) && (self =~ aPattern)
95
- return nil if aPattern.to_s == self
95
+ return nil if aPattern.to_s == self
96
96
  end
97
97
  self
98
98
  end
99
-
99
+
100
100
  def to_b(aDefault=false)
101
101
  return true if ['1','yes','y','true','on'].include?(self.downcase)
102
102
  return false if ['0','no','n','false','off'].include?(self.downcase)
103
103
  aDefault
104
104
  end
105
-
106
- # uses
105
+
106
+ # uses
107
107
  #URLIZE_PATTERN = /[ \/\\\(\)\[\]]/
108
108
  URLIZE_PATTERN_PS = /[ \\\(\)\[\]_]/
109
109
  def urlize(aSlashChar='+')
110
110
  return self if self.empty?
111
- result = self.gsub(URLIZE_PATTERN_PS,'-').downcase.gsub(/[^a-z0-9_\-+,\.\/]/,'')
111
+ result = self.gsub(URLIZE_PATTERN_PS,'-').downcase.gsub(/[^a-z0-9_\-+,\.\/]/,'').sub(/-+$/,'').sub(/^-+/,'')
112
112
  result.gsub!('/',aSlashChar) unless aSlashChar=='/'
113
113
  result.gsub!(/-{2,}/,'-')
114
114
  result
115
- end
116
-
115
+ end
116
+
117
117
  private
118
- CRC_LOOKUP = [
119
- 0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
120
- 0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
121
- 0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
122
- 0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
123
- 0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,
124
- 0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
125
- 0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,
126
- 0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
127
- 0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
128
- 0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
129
- 0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,
130
- 0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
131
- 0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,
132
- 0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
133
- 0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,
134
- 0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
135
- 0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,
136
- 0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
137
- 0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,
138
- 0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
139
- 0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
140
- 0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
141
- 0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,
142
- 0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
143
- 0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,
144
- 0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
145
- 0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
146
- 0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
147
- 0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
148
- 0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
149
- 0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
150
- 0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
151
- ]
152
- public
153
-
154
- def crc16
155
- crc = 0x00
156
- self.each_byte do |b|
157
- crc = ((crc >> 8) & 0xff) ^ CRC_LOOKUP[(crc ^ b) & 0xff]
158
- end
159
- crc
160
- end
161
-
162
-
163
- end
118
+ CRC_LOOKUP = [
119
+ 0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
120
+ 0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
121
+ 0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
122
+ 0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
123
+ 0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,
124
+ 0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
125
+ 0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,
126
+ 0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
127
+ 0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
128
+ 0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
129
+ 0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,
130
+ 0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
131
+ 0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,
132
+ 0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
133
+ 0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,
134
+ 0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
135
+ 0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,
136
+ 0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
137
+ 0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,
138
+ 0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
139
+ 0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
140
+ 0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
141
+ 0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,
142
+ 0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
143
+ 0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,
144
+ 0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
145
+ 0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
146
+ 0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
147
+ 0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
148
+ 0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
149
+ 0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
150
+ 0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
151
+ ]
152
+ public
153
+
154
+ def crc16
155
+ crc = 0x00
156
+ self.each_byte do |b|
157
+ crc = ((crc >> 8) & 0xff) ^ CRC_LOOKUP[(crc ^ b) & 0xff]
158
+ end
159
+ crc
160
+ end
161
+
162
+
163
+ end
164
164
 
165
165
 
166
166
  Time.class_eval do
167
167
 
168
- if !respond_to?(:change) # no activesupport loaded
169
- def change(options)
170
- ::Time.send(
171
- self.utc? ? :utc : :local,
172
- options[:year] || self.year,
173
- options[:month] || self.month,
174
- options[:day] || self.day,
175
- options[:hour] || self.hour,
176
- options[:min] || (options[:hour] ? 0 : self.min),
177
- options[:sec] || ((options[:hour] || options[:min]) ? 0 : self.sec),
178
- options[:usec] || ((options[:hour] || options[:min] || options[:sec]) ? 0 : self.usec)
179
- )
180
- end
181
-
182
- def seconds_since_midnight
183
- self.to_i - self.change(:hour => 0).to_i + (self.usec/1.0e+6)
184
- end
185
-
186
- def beginning_of_day
187
- (self - self.seconds_since_midnight).change(:usec => 0)
188
- end
189
-
190
- alias :midnight :beginning_of_day
191
- alias :at_midnight :beginning_of_day
192
- alias :at_beginning_of_day :beginning_of_day
193
-
194
- end
168
+ if !respond_to?(:change) # no activesupport loaded
169
+ def change(options)
170
+ ::Time.send(
171
+ self.utc? ? :utc : :local,
172
+ options[:year] || self.year,
173
+ options[:month] || self.month,
174
+ options[:day] || self.day,
175
+ options[:hour] || self.hour,
176
+ options[:min] || (options[:hour] ? 0 : self.min),
177
+ options[:sec] || ((options[:hour] || options[:min]) ? 0 : self.sec),
178
+ options[:usec] || ((options[:hour] || options[:min] || options[:sec]) ? 0 : self.usec)
179
+ )
180
+ end
181
+
182
+ def seconds_since_midnight
183
+ self.to_i - self.change(:hour => 0).to_i + (self.usec/1.0e+6)
184
+ end
185
+
186
+ def beginning_of_day
187
+ (self - self.seconds_since_midnight).change(:usec => 0)
188
+ end
189
+
190
+ alias :midnight :beginning_of_day
191
+ alias :at_midnight :beginning_of_day
192
+ alias :at_beginning_of_day :beginning_of_day
193
+
194
+ end
195
195
 
196
196
  # offset of local machine from UTC, in seconds eg +9.hours
197
197
  def self.local_offset
198
- local(2000).utc_offset
198
+ local(2000).utc_offset
199
199
  end
200
200
 
201
- def date
202
- self.at_beginning_of_day
203
- end
201
+ def date
202
+ self.at_beginning_of_day
203
+ end
204
204
 
205
205
  # index number of this day, from Time.at(0) + utc_offset
206
206
  def day_number
@@ -220,11 +220,11 @@ Time.class_eval do
220
220
  def date_numeric
221
221
  self.strftime('%Y%m%d')
222
222
  end
223
-
223
+
224
224
  def to_universal
225
- self.strftime("%d %b %Y")
225
+ self.strftime("%d %b %Y")
226
226
  end
227
-
227
+
228
228
  # create a new Time from eg. "20081231"
229
229
  def self.from_date_numeric(aString)
230
230
  return nil unless aString
@@ -239,60 +239,60 @@ Time.class_eval do
239
239
  self.strftime('%Y%m%d-%H%M%S')
240
240
  end
241
241
 
242
- def to_sql
242
+ def to_sql_format # was to_sql, but clashed with Rails 3
243
243
  self.strftime('%Y-%m-%d %H:%M:%S')
244
244
  end
245
245
 
246
246
  def to_w3c
247
- utc.strftime("%Y-%m-%dT%H:%M:%S+00:00")
247
+ utc.strftime("%Y-%m-%dT%H:%M:%S+00:00")
248
248
  end
249
249
  end
250
250
 
251
251
  module HashUtils
252
- def filter_include!(aKeys,aHash=nil)
253
- aHash ||= self
252
+ def filter_include!(aKeys,aHash=nil)
253
+ aHash ||= self
254
254
 
255
255
  if aKeys.is_a? Regexp
256
- return aHash.delete_if {|k,v| not k =~ aKeys }
256
+ return aHash.delete_if {|k,v| not k =~ aKeys }
257
257
  else
258
258
  aKeys = [aKeys] unless aKeys.is_a? Array
259
- return aHash.clear if aKeys.empty?
260
- return aHash.delete_if {|key, value| !((aKeys.include?(key)) || (key.is_a?(Symbol) and aKeys.include?(key.to_s)) || (key.is_a?(String) and aKeys.include?(key.to_sym)))}
261
- return aHash # last resort
259
+ return aHash.clear if aKeys.empty?
260
+ return aHash.delete_if {|key, value| !((aKeys.include?(key)) || (key.is_a?(Symbol) and aKeys.include?(key.to_s)) || (key.is_a?(String) and aKeys.include?(key.to_sym)))}
261
+ return aHash # last resort
262
262
  end
263
- end
263
+ end
264
264
 
265
- def filter_include(aKeys,aHash=nil)
266
- aHash ||= self
267
- filter_include!(aKeys,aHash.clone)
268
- end
265
+ def filter_include(aKeys,aHash=nil)
266
+ aHash ||= self
267
+ filter_include!(aKeys,aHash.clone)
268
+ end
269
269
 
270
- def filter_exclude!(aKeys,aHash=nil)
271
- aHash ||= self
270
+ def filter_exclude!(aKeys,aHash=nil)
271
+ aHash ||= self
272
272
 
273
273
  if aKeys.is_a? Regexp
274
- return aHash.delete_if {|k,v| k =~ aKeys }
274
+ return aHash.delete_if {|k,v| k =~ aKeys }
275
275
  else
276
276
  aKeys = [aKeys] unless aKeys.is_a? Array
277
- return aHash if aKeys.empty?
278
- return aHash.delete_if {|key, value| ((aKeys.include?(key)) || (key.is_a?(Symbol) and aKeys.include?(key.to_s)) || (key.is_a?(String) and aKeys.include?(key.to_sym)))}
277
+ return aHash if aKeys.empty?
278
+ return aHash.delete_if {|key, value| ((aKeys.include?(key)) || (key.is_a?(Symbol) and aKeys.include?(key.to_s)) || (key.is_a?(String) and aKeys.include?(key.to_sym)))}
279
279
  end
280
- end
280
+ end
281
281
 
282
- def filter_exclude(aKeys,aHash=nil)
283
- aHash ||= self
284
- filter_exclude!(aKeys,aHash.clone)
285
- end
282
+ def filter_exclude(aKeys,aHash=nil)
283
+ aHash ||= self
284
+ filter_exclude!(aKeys,aHash.clone)
285
+ end
286
286
 
287
287
  def has_values_for?(aKeys,aHash=nil)
288
- aHash ||= self
288
+ aHash ||= self
289
289
  # check all keys exist in aHash and their values are not nil
290
290
  aKeys.all? { |k,v| aHash[k] }
291
291
  end
292
292
 
293
293
  # give a block to execute without the given key in this hash
294
294
  # It will be replaced after the block (guaranteed by ensure)
295
- # eg.
295
+ # eg.
296
296
  # hash.without_key(:blah) do |aHash|
297
297
  # puts aHash.inspect
298
298
  # end
@@ -309,7 +309,7 @@ module HashUtils
309
309
  end
310
310
  return result
311
311
  end
312
-
312
+
313
313
  def symbolize_keys
314
314
  result = {}
315
315
  self.each { |k,v| k.is_a?(String) ? result[k.to_sym] = v : result[k] = v }
@@ -323,7 +323,7 @@ module HashUtils
323
323
  end
324
324
 
325
325
  Hash.class_eval do
326
- include HashUtils
326
+ include HashUtils
327
327
  end
328
328
 
329
329
  if defined? HashWithIndifferentAccess
@@ -333,40 +333,40 @@ if defined? HashWithIndifferentAccess
333
333
  end
334
334
 
335
335
  module ArrayUtils
336
- def filter_include!(aValues,aArray=nil)
337
- aArray ||= self
338
- if aValues.is_a? Array
339
- return aArray if aValues.empty?
340
- return aArray.delete_if {|v| not aValues.include? v }
341
- elsif aValues.is_a? Regexp
342
- return aArray.delete_if {|v| not v =~ aValues }
343
- else
344
- return filter_include!([aValues],aArray)
345
- end
346
- end
347
-
348
- def filter_include(aValues,aArray=nil)
349
- aArray ||= self
350
- filter_include!(aValues,aArray.clone)
351
- end
352
-
353
- def filter_exclude!(aValues,aArray=nil)
354
- aArray ||= self
355
- if aValues.is_a? Array
356
- return aArray if aValues.empty?
357
- return aArray.delete_if {|v| aValues.include? v }
358
- elsif aValues.is_a? Regexp
359
- return aArray.delete_if {|v| v =~ aValues }
360
- else
361
- return filter_exclude!([aValues],aArray)
362
- end
363
- end
364
-
365
- def filter_exclude(aValues,aArray=nil)
366
- aArray ||= self
367
- filter_exclude!(aValues,aArray.clone)
368
- end
369
-
336
+ def filter_include!(aValues,aArray=nil)
337
+ aArray ||= self
338
+ if aValues.is_a? Array
339
+ return aArray if aValues.empty?
340
+ return aArray.delete_if {|v| not aValues.include? v }
341
+ elsif aValues.is_a? Regexp
342
+ return aArray.delete_if {|v| not v =~ aValues }
343
+ else
344
+ return filter_include!([aValues],aArray)
345
+ end
346
+ end
347
+
348
+ def filter_include(aValues,aArray=nil)
349
+ aArray ||= self
350
+ filter_include!(aValues,aArray.clone)
351
+ end
352
+
353
+ def filter_exclude!(aValues,aArray=nil)
354
+ aArray ||= self
355
+ if aValues.is_a? Array
356
+ return aArray if aValues.empty?
357
+ return aArray.delete_if {|v| aValues.include? v }
358
+ elsif aValues.is_a? Regexp
359
+ return aArray.delete_if {|v| v =~ aValues }
360
+ else
361
+ return filter_exclude!([aValues],aArray)
362
+ end
363
+ end
364
+
365
+ def filter_exclude(aValues,aArray=nil)
366
+ aArray ||= self
367
+ filter_exclude!(aValues,aArray.clone)
368
+ end
369
+
370
370
  def to_nil
371
371
  self.empty? ? nil : self
372
372
  end
@@ -374,7 +374,7 @@ module ArrayUtils
374
374
  end
375
375
 
376
376
  Array.class_eval do
377
- include ArrayUtils
377
+ include ArrayUtils
378
378
 
379
379
  # fixes a memory leak in shift in Ruby 1.8 - should be fixed in 1.9
380
380
  # see http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/216055
@@ -390,27 +390,27 @@ Kernel.class_eval do
390
390
  end
391
391
  end
392
392
 
393
- if defined? ActiveRecord
394
- ActiveRecord::Base.class_eval do
395
-
396
- def self.find_any_id(aId)
397
- with_exclusive_scope { find(:first, {:conditions => {:id => aId}}) }
398
- end
399
-
400
- def self.find_any_all(aOptions={})
401
- with_exclusive_scope { find(:all, aOptions) }
402
- end
403
-
404
- def self.find_ids(aIds)
405
- find(:all, {:conditions=> ["id in (?)",aIds.join(',')]})
406
- end
407
-
408
- def self.find_any_ids(aIds)
409
- with_exclusive_scope { find(:all, {:conditions=> ["id in (?)",aIds.join(',')]}) }
410
- end
411
-
412
- end
413
- end
393
+ #if defined? ActiveRecord
394
+ # ActiveRecord::Base.class_eval do
395
+ #
396
+ # def self.find_any_id(aId)
397
+ # with_exclusive_scope { find(:first, {:conditions => {:id => aId}}) }
398
+ # end
399
+ #
400
+ # def self.find_any_all(aOptions={})
401
+ # with_exclusive_scope { find(:all, aOptions) }
402
+ # end
403
+ #
404
+ # def self.find_ids(aIds)
405
+ # find(:all, {:conditions=> ["id in (?)",aIds.join(',')]})
406
+ # end
407
+ #
408
+ # def self.find_any_ids(aIds)
409
+ # with_exclusive_scope { find(:all, {:conditions=> ["id in (?)",aIds.join(',')]}) }
410
+ # end
411
+ #
412
+ # end
413
+ #end
414
414
 
415
415
  Fixnum.class_eval do
416
416
 
@@ -429,57 +429,57 @@ Bignum.class_eval do
429
429
  def to_nil
430
430
  self==0 ? nil : self
431
431
  end
432
-
432
+
433
433
  def to_b(aDefault=false)
434
434
  self==0 ? false : true
435
435
  end
436
436
 
437
437
  end
438
438
 
439
- NilClass.class_eval do
439
+ NilClass.class_eval do
440
440
 
441
441
  def to_nil
442
- nil
442
+ nil
443
443
  end
444
-
444
+
445
445
  def to_b(aDefault=false)
446
446
  false
447
447
  end
448
-
448
+
449
449
  end
450
450
 
451
- TrueClass.class_eval do
451
+ TrueClass.class_eval do
452
452
 
453
453
  def to_nil
454
- self
454
+ self
455
455
  end
456
-
456
+
457
457
  def to_b(aDefault=false)
458
458
  self
459
- end
460
-
459
+ end
460
+
461
461
  end
462
462
 
463
- FalseClass.class_eval do
463
+ FalseClass.class_eval do
464
464
 
465
465
  def to_nil
466
- nil
466
+ nil
467
467
  end
468
-
468
+
469
469
  def to_b(aDefault=false)
470
470
  self
471
- end
472
-
471
+ end
472
+
473
473
  end
474
474
 
475
475
 
476
476
  Math.module_eval do
477
- def self.max(a, b)
478
- a > b ? a : b
479
- end
477
+ def self.max(a, b)
478
+ a > b ? a : b
479
+ end
480
480
 
481
- def self.min(a, b)
482
- a < b ? a : b
483
- end
481
+ def self.min(a, b)
482
+ a < b ? a : b
483
+ end
484
484
  end
485
485
 
@@ -30,6 +30,17 @@ module HtmlUtils
30
30
  aHtml = Sanitize.clean(aHtml)
31
31
  StringUtils.simplify_whitespace(aHtml)
32
32
  end
33
+
34
+ def self.plain_words(aHtmlText,aWords)
35
+ result = CGI.unescapeHTML(Sanitize.clean(aHtmlText))
36
+ StringUtils.crop_to_word_count(result,aWords)
37
+ end
38
+
39
+ def self.plain_chars(aHtmlText,aMaxLength)
40
+ result = CGI.unescapeHTML(Sanitize.clean(aHtmlText))
41
+ result = StringUtils.simplify_whitespace(result)
42
+ StringUtils.word_safe_truncate(result,aMaxLength)
43
+ end
33
44
 
34
45
  end
35
46
 
@@ -91,12 +91,9 @@ module MiscUtils
91
91
  return new_dir
92
92
  end
93
93
 
94
- def self.mkdir?(aPath,aPermissions)
95
- if File.exists?(aPath)
96
- File.chmod(aPermissions, aPath)
97
- else
98
- Dir.mkdir(aPath, aPermissions)
99
- end
94
+ def self.mkdir?(aPath,aPermissions=nil)
95
+ FileUtils.mkdir_p(aPath) unless File.exists?(aPath)
96
+ File.chmod(aPermissions, aPath) if aPermissions
100
97
  end
101
98
 
102
99
  def self.set_permissions_cmd(aFilepath,aUser=nil,aGroup=nil,aMode=nil,aSetGroupId=false,aSudo=true)
@@ -18,6 +18,13 @@ module StringUtils
18
18
  result = (match ? match.pre_match : aText)
19
19
  result
20
20
  end
21
+
22
+ def self.random_word(min,max)
23
+ len = min + rand(max-min+1)
24
+ result = ' '*len
25
+ (len-1).downto(0) {|i| result[i] = (?a + rand(?z-?a+1)).chr}
26
+ return result
27
+ end
21
28
 
22
29
  # aTemplate is a string containing tokens like ${SOME_TOKEN}
23
30
  # aValues is a hash of token names eg. 'SOME_TOKEN' and their values to substitute
@@ -90,6 +97,14 @@ module StringUtils
90
97
  aText.squeeze!(' ')
91
98
  aText
92
99
  end
100
+
101
+ def self.clean_tag(aTag)
102
+ aTag.downcase.gsub('_','-').gsub(/[^a-z0-9-]/,'').bite('-').chomp('-')
103
+ end
104
+
105
+ def self.clean_tags(aTags)
106
+ aTags.map {|t| clean_tag(t)}.uniq
107
+ end
93
108
 
94
109
  end
95
110
 
@@ -0,0 +1,23 @@
1
+ # To change this template, choose Tools | Templates
2
+ # and open the template in the editor.
3
+
4
+ $:.unshift File.join(File.dirname(__FILE__),'..','lib')
5
+
6
+ require 'rubygems'
7
+
8
+ require 'test/unit'
9
+ gem 'shoulda'; require 'shoulda'
10
+
11
+ require 'fileutils'
12
+
13
+ gem 'buzzcore'; require 'buzzcore'
14
+
15
+ class ExtendBaseClassesTest < Test::Unit::TestCase
16
+
17
+ should "urlize generate correct url" do
18
+ s = "Waiting for Grace (ii)"
19
+ u = s.urlize
20
+ assert_equal 'waiting-for-grace-ii',u
21
+ end
22
+
23
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: buzzcore
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 5
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 5
8
+ - 6
9
9
  - 1
10
- version: 0.5.1
10
+ version: 0.6.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - buzzware
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-22 00:00:00 +08:00
18
+ date: 2011-06-03 00:00:00 +08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -80,13 +80,12 @@ files:
80
80
  - lib/buzzcore/shell_extras.rb
81
81
  - lib/buzzcore/string_utils.rb
82
82
  - lib/buzzcore/text_doc.rb
83
- - lib/buzzcore/tweaks.rb
84
83
  - lib/buzzcore/xml_utils.rb
85
84
  - lib/buzzcore_dev.rb
86
- - rails/init.rb
87
85
  - test/buzzcore_test.rb
88
86
  - test/config_test.rb
89
87
  - test/credentials_test.rb
88
+ - test/extend_base_classes_test.rb
90
89
  - test/misc_test.rb
91
90
  - test/shell_test.rb
92
91
  - test/test_helper.rb
@@ -128,6 +127,7 @@ test_files:
128
127
  - test/buzzcore_test.rb
129
128
  - test/config_test.rb
130
129
  - test/credentials_test.rb
130
+ - test/extend_base_classes_test.rb
131
131
  - test/misc_test.rb
132
132
  - test/shell_test.rb
133
133
  - test/test_helper.rb
@@ -1,107 +0,0 @@
1
- module Buzzcore
2
-
3
- #class Tweak
4
- #
5
- # attr_reader :name
6
- #
7
- # def initialize(aName)
8
- # @name = aName.to_sym
9
- # end
10
- #
11
- # def config
12
- # Tweaks.config_for(name)
13
- # end
14
- #
15
- #end
16
-
17
- class Tweaks
18
-
19
- @@configs = {}
20
- @@switch = {}
21
- @@switch_default = {}
22
-
23
- #cattr_accessor :rails_config
24
-
25
- # load all tweaks from a path
26
- def self.load_all(aPath)
27
- aPath = File.expand_path(aPath)
28
- # get list of tweak_files
29
- tweak_files = Dir.glob(File.join(aPath,'*_tweak.rb'))
30
- tweak_files.each do |f|
31
- Tweaks.load(f)
32
- end
33
- end
34
-
35
- def self.load(aTweakFile)
36
- #name = aTweakFile.scan(/.*\/(.+)_tweak\.rb$/).flatten.pop
37
- #@@tweaks[name.to_sym] = Tweak.new(name)
38
- if defined? Rails
39
- require_dependency aTweakFile
40
- else
41
- ::Kernel.load(aTweakFile)
42
- end
43
- end
44
-
45
- def self.enable(*aTweakNames)
46
- aTweakNames = [aTweakNames] unless aTweakNames.is_a?(Array)
47
- aTweakNames.each do |n|
48
- @@switch[n.to_sym] = true
49
- end
50
- end
51
-
52
- def self.disable(*aTweakNames)
53
- aTweakNames = [aTweakNames] unless aTweakNames.is_a?(Array)
54
- aTweakNames.each do |n|
55
- @@switch[n.to_sym] = false
56
- end
57
- end
58
-
59
- def self.header_enabled?(aTweak)
60
- end
61
-
62
- def self.header_disabled?(aTweak)
63
- end
64
-
65
- # pass a hash to set
66
- # returns config hash for modification
67
- def self.config_for(aTweak,aHash=nil)
68
- aTweak = aTweak.to_sym
69
- if !(cf = @@configs[aTweak])
70
- cf = {}
71
- @@configs[aTweak] = cf
72
- end
73
- if aHash
74
- if cf.is_a?(ConfigClass)
75
- cf.reset()
76
- cf.read(aHash)
77
- else
78
- @@configs[aTweak] = cf = aHash
79
- end
80
- end
81
- cf
82
- end
83
-
84
- # pass a hash to set
85
- # returns config hash for modification
86
- #def self.defaults_for(aTweak,aHash=nil)
87
- # c = config
88
- #end
89
-
90
- # called by tweak code (not sure if necessary)
91
- # aDefaults creates an optional config object for this tweak with the given defaults and previously given values
92
- # aNormally :enabled or :disabled
93
- def self.define(aName,aNormally,aDefaults=nil)
94
- config = @@configs[aName.to_sym]
95
- @@configs[aName.to_sym] = config = ConfigClass.new(aDefaults,config) if aDefaults
96
- @@switch_default[aName.to_sym] = (aNormally==:enabled || aNormally==true ? true : false)
97
- en = enabled?(aName)
98
- yield(config,aName) if en
99
- en
100
- end
101
-
102
- def self.enabled?(aTweakName)
103
- @@switch[aTweakName.to_sym]==nil ? @@switch_default[aTweakName.to_sym] : @@switch[aTweakName.to_sym]
104
- end
105
-
106
- end
107
- end
data/rails/init.rb DELETED
@@ -1,16 +0,0 @@
1
- #gem_root = File.expand_path(File.join(File.dirname(__FILE__), ".."))
2
- #Cms.add_to_rails_paths gem_root
3
- #
4
- #throw RuntimeError.new('thumbs_cache folder must exist') unless File.exists? APP_CONFIG[:thumbs_cache]
5
- #
6
- #Cms::PageHelper.module_eval do
7
- #
8
- # extend BcmsTools::PageHelper
9
- #
10
- #end
11
- #
12
- config.after_initialize do
13
- #Buzzcore::Tweaks.rails_config = config
14
- Buzzcore::Tweaks.load_all(File.join(File.dirname(__FILE__),'../tweaks'))
15
- Buzzcore::Tweaks.load_all(File.join(RAILS_ROOT,'tweaks'))
16
- end