webget_ramp 1.7.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data.tar.gz.sig +0 -0
- data/lib/webget_ramp.rb +227 -0
- data/lib/webget_ramp/active_record.rb +119 -0
- data/lib/webget_ramp/active_record/connection_adapters/abstract/schema_statements.rb +24 -0
- data/lib/webget_ramp/array.rb +369 -0
- data/lib/webget_ramp/csv.rb +52 -0
- data/lib/webget_ramp/date.rb +87 -0
- data/lib/webget_ramp/enumerable.rb +369 -0
- data/lib/webget_ramp/file.rb +13 -0
- data/lib/webget_ramp/hash.rb +195 -0
- data/lib/webget_ramp/integer.rb +20 -0
- data/lib/webget_ramp/io.rb +63 -0
- data/lib/webget_ramp/kernel.rb +34 -0
- data/lib/webget_ramp/math.rb +18 -0
- data/lib/webget_ramp/nil.rb +9 -0
- data/lib/webget_ramp/numeric.rb +94 -0
- data/lib/webget_ramp/object.rb +18 -0
- data/lib/webget_ramp/process.rb +153 -0
- data/lib/webget_ramp/string.rb +220 -0
- data/lib/webget_ramp/symbol.rb +10 -0
- data/lib/webget_ramp/time.rb +9 -0
- data/lib/webget_ramp/xml.rb +120 -0
- data/lib/webget_ramp/yaml.rb +32 -0
- data/test/webget_ramp/active_record/connection_adapters/abstract/schema_statements_test.rb +9 -0
- data/test/webget_ramp/active_record_test.rb +64 -0
- data/test/webget_ramp/array_test.rb +171 -0
- data/test/webget_ramp/csv_test.rb +18 -0
- data/test/webget_ramp/date_test.rb +60 -0
- data/test/webget_ramp/enumerable_test.rb +271 -0
- data/test/webget_ramp/file_test.rb +15 -0
- data/test/webget_ramp/hash_test.rb +105 -0
- data/test/webget_ramp/integer_test.rb +19 -0
- data/test/webget_ramp/io_test.rb +31 -0
- data/test/webget_ramp/io_test.txt +1 -0
- data/test/webget_ramp/kernel_test.rb +15 -0
- data/test/webget_ramp/math_test.rb +17 -0
- data/test/webget_ramp/nil_test.rb +11 -0
- data/test/webget_ramp/numeric_test.rb +28 -0
- data/test/webget_ramp/object_test.rb +12 -0
- data/test/webget_ramp/process_test.rb +24 -0
- data/test/webget_ramp/string_test.rb +125 -0
- data/test/webget_ramp/symbol_test.rb +26 -0
- data/test/webget_ramp/time_test.rb +12 -0
- data/test/webget_ramp/xml_test.rb +50 -0
- data/test/webget_ramp/xml_test_1.xml +5 -0
- data/test/webget_ramp/xml_test_2.xml +5 -0
- data/test/webget_ramp/yaml_test.rb +32 -0
- data/test/webget_ramp/yaml_test_1.yml +38 -0
- data/test/webget_ramp/yaml_test_2.yml +38 -0
- metadata +124 -0
- metadata.gz.sig +0 -0
@@ -0,0 +1,52 @@
|
|
1
|
+
|
2
|
+
class CSV
|
3
|
+
|
4
|
+
# Return HTTP headers for a typical CSV file download without caching.
|
5
|
+
#
|
6
|
+
# ==Options
|
7
|
+
# - filename: defaults to "data.csv"
|
8
|
+
# - request: the incoming http request, which is used to return MSIE-specific headers
|
9
|
+
#
|
10
|
+
# ==Example
|
11
|
+
# headers = CSV.http_headers("myfile.csv")
|
12
|
+
#
|
13
|
+
# ==Example for Rails
|
14
|
+
# response.headers.merge CSV.http_headers("myfile.csv")
|
15
|
+
#
|
16
|
+
# Ideas from http://stackoverflow.com/questions/94502/in-rails-how-to-return-records-as-a-csv-file/94520
|
17
|
+
|
18
|
+
def self.http_headers(options={})
|
19
|
+
filename = options[:filename] || 'data.csv'
|
20
|
+
options=self.http_headers_adjust_for_broken_msie(options)
|
21
|
+
content_type = options[:content_type] || 'text/csv'
|
22
|
+
return options[:cache] \
|
23
|
+
? {
|
24
|
+
'Content-Type' => content_type,
|
25
|
+
'Content-Disposition' => "attachment; filename=\"#{filename}\"",
|
26
|
+
} \
|
27
|
+
: {
|
28
|
+
'Content-Type' => content_type,
|
29
|
+
'Cache-Control' => 'no-cache, must-revalidate, post-check=0, pre-check=0',
|
30
|
+
'Expires' => "0",
|
31
|
+
'Pragma' => 'no-cache',
|
32
|
+
'Content-Disposition' => "attachment; filename=\"#{filename}\"",
|
33
|
+
}
|
34
|
+
end
|
35
|
+
|
36
|
+
# Helper to try to "do the right thing" for the common case of Rails & MS IE.
|
37
|
+
#
|
38
|
+
# Rails automatically defines a _request_ object,
|
39
|
+
# that has an env HTTP_USER_AGENT.
|
40
|
+
|
41
|
+
def self.http_headers_adjust_for_broken_msie(options={})
|
42
|
+
request = options[:request] || request
|
43
|
+
msie = (request and request.env['HTTP_USER_AGENT'] =~ /msie/i)
|
44
|
+
if msie
|
45
|
+
options[:content_type]||='text/plain''})'
|
46
|
+
options[:cache]||=false
|
47
|
+
end
|
48
|
+
options
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
3
|
+
class Date
|
4
|
+
|
5
|
+
|
6
|
+
# Return date in a sql format: YYYY-MM-DD
|
7
|
+
#
|
8
|
+
# ==Example
|
9
|
+
# d=Date.today
|
10
|
+
# d.to_sql => "2007-12-31"
|
11
|
+
|
12
|
+
def to_sql
|
13
|
+
return to_time.strftime("%Y-%m-%d")
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
# Return true if the date is a weekday: Mon, Tue, Wed, Thu, Fri
|
18
|
+
#
|
19
|
+
# ==Example
|
20
|
+
# d = Date.parse('2008-01-01')
|
21
|
+
# d.wday => 2
|
22
|
+
# d.weekday? => true
|
23
|
+
|
24
|
+
def weekday?
|
25
|
+
wday>0 and wday<6
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
# Return true if the date is a weekend: Sat, Sun
|
30
|
+
#
|
31
|
+
# ==Example
|
32
|
+
# d = Date.parse('2008-01-05')
|
33
|
+
# d.wday => 6
|
34
|
+
# d.weekend? => true
|
35
|
+
|
36
|
+
def weekend?
|
37
|
+
wday==0 or wday==6
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
# Return a random date between min & max
|
42
|
+
#
|
43
|
+
# ==Example
|
44
|
+
# d1= Date.parse('2008-01-01')
|
45
|
+
# d2= Date.parse('2009-01-01')
|
46
|
+
# Date.between(d1,d3) => Date 2008-11-22
|
47
|
+
|
48
|
+
def self.between(min,max)
|
49
|
+
min+rand(max-min)
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
# Return the age in years for a given date.
|
54
|
+
#
|
55
|
+
# ==Example
|
56
|
+
#
|
57
|
+
# birthdate=Date.new(1980,10,31)
|
58
|
+
# birthdate.age_years => 28 (where 28 is the correct age for today)
|
59
|
+
#
|
60
|
+
# ==Example of custom dates
|
61
|
+
#
|
62
|
+
# birthdate=Date.new(1980,10,31)
|
63
|
+
#
|
64
|
+
# valentines = Date.new(2008,02,14)
|
65
|
+
# birthdate.age_years(valentines) => 27 # before the birthday
|
66
|
+
#
|
67
|
+
# halloween = Date.new(2008,10,31)
|
68
|
+
# birthdate.age_years(halloween) => 28 # on the birthday
|
69
|
+
#
|
70
|
+
# new_years_eve = Date.new(2008,12,31)
|
71
|
+
# birthdate.age_years(new_years_eve) => 28 # after the birthday
|
72
|
+
|
73
|
+
def age_years(compare_date=Date.today)
|
74
|
+
age=compare_date.year-year
|
75
|
+
age-=1 if compare_date.month < month or (compare_date.month==month and compare_date.day < day)
|
76
|
+
age
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
# Return the age in days for a given date.
|
81
|
+
|
82
|
+
def age_days(compare_to_date=Date.today)
|
83
|
+
(compare_to_date-self).to_i
|
84
|
+
end
|
85
|
+
|
86
|
+
|
87
|
+
end
|
@@ -0,0 +1,369 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
module Enumerable
|
3
|
+
|
4
|
+
|
5
|
+
########################################################################
|
6
|
+
#
|
7
|
+
# typecast
|
8
|
+
#
|
9
|
+
########################################################################
|
10
|
+
|
11
|
+
# Convert an enumerable to a hash.
|
12
|
+
#
|
13
|
+
# ==Example
|
14
|
+
# array=[[:a, :b],[:c, :d],[:e, :f]]
|
15
|
+
# array.to_h => {:a=>:b, :c=>:d, :e=>:f}
|
16
|
+
#
|
17
|
+
# If a key occurs more than once, then this will automatically
|
18
|
+
# convert the value to an array of the keys' values.
|
19
|
+
#
|
20
|
+
# ==Example
|
21
|
+
# array=[[:a,:b],[:a,:c],[:a,:d]]
|
22
|
+
# array.to_h => {:a=>[:b, :c, :d]}
|
23
|
+
|
24
|
+
def to_h
|
25
|
+
h={}
|
26
|
+
dupe={}
|
27
|
+
each{|k,v|
|
28
|
+
if h.key? k
|
29
|
+
if dupe.key? k
|
30
|
+
h[k] << v
|
31
|
+
else
|
32
|
+
h[k]=[h[k]]
|
33
|
+
h[k] << v
|
34
|
+
dupe[k]=true
|
35
|
+
end
|
36
|
+
else
|
37
|
+
h[k]=v
|
38
|
+
end
|
39
|
+
}
|
40
|
+
return h
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
# Convert the enumerable to a hash by mapping each item to a key,item pair.
|
45
|
+
#
|
46
|
+
# ==Example
|
47
|
+
# strings = ["red","blue","green"]
|
48
|
+
# strings.index_by{|a| a.size]}
|
49
|
+
# => {3 => "red", 4 => "blue", 5 => "green"}
|
50
|
+
#
|
51
|
+
# Rails has this method.
|
52
|
+
#
|
53
|
+
# From http://stackoverflow.com/questions/412771/cleanest-way-to-create-a-hash-from-an-array
|
54
|
+
#
|
55
|
+
# Compare #hash_by
|
56
|
+
|
57
|
+
def index_by
|
58
|
+
inject({}) {|hash, elem| hash.merge!(yield(elem) => elem) }
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
# Convert the enumerable to a hash by mapping each item to a key,value pair.
|
63
|
+
#
|
64
|
+
# ==Example
|
65
|
+
# strings = ["red","blue","green"]
|
66
|
+
# strings.hash_by{|a| [a.size, a.titlecase]}
|
67
|
+
# => {3 => "red", 4 => "blue", 5 => "green"}
|
68
|
+
#
|
69
|
+
# Compare #index_by
|
70
|
+
|
71
|
+
def hash_by
|
72
|
+
map{|x| yield(x)}.to_h
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
########################################################################
|
78
|
+
#
|
79
|
+
# map_to_xxx
|
80
|
+
#
|
81
|
+
########################################################################
|
82
|
+
|
83
|
+
|
84
|
+
# Map each item => item.id
|
85
|
+
#
|
86
|
+
# ==Example
|
87
|
+
# users = User.find(:all)
|
88
|
+
# users.map_id => [1,2,3,4,...]
|
89
|
+
#
|
90
|
+
# A typical use is to convert a list of ActiveRecord items to a list of id items.
|
91
|
+
#
|
92
|
+
# This method is a fast way to get the same results as items.map(&:id)
|
93
|
+
|
94
|
+
def map_id
|
95
|
+
map{|x| x.id}
|
96
|
+
end
|
97
|
+
|
98
|
+
|
99
|
+
# Map each item => item.to_a
|
100
|
+
#
|
101
|
+
# ==Example
|
102
|
+
# set1 = Set.new([:a,:b,:c])
|
103
|
+
# set2 = Set.new([:d,:e,:f])
|
104
|
+
# set3 = Set.new([:g,:h,:i])
|
105
|
+
# sets = [set1, set2, set3]
|
106
|
+
# sets.map_to_a => [[:a, :b, :c], [:d, :e, :f], [:g, :h, :i]]
|
107
|
+
#
|
108
|
+
# A typical use is to convert a list with Set items to a list of Array items,
|
109
|
+
# so you can more easily iterate over the the Array items.
|
110
|
+
#
|
111
|
+
# See http://www.ruby-doc.org/core/classes/Enumerable.html#M003148
|
112
|
+
|
113
|
+
def map_to_a
|
114
|
+
map{|x| [x]}
|
115
|
+
end
|
116
|
+
|
117
|
+
|
118
|
+
# Map each item => item.to_f
|
119
|
+
#
|
120
|
+
# ==Example
|
121
|
+
# strings = ["1","2","3"]
|
122
|
+
# strings.map_to_f => [1.0, 2.0, 3.0]
|
123
|
+
#
|
124
|
+
# A typical use is to convert a list of String items to a list of float items.
|
125
|
+
#
|
126
|
+
# This method is a fast way to get the same results as items.map(&:to_f)
|
127
|
+
|
128
|
+
def map_to_f
|
129
|
+
map{|x| x.to_f}
|
130
|
+
end
|
131
|
+
|
132
|
+
|
133
|
+
# Map each item => item.to_i
|
134
|
+
#
|
135
|
+
# ==Example
|
136
|
+
# strings = ["1","2","3"]
|
137
|
+
# strings.map_to_i => [1, 2, 3]
|
138
|
+
#
|
139
|
+
# A typical use is to convert a list of String items to a list of integer items.
|
140
|
+
#
|
141
|
+
# This method is a fast way to get the same results as items.map(&:to_i)
|
142
|
+
|
143
|
+
def map_to_i
|
144
|
+
map{|x| x.to_i}
|
145
|
+
end
|
146
|
+
|
147
|
+
|
148
|
+
# Map each item => item.to_s
|
149
|
+
#
|
150
|
+
# ==Example
|
151
|
+
# numbers = [1, 2, 3]
|
152
|
+
# numbers.map_to_s => ["1", "2", "3"]
|
153
|
+
#
|
154
|
+
# A typical use is to convert a list of Numeric items to a list of String items.
|
155
|
+
#
|
156
|
+
# This method is a fast way to get the same results as items.map(&:to_s)
|
157
|
+
|
158
|
+
def map_to_s
|
159
|
+
map{|x| x.to_s}
|
160
|
+
end
|
161
|
+
|
162
|
+
|
163
|
+
# Map each item => item.to_sym
|
164
|
+
#
|
165
|
+
# ==Example
|
166
|
+
# strings = ["foo", "goo", "hoo"]
|
167
|
+
# strings.map_to_sym => [:foo, :goo, :hoo]
|
168
|
+
#
|
169
|
+
# A typical use is to convert a list of Object items to a list of Symbol items.
|
170
|
+
#
|
171
|
+
# This method is a fast way to get the same results as items.map(&:to_sym)
|
172
|
+
|
173
|
+
def map_to_sym
|
174
|
+
map{|x| x.to_sym}
|
175
|
+
end
|
176
|
+
|
177
|
+
|
178
|
+
########################################################################
|
179
|
+
#
|
180
|
+
# select
|
181
|
+
#
|
182
|
+
########################################################################
|
183
|
+
|
184
|
+
|
185
|
+
# enum.select_while {|obj| block } => array
|
186
|
+
# Returns an array containing the leading elements for which block is not false or nil.
|
187
|
+
|
188
|
+
def select_while
|
189
|
+
a = []
|
190
|
+
each{|x| yield(x) ? (a << x) : break}
|
191
|
+
return a
|
192
|
+
end
|
193
|
+
|
194
|
+
|
195
|
+
# enum.select_until {|obj| block } => array
|
196
|
+
# Returns an array containing the leading elements for which block is false or nil.
|
197
|
+
|
198
|
+
def select_until
|
199
|
+
a = []
|
200
|
+
each{|x| yield(x) ? break : (a << x)}
|
201
|
+
return a
|
202
|
+
end
|
203
|
+
|
204
|
+
|
205
|
+
# enum.select_with_index {|obj,i| block } => array
|
206
|
+
# Calls block with two arguments, the item and its index, for each item in enum.
|
207
|
+
# Returns an array containing the leading elements for which block is not false or nil.
|
208
|
+
|
209
|
+
def select_with_index
|
210
|
+
i = 0
|
211
|
+
a = []
|
212
|
+
each{|x|
|
213
|
+
if yield(x,i)
|
214
|
+
a << x
|
215
|
+
i+=1
|
216
|
+
else
|
217
|
+
break
|
218
|
+
end
|
219
|
+
}
|
220
|
+
return a
|
221
|
+
end
|
222
|
+
|
223
|
+
|
224
|
+
########################################################################
|
225
|
+
#
|
226
|
+
# bisect
|
227
|
+
#
|
228
|
+
########################################################################
|
229
|
+
|
230
|
+
# enum.bisect {|obj| block} => array of positives, array of negatives
|
231
|
+
# Returns two arrays: the first contains the elements for which block is
|
232
|
+
# true, the second contains the elements for which block is false or nil.
|
233
|
+
|
234
|
+
def bisect
|
235
|
+
a=[]
|
236
|
+
b=[]
|
237
|
+
each{|x|
|
238
|
+
if yield(x)
|
239
|
+
a << x
|
240
|
+
else
|
241
|
+
b << x
|
242
|
+
end
|
243
|
+
}
|
244
|
+
return a,b
|
245
|
+
end
|
246
|
+
|
247
|
+
|
248
|
+
########################################################################
|
249
|
+
#
|
250
|
+
# nitems
|
251
|
+
#
|
252
|
+
########################################################################
|
253
|
+
|
254
|
+
|
255
|
+
# enum.nitems_while {| obj | block } => number of items
|
256
|
+
# Returns the number of leading elements for which block is not false or nil.
|
257
|
+
|
258
|
+
def nitems_while
|
259
|
+
n = 0
|
260
|
+
each{|x| yield(x) ? (n+=1) : break}
|
261
|
+
return n
|
262
|
+
end
|
263
|
+
|
264
|
+
|
265
|
+
# enum.nitems_until {| obj | block } => number of items
|
266
|
+
# Returns the number of leading elements for which block is false.
|
267
|
+
|
268
|
+
def nitems_until
|
269
|
+
n = 0
|
270
|
+
each{|x|
|
271
|
+
if yield(x)
|
272
|
+
break
|
273
|
+
else
|
274
|
+
n+=1
|
275
|
+
end
|
276
|
+
}
|
277
|
+
return n
|
278
|
+
irb
|
279
|
+
end
|
280
|
+
|
281
|
+
|
282
|
+
# enum.nitems_with_index {|obj,i| block } => number of items
|
283
|
+
# Calls block with two arguments, the item and its index, for each item in enum.
|
284
|
+
# Returns the number of leading elements for which block is true.
|
285
|
+
|
286
|
+
def nitems_with_index
|
287
|
+
i = 0
|
288
|
+
each{|x| yield(x,i) ? (i+=1) : break}
|
289
|
+
return i
|
290
|
+
end
|
291
|
+
|
292
|
+
|
293
|
+
# Shortcut to Array#join to concatenate the items into a string
|
294
|
+
|
295
|
+
def join(prefix=nil,suffix=nil)
|
296
|
+
to_a.join(prefix,suffix)
|
297
|
+
end
|
298
|
+
|
299
|
+
|
300
|
+
########################################################################
|
301
|
+
#
|
302
|
+
# set math
|
303
|
+
#
|
304
|
+
########################################################################
|
305
|
+
|
306
|
+
|
307
|
+
# Returns true if this _enum_ intersects another _enum_.
|
308
|
+
#
|
309
|
+
# @nb This implementation uses #to_a and array intersection.
|
310
|
+
# A developer may want to optimize this implementation for
|
311
|
+
# other classes, such as detecting whether a range intersects
|
312
|
+
# another range simply by comparing the ranges' min/max values.
|
313
|
+
#
|
314
|
+
# ==Examples
|
315
|
+
# ['a','b','c'].intersect?(['c','d','e'] => true
|
316
|
+
# ['a','b','c'].intersect?(['d','e','f'] => false
|
317
|
+
|
318
|
+
def intersect?(enum)
|
319
|
+
return ((self===enum and self.to_a.size>0) or ((self.to_a & enum.to_a).size>0))
|
320
|
+
end
|
321
|
+
|
322
|
+
|
323
|
+
# Return the cartesian product of the enumerations.
|
324
|
+
# http://en.wikipedia.org/wiki/Cartesian_product
|
325
|
+
#
|
326
|
+
# This is the fastest implementation we have found.
|
327
|
+
# It returns results in typical order.
|
328
|
+
#
|
329
|
+
# By Thomas Hafner
|
330
|
+
# See http://www.ruby-forum.com/topic/95519
|
331
|
+
#
|
332
|
+
# For our benchmarks, we also compared thesk:
|
333
|
+
# - By William James, http://www.ruby-forum.com/topic/95519
|
334
|
+
# - By Brian Schröäer, http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/151857
|
335
|
+
|
336
|
+
def self.cartesian_product(*enums)
|
337
|
+
result = [[]]
|
338
|
+
while [] != enums
|
339
|
+
t, result = result, []
|
340
|
+
b, *enums = enums
|
341
|
+
t.each do |a|
|
342
|
+
b.each do |n|
|
343
|
+
result << a + [n]
|
344
|
+
end
|
345
|
+
end
|
346
|
+
end
|
347
|
+
result
|
348
|
+
end
|
349
|
+
|
350
|
+
def cartesian_product(*enums)
|
351
|
+
Enumerable.cartesian_product(self,*enums)
|
352
|
+
end
|
353
|
+
|
354
|
+
|
355
|
+
# Return the power set: an array with all subsets of the enum's elements.
|
356
|
+
# http://en.wikipedia.org/wiki/Power_set
|
357
|
+
#
|
358
|
+
# This implementation is from
|
359
|
+
# http://johncarrino.net/blog/2006/08/11/powerset-in-ruby/
|
360
|
+
#
|
361
|
+
# ==Example
|
362
|
+
# [1,2,3].power_set.sort
|
363
|
+
# => [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]]
|
364
|
+
|
365
|
+
def power_set
|
366
|
+
inject([[]]){|c,y|r=[];c.each{|i|r<<i;r<<i+[y]};r}
|
367
|
+
end
|
368
|
+
|
369
|
+
end
|