webget_ramp 1.7.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data.tar.gz.sig +0 -0
  2. data/lib/webget_ramp.rb +227 -0
  3. data/lib/webget_ramp/active_record.rb +119 -0
  4. data/lib/webget_ramp/active_record/connection_adapters/abstract/schema_statements.rb +24 -0
  5. data/lib/webget_ramp/array.rb +369 -0
  6. data/lib/webget_ramp/csv.rb +52 -0
  7. data/lib/webget_ramp/date.rb +87 -0
  8. data/lib/webget_ramp/enumerable.rb +369 -0
  9. data/lib/webget_ramp/file.rb +13 -0
  10. data/lib/webget_ramp/hash.rb +195 -0
  11. data/lib/webget_ramp/integer.rb +20 -0
  12. data/lib/webget_ramp/io.rb +63 -0
  13. data/lib/webget_ramp/kernel.rb +34 -0
  14. data/lib/webget_ramp/math.rb +18 -0
  15. data/lib/webget_ramp/nil.rb +9 -0
  16. data/lib/webget_ramp/numeric.rb +94 -0
  17. data/lib/webget_ramp/object.rb +18 -0
  18. data/lib/webget_ramp/process.rb +153 -0
  19. data/lib/webget_ramp/string.rb +220 -0
  20. data/lib/webget_ramp/symbol.rb +10 -0
  21. data/lib/webget_ramp/time.rb +9 -0
  22. data/lib/webget_ramp/xml.rb +120 -0
  23. data/lib/webget_ramp/yaml.rb +32 -0
  24. data/test/webget_ramp/active_record/connection_adapters/abstract/schema_statements_test.rb +9 -0
  25. data/test/webget_ramp/active_record_test.rb +64 -0
  26. data/test/webget_ramp/array_test.rb +171 -0
  27. data/test/webget_ramp/csv_test.rb +18 -0
  28. data/test/webget_ramp/date_test.rb +60 -0
  29. data/test/webget_ramp/enumerable_test.rb +271 -0
  30. data/test/webget_ramp/file_test.rb +15 -0
  31. data/test/webget_ramp/hash_test.rb +105 -0
  32. data/test/webget_ramp/integer_test.rb +19 -0
  33. data/test/webget_ramp/io_test.rb +31 -0
  34. data/test/webget_ramp/io_test.txt +1 -0
  35. data/test/webget_ramp/kernel_test.rb +15 -0
  36. data/test/webget_ramp/math_test.rb +17 -0
  37. data/test/webget_ramp/nil_test.rb +11 -0
  38. data/test/webget_ramp/numeric_test.rb +28 -0
  39. data/test/webget_ramp/object_test.rb +12 -0
  40. data/test/webget_ramp/process_test.rb +24 -0
  41. data/test/webget_ramp/string_test.rb +125 -0
  42. data/test/webget_ramp/symbol_test.rb +26 -0
  43. data/test/webget_ramp/time_test.rb +12 -0
  44. data/test/webget_ramp/xml_test.rb +50 -0
  45. data/test/webget_ramp/xml_test_1.xml +5 -0
  46. data/test/webget_ramp/xml_test_2.xml +5 -0
  47. data/test/webget_ramp/yaml_test.rb +32 -0
  48. data/test/webget_ramp/yaml_test_1.yml +38 -0
  49. data/test/webget_ramp/yaml_test_2.yml +38 -0
  50. metadata +124 -0
  51. metadata.gz.sig +0 -0
