facets 2.8.2 → 2.8.3
Sign up to get free protection for your applications and to get access to all the features.
- data/AUTHORS +13 -11
- data/HISTORY.rdoc +58 -0
- data/lib/core/facets/array/recursive.rb +91 -0
- data/lib/core/facets/array/recursively.rb +2 -2
- data/lib/core/facets/array/traverse.rb +23 -6
- data/lib/core/facets/enumerable/collisions.rb +1 -0
- data/lib/core/facets/enumerable/commonality.rb +4 -2
- data/lib/core/facets/enumerable/graph.rb +37 -1
- data/lib/core/facets/enumerable/mash.rb +1 -39
- data/lib/core/facets/enumerable/recursive.rb +75 -0
- data/lib/core/facets/enumerable/visit.rb +30 -0
- data/lib/core/facets/file/ext.rb +36 -0
- data/lib/core/facets/hash/graph.rb +18 -0
- data/lib/core/facets/hash/mash.rb +1 -18
- data/lib/core/facets/hash/recursive.rb +180 -0
- data/lib/core/facets/hash/recursive_merge.rb +6 -0
- data/lib/core/facets/hash/recursively.rb +2 -2
- data/lib/core/facets/hash/to_module.rb +26 -0
- data/lib/core/facets/hash/to_proc.rb +2 -2
- data/lib/core/facets/hash/traverse.rb +19 -13
- data/lib/core/facets/kernel/assign.rb +63 -0
- data/lib/core/facets/kernel/assign_from.rb +45 -0
- data/lib/core/facets/kernel/dup.rb +63 -0
- data/lib/core/facets/kernel/instance.rb +156 -0
- data/lib/core/facets/kernel/instance_assign.rb +1 -22
- data/lib/core/facets/kernel/meta_def.rb +4 -0
- data/lib/core/facets/kernel/populate.rb +1 -74
- data/lib/core/facets/kernel/set_from.rb +2 -0
- data/lib/core/facets/kernel/try_dup.rb +1 -0
- data/lib/core/facets/module/set.rb +36 -0
- data/lib/core/facets/objectspace/reflect.rb +45 -0
- data/lib/core/facets/struct/attributes.rb +6 -2
- data/lib/core/facets/symbol/op_div.rb +19 -0
- data/lib/core/facets/to_hash.rb +12 -0
- data/lib/more/facets/casting_hash.rb +172 -0
- data/lib/more/facets/pathname.rb +36 -0
- data/lib/more/facets/prepend.rb +57 -0
- data/lib/more/facets/random.rb +19 -3
- data/lib/more/facets/roman.rb +46 -153
- data/lib/more/facets/stash.rb +148 -33
- data/meta/released +1 -1
- data/meta/version +1 -1
- data/test/core/array/test_recursive.rb +18 -0
- data/test/core/enumerable/test_recursive.rb +18 -0
- data/test/core/file/test_ext.rb +31 -0
- data/test/core/hash/test_recursive.rb +23 -0
- data/test/core/hash/test_to_module.rb +21 -0
- data/test/core/kernel/test_assign.rb +57 -0
- data/test/core/kernel/test_assign_from.rb +20 -0
- data/test/more/test_prepend.rb +28 -0
- data/test/more/test_random.rb +40 -4
- metadata +39 -10
- data/lib/core/facets/kernel/instance_variables.rb +0 -97
- data/lib/more/facets/instance_eval.rb +0 -50
- data/lib/more/facets/ioredirect.rb +0 -77
- data/lib/more/facets/plugin_manager.rb +0 -50
- data/test/core/kernel/test_populate.rb +0 -46
data/lib/more/facets/stash.rb
CHANGED
@@ -1,65 +1,180 @@
|
|
1
|
-
require 'facets/hash/rekey'
|
2
|
-
|
3
|
-
# = Hash
|
4
|
-
#
|
5
1
|
# Stash is just like Hash, except that all keys are
|
6
2
|
# converted to Strings.
|
7
3
|
#
|
8
|
-
#
|
9
|
-
|
10
|
-
# keys are strings when using default_proc.
|
11
|
-
#
|
4
|
+
# Note this doesn't yet handle default_proc.
|
5
|
+
|
12
6
|
class Stash < Hash
|
13
7
|
|
14
|
-
|
15
|
-
|
8
|
+
#
|
9
|
+
def self.[](*hash)
|
10
|
+
s = new
|
11
|
+
super(*hash).each{ |k,v| s[k] = v }
|
12
|
+
s
|
16
13
|
end
|
17
14
|
|
18
|
-
|
19
|
-
|
15
|
+
#
|
16
|
+
def [](key)
|
17
|
+
super(convert_key(key))
|
20
18
|
end
|
21
19
|
|
22
|
-
|
23
|
-
|
20
|
+
#
|
21
|
+
def []=(key,value)
|
22
|
+
super(convert_key(key), value)
|
24
23
|
end
|
25
24
|
|
26
|
-
|
27
|
-
|
25
|
+
#
|
26
|
+
def <<(other)
|
27
|
+
case other
|
28
|
+
when Hash
|
29
|
+
super(other.rekey{ |key| convert_key(key) })
|
30
|
+
when Array
|
31
|
+
self[other[0]] = other[1]
|
32
|
+
else
|
33
|
+
raise ArgumentError
|
34
|
+
end
|
28
35
|
end
|
29
36
|
|
30
|
-
|
31
|
-
|
37
|
+
#
|
38
|
+
def fetch(key)
|
39
|
+
super(convert_key(key))
|
32
40
|
end
|
33
41
|
|
34
|
-
|
35
|
-
|
42
|
+
#
|
43
|
+
def store(key, value)
|
44
|
+
super(convert_key(key), value)
|
36
45
|
end
|
37
46
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
47
|
+
#
|
48
|
+
def key?(key)
|
49
|
+
super(convert_key(key))
|
50
|
+
end
|
51
|
+
|
52
|
+
#
|
53
|
+
def has_key?(key)
|
54
|
+
super(convert_key(key))
|
55
|
+
end
|
56
|
+
|
57
|
+
#
|
58
|
+
def include?(key)
|
59
|
+
super(convert_key(key))
|
60
|
+
end
|
61
|
+
|
62
|
+
#
|
63
|
+
def member?(key)
|
64
|
+
super(convert_key(key))
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
# Synonym for Hash#rekey, but modifies the receiver in place (and returns it).
|
69
|
+
#
|
70
|
+
# foo = { :name=>'Gavin', :wife=>:Lisa }.to_stash
|
71
|
+
# foo.rekey!{ |k| k.upcase } #=> { "NAME"=>"Gavin", "WIFE"=>:Lisa }
|
72
|
+
# foo.inspect #=> { "NAME"=>"Gavin", "WIFE"=>:Lisa }
|
73
|
+
#
|
74
|
+
def rekey!(*args, &block)
|
75
|
+
# for backward comptability (TODO: DEPRECATE?).
|
76
|
+
block = args.pop.to_sym.to_proc if args.size == 1
|
77
|
+
if args.empty?
|
78
|
+
block = lambda{|k| k} unless block
|
79
|
+
keys.each do |k|
|
80
|
+
nk = block[k]
|
81
|
+
self[nk.to_s]=delete(k) #if nk
|
82
|
+
end
|
44
83
|
else
|
45
|
-
raise ArgumentError
|
84
|
+
raise ArgumentError, "3 for 2" if block
|
85
|
+
to, from = *args
|
86
|
+
self[to] = delete(from) if has_key?(from)
|
46
87
|
end
|
88
|
+
self
|
89
|
+
end
|
90
|
+
|
91
|
+
#
|
92
|
+
def rekey(*args, &block)
|
93
|
+
dup.rekey!(*args, &block)
|
94
|
+
end
|
95
|
+
|
96
|
+
#
|
97
|
+
def delete(key)
|
98
|
+
super(convert_key(key))
|
47
99
|
end
|
48
100
|
|
101
|
+
#
|
49
102
|
def update(other)
|
50
|
-
super(other.rekey(
|
103
|
+
super(other.rekey{ |key| convert_key(key) })
|
51
104
|
end
|
52
105
|
|
106
|
+
# Same as #update.
|
53
107
|
def merge!(other)
|
54
|
-
super(other.rekey(
|
108
|
+
super(other.rekey{ |key| convert_key(key) })
|
55
109
|
end
|
56
|
-
|
57
|
-
|
58
|
-
|
110
|
+
|
111
|
+
#
|
112
|
+
def merge(other)
|
113
|
+
super(other.rekey{ |key| convert_key(key) })
|
114
|
+
end
|
115
|
+
|
116
|
+
#
|
117
|
+
def replace(other)
|
118
|
+
super(other.rekey{ |key| convert_key(key) })
|
59
119
|
end
|
60
120
|
|
121
|
+
#
|
61
122
|
def values_at(*keys)
|
62
|
-
super(keys.map{|
|
123
|
+
super(*keys.map{ |key| convert_key(key) })
|
124
|
+
end
|
125
|
+
|
126
|
+
#
|
127
|
+
def to_hash
|
128
|
+
h = {}
|
129
|
+
each{ |k,v| h[k] = v }
|
130
|
+
h
|
131
|
+
end
|
132
|
+
|
133
|
+
alias_method :to_h, :to_hash
|
134
|
+
|
135
|
+
private
|
136
|
+
|
137
|
+
def convert_key(key)
|
138
|
+
key.to_s
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
142
|
+
|
143
|
+
class Hash
|
144
|
+
|
145
|
+
# Convert a Hash to a Stash object.
|
146
|
+
def to_stash
|
147
|
+
Stash[self]
|
148
|
+
end
|
149
|
+
|
150
|
+
# Synonym for Hash#rekey, but modifies the receiver in place (and returns it).
|
151
|
+
#
|
152
|
+
# foo = { :name=>'Gavin', :wife=>:Lisa }
|
153
|
+
# foo.rekey!{ |k| k.to_s } #=> { "name"=>"Gavin", "wife"=>:Lisa }
|
154
|
+
# foo.inspect #=> { "name"=>"Gavin", "wife"=>:Lisa }
|
155
|
+
#
|
156
|
+
# This method comes from Ruby Facets.
|
157
|
+
|
158
|
+
def rekey!(*args, &block)
|
159
|
+
# for backward comptability (TODO: DEPRECATE).
|
160
|
+
block = args.pop.to_sym.to_proc if args.size == 1
|
161
|
+
if args.empty?
|
162
|
+
block = lambda{|k| k.to_sym} unless block
|
163
|
+
keys.each do |k|
|
164
|
+
nk = block[k]
|
165
|
+
self[nk]=delete(k) if nk
|
166
|
+
end
|
167
|
+
else
|
168
|
+
raise ArgumentError, "3 for 2" if block
|
169
|
+
to, from = *args
|
170
|
+
self[to] = self.delete(from) if self.has_key?(from)
|
171
|
+
end
|
172
|
+
self
|
173
|
+
end
|
174
|
+
|
175
|
+
# Non-inplace #rekey! method.
|
176
|
+
def rekey(*args, &block)
|
177
|
+
dup.rekey!(*args, &block)
|
63
178
|
end
|
64
179
|
|
65
180
|
end
|
data/meta/released
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2010-
|
1
|
+
2010-04-10
|
data/meta/version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.8.
|
1
|
+
2.8.3
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'facets/array/recursive'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class TestArrayRecursive < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_each
|
7
|
+
a = []
|
8
|
+
[1,2,['a','b']].recursive.each{ |v| a << v }
|
9
|
+
assert_equal([1,2,'a','b'], a)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_map
|
13
|
+
a = [1,2,['a','b']].recursive.map{ |v| v.succ }
|
14
|
+
assert_equal([2,3,['b','c']], a)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'facets/enumerable/recursive'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class TestEnumerableRecursive < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_each
|
7
|
+
a = []
|
8
|
+
[1,2,['a','b']].recursive.each{ |v| a << v }
|
9
|
+
assert_equal([1,2,'a','b'], a)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_map
|
13
|
+
a = [1,2,['a','b']].recursive.map{ |v| v.succ }
|
14
|
+
assert_equal([2,3,['b','c']], a)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'facets/file/ext'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class Test_File_Ext < Test::Unit::TestCase
|
5
|
+
|
6
|
+
# Standard file names.
|
7
|
+
def test_ext_standard
|
8
|
+
s = 'file.txt'
|
9
|
+
assert_equal('txt', File.ext(s))
|
10
|
+
assert_equal('file.old', File.ext(s, 'old'))
|
11
|
+
assert_equal('file', File.ext(s, ''))
|
12
|
+
end
|
13
|
+
|
14
|
+
# Initially no extension.
|
15
|
+
def test_ext_none
|
16
|
+
s = 'file'
|
17
|
+
assert_equal('', File.ext(s))
|
18
|
+
assert_equal('file.txt', File.ext(s, 'txt'))
|
19
|
+
assert_equal('file', File.ext(s, ''))
|
20
|
+
end
|
21
|
+
|
22
|
+
# Some non-standard case.
|
23
|
+
def test_ext_non_standard
|
24
|
+
s = '.profile'
|
25
|
+
assert_equal('', File.ext(s))
|
26
|
+
assert_equal('.profile.new', File.ext(s, 'new'))
|
27
|
+
assert_equal('.profile', File.ext(s, ''))
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'facets/hash/recursive'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class TestHashRecursive < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_each
|
7
|
+
a = []
|
8
|
+
{:a=>1,:b=>{:c=>3}}.recursive.each{ |k,v| a << [k,v] }
|
9
|
+
assert_equal([[:a,1],[:c,3], [:b,{:c=>3}]], a)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_map_moot
|
13
|
+
a = {:a=>1,:b=>{:c=>3}}.recursive.map{ |k,v| [k, v] }
|
14
|
+
assert_equal([[:a,1],[:b,[[:c,3]]]], a)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_map
|
18
|
+
a = {:a=>1,:b=>{:c=>3}}.recursive.map{ |k,v| Hash===v ? [k, v.succ] : [k,v] }
|
19
|
+
assert_equal([[:a,1],[:b,[[:c,3]]]], a)
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'facets/hash/to_module'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class TC_Hash_To_Module < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_to_module
|
7
|
+
h = { :a=>1, :b=>2 }
|
8
|
+
m = h.to_module
|
9
|
+
assert(Module === m)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_to_module_methods
|
13
|
+
h = { :a=>1, :b=>2 }
|
14
|
+
m = h.to_module
|
15
|
+
x = Class.new{ include m }.new
|
16
|
+
assert_equal( 1, x.a )
|
17
|
+
assert_equal( 2, x.b )
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'facets/kernel/assign.rb'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class TestKernelAssign < Test::Unit::TestCase
|
5
|
+
|
6
|
+
C = Struct.new(:name, :address, :zip)
|
7
|
+
|
8
|
+
def test_assign_with_arguments
|
9
|
+
bob = C.new
|
10
|
+
|
11
|
+
bob.assign(:name, "Bob Sawyer")
|
12
|
+
bob.assign(:address, "123 Maple, Anytown NC")
|
13
|
+
bob.assign(:zip, 12345)
|
14
|
+
|
15
|
+
assert_equal("Bob Sawyer", bob.name)
|
16
|
+
assert_equal("123 Maple, Anytown NC", bob.address)
|
17
|
+
assert_equal(12345, bob.zip)
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_assign_with_hash
|
21
|
+
bob = C.new
|
22
|
+
|
23
|
+
x = { :name => "Bob Sawyer", :address => "123 Maple, Anytown NC", :zip => 12345 }
|
24
|
+
|
25
|
+
bob.assign(x)
|
26
|
+
|
27
|
+
assert_equal(x[:name], bob.name)
|
28
|
+
assert_equal(x[:address], bob.address)
|
29
|
+
assert_equal(x[:zip], bob.zip)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_assign_with_assoc_array
|
33
|
+
bob = C.new
|
34
|
+
|
35
|
+
x = [[:name, "Bob Sawyer"], [:address, "123 Maple, Anytown NC"], [:zip, 12345]]
|
36
|
+
|
37
|
+
bob.assign(x)
|
38
|
+
|
39
|
+
assert_equal("Bob Sawyer", bob.name)
|
40
|
+
assert_equal("123 Maple, Anytown NC", bob.address)
|
41
|
+
assert_equal(12345, bob.zip)
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_assign_with_block
|
45
|
+
bob = C.new
|
46
|
+
|
47
|
+
x = lambda {|s| s.name = "Bob Sawyer"; s.address = "123 Maple, Anytown NC"; s.zip = 12345 }
|
48
|
+
|
49
|
+
bob.assign(&x)
|
50
|
+
|
51
|
+
assert_equal("Bob Sawyer", bob.name)
|
52
|
+
assert_equal("123 Maple, Anytown NC", bob.address)
|
53
|
+
assert_equal(12345, bob.zip)
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'facets/kernel/assign_from.rb'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
class TestKernelAssignFrom < Test::Unit::TestCase
|
5
|
+
|
6
|
+
C = Struct.new(:name, :address, :zip)
|
7
|
+
|
8
|
+
def test_assign_from
|
9
|
+
bob = C.new("Bob Sawyer", "123 Maple, Anytown NC", 12345)
|
10
|
+
joe = C.new("Joe Pitare")
|
11
|
+
|
12
|
+
joe.assign_from(bob, :address, :zip)
|
13
|
+
|
14
|
+
assert_equal("Joe Pitare", joe.name)
|
15
|
+
assert_equal("123 Maple, Anytown NC", joe.address)
|
16
|
+
assert_equal(12345, joe.zip)
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'facets/prepend'
|
3
|
+
|
4
|
+
class TC_Class_Prepend < Test::Unit::TestCase
|
5
|
+
|
6
|
+
class C
|
7
|
+
def f
|
8
|
+
"f"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module M
|
13
|
+
def f
|
14
|
+
'{' + super + '}'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class C
|
19
|
+
prepend M
|
20
|
+
end
|
21
|
+
|
22
|
+
#
|
23
|
+
def test_prepend
|
24
|
+
c = C.new
|
25
|
+
assert_equal("{f}", c.f)
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|