sane 0.20.3 → 0.21.0
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/VERSION +1 -1
- data/lib/sane/map_by.rb +77 -0
- data/lib/sane/pp.rb +2 -3
- data/spec/files/pp_after.rb +4 -0
- data/spec/files/pp_before_hand.rb +4 -0
- data/spec/files/test.rb +9 -0
- data/spec/spec.sane.rb +28 -5
- metadata +7 -2
- data/lib/sane/enumerable-extra.rb +0 -163
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.21.0
|
data/lib/sane/map_by.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# stolen originally from enumerable-extra
|
2
|
+
module Enumerable
|
3
|
+
# Returns a new array containing the results of running +method+ once for
|
4
|
+
# every element in the enumerable object. If both arguments and a block
|
5
|
+
# are provided the arguments are processed first, then passed to
|
6
|
+
# the block.
|
7
|
+
#
|
8
|
+
# If no method is provided, then it behaves as the standard MRI method.
|
9
|
+
#
|
10
|
+
# Examples:
|
11
|
+
#
|
12
|
+
# array = ['foo', 'bar']
|
13
|
+
#
|
14
|
+
# # No arguments
|
15
|
+
# array.map_by(:capitalize) => ['Foo', 'Bar']
|
16
|
+
#
|
17
|
+
def map_by(method)
|
18
|
+
array = []
|
19
|
+
method = method.to_sym unless method.is_a?(Symbol)
|
20
|
+
|
21
|
+
each{ |obj|
|
22
|
+
temp = obj.send(method)
|
23
|
+
array << temp
|
24
|
+
}
|
25
|
+
array
|
26
|
+
end
|
27
|
+
|
28
|
+
# Reset the aliases
|
29
|
+
alias collect_by map_by
|
30
|
+
end
|
31
|
+
|
32
|
+
class Array
|
33
|
+
# Returns a new array containing the results of running +block+ once for
|
34
|
+
# every element in the +array+.
|
35
|
+
#
|
36
|
+
# Examples:
|
37
|
+
#
|
38
|
+
# array = ['foo', 'bar']
|
39
|
+
#
|
40
|
+
# # No arguments
|
41
|
+
# array.map_by(:capitalize) => ['Foo', 'Bar']
|
42
|
+
#
|
43
|
+
# # With arguments and a block
|
44
|
+
# array.map_by(:capitalize)
|
45
|
+
#--
|
46
|
+
# The Array class actually has its own implementation of the +map+ method,
|
47
|
+
# hence the duplication.
|
48
|
+
#
|
49
|
+
def map_by(method)
|
50
|
+
array = []
|
51
|
+
method = method.to_sym unless method.is_a?(Symbol)
|
52
|
+
each{ |obj|
|
53
|
+
temp = obj.send(method)
|
54
|
+
array << temp
|
55
|
+
}
|
56
|
+
array
|
57
|
+
end
|
58
|
+
|
59
|
+
# Same as Array#map, but modifies the receiver in place. Also note that
|
60
|
+
# a block is _not_ required. If no block is given, an array of values
|
61
|
+
# is returned instead
|
62
|
+
#
|
63
|
+
def map_by!(method)
|
64
|
+
method = method.to_sym unless method.is_a?(Symbol)
|
65
|
+
i = 0
|
66
|
+
length = self.size
|
67
|
+
while(i < length)
|
68
|
+
self[i] = self[i].send(method)
|
69
|
+
i+=1
|
70
|
+
end
|
71
|
+
self
|
72
|
+
end
|
73
|
+
|
74
|
+
# Set the aliases
|
75
|
+
alias collect_by map_by
|
76
|
+
alias collect_by! map_by!
|
77
|
+
end
|
data/lib/sane/pp.rb
CHANGED
data/spec/files/test.rb
ADDED
data/spec/spec.sane.rb
CHANGED
@@ -1,8 +1,22 @@
|
|
1
|
-
|
2
|
-
require
|
3
|
-
|
1
|
+
$:.unshift File.expand_path('../lib')
|
2
|
+
require 'rubygems'
|
3
|
+
require 'fast_require' if RUBY_VERSION >= '1.9'
|
4
|
+
require File.dirname(__FILE__) + '/../lib/sane'
|
4
5
|
require 'spec/autorun'
|
5
6
|
|
7
|
+
class Object
|
8
|
+
alias :yes :should # a.yes == [3]
|
9
|
+
def yes!
|
10
|
+
self
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class FalseClass
|
15
|
+
def yes!
|
16
|
+
raise 'failed' # a.true!
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
6
20
|
describe Sane do
|
7
21
|
|
8
22
|
before do
|
@@ -86,6 +100,10 @@ describe Sane do
|
|
86
100
|
it "should allow for map_by" do
|
87
101
|
["1"].map_by(:to_i).should == [1]
|
88
102
|
["1"].collect_by(:to_i).should == [1]
|
103
|
+
a = ["1"]
|
104
|
+
a.map_by!(:to_i)
|
105
|
+
a.yes == [1]
|
106
|
+
a == [1]
|
89
107
|
end
|
90
108
|
|
91
109
|
it "should allow for contain? and include?" do
|
@@ -95,8 +113,8 @@ describe Sane do
|
|
95
113
|
end
|
96
114
|
|
97
115
|
it "should have include and contain for arrays" do
|
98
|
-
assert ['a'].include?( "a"
|
99
|
-
assert ['a'].contain?( "a"
|
116
|
+
assert ['a'].include?( "a")
|
117
|
+
assert ['a'].contain?( "a")
|
100
118
|
end
|
101
119
|
|
102
120
|
it "should have blank? and empty? for arrays and strings" do
|
@@ -120,6 +138,11 @@ describe Sane do
|
|
120
138
|
|
121
139
|
it "should have an auto-loading pp method" do
|
122
140
|
pp 1,2,3
|
141
|
+
pp 1,2,3
|
142
|
+
end
|
143
|
+
|
144
|
+
it "should work if you require 'pp' before hand" do
|
145
|
+
system("ruby -v -I../lib files/pp_before_hand.rb").should be_true
|
123
146
|
end
|
124
147
|
|
125
148
|
it "should have require_relative" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sane
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.21.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Roger Pack
|
@@ -56,13 +56,13 @@ files:
|
|
56
56
|
- lib/sane/array_select_bang.rb
|
57
57
|
- lib/sane/bugs.rb
|
58
58
|
- lib/sane/contain.rb
|
59
|
-
- lib/sane/enumerable-extra.rb
|
60
59
|
- lib/sane/file.rb
|
61
60
|
- lib/sane/file_filename.rb
|
62
61
|
- lib/sane/float.rb
|
63
62
|
- lib/sane/hash_hashes.rb
|
64
63
|
- lib/sane/hash_minus_hash.rb
|
65
64
|
- lib/sane/irb_startup_options.rb
|
65
|
+
- lib/sane/map_by.rb
|
66
66
|
- lib/sane/pp.rb
|
67
67
|
- lib/sane/pps.rb
|
68
68
|
- lib/sane/require_relative.rb
|
@@ -70,6 +70,8 @@ files:
|
|
70
70
|
- lib/sane/string_blank.rb
|
71
71
|
- lib/sane/test.rb
|
72
72
|
- lib/sane/test/assertions.rb
|
73
|
+
- spec/files/pp_after.rb
|
74
|
+
- spec/files/pp_before_hand.rb
|
73
75
|
- spec/go2.rb
|
74
76
|
- spec/spec.sane.rb
|
75
77
|
- spec/subdir/go.rb
|
@@ -104,6 +106,9 @@ signing_key:
|
|
104
106
|
specification_version: 3
|
105
107
|
summary: Helpers for ruby core to make it easier to work with--things that are missing from core but should arguably be there
|
106
108
|
test_files:
|
109
|
+
- spec/files/pp_after.rb
|
110
|
+
- spec/files/pp_before_hand.rb
|
111
|
+
- spec/files/test.rb
|
107
112
|
- spec/go2.rb
|
108
113
|
- spec/spec.sane.rb
|
109
114
|
- spec/subdir/go.rb
|
@@ -1,163 +0,0 @@
|
|
1
|
-
module Enumerable
|
2
|
-
EXTRA_VERSION = '0.1.0'
|
3
|
-
|
4
|
-
alias old_map map
|
5
|
-
alias old_collect collect
|
6
|
-
=begin unused as it clashes with rails' own #sum
|
7
|
-
apparently
|
8
|
-
C:\dev\ruby\bridal>ruby test\functional\store_controller_test.rb
|
9
|
-
test/functional/../test_helper
|
10
|
-
C:/dev/ruby/bridal/config/../lib/sane-0.1.4/lib/enumerable-extra.rb:12:in `+': can't convert String into Array (TypeError)
|
11
|
-
from C:/dev/ruby/bridal/config/../lib/sane-0.1.4/lib/enumerable-extra.rb:12:in `sum'
|
12
|
-
from C:/dev/ruby/bridal/config/../lib/sane-0.1.4/lib/enumerable-extra.rb:12:in `each'
|
13
|
-
from C:/dev/ruby/bridal/config/../lib/sane-0.1.4/lib/enumerable-extra.rb:12:in `sum'
|
14
|
-
from C:/dev/ruby/bridal/vendor/rails/actionpack/lib/action_controller/layout.rb:189:in `layout_list'
|
15
|
-
|
16
|
-
2.1
|
17
|
-
|
18
|
-
# Returns the numeric total of the elements of +enum+, using +total+ as
|
19
|
-
# an accumulator (0 by default). Raises an error if any of the elements
|
20
|
-
# are non-numeric.
|
21
|
-
#
|
22
|
-
def sum(total = 0)
|
23
|
-
each{ |val| total += val }
|
24
|
-
total
|
25
|
-
end
|
26
|
-
=end
|
27
|
-
|
28
|
-
# Returns a new array containing the results of running +method+ once for
|
29
|
-
# every element in the enumerable object. If both arguments and a block
|
30
|
-
# are provided the arguments are processed first, then passed to
|
31
|
-
# the block.
|
32
|
-
#
|
33
|
-
# If no method is provided, then it behaves as the standard MRI method.
|
34
|
-
#
|
35
|
-
# Examples:
|
36
|
-
#
|
37
|
-
# array = ['foo', 'bar']
|
38
|
-
#
|
39
|
-
# # No arguments
|
40
|
-
# array.map(:capitalize) => ['Foo', 'Bar']
|
41
|
-
#
|
42
|
-
# # With arguments
|
43
|
-
# array.map(:+, 'x') => ['foox', 'barx']
|
44
|
-
#
|
45
|
-
# # With arguments and a block
|
46
|
-
# array.map(:capitalize){ |e| e + 'x' } => ['Foox', 'Barx']
|
47
|
-
#
|
48
|
-
def map(method=nil, *args, &block)
|
49
|
-
if method
|
50
|
-
array = []
|
51
|
-
method = method.to_sym unless method.is_a?(Symbol)
|
52
|
-
|
53
|
-
each{ |obj|
|
54
|
-
temp = obj.send(method, *args)
|
55
|
-
if block
|
56
|
-
array << block.call(temp)
|
57
|
-
else
|
58
|
-
array << temp
|
59
|
-
end
|
60
|
-
}
|
61
|
-
|
62
|
-
array
|
63
|
-
else
|
64
|
-
old_map(&block)
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
# Reset the aliases
|
69
|
-
alias collect map
|
70
|
-
end
|
71
|
-
|
72
|
-
class Array
|
73
|
-
alias old_map map
|
74
|
-
alias old_map! map!
|
75
|
-
alias old_collect collect
|
76
|
-
alias old_collect! collect!
|
77
|
-
#alias old_select select
|
78
|
-
|
79
|
-
# Returns a new array containing the results of running +block+ once for
|
80
|
-
# every element in the +array+.
|
81
|
-
#
|
82
|
-
# Examples:
|
83
|
-
#
|
84
|
-
# array = ['foo', 'bar']
|
85
|
-
#
|
86
|
-
# # No arguments
|
87
|
-
# array.map(:capitalize) => ['Foo', 'Bar']
|
88
|
-
#
|
89
|
-
# # With arguments
|
90
|
-
# array.map(:+, 'x') => ['foox', 'barx']
|
91
|
-
#
|
92
|
-
# # With arguments and a block
|
93
|
-
# array.map(:capitalize){ |e| e + 'x' } => ['Foox', 'Barx']
|
94
|
-
#--
|
95
|
-
# The Array class actually has its own implementation of the +map+ method,
|
96
|
-
# hence the duplication.
|
97
|
-
#
|
98
|
-
def map(method=nil, *args, &block)
|
99
|
-
if method
|
100
|
-
array = []
|
101
|
-
method = method.to_sym unless method.is_a?(Symbol)
|
102
|
-
|
103
|
-
each{ |obj|
|
104
|
-
temp = obj.send(method, *args)
|
105
|
-
if block
|
106
|
-
array << block.call(temp)
|
107
|
-
else
|
108
|
-
array << temp
|
109
|
-
end
|
110
|
-
}
|
111
|
-
array
|
112
|
-
else
|
113
|
-
old_map(&block)
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
# Same as Array#map, but modifies the receiver in place. Also note that
|
118
|
-
# a block is _not_ required. If no block is given, an array of values
|
119
|
-
# is returned instead
|
120
|
-
#
|
121
|
-
def map!(method=nil, *args, &block)
|
122
|
-
self.replace(map(method, *args, &block))
|
123
|
-
end
|
124
|
-
|
125
|
-
=begin
|
126
|
-
def select(method=nil, condition = nil, *args, &block)
|
127
|
-
array = [] unless block
|
128
|
-
if method
|
129
|
-
if block
|
130
|
-
warn 'block ignored when arguments provided'
|
131
|
-
end
|
132
|
-
|
133
|
-
if condition.nil?
|
134
|
-
raise 'condition must be provided if method is provided'
|
135
|
-
end
|
136
|
-
|
137
|
-
method = method.to_sym unless method.is_a?(Symbol)
|
138
|
-
|
139
|
-
each{ |obj|
|
140
|
-
if args.length > 0
|
141
|
-
if obj.send(method, condition, *args)
|
142
|
-
array << obj
|
143
|
-
end
|
144
|
-
else
|
145
|
-
if obj.send(method, condition)
|
146
|
-
array << obj
|
147
|
-
end
|
148
|
-
end
|
149
|
-
}
|
150
|
-
|
151
|
-
return array
|
152
|
-
else
|
153
|
-
old_select(&block)
|
154
|
-
end
|
155
|
-
end
|
156
|
-
=end
|
157
|
-
|
158
|
-
# Reset the aliases
|
159
|
-
alias collect map
|
160
|
-
alias collect! map!
|
161
|
-
alias collect_by collect
|
162
|
-
alias map_by map
|
163
|
-
end
|