webget_ruby_ramp 1.7.2 → 1.7.3
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.
- data.tar.gz.sig +0 -0
- data/lib/webget_ruby_ramp.rb +6 -23
- data/lib/webget_ruby_ramp/array.rb +44 -23
- data/lib/webget_ruby_ramp/enumerable.rb +19 -19
- data/lib/webget_ruby_ramp/string.rb +5 -5
- data/lib/webget_ruby_ramp/xml.rb +1 -1
- data/test/webget_ruby_ramp/array_test.rb +86 -21
- data/test/webget_ruby_ramp/enumerable_test.rb +1 -1
- data/test/webget_ruby_ramp/yaml_test.rb +7 -7
- metadata +2 -8
- metadata.gz.sig +1 -1
- data/lib/webget_ruby_ramp/active_record.rb +0 -119
- data/lib/webget_ruby_ramp/active_record/connection_adapters/abstract/schema_statements.rb +0 -24
- data/lib/webget_ruby_ramp/active_record/save_extensions.rb +0 -35
- data/test/webget_ruby_ramp/active_record/connection_adapters/abstract/schema_statements_test.rb +0 -9
- data/test/webget_ruby_ramp/active_record/save_extensions_test.rb +0 -7
- data/test/webget_ruby_ramp/active_record_test.rb +0 -64
data.tar.gz.sig
CHANGED
Binary file
|
data/lib/webget_ruby_ramp.rb
CHANGED
@@ -1,39 +1,21 @@
|
|
1
1
|
=begin rdoc
|
2
2
|
|
3
|
-
= WebGet Ruby Gem: Ramp is a toolkit of
|
3
|
+
= WebGet Ruby Gem: Ramp is a toolkit of Ruby base class extensions
|
4
4
|
|
5
5
|
Author:: Joel Parker Henderson, joelparkerhenderson@gmail.com
|
6
6
|
Copyright:: Copyright (c) 2006-2010 Joel Parker Henderson
|
7
7
|
License:: CreativeCommons License, Non-commercial Share Alike
|
8
8
|
License:: LGPL, GNU Lesser General Public License
|
9
9
|
|
10
|
-
Ramp is a library of extensions to Ruby
|
10
|
+
Ramp is a library of extensions to Ruby base classes, including Array, Date, Enumerable, Hash, Kernel, Numeric, Object, Process, String, Time, and YAML.
|
11
11
|
|
12
12
|
Testing:
|
13
13
|
<ul>
|
14
14
|
<li>Each has an associated test class, e.g., ArrayTest, DateTest, etc.
|
15
|
-
<li>The easy way to run the tests: gem install
|
16
|
-
<li>Some of the ActiveRecord extensions use sqlite for testing. We don't install sqlite automatically because it requires some native extensions. If you need sqlite: gem install sqlite3-ruby
|
15
|
+
<li>The easy way to run the tests: gem install webget_ruby_ramp --test
|
17
16
|
</ul>
|
18
17
|
|
19
18
|
|
20
|
-
== ActiveRecord
|
21
|
-
|
22
|
-
* create_or_update_by: create a record, or update a record if value passed matches a field (or fields) in the AR object; includes method_missing function to make code more readable.
|
23
|
-
* seed: syntactic sugar alias for #create_or_update_by
|
24
|
-
|
25
|
-
|
26
|
-
== ActiveRecord::ConnectionAdapters::SchemaStatements
|
27
|
-
|
28
|
-
* add_column_and_index: database migration helper to add a table column and index at the same time.
|
29
|
-
* remove_column_and_index: database migration helper to add a table column and index at the same time.
|
30
|
-
|
31
|
-
|
32
|
-
== ActiveRecord::SaveExtensions
|
33
|
-
|
34
|
-
* save_false_then_reload!: a transaction to save and reload a record, to help repair associations
|
35
|
-
|
36
|
-
|
37
19
|
== Array
|
38
20
|
|
39
21
|
* car, cdr: aka first, rest (see shifted)
|
@@ -205,7 +187,8 @@ Extensions that help debug Ruby programs.
|
|
205
187
|
|
206
188
|
== Changes
|
207
189
|
|
208
|
-
- 1.7.
|
190
|
+
- 1.7.3 Refactor Rails classes to their own gem
|
191
|
+
- 1.7.2 Gemcutter update
|
209
192
|
- 1.7.1.8 Add Enumerable#map_with_index
|
210
193
|
- 1.7.1.6 Add ActiveRecord::SaveExtensions#save_false_then_reload!
|
211
194
|
- 1.7.1.3 Add XML#strip_xxx
|
@@ -244,7 +227,7 @@ Extensions that help debug Ruby programs.
|
|
244
227
|
|
245
228
|
=end
|
246
229
|
|
247
|
-
%w{
|
230
|
+
%w{array csv date enumerable file hash integer io kernel math nil numeric object process string symbol time xml yaml}.map{|x|
|
248
231
|
require File.dirname(__FILE__) + "/webget_ruby_ramp/#{x}.rb"
|
249
232
|
}
|
250
233
|
|
@@ -34,14 +34,14 @@ class Array
|
|
34
34
|
when 2
|
35
35
|
prefix=fixes[0].to_s
|
36
36
|
suffix=fixes[1].to_s
|
37
|
-
return self.map{|
|
37
|
+
return self.map{|item| prefix + item.to_s + suffix}.ruby_join()
|
38
38
|
when 3
|
39
39
|
infix =fixes[0].to_s
|
40
40
|
prefix=fixes[1].to_s
|
41
41
|
suffix=fixes[2].to_s
|
42
|
-
return map{|
|
42
|
+
return self.map{|item| prefix + item.to_s + suffix}.ruby_join(infix)
|
43
43
|
else
|
44
|
-
raise "join(
|
44
|
+
raise ArgumentError, "join() takes 0-3 arguments; you gave #{fixes.size}]"
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
@@ -62,11 +62,14 @@ class Array
|
|
62
62
|
# ==Examples
|
63
63
|
# [1,2,3,4].rotate! => [2,3,4,1]
|
64
64
|
# ['a','b','c'].rotate! => ['b','c','a']
|
65
|
+
# [].rotate! => []
|
65
66
|
#
|
66
67
|
# Return self
|
67
68
|
|
68
69
|
def rotate!
|
69
|
-
|
70
|
+
if size>0
|
71
|
+
push item=shift
|
72
|
+
end
|
70
73
|
self
|
71
74
|
end
|
72
75
|
|
@@ -109,6 +112,7 @@ class Array
|
|
109
112
|
# This is identical to calling foo.zip(values).to_h
|
110
113
|
|
111
114
|
def onto(values)
|
115
|
+
size==values.size or raise ArgumentError, "Array size #{size} must match values size #{size}"
|
112
116
|
zip(values).to_h
|
113
117
|
end
|
114
118
|
|
@@ -133,11 +137,13 @@ class Array
|
|
133
137
|
# [1,2,3,4,5,6,7,8].slices(5) => [[1,2,3,4,5],[6,7,8]]
|
134
138
|
|
135
139
|
def slices(slice_length)
|
140
|
+
(slice_length.is_a? Integer) or (raise ArgumentError, "slice_length must be an integer")
|
141
|
+
(slice_length > 0) or (raise ArgumentError, "slice_length must be > 0")
|
136
142
|
arr=[]
|
137
|
-
|
138
|
-
while
|
139
|
-
arr.push self[
|
140
|
-
|
143
|
+
index=0
|
144
|
+
while index<length
|
145
|
+
arr.push self[index...(index+slice_length)]
|
146
|
+
index+=slice_length
|
141
147
|
end
|
142
148
|
return arr
|
143
149
|
end
|
@@ -162,6 +168,8 @@ class Array
|
|
162
168
|
# [1,2,3,4,5,6].divvy(4) => [[1,2],[3,4],[5,6]]
|
163
169
|
|
164
170
|
def divvy(number_of_slices)
|
171
|
+
(number_of_slices.is_a? Integer) or (raise ArgumentError, "number_of_slices must be an integer")
|
172
|
+
(number_of_slices > 0) or (raise ArgumentError, "number_of_slices must be > 0")
|
165
173
|
return slices((length.to_f/number_of_slices.to_f).ceil)
|
166
174
|
end
|
167
175
|
|
@@ -177,22 +185,18 @@ class Array
|
|
177
185
|
# In typical use, each item is an array.
|
178
186
|
#
|
179
187
|
# ==Example using Ruby Array pipe
|
180
|
-
#
|
181
|
-
#
|
182
|
-
# a | b => [1,2,3,4]
|
183
|
-
#
|
184
|
-
# ==Example using union method
|
185
|
-
# arr=[a,b]
|
186
|
-
# => [1,2,3,4]
|
188
|
+
# arr=[[1,2,3,4],[3,4,5,6]]
|
189
|
+
# arr.union => [1,2,3,4,5,6]
|
187
190
|
#
|
188
|
-
# This is identical to
|
189
|
-
|
190
191
|
# ==Examples with proc
|
191
192
|
# arr.map(&:foo).union
|
192
193
|
# => foos that are in any of the array items
|
194
|
+
#
|
195
|
+
# ==Example with nil
|
196
|
+
# [].union => []
|
193
197
|
|
194
198
|
def union
|
195
|
-
inject{|inj,item| inj | item.to_a }
|
199
|
+
inject{|inj,item| inj | item.to_a } || []
|
196
200
|
end
|
197
201
|
|
198
202
|
|
@@ -200,16 +204,19 @@ class Array
|
|
200
204
|
# In typical usage, each item is an array.
|
201
205
|
#
|
202
206
|
# ==Examples
|
203
|
-
# arr=[[1,2,3,4],[
|
207
|
+
# arr=[[1,2,3,4],[3,4,5,6]]
|
204
208
|
# arr.intersect
|
205
209
|
# => [3,4]
|
206
210
|
#
|
207
211
|
# ==Examples with proc
|
208
212
|
# arr.map(&:foo).intersect
|
209
213
|
# => foos that are in all of the array items
|
214
|
+
#
|
215
|
+
# ==Example with nil
|
216
|
+
# [].intersect => []
|
210
217
|
|
211
218
|
def intersect
|
212
|
-
inject{|inj,item| inj & item.to_a }
|
219
|
+
inject{|inj,item| inj & item.to_a } || []
|
213
220
|
end
|
214
221
|
|
215
222
|
|
@@ -243,7 +250,9 @@ class Array
|
|
243
250
|
#
|
244
251
|
|
245
252
|
def shifted(number_of_items=1)
|
246
|
-
|
253
|
+
(number_of_items.is_a? Integer) or (raise ArgumentError, "number_of_items must be an integer")
|
254
|
+
(number_of_items >= 0) or (raise ArgumentError, "number_of_items must be >= 0")
|
255
|
+
slice(number_of_items,self.length-number_of_items)
|
247
256
|
end
|
248
257
|
|
249
258
|
alias :car :first
|
@@ -266,8 +275,10 @@ class Array
|
|
266
275
|
# If _n_ is greater than the array size, then return []
|
267
276
|
|
268
277
|
def shifted!(number_of_items=1)
|
269
|
-
|
270
|
-
|
278
|
+
(number_of_items.is_a? Integer) or (raise ArgumentError, "number_of_items must be an integer")
|
279
|
+
(number_of_items >= 0) or (raise ArgumentError, "number_of_items must be >= 0")
|
280
|
+
slice!(0,number_of_items)
|
281
|
+
return self
|
271
282
|
end
|
272
283
|
|
273
284
|
alias :cdr! :shifted!
|
@@ -333,11 +344,17 @@ class Array
|
|
333
344
|
#
|
334
345
|
# [[1,2,3],[4,5,6]] => "1,2,3\n4,5,6\n"
|
335
346
|
#
|
347
|
+
# ==Example of a blank array
|
348
|
+
#
|
349
|
+
# [].to_csv => ""
|
350
|
+
#
|
336
351
|
# N.b. this method uses the multi-dimensional if the
|
337
352
|
# array's first item is also an array.
|
338
353
|
|
339
354
|
def to_csv(ops={})
|
340
355
|
|
356
|
+
return "" if size==0
|
357
|
+
|
341
358
|
generator = RUBY_VERSION >= "1.9" ? CSV : CSV::Writer
|
342
359
|
|
343
360
|
str=''
|
@@ -360,6 +377,10 @@ class Array
|
|
360
377
|
# representation of a multi-dimensional array.
|
361
378
|
#
|
362
379
|
# Each subarray becomes one 'line' in the output.
|
380
|
+
#
|
381
|
+
# ==Example of a blank array
|
382
|
+
#
|
383
|
+
# [].to_csv => ""
|
363
384
|
|
364
385
|
def to_tsv(ops={})
|
365
386
|
self.map{|row| row.join("\t")+"\n"}.join
|
@@ -23,22 +23,22 @@ module Enumerable
|
|
23
23
|
# array.to_h => {:a=>[:b, :c, :d]}
|
24
24
|
|
25
25
|
def to_h
|
26
|
-
|
26
|
+
hash={}
|
27
27
|
dupe={}
|
28
|
-
each{|
|
29
|
-
if
|
30
|
-
if dupe.key?
|
31
|
-
|
28
|
+
each{|key,val|
|
29
|
+
if hash.key? key
|
30
|
+
if dupe.key? key
|
31
|
+
hash[key] << val
|
32
32
|
else
|
33
|
-
|
34
|
-
|
35
|
-
dupe[
|
33
|
+
hash[key]=[hash[key]]
|
34
|
+
hash[key] << val
|
35
|
+
dupe[key]=true
|
36
36
|
end
|
37
37
|
else
|
38
|
-
|
38
|
+
hash[key]=val
|
39
39
|
end
|
40
40
|
}
|
41
|
-
return
|
41
|
+
return hash
|
42
42
|
end
|
43
43
|
|
44
44
|
|
@@ -64,8 +64,8 @@ module Enumerable
|
|
64
64
|
#
|
65
65
|
# ==Example
|
66
66
|
# strings = ["red","blue","green"]
|
67
|
-
# strings.hash_by{|a| [a.size, a.
|
68
|
-
# => {3 => "
|
67
|
+
# strings.hash_by{|a| [a.size, a.upcase]}
|
68
|
+
# => {3 => "RED", 4 => "BLUE", 5 => "GREEN"}
|
69
69
|
#
|
70
70
|
# Compare #index_by
|
71
71
|
|
@@ -249,16 +249,16 @@ module Enumerable
|
|
249
249
|
# true, the second contains the elements for which block is false or nil.
|
250
250
|
|
251
251
|
def bisect
|
252
|
-
|
253
|
-
|
254
|
-
each{|
|
255
|
-
if yield(
|
256
|
-
|
252
|
+
accept=[]
|
253
|
+
reject=[]
|
254
|
+
each{|item|
|
255
|
+
if yield(item)
|
256
|
+
accept << item
|
257
257
|
else
|
258
|
-
|
258
|
+
reject << item
|
259
259
|
end
|
260
260
|
}
|
261
|
-
return
|
261
|
+
return accept,reject
|
262
262
|
end
|
263
263
|
|
264
264
|
|
@@ -127,11 +127,11 @@ class String
|
|
127
127
|
# String.prev_char('0') => '9', true, true # change & carry
|
128
128
|
# String.prev_char('-') => '-', false, false # unchanged
|
129
129
|
|
130
|
-
def self.prev_char(
|
131
|
-
case
|
130
|
+
def self.prev_char(chr) #=> prev_char, changed_flag, carry_flag
|
131
|
+
case chr
|
132
132
|
when '1'..'9', 'B'..'Z', 'b'..'z'
|
133
|
-
|
134
|
-
return (
|
133
|
+
pos=(chr.respond_to?(:ord) ? chr.ord : chr[0])
|
134
|
+
return (pos-1).chr, true, false
|
135
135
|
when '0'
|
136
136
|
return '9', true, true
|
137
137
|
when 'A'
|
@@ -139,7 +139,7 @@ class String
|
|
139
139
|
when 'a'
|
140
140
|
return 'z', true, true
|
141
141
|
else
|
142
|
-
return
|
142
|
+
return chr, false, false
|
143
143
|
end
|
144
144
|
end
|
145
145
|
|
data/lib/webget_ruby_ramp/xml.rb
CHANGED
@@ -165,7 +165,7 @@ class REXML::Document
|
|
165
165
|
# cf. Element#remove_attributes
|
166
166
|
|
167
167
|
def remove_attributes
|
168
|
-
self.elements.each("//") { |
|
168
|
+
self.elements.each("//") { |elem| elem.attributes.each_attribute{|attribute| attribute.remove }}
|
169
169
|
self
|
170
170
|
end
|
171
171
|
|
@@ -7,13 +7,16 @@ class ArrayTest < Test::Unit::TestCase
|
|
7
7
|
def test_join
|
8
8
|
a=['a','b','c']
|
9
9
|
assert_equal('abc',a.join)
|
10
|
+
assert_equal('abc',a.join(''))
|
10
11
|
assert_equal('a*b*c',a.join('*'))
|
11
12
|
assert_equal('[a]*[b]*[c]',a.join('*','[',']'))
|
12
13
|
assert_equal('[a][b][c]',a.join('[',']'))
|
14
|
+
assert_raise(ArgumentError){ a.join('','','','') }
|
13
15
|
end
|
14
16
|
|
15
17
|
def test_size
|
16
18
|
assert_equal(false,[].size?)
|
19
|
+
assert_equal(true,[nil].size?)
|
17
20
|
assert_equal(true,[1].size?)
|
18
21
|
assert_equal(true,[1,2].size?)
|
19
22
|
assert_equal(true,[[]].size?)
|
@@ -25,56 +28,75 @@ class ArrayTest < Test::Unit::TestCase
|
|
25
28
|
assert_equal([2,3,4,1],a)
|
26
29
|
a.rotate!
|
27
30
|
assert_equal([3,4,1,2],a)
|
31
|
+
a.rotate!
|
32
|
+
a=[]
|
33
|
+
a.rotate!
|
34
|
+
assert_equal([],a)
|
28
35
|
end
|
29
36
|
|
30
37
|
def test_choice
|
31
38
|
a=[1,2,3,4]
|
32
|
-
|
33
|
-
|
34
|
-
|
39
|
+
5.times {
|
40
|
+
c=a.choice
|
41
|
+
assert_equal(Fixnum,c.class)
|
42
|
+
assert(a.include?(c))
|
43
|
+
}
|
35
44
|
end
|
36
45
|
|
37
46
|
def test_choices
|
38
47
|
a=[1,2,3,4]
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
+
5.times {
|
49
|
+
c=a.choices(2)
|
50
|
+
assert_equal(2,c.size)
|
51
|
+
assert(a.include?(c[0]))
|
52
|
+
assert(a.include?(c[1]))
|
53
|
+
c=a.choices(3)
|
54
|
+
assert_equal(3,c.size)
|
55
|
+
assert(a.include?(c[0]))
|
56
|
+
assert(a.include?(c[1]))
|
57
|
+
assert(a.include?(c[2]))
|
58
|
+
}
|
48
59
|
end
|
49
60
|
|
50
61
|
def test_onto
|
51
62
|
foo=[:a,:b,:c]
|
52
63
|
goo=[:x,:y,:z]
|
53
64
|
assert_equal({:a=>:x, :b=>:y, :c=>:z},foo.onto(goo))
|
65
|
+
assert_raise(ArgumentError){ foo.onto([]) }
|
54
66
|
end
|
55
67
|
|
56
68
|
def test_slices
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
69
|
+
a=[1,2,3,4,5,6,7,8]
|
70
|
+
assert_equal([[1,2],[3,4],[5,6],[7,8]],a.slices(2))
|
71
|
+
assert_equal([[1,2,3,4],[5,6,7,8]], a.slices(4))
|
72
|
+
assert_equal([[1,2,3],[4,5,6],[7,8]],a.slices(3))
|
73
|
+
assert_equal([[1,2,3,4,5],[6,7,8]],a.slices(5))
|
74
|
+
assert_equal([],[].slices(1))
|
75
|
+
assert_raise(ArgumentError){ a.slices(-1) }
|
76
|
+
assert_raise(ArgumentError){ a.slices("") }
|
77
|
+
assert_raise(ArgumentError){ a.slices(0.123) }
|
62
78
|
end
|
63
79
|
|
64
|
-
def
|
80
|
+
def test_divvy
|
81
|
+
assert_equal([[1,2,3,4,5,6]],[1,2,3,4,5,6].divvy(1),'division into one slice')
|
65
82
|
assert_equal([[1,2,3],[4,5]],[1,2,3,4,5].divvy(2),'division with equal slices')
|
66
83
|
assert_equal([[1,2,3],[4,5,6],[7]],[1,2,3,4,5,6,7].divvy(3),'division with remainer slice')
|
67
84
|
assert_equal([[1,2],[3,4],[5,6]],[1,2,3,4,5,6].divvy(4),'division with small numbers')
|
85
|
+
assert_raise(ArgumentError){ [].divvy(-1) }
|
86
|
+
assert_raise(ArgumentError){ [].divvy("") }
|
87
|
+
assert_raise(ArgumentError){ [].divvy(0.123) }
|
68
88
|
end
|
69
89
|
|
70
90
|
def test_union
|
71
|
-
a=[[2,3,4],[4,5
|
72
|
-
assert_equal([2,3,4,5,6
|
91
|
+
a=[[1,2,3,4],[2,3,4,5],[3,4,5,6]]
|
92
|
+
assert_equal([1,2,3,4,5,6],a.union)
|
93
|
+
assert_equal([],[].union)
|
73
94
|
end
|
74
95
|
|
75
96
|
def test_intersect
|
76
|
-
a=[[2,3,4],[
|
97
|
+
a=[[1,2,3,4],[2,3,4,5],[3,4,5,6]]
|
77
98
|
assert_equal([3,4],a.intersect)
|
99
|
+
assert_equal([],[].intersect)
|
78
100
|
end
|
79
101
|
|
80
102
|
def test_shifted
|
@@ -85,6 +107,9 @@ class ArrayTest < Test::Unit::TestCase
|
|
85
107
|
assert_equal([ 'c'],a.shifted(2))
|
86
108
|
assert_equal([ ],a.shifted(3))
|
87
109
|
assert_equal(nil ,a.shifted(4))
|
110
|
+
assert_raise(ArgumentError){ a.shifted(-1) }
|
111
|
+
assert_raise(ArgumentError){ a.shifted("") }
|
112
|
+
assert_raise(ArgumentError){ a.shifted(0.123) }
|
88
113
|
end
|
89
114
|
|
90
115
|
# test_cdr must be idential to test_shifted
|
@@ -97,6 +122,9 @@ class ArrayTest < Test::Unit::TestCase
|
|
97
122
|
assert_equal([ 'c'],a.cdr(2))
|
98
123
|
assert_equal([ ],a.cdr(3))
|
99
124
|
assert_equal(nil ,a.cdr(4))
|
125
|
+
assert_raise(ArgumentError){ a.cdr(-1) }
|
126
|
+
assert_raise(ArgumentError){ a.cdr("") }
|
127
|
+
assert_raise(ArgumentError){ a.cdr(0.123) }
|
100
128
|
end
|
101
129
|
|
102
130
|
# test_rest must be idential to test_shifted
|
@@ -109,6 +137,9 @@ class ArrayTest < Test::Unit::TestCase
|
|
109
137
|
assert_equal([ 'c'],a.rest(2))
|
110
138
|
assert_equal([ ],a.rest(3))
|
111
139
|
assert_equal(nil ,a.rest(4))
|
140
|
+
assert_raise(ArgumentError){ a.rest(-1) }
|
141
|
+
assert_raise(ArgumentError){ a.rest("") }
|
142
|
+
assert_raise(ArgumentError){ a.rest(0.123) }
|
112
143
|
end
|
113
144
|
|
114
145
|
def test_shifted_bang
|
@@ -118,6 +149,10 @@ class ArrayTest < Test::Unit::TestCase
|
|
118
149
|
a=['a','b','c']; a.shifted!(2); assert_equal([ 'c'],a)
|
119
150
|
a=['a','b','c']; a.shifted!(3); assert_equal([ ],a)
|
120
151
|
a=['a','b','c']; a.shifted!(4); assert_equal([ ],a)
|
152
|
+
a=[]
|
153
|
+
assert_raise(ArgumentError){ a.shifted!(-1) }
|
154
|
+
assert_raise(ArgumentError){ a.shifted!("") }
|
155
|
+
assert_raise(ArgumentError){ a.shifted!(0.123) }
|
121
156
|
end
|
122
157
|
|
123
158
|
# alias: test_cdr_bang must be identical to test_shifted_bang
|
@@ -128,6 +163,10 @@ class ArrayTest < Test::Unit::TestCase
|
|
128
163
|
a=['a','b','c']; a.cdr!(2); assert_equal([ 'c'],a)
|
129
164
|
a=['a','b','c']; a.cdr!(3); assert_equal([ ],a)
|
130
165
|
a=['a','b','c']; a.cdr!(4); assert_equal([ ],a)
|
166
|
+
a=[]
|
167
|
+
assert_raise(ArgumentError){ a.cdr!(-1) }
|
168
|
+
assert_raise(ArgumentError){ a.cdr!("") }
|
169
|
+
assert_raise(ArgumentError){ a.cdr!(0.123) }
|
131
170
|
end
|
132
171
|
|
133
172
|
# alias: test_rest_band must be identical to test_shifted_bang
|
@@ -138,6 +177,10 @@ class ArrayTest < Test::Unit::TestCase
|
|
138
177
|
a=['a','b','c']; a.rest!(2); assert_equal([ 'c'],a)
|
139
178
|
a=['a','b','c']; a.rest!(3); assert_equal([ ],a)
|
140
179
|
a=['a','b','c']; a.rest!(4); assert_equal([ ],a)
|
180
|
+
a=[]
|
181
|
+
assert_raise(ArgumentError){ a.rest!(-1) }
|
182
|
+
assert_raise(ArgumentError){ a.rest!("") }
|
183
|
+
assert_raise(ArgumentError){ a.rest!(0.123) }
|
141
184
|
end
|
142
185
|
|
143
186
|
def test_car
|
@@ -145,9 +188,29 @@ class ArrayTest < Test::Unit::TestCase
|
|
145
188
|
assert_equal('a',a.car)
|
146
189
|
end
|
147
190
|
|
191
|
+
def test_shuffle
|
192
|
+
a=[1,2,3,4]
|
193
|
+
5.times {
|
194
|
+
b=a.shuffle
|
195
|
+
assert_equal(a.size,b.size)
|
196
|
+
a.each{|item| assert(b.include?(item)) }
|
197
|
+
}
|
198
|
+
end
|
199
|
+
|
200
|
+
def test_shuffle_bang
|
201
|
+
a=[1,2,3,4]
|
202
|
+
b=a.dup
|
203
|
+
5.times {
|
204
|
+
b.shuffle!
|
205
|
+
assert_equal(a.size,b.size)
|
206
|
+
a.each{|item| assert(b.include?(item)) }
|
207
|
+
}
|
208
|
+
end
|
209
|
+
|
148
210
|
def test_to_csv_one_dimensional
|
149
211
|
a=["a","b","c"]
|
150
212
|
assert_equal("a,b,c\n",a.to_csv)
|
213
|
+
assert_equal("",[].to_csv)
|
151
214
|
end
|
152
215
|
|
153
216
|
def test_to_csv_multi_dimensional
|
@@ -158,12 +221,14 @@ class ArrayTest < Test::Unit::TestCase
|
|
158
221
|
def test_to_tsv
|
159
222
|
a=[["a", "b"], ["c", "d"], ["e", "f"]]
|
160
223
|
assert_equal("a\tb\nc\td\ne\tf\n",a.to_tsv)
|
224
|
+
assert_equal("",[].to_tsv)
|
161
225
|
end
|
162
226
|
|
163
227
|
# alias: test_to_tdf must be identical to test_to_tsv
|
164
228
|
def test_to_tdf
|
165
229
|
a=[["a", "b"], ["c", "d"], ["e", "f"]]
|
166
230
|
assert_equal("a\tb\nc\td\ne\tf\n",a.to_tdf)
|
231
|
+
assert_equal("",[].to_tdf)
|
167
232
|
end
|
168
233
|
|
169
234
|
end
|
@@ -15,15 +15,15 @@ class YAMLTest < Test::Unit::TestCase
|
|
15
15
|
def test_load_dir
|
16
16
|
dirpath=File.join(MYDIR,'yaml_test_*.yml')
|
17
17
|
expect=[
|
18
|
-
"a:a1,a10,a2,a20,a3,
|
19
|
-
"d:d1,d10,d2,d20,d3,
|
20
|
-
"g:g1,g10,g2,g20,g3,
|
21
|
-
"j:j1,j10,j2,j20,j3,
|
22
|
-
"m:m1,m10,m2,m20,m3,
|
23
|
-
"p:p1,p10,p2,p20,p3,
|
18
|
+
"a:a1,a10,a2,a20,a3,a30;b:b1,b10,b2,b20,b3,b30;c:c1,c10,c2,c20,c3,c30",
|
19
|
+
"d:d1,d10,d2,d20,d3,d30;e:e1,e10,e2,e20,e3,e30;f:f1,f10,f2,f20,f3,f30",
|
20
|
+
"g:g1,g10,g2,g20,g3,g30;h:h1,h10,h2,h20,h3,h30;i:i1,i10,i2,i20,i3,i30",
|
21
|
+
"j:j1,j10,j2,j20,j3,j30;k:k1,k10,k2,k20,k3,k30;l:l1,l10,l2,l20,l3,l30",
|
22
|
+
"m:m1,m10,m2,m20,m3,m30;n:n1,n10,n2,n20,n3,n30;o:o1,o10,o2,o20,o3,o30",
|
23
|
+
"p:p1,p10,p2,p20,p3,p30;q:q1,q10,q2,q20,q3,q30;r:r1,r10,r2,r20,r3,r30",
|
24
24
|
]
|
25
25
|
actual=[]
|
26
|
-
YAML.load_dir(dirpath){|doc| actual << doc.keys.map{|k| v=doc[k]; "#{k}:#{v.sort.join(",")}" }.join("") }
|
26
|
+
YAML.load_dir(dirpath){|doc| actual << doc.keys.map{|k| v=doc[k]; "#{k}:#{v.sort.join(",")}" }.sort.join(";") }
|
27
27
|
actual.sort!
|
28
28
|
assert_equal(expect,actual,'YAML.load_dir')
|
29
29
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: webget_ruby_ramp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.7.
|
4
|
+
version: 1.7.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- WebGet
|
@@ -32,7 +32,7 @@ cert_chain:
|
|
32
32
|
DXnLFY0cVuBnNDMOOFl8vk1qIcZjcTovhzgcixpG6Uk5qmUsKHRLQf4oQJx7TfLK
|
33
33
|
-----END CERTIFICATE-----
|
34
34
|
|
35
|
-
date: 2010-02-
|
35
|
+
date: 2010-02-17 00:00:00 -08:00
|
36
36
|
default_executable:
|
37
37
|
dependencies: []
|
38
38
|
|
@@ -46,9 +46,6 @@ extra_rdoc_files: []
|
|
46
46
|
|
47
47
|
files:
|
48
48
|
- lib/webget_ruby_ramp.rb
|
49
|
-
- lib/webget_ruby_ramp/active_record.rb
|
50
|
-
- lib/webget_ruby_ramp/active_record/connection_adapters/abstract/schema_statements.rb
|
51
|
-
- lib/webget_ruby_ramp/active_record/save_extensions.rb
|
52
49
|
- lib/webget_ruby_ramp/array.rb
|
53
50
|
- lib/webget_ruby_ramp/csv.rb
|
54
51
|
- lib/webget_ruby_ramp/date.rb
|
@@ -104,9 +101,6 @@ signing_key:
|
|
104
101
|
specification_version: 3
|
105
102
|
summary: "WebGet Ruby Gem: Ramp gem provides base extensions to ruby classes and rails classes."
|
106
103
|
test_files:
|
107
|
-
- test/webget_ruby_ramp/active_record_test.rb
|
108
|
-
- test/webget_ruby_ramp/active_record/connection_adapters/abstract/schema_statements_test.rb
|
109
|
-
- test/webget_ruby_ramp/active_record/save_extensions_test.rb
|
110
104
|
- test/webget_ruby_ramp/array_test.rb
|
111
105
|
- test/webget_ruby_ramp/csv_test.rb
|
112
106
|
- test/webget_ruby_ramp/date_test.rb
|
metadata.gz.sig
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
���z(���1����_9�oDE%*H�TZ��M��f�I��GK�.*�?_���@����_t��G���!�6�g���� �0H8�ț��I�6��ڢ�y`��߮���Bi���d�2)s!�|��/2
|
@@ -1,119 +0,0 @@
|
|
1
|
-
require 'activerecord'
|
2
|
-
require 'active_record'
|
3
|
-
|
4
|
-
#:startdoc:
|
5
|
-
# ActiveRecord extensions
|
6
|
-
|
7
|
-
module ActiveRecord #:doc:
|
8
|
-
|
9
|
-
class Base #:doc:
|
10
|
-
|
11
|
-
# Create a record, or update a record if value passed matches a field in the AR object;
|
12
|
-
# includes method_missing function to make code more readable
|
13
|
-
#
|
14
|
-
# Most common use will be for testing (fixture/mock object generation)
|
15
|
-
#
|
16
|
-
# Three versions of method included:
|
17
|
-
# create_or_update
|
18
|
-
# create_or_update_by
|
19
|
-
# create_or_update_by_xxx (where xxx is a field name)
|
20
|
-
#
|
21
|
-
# Inspired by http://www.intridea.com/2008/2/19/activerecord-base-create_or_update-on-steroids-2
|
22
|
-
#
|
23
|
-
# ==Example
|
24
|
-
# { "admin" => ["Administrator", 1000],
|
25
|
-
# "member" => ["Member", 1],
|
26
|
-
# "moderator" => ["Moderator", 100],
|
27
|
-
# "disabled" => ["Disabled User", -1] }.each_pair do |key, val|
|
28
|
-
# Role.create_or_update_by_key(:key => key, :name => val[0], :value => val[1])
|
29
|
-
# end
|
30
|
-
|
31
|
-
def self.create_or_update(options = {})
|
32
|
-
self.create_or_update_by(:id, options)
|
33
|
-
end
|
34
|
-
|
35
|
-
|
36
|
-
# Create or update a record by field (or fields).
|
37
|
-
# This will look for each field name as a key in the options hash.
|
38
|
-
#
|
39
|
-
# ==Example
|
40
|
-
# attributes={:name="John Smith", :email=>"john@example.com", :birthdate=>'1980/01/01'}
|
41
|
-
# User.create_or_update_by(:email,attributes)
|
42
|
-
# => if a user with that email exists then update his name and birthdate, else create him
|
43
|
-
#
|
44
|
-
# ==Example with multiple conditions
|
45
|
-
# attributes={:name="John Smith", :email=>"john@example.com", :birthdate=>'1980/01/01'}
|
46
|
-
# User.create_or_update_by([:name,:birthdate],attributes)
|
47
|
-
# => if a user with that name and birthdate exists then update his email, else create him
|
48
|
-
#
|
49
|
-
# The fields can be any mix of symbols or strings.
|
50
|
-
# The option keys can be any mix of symbols or strings.
|
51
|
-
|
52
|
-
def self.create_or_update_by(condition_keys, attribute_pairs = {})
|
53
|
-
condition_pairs=[*condition_keys].map{|key| [key,(attribute_pairs.delete(key)||attribute_pairs.delete(key.to_s)||attribute_pairs.delete(key.to_sym))]}.to_h
|
54
|
-
record = find(:first, :conditions => condition_pairs) || self.new
|
55
|
-
if record.new_record? then attribute_pairs.merge!(condition_pairs) end
|
56
|
-
attribute_pairs.each_pair{|key,val| record.send key.to_s+'=', val}
|
57
|
-
record.save!
|
58
|
-
return record
|
59
|
-
end
|
60
|
-
|
61
|
-
|
62
|
-
# Set up a database with initial data, e.g. in rake db:seed method.
|
63
|
-
#
|
64
|
-
# This will look for each field name as a key in the options hash.
|
65
|
-
#
|
66
|
-
# This method calls #create_or_update_by (and you may want to change
|
67
|
-
# this behavior to do more, e.g. to test that a DB and table exists).
|
68
|
-
#
|
69
|
-
# ==Example
|
70
|
-
# attributes={:name="John Smith", :email=>"john@example.com", :birthdate=>'1980/01/01'}
|
71
|
-
# User.create_or_update_by(:email,attributes)
|
72
|
-
# => if a user with that email exists then update his name and birthdate, else create him
|
73
|
-
#
|
74
|
-
# ==Example with multiple conditions
|
75
|
-
# attributes={:name="John Smith", :email=>"john@example.com", :birthdate=>'1980/01/01'}
|
76
|
-
# User.create_or_update_by([:name,:birthdate],attributes)
|
77
|
-
# => if a user with that name and birthdate exists then update his email, else create him
|
78
|
-
#
|
79
|
-
# The fields can be any mix of symbols or strings.
|
80
|
-
# The option keys can be any mix of symbols or strings.
|
81
|
-
|
82
|
-
def self.seed(condition_keys, attribute_pairs = {})
|
83
|
-
self.create_or_update_by(condition_keys, attribute_pairs)
|
84
|
-
end
|
85
|
-
|
86
|
-
|
87
|
-
# Method missing for create_or_update_by_xxx that forwards to #create_or_update_by
|
88
|
-
#
|
89
|
-
# ==Example
|
90
|
-
# MyModel.create_or_update_by_foo(options)
|
91
|
-
# => MyModel.create_or_update_by(:foo,option)
|
92
|
-
|
93
|
-
def self.method_missing_with_create_or_update(method_name, *args)
|
94
|
-
if match = method_name.to_s.match(/create_or_update_by_([a-z0-9_]+)/)
|
95
|
-
field = match[1]
|
96
|
-
return create_or_update_by(field,*args)
|
97
|
-
end
|
98
|
-
return method_missing_without_create_or_update(method_name, *args)
|
99
|
-
end
|
100
|
-
|
101
|
-
|
102
|
-
# For alias method chain
|
103
|
-
def self.included(base)
|
104
|
-
# base == ActiveRecord::Base (the class)
|
105
|
-
base.class_eval do
|
106
|
-
# class_eval makes self == ActiveRecord::Base, and makes def define instance methods.
|
107
|
-
extend ClassMethods
|
108
|
-
# If has_many were an instance method, we could do this
|
109
|
-
# alias_method_chain :has_many, :association_option; end
|
110
|
-
# but it's a class method, so we have to do the alias_method_chain on
|
111
|
-
# the meta-class for ActiveRecord::Base, which is what class << self does.
|
112
|
-
class << self; alias_method_chain :method_missing, :create_or_update; end
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
|
117
|
-
end
|
118
|
-
|
119
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
module ActiveRecord
|
2
|
-
module ConnectionAdapters
|
3
|
-
module SchemaStatements
|
4
|
-
|
5
|
-
# Add a column and its index.
|
6
|
-
# This just calls #add_column then #add_index
|
7
|
-
|
8
|
-
def add_column_and_index(table_name, column_name, type, options = {})
|
9
|
-
add_column(table_name, column_name, type, options)
|
10
|
-
add_index(table_name, column_name, type, options)
|
11
|
-
end
|
12
|
-
|
13
|
-
|
14
|
-
# Remove a column and its index in one step.
|
15
|
-
# This just calls #remove_column then #remove_index.
|
16
|
-
|
17
|
-
def remove_column_and_index(table_name, column_name, type, options = {})
|
18
|
-
remove_column(table_name, column_name, type, options)
|
19
|
-
remove_index(table_name, column_name, type, options)
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
module ActiveRecord::SaveExtensions
|
2
|
-
|
3
|
-
# Save the record without validation, then reload it.
|
4
|
-
# If the record is valid then return true, otherwise raise RecordInvalid.
|
5
|
-
# This solves an issue we found with Rails associations not saving.
|
6
|
-
#
|
7
|
-
# By Andrew Carpenter (acarpen@wested.org)
|
8
|
-
#
|
9
|
-
# Deprecated, superceded by #save_redux!
|
10
|
-
|
11
|
-
def save_false_then_reload!
|
12
|
-
transaction do
|
13
|
-
save(false)
|
14
|
-
reload
|
15
|
-
valid? or raise ActiveRecord::RecordInvalid.new(self)
|
16
|
-
end
|
17
|
-
return true
|
18
|
-
end
|
19
|
-
|
20
|
-
|
21
|
-
# Save the record without validation, then reload, then save with validtion.
|
22
|
-
# This ensure that rails brings back the correct associations to validate.
|
23
|
-
# This solves an issue we found with Rails associations not saving.
|
24
|
-
|
25
|
-
def save_redux!
|
26
|
-
transaction do
|
27
|
-
save(false)
|
28
|
-
reload
|
29
|
-
save!
|
30
|
-
end
|
31
|
-
return true
|
32
|
-
end
|
33
|
-
|
34
|
-
end
|
35
|
-
ActiveRecord::Base.send(:include, ActiveRecord::SaveExtensions)
|
@@ -1,64 +0,0 @@
|
|
1
|
-
require 'test/unit'
|
2
|
-
require 'webget_ruby_ramp'
|
3
|
-
require 'active_record'
|
4
|
-
|
5
|
-
begin
|
6
|
-
require 'sqlite3'
|
7
|
-
rescue
|
8
|
-
# noop because sqlite may already be available,
|
9
|
-
# e.g. in JRuby on Ubuntu you can install it:
|
10
|
-
# apt-get install libsqlite3-ruby
|
11
|
-
end
|
12
|
-
|
13
|
-
begin
|
14
|
-
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
15
|
-
rescue
|
16
|
-
raise ""+
|
17
|
-
"ActiveRecord cannot establish connection to sqlite3 database memory.\n" +
|
18
|
-
"Please verify that you have the sqlite3 database installed for testing.\n" +
|
19
|
-
"To install it for Ruby typically do: sudo gem install sqlite3-ruby\n" +
|
20
|
-
"To install it for JRuby + Ubuntu do: sudo apt-get install libsqlite3-ruby"
|
21
|
-
end
|
22
|
-
|
23
|
-
ActiveRecord::Schema.define(:version => 1) do
|
24
|
-
create_table :foos do |t|
|
25
|
-
t.string :a
|
26
|
-
t.string :b
|
27
|
-
t.string :c
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
class Foo < ActiveRecord::Base
|
32
|
-
end
|
33
|
-
|
34
|
-
class ActiveRecordTest < Test::Unit::TestCase
|
35
|
-
|
36
|
-
def test_prelim_count
|
37
|
-
assert_equal(0,Foo.count)
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_prelim_create
|
41
|
-
f=Foo.new
|
42
|
-
f.a='aa'
|
43
|
-
f.b='bb'
|
44
|
-
f.c='cc'
|
45
|
-
f.save
|
46
|
-
assert_equal(1,Foo.count)
|
47
|
-
end
|
48
|
-
|
49
|
-
def test_seed_with_create
|
50
|
-
n1=Foo.count
|
51
|
-
Foo.seed(:a,{:a=>'xxx',:b=>'yyy'})
|
52
|
-
n2=Foo.count
|
53
|
-
assert_equal(n1+1,n2)
|
54
|
-
end
|
55
|
-
|
56
|
-
def test_seed_with_update
|
57
|
-
n1=Foo.count
|
58
|
-
f1=Foo.find(:first)
|
59
|
-
Foo.seed(:a,{:a=>f1.a,:b=>'bbb'})
|
60
|
-
n2=Foo.count
|
61
|
-
assert_equal(n1,n2)
|
62
|
-
end
|
63
|
-
|
64
|
-
end
|