facets 2.8.1 → 2.8.2
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY.rdoc +121 -91
- data/NOTES +13 -1
- data/demo/scenario_require.rdoc +0 -5
- data/lib/core/facets.rb +6 -382
- data/lib/core/facets/array.rb +4 -3
- data/lib/core/facets/array/product.rb +0 -9
- data/lib/core/facets/array/uniq_by.rb +18 -0
- data/lib/core/facets/binding/caller.rb +14 -18
- data/lib/core/facets/boolean.rb +75 -78
- data/lib/core/facets/denumerable.rb +3 -3
- data/lib/core/facets/dir/ascend.rb +2 -1
- data/lib/core/facets/enumerable/collapse.rb +12 -0
- data/lib/core/facets/enumerable/compact_map.rb +8 -4
- data/lib/core/facets/enumerable/defer.rb +3 -2
- data/lib/core/facets/enumerable/each_with_object.rb +38 -0
- data/lib/core/facets/enumerable/every.rb +4 -4
- data/lib/core/facets/enumerable/ewise.rb +22 -16
- data/lib/core/facets/enumerable/exclude.rb +13 -0
- data/lib/core/facets/enumerable/filter.rb +18 -8
- data/lib/core/facets/enumerable/find_yield.rb +37 -0
- data/lib/core/facets/enumerable/map_detect.rb +1 -28
- data/lib/core/facets/enumerable/map_with_index.rb +2 -2
- data/lib/core/facets/enumerable/per.rb +2 -2
- data/lib/core/facets/enumerable/purge.rb +43 -0
- data/lib/core/facets/enumerator.rb +70 -0
- data/lib/core/facets/enumerator/fx.rb +20 -0
- data/lib/core/facets/integer/multiple.rb +6 -2
- data/lib/core/facets/kernel/false.rb +2 -0
- data/lib/core/facets/kernel/true.rb +26 -0
- data/lib/core/facets/numeric/length.rb +2 -2
- data/lib/core/facets/numeric/spacing.rb +20 -0
- data/lib/core/facets/proc/bind_to.rb +9 -0
- data/lib/core/facets/string/exclude.rb +10 -0
- data/lib/core/facets/string/expand_tab.rb +6 -6
- data/lib/core/facets/time/future.rb +14 -0
- data/lib/core/facets/time/past.rb +1 -0
- data/lib/core/facets/unboundmethod/name.rb +22 -18
- data/lib/more/facets/date.rb +38 -3
- data/lib/more/facets/fileutils/cp_rx.rb +42 -0
- data/lib/more/facets/pathname.rb +1 -1
- data/meta/homepage +1 -1
- data/meta/released +1 -1
- data/meta/version +1 -1
- data/test/core/array/test_product.rb +0 -5
- data/test/core/enumerable/test_find_yield.rb +75 -0
- data/test/core/numeric/test_spacing.rb +12 -0
- data/test/core/unboundmethod/test_name.rb +3 -3
- metadata +36 -23
- data/MANIFEST +0 -756
- data/lib/core/facets/enumerable/collect.rb +0 -4
- data/lib/core/facets/enumerable/inject.rb +0 -30
- data/lib/core/facets/numeric/size.rb +0 -10
- data/lib/more/facets/enumerator.rb +0 -62
- data/lib/more/facets/tracepoint.rb +0 -209
- data/test/core/enumerable/test_map_detect.rb +0 -75
@@ -1,30 +0,0 @@
|
|
1
|
-
module Enumerable
|
2
|
-
|
3
|
-
# A small variation of #Inject that save one from having to
|
4
|
-
# return the aggregating or memo argument.
|
5
|
-
#
|
6
|
-
# Say you want to count letters.
|
7
|
-
#
|
8
|
-
# some_text.inject!(Hash.new(0)) {|h,l| h[l] += 1}
|
9
|
-
#
|
10
|
-
# vs
|
11
|
-
#
|
12
|
-
# some_text.inject(Hash.new(0)) {|h,l| h[l] +=1; h}
|
13
|
-
#
|
14
|
-
# CREDIT: David Black, Louis J Scoras
|
15
|
-
|
16
|
-
def inject!(s)
|
17
|
-
k = s
|
18
|
-
each { |i| yield(k, i) }
|
19
|
-
k
|
20
|
-
end
|
21
|
-
|
22
|
-
# HERE AS BACKUP, TO ENSURE THE ABOVE HAS THE SAME FUNCTIONALITY.
|
23
|
-
#def injecting(s)
|
24
|
-
# inject(s) do |k, i|
|
25
|
-
# yield(k, i); k
|
26
|
-
# end
|
27
|
-
#end
|
28
|
-
|
29
|
-
end
|
30
|
-
|
@@ -1,62 +0,0 @@
|
|
1
|
-
begin
|
2
|
-
require 'enumerator' #if RUBY_VERSION < 1.9
|
3
|
-
# for Ruby 1.8 -> 1.9 transition
|
4
|
-
Enumerator = Enumerable::Enumerator unless defined? ::Enumerator
|
5
|
-
rescue LoadError # Ruby 1.9 already has it built-in.
|
6
|
-
end
|
7
|
-
|
8
|
-
class Enumerator
|
9
|
-
|
10
|
-
alias :old_initialize :initialize
|
11
|
-
|
12
|
-
# Provides the ruby-1.9 block form of Enumerator, where you can write:
|
13
|
-
#
|
14
|
-
# obj = Enumerator.new do |yielder|
|
15
|
-
# .. do stuff
|
16
|
-
# yielder.yield data # or: yielder << data
|
17
|
-
# .. etc
|
18
|
-
# end
|
19
|
-
#
|
20
|
-
# When obj.each is called, the block is run once. It should call
|
21
|
-
# yielder.yield with each item it wishes to generate.
|
22
|
-
#
|
23
|
-
# Example:
|
24
|
-
#
|
25
|
-
# fib = Enumerator.new { |y|
|
26
|
-
# a = b = 1
|
27
|
-
# loop {
|
28
|
-
# y << a
|
29
|
-
# a, b = b, a + b
|
30
|
-
# }
|
31
|
-
# }
|
32
|
-
#
|
33
|
-
# assert_equal [1, 1, 2, 3, 5, 8, 13, 21, 34, 55], fib.take(10)
|
34
|
-
|
35
|
-
def initialize(*args, &block)
|
36
|
-
if block_given?
|
37
|
-
@body = block
|
38
|
-
old_initialize(self, :_start)
|
39
|
-
else
|
40
|
-
old_initialize(*args)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
def _start(*args,&receiver) #:nodoc:
|
45
|
-
@body.call(Yielder.new(receiver), *args)
|
46
|
-
end
|
47
|
-
|
48
|
-
# Wrapper to allow yielder.yield(output) or yielder << output
|
49
|
-
# in the same way as ruby-1.9
|
50
|
-
|
51
|
-
class Yielder #:nodoc:
|
52
|
-
def initialize(proc)
|
53
|
-
@proc = proc
|
54
|
-
end
|
55
|
-
def yield(*args)
|
56
|
-
@proc[*args]
|
57
|
-
end
|
58
|
-
alias :<< :yield
|
59
|
-
end
|
60
|
-
|
61
|
-
end
|
62
|
-
|
@@ -1,209 +0,0 @@
|
|
1
|
-
# = TracePoint
|
2
|
-
#
|
3
|
-
# A TracePoint is a Binding with the addition event information.
|
4
|
-
# And it's a better way to use set_trace_func.
|
5
|
-
#
|
6
|
-
# A TracePoint is a Binding with the addition of event information.
|
7
|
-
# Among other things, it functions very well as the join-point for
|
8
|
-
# Event-based AOP.
|
9
|
-
#
|
10
|
-
# == Usage
|
11
|
-
#
|
12
|
-
# TracePoint.trace { |tp|
|
13
|
-
# puts "#{tp.self.class}\t#{tp.called}\t#{tp.event}\t#{tp.return?}\t#{tp.back == tp.bind}"
|
14
|
-
# }
|
15
|
-
#
|
16
|
-
# 1 + 1
|
17
|
-
#
|
18
|
-
# produces
|
19
|
-
#
|
20
|
-
# Class trace return true false
|
21
|
-
# Object line false false
|
22
|
-
# Fixnum + c-call false false
|
23
|
-
# Fixnum + c-return false false
|
24
|
-
#
|
25
|
-
# == Notes
|
26
|
-
#
|
27
|
-
# You can't subclass Binding, so we delegate (which is better anyway).
|
28
|
-
#
|
29
|
-
# == Authors
|
30
|
-
#
|
31
|
-
# * Thomas Sawyer
|
32
|
-
#
|
33
|
-
# == Copying
|
34
|
-
#
|
35
|
-
# Copyright (c) 2005 Thomas Sawyer
|
36
|
-
#
|
37
|
-
# Ruby License
|
38
|
-
#
|
39
|
-
# This module is free software. You may use, modify, and/or redistribute this
|
40
|
-
# software under the same terms as Ruby.
|
41
|
-
#
|
42
|
-
# This program is distributed in the hope that it will be useful, but WITHOUT
|
43
|
-
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
44
|
-
# FOR A PARTICULAR PURPOSE.
|
45
|
-
|
46
|
-
require 'facets/binding'
|
47
|
-
|
48
|
-
# = CodePoint
|
49
|
-
#
|
50
|
-
# This is the same as a Binding. Not really needed, but I like consistency :)
|
51
|
-
|
52
|
-
CodePoint = Binding
|
53
|
-
|
54
|
-
# = TracePoint
|
55
|
-
#
|
56
|
-
# A TracePoint is a Binding with the addition of event information.
|
57
|
-
# Among other things, it functions very well as the join-point for
|
58
|
-
# Event-based AOP.
|
59
|
-
#
|
60
|
-
# == Usage
|
61
|
-
#
|
62
|
-
# TracePoint.trace { |tp|
|
63
|
-
# puts "#{tp.self.class}\t#{tp.called}\t#{tp.event}\t#{tp.return?}\t#{tp.back == tp.bind}"
|
64
|
-
# }
|
65
|
-
#
|
66
|
-
# 1 + 1
|
67
|
-
#
|
68
|
-
# produces
|
69
|
-
#
|
70
|
-
# Class trace return true false
|
71
|
-
# Object line false false
|
72
|
-
# Fixnum + c-call false false
|
73
|
-
# Fixnum + c-return false false
|
74
|
-
#
|
75
|
-
# == Notes
|
76
|
-
#
|
77
|
-
# You can't subclass Binding, so we delegate (which is better anyway).
|
78
|
-
|
79
|
-
class TracePoint #< Codepoint
|
80
|
-
|
81
|
-
# -- class ---------------------
|
82
|
-
class << self
|
83
|
-
|
84
|
-
@@active = false
|
85
|
-
|
86
|
-
def active ; @@active ; end
|
87
|
-
|
88
|
-
def active=(x)
|
89
|
-
@@active = x ? true : false
|
90
|
-
unless @@active
|
91
|
-
set_trace_func nil
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
# Trace execution using a TracePoint.
|
96
|
-
def trace # :yield:
|
97
|
-
if active
|
98
|
-
bb_stack = []
|
99
|
-
set_trace_func proc{ |e, f, l, m, b, k|
|
100
|
-
#(p e, f, l, m, b, k, @@bb_stack; puts "---") if $DEBUG
|
101
|
-
if ['call','c-call','class'].include?(e)
|
102
|
-
bb_stack << b
|
103
|
-
elsif ['return','c-return','end'].include?(e)
|
104
|
-
bb = bb_stack.pop
|
105
|
-
end
|
106
|
-
b = bb if ! b # this sucks!
|
107
|
-
tp = TracePoint.new(e,m,b,bb)
|
108
|
-
yield(tp)
|
109
|
-
}
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
end #class
|
114
|
-
|
115
|
-
# -- instance -------------------
|
116
|
-
|
117
|
-
attr_accessor :event, :binding, :back_binding
|
118
|
-
|
119
|
-
# Until Ruby has a built-in way to get the name of the calling method
|
120
|
-
# that information must be passed into the TracePoint.
|
121
|
-
def initialize( event, method, bind, back_binding=bind )
|
122
|
-
@event = event
|
123
|
-
@method = method
|
124
|
-
@binding = bind
|
125
|
-
@back_binding = back_binding
|
126
|
-
end
|
127
|
-
|
128
|
-
# shorthand for binding
|
129
|
-
def bind ; @binding ; end
|
130
|
-
|
131
|
-
# shorthand for back_binding
|
132
|
-
def back ; @back_binding ; end
|
133
|
-
|
134
|
-
# Delegates "self" to the binding which
|
135
|
-
# in turn delegates the binding object.
|
136
|
-
def self ; @binding.self ; end
|
137
|
-
|
138
|
-
# Returns the name of the event's method.
|
139
|
-
# This could delegate to the binding if Ruby had
|
140
|
-
# an internal way to retrieve the current method name.
|
141
|
-
def callee ; @method ; end
|
142
|
-
#def method ; @method ; end # TODO Conflict with Kernel#method?
|
143
|
-
alias_method( :called, :callee ) # TODO deprecate
|
144
|
-
alias_method( :method_name, :called ) # TODO deprecate
|
145
|
-
|
146
|
-
# delegate to binding
|
147
|
-
#def method_missing(meth, *args, &blk)
|
148
|
-
# @binding.send(meth, *args, &blk)
|
149
|
-
#end
|
150
|
-
|
151
|
-
### methods for working with events
|
152
|
-
|
153
|
-
EVENT_MAP = {
|
154
|
-
:all => ['call', 'c-call', 'return', 'c-return', 'line', 'class', 'end', 'raise'],
|
155
|
-
:before => ['call', 'c-call'],
|
156
|
-
:after => ['return', 'c-return'],
|
157
|
-
:call => ['call'],
|
158
|
-
:return => ['return'],
|
159
|
-
:ccall => ['c-call'],
|
160
|
-
:creturn => ['c-return'],
|
161
|
-
:line => ['line'],
|
162
|
-
:class => ['class'],
|
163
|
-
:end => ['end'],
|
164
|
-
:raise => ['raise']
|
165
|
-
}
|
166
|
-
def event_map(e) ; EVENT_MAP[e] ; end
|
167
|
-
|
168
|
-
# Is the trace point defined or undefined?
|
169
|
-
def event? ; !! @event ; end
|
170
|
-
def eventless? ; ! @event ; end
|
171
|
-
|
172
|
-
# For use in case conditions
|
173
|
-
def ===(e)
|
174
|
-
EVENT_MAP[e].include?(@event)
|
175
|
-
end
|
176
|
-
|
177
|
-
# Creates an <event>? method for each of the above event mappings.
|
178
|
-
EVENT_MAP.each_pair { |m,v|
|
179
|
-
define_method( "#{m}?" ){ v.include?(@event) }
|
180
|
-
}
|
181
|
-
|
182
|
-
end
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
# _____ _
|
187
|
-
# |_ _|__ ___| |_
|
188
|
-
# | |/ _ \/ __| __|
|
189
|
-
# | | __/\__ \ |_
|
190
|
-
# |_|\___||___/\__|
|
191
|
-
#
|
192
|
-
|
193
|
-
# TODO
|
194
|
-
|
195
|
-
=begin #test
|
196
|
-
|
197
|
-
# Note: TracePoint will probably prove tricky to test, since
|
198
|
-
# manipulating set_trace_func tends to wreak havoc on errors,
|
199
|
-
# the call stack, and so on.
|
200
|
-
|
201
|
-
TracePoint.active = true
|
202
|
-
|
203
|
-
TracePoint.trace { |tp|
|
204
|
-
puts "#{tp.self.class}\t#{tp.called}\t#{tp.event}\t#{tp.return?}\t#{tp.back == tp.bind}"
|
205
|
-
}
|
206
|
-
|
207
|
-
1 + 1
|
208
|
-
|
209
|
-
=end
|
@@ -1,75 +0,0 @@
|
|
1
|
-
require 'facets/enumerable/map_detect'
|
2
|
-
require 'test/unit'
|
3
|
-
|
4
|
-
class TestEnumerable < Test::Unit::TestCase
|
5
|
-
|
6
|
-
def test_map_detects_a_value
|
7
|
-
assert_equal true, [true].map_detect { |value| value }
|
8
|
-
end
|
9
|
-
|
10
|
-
def test_map_detect_detects_correct_value
|
11
|
-
assert_equal 1, [1].map_detect { |value| value }
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_map_detect_returns_value_of_block
|
15
|
-
assert_equal 4, [1].map_detect { |v| v + 3 }
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_map_detect_detects_first_truthy_value
|
19
|
-
assert_equal 1, [false, false, 1].map_detect { |value| value }
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_map_detect_returns_value_when_block_is_true
|
23
|
-
assert_equal true, [false].map_detect { |value| true }
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_map_detect_returns_early_when_block_is_true
|
27
|
-
val1 = lambda { :something }
|
28
|
-
val2 = lambda { raise "This shouldn't be called" }
|
29
|
-
|
30
|
-
assert_equal :something, [val1, val2].map_detect { |obj| obj.call }
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_map_detect_returns_nil_when_block_returns_false_for_all_elements
|
34
|
-
assert_equal nil, [1,2,3,4].map_detect { |value| false }
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_map_detect_returns_nil_when_no_elements_in_collection
|
38
|
-
assert_equal nil, [].map_detect { |v| }
|
39
|
-
end
|
40
|
-
|
41
|
-
def test_map_detect_can_have_return_value_specified_when_block_isnt_true
|
42
|
-
assert_equal :a_value, [1,2,3].map_detect(:a_value) { |value| false }
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_map_detect_documentation_correct
|
46
|
-
obj1, obj2 = Object.new, Object.new
|
47
|
-
|
48
|
-
class << obj1
|
49
|
-
def foo?
|
50
|
-
false
|
51
|
-
end
|
52
|
-
|
53
|
-
def foo
|
54
|
-
raise
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
class << obj2
|
59
|
-
def foo?
|
60
|
-
true
|
61
|
-
end
|
62
|
-
|
63
|
-
def foo
|
64
|
-
"a value"
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
assert_equal false, obj1.foo?
|
69
|
-
assert_equal true, obj2.foo?
|
70
|
-
assert_equal "a value", obj2.foo
|
71
|
-
|
72
|
-
result = [obj1, obj2].map_detect { |obj| obj.foo if obj.foo? }
|
73
|
-
assert_equal result, "a value"
|
74
|
-
end
|
75
|
-
end
|