webget_ramp 1.7.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,13 @@
1
+ class File
2
+
3
+ # Return File.join(File.dirname(dirname),strings)
4
+ #
5
+ # ==Example
6
+ # File.joindir(__FILE__,'foo.txt')
7
+ # => '/home/john/foo.txt'
8
+
9
+ def File.joindir(dirname,*strings)
10
+ File.join(File.dirname(dirname),strings)
11
+ end
12
+
13
+ end
@@ -0,0 +1,195 @@
1
+ require 'yaml'
2
+
3
+ class Hash
4
+
5
+
6
+ # Return true if size > 0
7
+ def size?
8
+ size>0
9
+ end
10
+
11
+
12
+ # Calls block once for each key in hsh, passing the key and value to the block as a two-element array.
13
+ #
14
+ # The keys are sorted.
15
+
16
+ def each_sort
17
+ keys.sort.each{|key| yield key,self[key] }
18
+ end
19
+
20
+
21
+ # Calls block once for each key in hsh,
22
+ # passing the key as a parameter,
23
+ # and updating it in place.
24
+ #
25
+ # ==Example
26
+ # h = { "a" => "b", "c" => "d" }
27
+ # h.each_key! {|key| key.upcase }
28
+ # h => { "A" => "b", "C" => "d" }
29
+ #
30
+ # Return self.
31
+
32
+ def each_key!
33
+ each_pair{|key,value|
34
+ key2=yield(key)
35
+ if key===key2
36
+ #nop
37
+ else
38
+ self.delete(key)
39
+ self[key2]=value
40
+ end
41
+ }
42
+ end
43
+
44
+
45
+ # Calls block once for each key in hsh,
46
+ # passing the key and value as parameters,
47
+ # and updated them in place.
48
+ #
49
+ # ==Example
50
+ # h = { "a" => "b", "c" => "d" }
51
+ # h.each_pair! {|key,value| key.upcase, value.upcase }
52
+ # h => { "A" => "B", "C" => "D" }
53
+ #
54
+ # Return self.
55
+
56
+ def each_pair!
57
+ each_pair{|key,value|
58
+ key2,value2=yield(key,value)
59
+ if key===key2
60
+ if value===value2
61
+ #nop
62
+ else
63
+ self[key]=value2
64
+ end
65
+ else
66
+ self.delete(key)
67
+ self[key2]=value2
68
+ end
69
+ }
70
+ return self
71
+ end
72
+
73
+
74
+ # Calls block once for each key in hsh,
75
+ # passing the value as a parameter,
76
+ # and updating it in place.
77
+ #
78
+ # ==Example
79
+ # h = { "a" => "b", "c" => "d" }
80
+ # h.each_value! {|value| value.upcase }
81
+ # h => { "a" => "B", "c" => "d" }
82
+ #
83
+ # Return self.
84
+
85
+ def each_value!
86
+ each_pair{|key,value|
87
+ value2=yield(value)
88
+ if value===value2
89
+ #nop
90
+ else
91
+ self[key]=yield(value)
92
+ end
93
+ }
94
+ return self
95
+ end
96
+
97
+
98
+ # Calls block once for each key-value pair in hsh,
99
+ # passing the key and value as paramters to the block.
100
+ #
101
+ # ==Example
102
+ # h = {"a"=>"b", "c"=>"d", "e"=>"f" }
103
+ # h.map_pair{|key,value| key+value }
104
+ # => ["ab","cd","ef"]
105
+
106
+ def map_pair
107
+ keys.map{|key| yield key, self[key] }
108
+ end
109
+
110
+
111
+ # Hash#to_yaml with sort
112
+ #
113
+ # From http://snippets.dzone.com/tag/yaml
114
+
115
+ def to_yaml_sort( opts = {} )
116
+ YAML::quick_emit( object_id, opts ) do |out|
117
+ out.map( taguri, to_yaml_style ) do |map|
118
+ sort.each do |k, v| # <-- here's my addition (the 'sort')
119
+ map.add( k, v )
120
+ end
121
+ end
122
+ end
123
+ end
124
+
125
+
126
+ # Hash#pivot aggregates values for a hash of hashes,
127
+ # for example to calculate subtotals and groups.
128
+ #
129
+ # Example:
130
+ # h={
131
+ # "a"=>{"x"=>1,"y"=>2,"z"=>3},
132
+ # "b"=>{"x"=>4,"y"=>5,"z"=>6},
133
+ # "c"=>{"x"=>7,"y"=>8,"z"=>9},
134
+ # }
135
+ # h.pivot(:keys) => {"a"=>[1,2,3],"b"=>[4,5,6],"c"=>[7,8,9]}
136
+ # h.pivot(:vals) => {"x"=>[1,4,7],"y"=>[2,5,8],"z"=>[3,6,9]}
137
+ #
138
+ # = Calculating subtotals
139
+ #
140
+ # The pivot method is especially useful for calculating subtotals.
141
+ #
142
+ # ==Example
143
+ # r = h.pivot(:keys)
144
+ # r['a'].sum => 6
145
+ # r['b'].sum => 15
146
+ # r['c'].sum => 24
147
+ #
148
+ # ==Example
149
+ # r=h.pivot(:vals)
150
+ # r['x'].sum => 12
151
+ # r['y'].sum => 15
152
+ # r['z'].sum => 18
153
+ #
154
+ # = Block customization
155
+ #
156
+ # You can provide a block that will be called for the pivot items.
157
+ #
158
+ # ==Examples
159
+ # h.pivot(:keys){|items| items.max } => {"a"=>3,"b"=>6,"c"=>9}
160
+ # h.pivot(:keys){|items| items.join("/") } => {"a"=>"1/2/3","b"=>"4/5/6","c"=>"7/8/9"}
161
+ # h.pivot(:keys){|items| items.inject{|sum,x| sum+=x } } => {"a"=>6,"b"=>15,"c"=>24}
162
+ #
163
+ # ==Examples
164
+ # h.pivot(:vals){|items| items.max } => {"a"=>7,"b"=>8,"c"=>9}
165
+ # h.pivot(:vals){|items| items.join("-") } => {"a"=>"1-4-7","b"=>"2-5-8","c"=>"3-6-9"}
166
+ # h.pivot(:vals){|items| items.inject{|sum,x| sum+=x } } => {"a"=>12,"b"=>15,"c"=>18}
167
+
168
+ def pivot(direction='keys',&b)
169
+ a=self.class.new
170
+ direction=direction.to_s
171
+ up=pivot_direction_up?(direction)
172
+ each_pair{|k1,v1|
173
+ v1.each_pair{|k2,v2|
174
+ k = up ? k1 : k2
175
+ a[k]=[] if (a[k]==nil or a[k]=={})
176
+ a[k]<<(v2)
177
+ }
178
+ }
179
+ if block_given?
180
+ a.each_pair{|k,v| a[k]=(yield v)}
181
+ end
182
+ a
183
+ end
184
+
185
+ protected
186
+
187
+ def pivot_direction_up?(direction_name)
188
+ case direction_name
189
+ when 'key','keys','up','left','out' then return true
190
+ when 'val','vals','down','right','in' then return false
191
+ else raise 'Pivot direction must be either: up/left/out or down/right/in'
192
+ end
193
+ end
194
+
195
+ end
@@ -0,0 +1,20 @@
1
+ class Integer
2
+
3
+ # Syntactic sugar to yield n times to a block.
4
+ #
5
+ # Return an array of any results.
6
+ #
7
+ # ==Example
8
+ # 3.maps{rand} => [0.0248131784304143, 0.814666170190905, 0.15812816258206]
9
+ #
10
+ # ==Parallel to Integer#times
11
+ #
12
+ # Integer#maps is similar to Integer#times except that the output from each
13
+ # call to the block is captured in an array element and that array is
14
+ # returned to the calling code.
15
+
16
+ def maps
17
+ (0...self).map{|i| yield i}
18
+ end
19
+
20
+ end
@@ -0,0 +1,63 @@
1
+ class IO
2
+
3
+
4
+ # Reads the entire file specified by name as individual lines as with IO#readlines,
5
+ # and returns those lines in an array of rows, where each row is an array of fields.
6
+ #
7
+ # ==Example
8
+ # IO.readrows("my.tsv")
9
+ # => [["A1","B1","C1"],["A2","B2","C2"],["A3","B3","C3"]]
10
+ #
11
+ # ==Options
12
+ # - Rows are separated by _row_ option, which defaults to Ruby's record separator $/ or "\n"
13
+ # - Cols are separated by _col_ option, which defaults to Ruby's string split separator $; or "\t"
14
+ #
15
+ # ==Example with options suitable for a file using TSV (Tab Separated Values)
16
+ # IO.readrows("my.tsv", :row=>"\n", :col=>"\t")
17
+ #
18
+ # Note: the col option is sent along to String#split, so can be a string or a regexp.
19
+ #
20
+ # See:
21
+ # - File#readline
22
+ # - File#readlines
23
+ # - File#readrow
24
+
25
+ def IO.readrows(name, options={})
26
+ row_sep||=options[:row]||$/||"\n"
27
+ col_sep||=options[:col]||$;||"\t"
28
+ return IO.readlines(name, row_sep).map{|line| line.chomp(row_sep).split(col_sep)}
29
+ end
30
+
31
+
32
+ # Read a line as with IO#readline and return the line as a row of fields.
33
+ #
34
+ # ==Example
35
+ # file = File.new("my.tsv")
36
+ # file.readrow() => ["A1","B1","C1"]
37
+ # file.readrow() => ["A2","B2","C2"]
38
+ # file.readrow() => ["A3","B3","C3"]]
39
+ #
40
+ # ==Options
41
+ # - Rows are separated by _row_ option, which defaults to Ruby's record separator $/ or "\n"
42
+ # - Cols are separated by _col_ option, which defaults to Ruby's string split separator $; or "\t"
43
+ #
44
+ # ==Example with options suitable for a file using TSV (Tab Separated Values)
45
+ # file = File.new("my.tsv")
46
+ # file.readrow(:row=>"\n", :col=>"\t") => ["A1","B1","C1"]
47
+ # file.readrow(:row=>"\n", :col=>"\t") => ["A2","B2","C2"]
48
+ # file.readrow(:row=>"\n", :col=>"\t") => ["A3","B3","C3"]
49
+ #
50
+ # Note: the col option is sent along to String#split, so can be a string or a regexp.
51
+ #
52
+ # See:
53
+ # - File#readline
54
+ # - File#readlines
55
+ # - File#readrows
56
+
57
+ def readrow(options={})
58
+ row_sep||=options[:row]||$/||"\n"
59
+ col_sep||=options[:col]||$;||"\t"
60
+ return readline(row_sep).chomp(row_sep).split(col_sep)
61
+ end
62
+
63
+ end
@@ -0,0 +1,34 @@
1
+ require 'pp'
2
+ require 'stringio'
3
+
4
+ module Kernel
5
+
6
+ # See:
7
+ # - http://www.ruby-forum.com/topic/75258
8
+ # - In 1.9 (Ruby CVS HEAD) there is #__method__ and #__callee__
9
+ # - http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l90
10
+
11
+ def method_name(caller_index=0)
12
+ # Make this fast because its often doing logging & reporting.
13
+ # Inline this and use $1 because it's empirically faster than /1
14
+ caller[caller_index] =~ /`([^']*)'/ and $1
15
+ end
16
+
17
+ # Pretty print to a string.
18
+ #
19
+ # Created by Graeme Mathieson.
20
+ #
21
+ # See http://rha7dotcom.blogspot.com/2008/07/ruby-and-rails-how-to-get-pp-pretty.html
22
+
23
+ def pp_s(*objs)
24
+ s = StringIO.new
25
+ objs.each {|obj|
26
+ PP.pp(obj, s)
27
+ }
28
+ s.rewind
29
+ s.read
30
+ end
31
+ module_function :pp_s
32
+
33
+ end
34
+
@@ -0,0 +1,18 @@
1
+ module Math
2
+
3
+
4
+ # Return the natural log of x
5
+
6
+ def Math.ln(x)
7
+ Math.log(x)
8
+ end
9
+
10
+
11
+ # Return the log of x in base b.
12
+
13
+ def Math.logn(x,b)
14
+ Math.ln(x)/Math.ln(b)
15
+ end
16
+
17
+
18
+ end
@@ -0,0 +1,9 @@
1
+ class NilClass
2
+
3
+ # Return false
4
+ def size?
5
+ return false
6
+ end
7
+
8
+ end
9
+
@@ -0,0 +1,94 @@
1
+ class Numeric
2
+
3
+
4
+ # Return 0 if the given flag is any of: nil, false, 0, [], {}
5
+ #
6
+ # This is useful for multiplying a number if and only if a flag is set.
7
+ #
8
+ # ==Example
9
+ # sum = x.if(foo) + y.if(bar)
10
+
11
+ def if(flag)
12
+ (flag==nil or flag==false or flag==0 or flag==[] or flag=={}) ? 0 : self
13
+ end
14
+
15
+
16
+ # Return self if flag is nil, false, 0, [], {}; otherwise return 0.
17
+ #
18
+ # This is useful for multiplying a number if and only if a flag is unset.
19
+ #
20
+ # ==Example
21
+ # sum = w.unless(foo) +y.unless(bar)
22
+
23
+ def unless(flag)
24
+ (flag==nil or flag==false or flag==0 or flag==[] or flag=={}) ? self : 0
25
+ end
26
+
27
+
28
+ ###
29
+ # Metric conversions
30
+ #
31
+ ###
32
+
33
+ # Return self / 10^15
34
+ def peta
35
+ self/1000000000000000
36
+ end
37
+
38
+ # Return self / 10^12
39
+ def tera
40
+ self/1000000000000
41
+ end
42
+
43
+ # Return self / 10^9
44
+ def giga
45
+ self/1000000000
46
+ end
47
+
48
+ # Return self / 10^6
49
+ def mega
50
+ self/100000
51
+ end
52
+
53
+ # Return self / 10^3
54
+ def kilo
55
+ self/1000
56
+ end
57
+
58
+ # Return self / 10^2
59
+ def hecto
60
+ self/100
61
+ end
62
+
63
+ # Return self / 10
64
+ def deka
65
+ self/10
66
+ end
67
+
68
+ # Return self * 10
69
+ def deci
70
+ self*10
71
+ end
72
+
73
+ # Return self * 10^2
74
+ def centi
75
+ self*100
76
+ end
77
+
78
+ # Return self * 10^3
79
+ def milli
80
+ self*1000
81
+ end
82
+
83
+ # Return self * 10^6
84
+ def micro
85
+ self*1000000
86
+ end
87
+
88
+ # Return self * 10^9
89
+ def nano
90
+ self*1000000000
91
+ end
92
+
93
+
94
+ end