arraysugar 0.1.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 +124 -0
- metadata +46 -0
data/lib/array_sugar.rb
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
## array_sugar.rb -- sweet, sweet, syntax for ruby arrays
|
2
|
+
## Copyright (c) David Salamon, 2007, all rights reserved
|
3
|
+
#
|
4
|
+
# Redistribution and use of this software in source and binary forms, with or without modification, are
|
5
|
+
# permitted provided that the following conditions are met:
|
6
|
+
#
|
7
|
+
# * Redistributions of source code must retain the above
|
8
|
+
# copyright notice, this list of conditions and the
|
9
|
+
# following disclaimer.
|
10
|
+
#
|
11
|
+
# * Redistributions in binary form must reproduce the above
|
12
|
+
# copyright notice, this list of conditions and the
|
13
|
+
# following disclaimer in the documentation and/or other
|
14
|
+
# materials provided with the distribution.
|
15
|
+
#
|
16
|
+
# * Neither the name of David Salamon nor the names of his
|
17
|
+
# contributors may be used to endorse or promote products
|
18
|
+
# derived from this software without specific prior
|
19
|
+
# written permission from David Salamon.
|
20
|
+
#
|
21
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
|
22
|
+
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
23
|
+
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
24
|
+
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
25
|
+
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
26
|
+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
|
27
|
+
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
28
|
+
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
29
|
+
|
30
|
+
module ArraySugar
|
31
|
+
class IterationProxy
|
32
|
+
instance_methods.each do |m|
|
33
|
+
undef_method(m) unless m =~ /(^__|^nil\\?|^send)/
|
34
|
+
end
|
35
|
+
|
36
|
+
def initialize(array, mode)
|
37
|
+
@array = array
|
38
|
+
@mode = mode
|
39
|
+
@depth = 0
|
40
|
+
end
|
41
|
+
|
42
|
+
def __do_call(e, depth = @depth)
|
43
|
+
if (depth != 0) && (e.is_a?(Array) || e.is_a?(Range))
|
44
|
+
e.send(@mode) {|e| __do_call(e, depth - 1) }
|
45
|
+
else
|
46
|
+
if @nils_okay && e.nil?
|
47
|
+
nil
|
48
|
+
else
|
49
|
+
e.send(@s, *@a, &@b)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def method_missing(s, *a, &b)
|
55
|
+
@s = s; @a = a; @b = b
|
56
|
+
@array = @array.send(@mode) {|e| __do_call(e) }
|
57
|
+
self
|
58
|
+
end
|
59
|
+
|
60
|
+
def to_a
|
61
|
+
@array
|
62
|
+
end
|
63
|
+
|
64
|
+
def leafs
|
65
|
+
@depth = -1
|
66
|
+
self
|
67
|
+
end
|
68
|
+
def deep(depth = -1)
|
69
|
+
@depth = -1
|
70
|
+
end
|
71
|
+
def shallow(depth = 0)
|
72
|
+
@depth = depth
|
73
|
+
end
|
74
|
+
|
75
|
+
def nils_okay
|
76
|
+
@nils_okay = true
|
77
|
+
self
|
78
|
+
end
|
79
|
+
alias_method :nils_ok, :nils_okay
|
80
|
+
|
81
|
+
def inspect
|
82
|
+
to_a.inspect
|
83
|
+
end
|
84
|
+
|
85
|
+
[:all?, :any?, :map, :map!, :collect, :collect!, :detect, :find, :find_all,
|
86
|
+
:reject, :reject!, :reverse_each, :select, :sort_by, :each].each do |m|
|
87
|
+
class_eval <<-CODE
|
88
|
+
def #{m}(*args, &b)
|
89
|
+
if args.size == 0 && !block_given?
|
90
|
+
@mode = :#{m}
|
91
|
+
self
|
92
|
+
else
|
93
|
+
@array.orig_#{m}(*args, &b)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
CODE
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
class Array
|
102
|
+
[:all?, :any?, :map, :map!, :collect, :collect!, :detect, :find, :find_all,
|
103
|
+
:reject, :reject!, :reverse_each, :select, :sort_by, :each].each do |m|
|
104
|
+
class_eval <<-CODE
|
105
|
+
alias_method :orig_#{m}, :#{m}
|
106
|
+
def #{m}(*args, &b)
|
107
|
+
return ArraySugar::IterationProxy.new(self, :#{m}) if args.size == 0 && !block_given?
|
108
|
+
orig_#{m}(*args, &b)
|
109
|
+
end
|
110
|
+
CODE
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
class Range
|
115
|
+
[:all?, :any?, :map, :collect, :detect, :find, :find_all, :reject, :select, :sort_by, :each].each do |m|
|
116
|
+
class_eval <<-CODE
|
117
|
+
alias_method :orig_#{m}, :#{m}
|
118
|
+
def #{m}(*args, &b)
|
119
|
+
return ArraySugar::IterationProxy.new(self, :#{m}) if args.size == 0 && !block_given?
|
120
|
+
orig_#{m}(*args, &b)
|
121
|
+
end
|
122
|
+
CODE
|
123
|
+
end
|
124
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.9.0
|
3
|
+
specification_version: 1
|
4
|
+
name: arraysugar
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.1.0
|
7
|
+
date: 2007-10-06 00:00:00 +00:00
|
8
|
+
summary: Nice Ruby Array manipulation syntax
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email:
|
12
|
+
homepage:
|
13
|
+
rubyforge_project:
|
14
|
+
description:
|
15
|
+
autorequire:
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: false
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.8.1
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
post_install_message:
|
29
|
+
authors: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- lib/array_sugar.rb
|
33
|
+
test_files: []
|
34
|
+
|
35
|
+
rdoc_options: []
|
36
|
+
|
37
|
+
extra_rdoc_files: []
|
38
|
+
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
requirements: []
|
44
|
+
|
45
|
+
dependencies: []
|
46
|
+
|