arraysugar 0.1.0 → 0.2.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/lib/{array_sugar.rb → arraysugar.rb} +21 -16
- metadata +3 -3
@@ -26,7 +26,6 @@
|
|
26
26
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
|
27
27
|
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
28
28
|
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
29
|
-
|
30
29
|
module ArraySugar
|
31
30
|
class IterationProxy
|
32
31
|
instance_methods.each do |m|
|
@@ -36,12 +35,12 @@ module ArraySugar
|
|
36
35
|
def initialize(array, mode)
|
37
36
|
@array = array
|
38
37
|
@mode = mode
|
39
|
-
@
|
38
|
+
@leafs = false
|
40
39
|
end
|
41
40
|
|
42
|
-
def __do_call(e
|
43
|
-
if
|
44
|
-
e.send(@mode) {|e| __do_call(e
|
41
|
+
def __do_call(e)
|
42
|
+
if @leafs && (e.is_a?(Array) || e.is_a?(Range))
|
43
|
+
e.send(@mode) {|e| __do_call(e) }
|
45
44
|
else
|
46
45
|
if @nils_okay && e.nil?
|
47
46
|
nil
|
@@ -57,20 +56,11 @@ module ArraySugar
|
|
57
56
|
self
|
58
57
|
end
|
59
58
|
|
60
|
-
def to_a
|
61
|
-
@array
|
62
|
-
end
|
63
|
-
|
64
59
|
def leafs
|
65
|
-
@
|
60
|
+
throw 'sorry, select does not yet work with leafs' if @mode =~ /select/ || @mode =~ /find_all/
|
61
|
+
@leafs = true
|
66
62
|
self
|
67
63
|
end
|
68
|
-
def deep(depth = -1)
|
69
|
-
@depth = -1
|
70
|
-
end
|
71
|
-
def shallow(depth = 0)
|
72
|
-
@depth = depth
|
73
|
-
end
|
74
64
|
|
75
65
|
def nils_okay
|
76
66
|
@nils_okay = true
|
@@ -78,15 +68,30 @@ module ArraySugar
|
|
78
68
|
end
|
79
69
|
alias_method :nils_ok, :nils_okay
|
80
70
|
|
71
|
+
def to_a
|
72
|
+
@array
|
73
|
+
end
|
74
|
+
|
81
75
|
def inspect
|
82
76
|
to_a.inspect
|
83
77
|
end
|
84
78
|
|
79
|
+
def compact
|
80
|
+
@array = @array.compact
|
81
|
+
self
|
82
|
+
end
|
83
|
+
|
84
|
+
def uniq
|
85
|
+
@array = @array.uniq
|
86
|
+
self
|
87
|
+
end
|
88
|
+
|
85
89
|
[:all?, :any?, :map, :map!, :collect, :collect!, :detect, :find, :find_all,
|
86
90
|
:reject, :reject!, :reverse_each, :select, :sort_by, :each].each do |m|
|
87
91
|
class_eval <<-CODE
|
88
92
|
def #{m}(*args, &b)
|
89
93
|
if args.size == 0 && !block_given?
|
94
|
+
throw 'sorry, select does not yet work with leafs' if @leafs && @mode =~ /select/ || @mode =~ /find_all/
|
90
95
|
@mode = :#{m}
|
91
96
|
self
|
92
97
|
else
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
|
|
3
3
|
specification_version: 1
|
4
4
|
name: arraysugar
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
7
|
-
date: 2007-10-
|
6
|
+
version: 0.2.0
|
7
|
+
date: 2007-10-07 00:00:00 +00:00
|
8
8
|
summary: Nice Ruby Array manipulation syntax
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -29,7 +29,7 @@ post_install_message:
|
|
29
29
|
authors: []
|
30
30
|
|
31
31
|
files:
|
32
|
-
- lib/
|
32
|
+
- lib/arraysugar.rb
|
33
33
|
test_files: []
|
34
34
|
|
35
35
|
rdoc_options: []
|