@@ -0,0 +1,18 @@
1
+ class Object
2
+
3
+ # Syntactic sugar for arrays.
4
+ #
5
+ # ==Definition
6
+ # object.in? array === array.include? object
7
+ #
8
+ # ==Example
9
+ # array=['a','b','c']
10
+ # object='b'
11
+ # object.in? array
12
+ # => true
13
+
14
+ def in?(array)
15
+ array.include?(self)
16
+ end
17
+
18
+ end
@@ -0,0 +1,153 @@
1
+ # Process extensions to help debug Ruby programs.
2
+ #
3
+ # ==Examples
4
+ #
5
+ # p = Process.ps
6
+ # puts p
7
+ # => the results of the 'ps' command for the current process id
8
+ #
9
+ # p = Process.ps(1234)
10
+ # puts p
11
+ # => the results of the 'ps' command for process id 1234
12
+ #
13
+ # p = Process.pss
14
+ # p['%cpu'] => percentage of cpu use, as a float
15
+ # p['%mem'] => percentage of memory use, as a float
16
+ ##
17
+
18
+ module Process
19
+
20
+
21
+ # Get the 'ps' command as one long text string.
22
+ #
23
+ # This is typically useful for logging to a text file.
24
+
25
+ def self.ps(pid=Process.pid)
26
+ `#{self.ps_command} #{pid.to_i}`
27
+ end
28
+
29
+
30
+ # Get the 'ps' command as a hash of keys and values.
31
+ # -
32
+ # OPTIMIZE: add dates, times
33
+
34
+ def self.pss(pid=Process.pid)
35
+ ps=self.ps(pid)
36
+ h=Hash[*self.ps_keys.zip(ps.split).flatten]
37
+ h['c'] =h['c'].to_i
38
+ h['cp'] =h['cp'].to_f
39
+ h['egid'] =h['egid'].to_i
40
+ h['egroup'] =h['egroup'].to_i
41
+ h['uid'] =h['uid'].to_i
42
+ h['fgid'] =h['fgid'].to_i
43
+ h['lwp'] =h['lwp'].to_i
44
+ h['ni'] =h['ni'].to_i
45
+ h['nlwp'] =h['nlwp'].to_i
46
+ h['pcpu'] =h['pcpu'].to_f
47
+ h['pgid'] =h['pgid'].to_i
48
+ h['pid'] =h['pid'].to_i
49
+ h['pmem'] =h['pmem'].to_f
50
+ h['ppid'] =h['ppid'].to_i
51
+ h['rgid'] =h['rgid'].to_i
52
+ h['rss'] =h['rss'].to_i
53
+ h['ruid'] =h['ruid'].to_i
54
+ h['sid'] =h['sid'].to_i
55
+ h['sgid'] =h['sgid'].to_i
56
+ h['suid'] =h['suid'].to_i
57
+ self.ps_aliases.each_pair{|k,v| h[k]=h[v]}
58
+ return h
59
+ end
60
+
61
+ # Get the list of process alias keywords as typically defined by the shell.
62
+ #
63
+ # For example, a shell may consider "%cpu" and "pcpu" to be identical.
64
+
65
+ def self.ps_aliases
66
+ @@ps_aliases||=Hash[*%w'
67
+ %cpu pcpu
68
+ %mem pmem
69
+ sig_block blocked
70
+ sigmask blocked
71
+ sig_catch caught
72
+ sigcatch caught
73
+ cls class
74
+ cls policy
75
+ cputime time
76
+ gid egid
77
+ group egroup
78
+ uid euid
79
+ uname euser
80
+ user euser
81
+ flag f
82
+ flags f
83
+ fsuid fuid
84
+ sig_ignore ignored
85
+ sigignore ignored
86
+ spid lwp
87
+ tid lwp
88
+ nice ni
89
+ thcount nlwp
90
+ sig pending
91
+ sig_pend pending
92
+ pgrp pgid
93
+ rssize rss
94
+ rsz rss
95
+ state s
96
+ sess sid
97
+ session sid
98
+ svgid sgid
99
+ tt tname
100
+ tty tname
101
+ vsz vsize
102
+ ']
103
+ end
104
+
105
+
106
+ # Set the list of process alias keywords.
107
+
108
+ def self.ps_aliases=(aliases)
109
+ @@ps_aliases=aliases
110
+ end
111
+
112
+
113
+ # Get the list of process keywords.
114
+ #
115
+ # ==Example
116
+ # Process.ps_keys => ["blocked","group","pending","size"]
117
+
118
+ def self.ps_keys
119
+ @@ps_keys||=%w'blocked bsdtime c caught class cp egid egroup eip esp etime euid euser f fgid fgroup fuid fuser group ignored label lwp ni nlwp nwchan pending pcpu pgid pid pmem ppid pri psr rgid rgroup rss rtprio ruid ruser s sched sgi_p sgid sgroup sid sig size stackp start_time stat suid suser sz time tname tpgid vsize wchan'
120
+ end
121
+
122
+
123
+ # Set the list of process keywords.
124
+ #
125
+ # ==Example
126
+ # Process.ps_keys = ["blocked","group","pending","size"]
127
+
128
+ def self.ps_keys=(keys)
129
+ @@ps_keys=keys
130
+ end
131
+
132
+
133
+ # Get the process command, i.e. what the sytem will call for the "ps" command.
134
+ #
135
+ # ==Example
136
+ # Process.ps_command => "ps h ww -o blocked,group,pending,size"
137
+
138
+ def self.ps_command
139
+ @@ps_command||='ps h ww -o "'+self.ps_keys.join(',')+'"'
140
+ end
141
+
142
+
143
+ # Set the process command, i.e. what the sytem will call for the "ps" command.
144
+ #
145
+ # ==Example
146
+ # Process.ps_command = "ps h ww -o blocked,group,pending,size"
147
+
148
+ def self.ps_command=(command)
149
+ @@ps_comannd=command
150
+ end
151
+
152
+
153
+ end
@@ -0,0 +1,220 @@
1
+ # -*- coding: utf-8 -*-
2
+ class String
3
+
4
+ ACCENTS = Hash[*'
5
+ à a á a â a ã a ä a å a ā a ă a
6
+ æ ae
7
+ ď d đ d
8
+ ç c ć c č c ĉ c ċ c
9
+ è e é e ê e ë e ē e ę e ě e ĕ e ė e
10
+ ƒ f
11
+ ĝ g ğ g ġ g ģ g
12
+ ĥ h ħ h
13
+ ì i ì i í i î i ï i ī i ĩ i ĭ i
14
+ į j ı j ij j ĵ j
15
+ ķ k ĸ k
16
+ ł l ľ l ĺ l ļ l ŀ l
17
+ ñ n ń n ň n ņ n ʼn n ŋ n
18
+ ò o ó o ô o õ o ö o ø o ō o ő o ŏ o ŏ o
19
+ œ oek
20
+ ą q
21
+ ŕ r ř r ŗ r
22
+ ś s š s ş s ŝ s ș s
23
+ ť t ţ t ŧ t ț t
24
+ ù u ú u û u ü u ū u ů u ű u ŭ u ũ u ų u
25
+ ŵ w
26
+ ý y ÿ y ŷ y
27
+ ž z ż z ź z
28
+ '.split]
29
+
30
+
31
+ # Return the string with words capitalized
32
+
33
+ def capitalize_words
34
+ split(/\b/).map{|x| x.capitalize }.join
35
+ end
36
+
37
+
38
+ # Return an array that is the string split into words, i.e. split(\W*\b\*)
39
+
40
+ def words
41
+ split(/\W*\b\W*/)
42
+ end
43
+
44
+
45
+ # Return an array that is the string split at tabs, i.e. split(/\t/)
46
+
47
+ def split_tab
48
+ split(/\t/)
49
+ end
50
+
51
+
52
+ # Return an array that is the string split at newlines, then tabs.
53
+ # This is useful to split a TSV (Tab Separated Values) string
54
+ # into an array of rows, and each row into an array of fields.
55
+
56
+ def split_tsv
57
+ split(/\n/).map{|x| x.split(/\t/)}
58
+ end
59
+
60
+
61
+ # Return the string in lowercase, with any non-word-characters
62
+ # replaced with single underscores (aka low dashes).
63
+ #
64
+ # ==Example
65
+ # 'Foo Goo Hoo' => 'foo_goo_hoo'
66
+ # 'Foo***Goo***Hoo' => 'foo_goo_hoo'
67
+
68
+ def lowcase
69
+ downcase.gsub(/[_\W]+/,'_')
70
+ end
71
+
72
+
73
+ # Return the string as an XML id, which is the same as #lowcase
74
+ #
75
+ # ==Example
76
+ # "Foo Hoo Goo" => 'foo_goo_hoo'
77
+ # "Foo***Goo***Hoo" => 'foo_goo_hoo'
78
+
79
+ def to_xid
80
+ self.lowcase
81
+ end
82
+
83
+ # Ruby String#to_class method to convert from a String to a class
84
+ #
85
+ # From Mirage at http://infovore.org/archives/2006/08/02/getting-a-class-object-in-ruby-from-a-string-containing-that-classes-name/
86
+
87
+ def to_class
88
+ split('::').inject(Kernel) {|scope, const_name| scope.const_get(const_name)}
89
+ end
90
+
91
+
92
+ # Increment the rightmost natural number
93
+ #
94
+ # ==Example
95
+ # 'foo5bar'.increment => 'foo4bar'
96
+ # 'foo5bar'.increment(3) => 'foo8bar'
97
+ # 'foo9bar'.increment => 'foo10bar'
98
+ #
99
+ # - see String#decrement
100
+
101
+ def increment(step=1)
102
+ self=~/\d+/ ? $`+($&.to_i+step).to_s+$' : self
103
+ end
104
+
105
+
106
+ # Decrement the rightmost natural number
107
+ #
108
+ # ==Example
109
+ # 'foo5bar'.decrement => 'foo4bar'
110
+ # 'foo5bar'.decrement(3) => 'foo2bar'
111
+ # 'foo10bar'.derement => 'foo9bar'
112
+ #
113
+ # - see String#increment
114
+
115
+ def decrement(step=1)
116
+ self=~/\d+/ ? $`+($&.to_i-step).to_s+$' : self
117
+ end
118
+
119
+
120
+ # Return the previous character, with a changed flag and carry flag
121
+ #
122
+ # ==Examples
123
+ # String.prev_char('n') => 'm', true, false # change
124
+ # String.prev_char('a') => 'z', true, true # change & carry
125
+ # String.prev_char('6') => '5', true, false # change
126
+ # String.prev_char('0') => '9', true, true # change & carry
127
+ # String.prev_char('-') => '-', false, false # unchanged
128
+
129
+ def self.prev_char(c) #=> prev_char, changed_flag, carry_flag
130
+ case c
131
+ when '1'..'9', 'B'..'Z', 'b'..'z'
132
+ i=(c.respond_to?(:ord) ? c.ord : c[0])
133
+ return (i-1).chr, true, false
134
+ when '0'
135
+ return '9', true, true
136
+ when 'A'
137
+ return 'Z', true, true
138
+ when 'a'
139
+ return 'z', true, true
140
+ else
141
+ return c, false, false
142
+ end
143
+ end
144
+
145
+ # Return the previous string
146
+ #
147
+ # c.f. String#next
148
+ #
149
+ # ==Examples
150
+ # '888'.prev => '887'
151
+ # 'n'.prev => 'm'
152
+ # 'N'.prev => 'M'
153
+ #
154
+ # ==Examples with carry
155
+ # '880'.prev => '879'
156
+ # 'nna'.prev => 'nmz'
157
+ # 'NNA'.prev => 'NMZ'
158
+ # 'nn0aA'.prev => 'nm9zZ'
159
+
160
+ def prev
161
+ self.clone.prev!
162
+ end
163
+
164
+
165
+ # Do String#prev in place
166
+
167
+ def prev!
168
+ return self if length==0
169
+ i=length-1 # rightmost
170
+ while true do
171
+ c=self[i].chr
172
+ prev_c,changed_flag,carry_flag=String.prev_char(c)
173
+ return self if !changed_flag
174
+ self[i]=prev_c
175
+ return self if !carry_flag
176
+ i-=1
177
+ return nil if i<0
178
+ end
179
+ end
180
+
181
+ alias pred prev # String#pred : predecessor :: String#succ : successor
182
+ alias pred! prev!
183
+
184
+ class << self
185
+ alias_method :pred_char, :prev_char
186
+ end
187
+
188
+ # Helpful constants
189
+
190
+ LOWERCASE_ENGLISH_CHARS = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
191
+ UPPERCASE_ENGLISH_CHARS = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
192
+
193
+
194
+ ##
195
+ #
196
+ # Lorem Ipsum random text generator
197
+ #
198
+ ##
199
+
200
+ # Return a random length suitable for a "lorem ipsum" string.
201
+ #
202
+ # This method uses 1+rand(10)
203
+
204
+ def self.lorem_length
205
+ 1+rand(10)
206
+ end
207
+
208
+ # Return a random string suitable for "lorem ipsum" text.
209
+ #
210
+ # This method chooses from lowercase letters a-z.
211
+ #
212
+ # This method defaults to length = self.lorem_length.
213
+
214
+ def self.lorem(length=self.lorem_length)
215
+ ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'].choices(length).join
216
+ end
217
+
218
+
219
+ end
220
+
@@ -0,0 +1,10 @@
1
+ # -*- coding: utf-8 -*-
2
+ class Symbol
3
+
4
+ include Comparable
5
+ def <=>(that)
6
+ self.to_s<=>that.to_s
7
+ end
8
+
9
+ end
10
+
@@ -0,0 +1,9 @@
1
+ class Time
2
+
3
+ # Return current time in UTC as a timestamp string "YYYYMMDDHHMMSS"
4
+
5
+ def self.stamp
6
+ now.utc.strftime('%Y%m%d%H%M%S')
7
+ end
8
+
9
+ end
@@ -0,0 +1,120 @@
1
+ require 'rexml/document'
2
+
3
+ module XML
4
+
5
+
6
+ # Specify one or more directory patterns and pass each XML file in the matching directories to a block.
7
+ #
8
+ # See [Dir#glob](http://www.ruby-doc.org/core/classes/Dir.html#M002347) for pattern details.
9
+ #
10
+ # ==Example
11
+ # XML.load_dir('/tmp/*.xml'){|xml_document|
12
+ # #...whatever you want to do with each xml document
13
+ # }
14
+ #
15
+ # ==Example to load xml documents in files beginning in "foo" or "bar"
16
+ # XML.load_dir('/tmp/foo*.yaml','/tmp/bar*.xml','){|xml_document|
17
+ # #...whatever you want to do with the xml document
18
+ # }
19
+
20
+ def XML.load_dir(*dirpaths)
21
+ dirpaths=[*dirpaths.flatten]
22
+ dirpaths.each do |dirpath|
23
+ Dir[dirpath].sort.each do |filename|
24
+ File.open(filename) do |file|
25
+ doc = REXML::Document.new file
26
+ yield doc
27
+ end #file
28
+ end #dir
29
+ end #each
30
+ end #def
31
+
32
+
33
+ # Sugar to load elements from a file.
34
+ #
35
+ # ==Example
36
+ # XML.load_attributes('config.xml','userlist/user'){|element| pp element.attributes['first_name'] }
37
+
38
+ def XML.load_elements(dirpath,xpath)
39
+ XML.load_dir(dirpath){|doc|
40
+ doc.elements.each(xpath){|e|
41
+ yield e
42
+ }
43
+ }
44
+ end
45
+
46
+
47
+ # Sugar to load attributes from a file.
48
+ #
49
+ # ==Example
50
+ # XML.load_attributes('config.xml','userlist/user'){|attributes| pp attributes['first_name'] }
51
+
52
+ def XML.load_attributes(dirpath,xpath)
53
+ XML.load_elements(dirpath,xpath){|e|
54
+ yield e.attributes
55
+ }
56
+ end
57
+
58
+ # Sugar to load attributes hash from a file.
59
+ #
60
+ # ==Example
61
+ # XML.load_attributes('config.xml','userlist/user'){|attributes| pp attributes['first_name'] }
62
+
63
+ def XML.load_attributes_hash(dirpath,xpath)
64
+ XML.load_elements(dirpath,xpath){|e|
65
+ yield e.attributes.to_hash
66
+ }
67
+ end
68
+
69
+ end
70
+
71
+
72
+ class REXML::Attributes
73
+
74
+ # Return a new hash of the attribute keys and values.
75
+ #
76
+ # ==Example
77
+ # attributes.to_hash => {"src"=>"pic.jpg", "height" => "100", "width" => "200"}
78
+
79
+ def to_hash
80
+ h=Hash.new
81
+ self.keys.each{|k| h[k]=self[k]}
82
+ h
83
+ end
84
+
85
+ end
86
+
87
+
88
+ class REXML::Document
89
+
90
+ # Remove all attributes from the document's elements.
91
+ #
92
+ # Return the document.
93
+ #
94
+ # cf. Element#remove_attributes
95
+
96
+ def remove_attributes
97
+ self.elements.each("//") { |e| e.attributes.each_attribute{|attribute| attribute.remove }}
98
+ self
99
+ end
100
+
101
+ end
102
+
103
+
104
+ class REXML::Element
105
+
106
+ # Remove all attributes from the element.
107
+ #
108
+ # Return the element.
109
+ #
110
+ # cf. Document#remove_attributes
111
+
112
+ def remove_attributes
113
+ self.attributes.each_attribute{|attribute| attribute.remove }
114
+ self
115
+ end
116
+
117
+ end
118
+
119
+
120
+