as-extensions 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/LICENSE +189 -0
- data/README.md +25 -0
- data/ext/array.rb +113 -0
- data/ext/boolean.rb +36 -0
- data/ext/datetime.rb +39 -0
- data/ext/dir.rb +29 -0
- data/ext/enumerable.rb +28 -0
- data/ext/file.rb +11 -0
- data/ext/hash.rb +167 -0
- data/ext/io.rb +27 -0
- data/ext/object.rb +38 -0
- data/ext/set.rb +27 -0
- data/ext/socket.rb +40 -0
- data/ext/string.rb +82 -0
- data/ext/symbol.rb +28 -0
- data/ext/time.rb +34 -0
- data/ext/uri.rb +61 -0
- data/lib/as-extensions/deep.rb +38 -0
- data/lib/as-extensions/enum.rb +64 -0
- data/lib/as-extensions/ext-logic/io.rb +34 -0
- data/lib/as-extensions/ext-logic/string.rb +42 -0
- data/lib/as-extensions/ext-logic.rb +20 -0
- data/lib/as-extensions/extra-ext/libxml.rb +60 -0
- data/lib/as-extensions/extra-ext.rb +32 -0
- data/lib/as-extensions/fs.rb +42 -0
- data/lib/as-extensions/log.rb +52 -0
- data/lib/as-extensions/need.rb +47 -0
- data/lib/as-extensions/net.rb +38 -0
- data/lib/as-extensions/test.rb +159 -0
- data/lib/as-extensions/time.rb +67 -0
- data/lib/as-extensions.rb +64 -0
- metadata +146 -0
data/LICENSE
ADDED
@@ -0,0 +1,189 @@
|
|
1
|
+
Copyright (c) 2010-2011 Moodstocks SAS
|
2
|
+
|
3
|
+
Apache License Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction, and
|
11
|
+
distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by the
|
14
|
+
copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all other
|
17
|
+
entities that control, are controlled by, or are under common control with
|
18
|
+
that entity. For the purposes of this definition, "control" means (i) the
|
19
|
+
power, direct or indirect, to cause the direction or management of such
|
20
|
+
entity, whether by contract or otherwise, or (ii) ownership of fifty percent
|
21
|
+
(50%) or more of the outstanding shares, or (iii) beneficial ownership of such
|
22
|
+
entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
25
|
+
permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation source, and
|
29
|
+
configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical transformation or
|
32
|
+
translation of a Source form, including but not limited to compiled object
|
33
|
+
code, generated documentation, and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or Object form,
|
36
|
+
made available under the License, as indicated by a copyright notice that is
|
37
|
+
included in or attached to the work (an example is provided in the Appendix
|
38
|
+
below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that
|
41
|
+
is based on (or derived from) the Work and for which the editorial revisions,
|
42
|
+
annotations, elaborations, or other modifications represent, as a whole, an
|
43
|
+
original work of authorship. For the purposes of this License, Derivative
|
44
|
+
Works shall not include works that remain separable from, or merely link (or
|
45
|
+
bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
46
|
+
|
47
|
+
"Contribution" shall mean any work of authorship, including the original
|
48
|
+
version of the Work and any modifications or additions to that Work or
|
49
|
+
Derivative Works thereof, that is intentionally submitted to Licensor for
|
50
|
+
inclusion in the Work by the copyright owner or by an individual or Legal
|
51
|
+
Entity authorized to submit on behalf of the copyright owner. For the purposes
|
52
|
+
of this definition, "submitted" means any form of electronic, verbal, or
|
53
|
+
written communication sent to the Licensor or its representatives, including
|
54
|
+
but not limited to communication on electronic mailing lists, source code
|
55
|
+
control systems, and issue tracking systems that are managed by, or on behalf
|
56
|
+
of, the Licensor for the purpose of discussing and improving the Work, but
|
57
|
+
excluding communication that is conspicuously marked or otherwise designated
|
58
|
+
in writing by the copyright owner as "Not a Contribution."
|
59
|
+
|
60
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
|
61
|
+
of whom a Contribution has been received by Licensor and subsequently
|
62
|
+
incorporated within the Work.
|
63
|
+
|
64
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this
|
65
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
66
|
+
non-exclusive, no-charge, royalty-free, irrevocable copyright license to
|
67
|
+
reproduce, prepare Derivative Works of, publicly display, publicly perform,
|
68
|
+
sublicense, and distribute the Work and such Derivative Works in Source or
|
69
|
+
Object form.
|
70
|
+
|
71
|
+
3. Grant of Patent License. Subject to the terms and conditions of this
|
72
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
73
|
+
non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this
|
74
|
+
section) patent license to make, have made, use, offer to sell, sell, import,
|
75
|
+
and otherwise transfer the Work, where such license applies only to those
|
76
|
+
patent claims licensable by such Contributor that are necessarily infringed by
|
77
|
+
their Contribution(s) alone or by combination of their Contribution(s) with
|
78
|
+
the Work to which such Contribution(s) was submitted. If You institute patent
|
79
|
+
litigation against any entity (including a cross-claim or counterclaim in a
|
80
|
+
lawsuit) alleging that the Work or a Contribution incorporated within the Work
|
81
|
+
constitutes direct or contributory patent infringement, then any patent
|
82
|
+
licenses granted to You under this License for that Work shall terminate as of
|
83
|
+
the date such litigation is filed.
|
84
|
+
|
85
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or
|
86
|
+
Derivative Works thereof in any medium, with or without modifications, and in
|
87
|
+
Source or Object form, provided that You meet the following conditions:
|
88
|
+
|
89
|
+
1. You must give any other recipients of the Work or Derivative Works a copy
|
90
|
+
of this License; and
|
91
|
+
|
92
|
+
2. You must cause any modified files to carry prominent notices stating that
|
93
|
+
You changed the files; and
|
94
|
+
|
95
|
+
3. You must retain, in the Source form of any Derivative Works that You
|
96
|
+
distribute, all copyright, patent, trademark, and attribution notices from the
|
97
|
+
Source form of the Work, excluding those notices that do not pertain to any
|
98
|
+
part of the Derivative Works; and
|
99
|
+
|
100
|
+
4. If the Work includes a "NOTICE" text file as part of its distribution,
|
101
|
+
then any Derivative Works that You distribute must include a readable copy of
|
102
|
+
the attribution notices contained within such NOTICE file, excluding those
|
103
|
+
notices that do not pertain to any part of the Derivative Works, in at least
|
104
|
+
one of the following places: within a NOTICE text file distributed as part of
|
105
|
+
the Derivative Works; within the Source form or documentation, if provided
|
106
|
+
along with the Derivative Works; or, within a display generated by the
|
107
|
+
Derivative Works, if and wherever such third-party notices normally appear.
|
108
|
+
The contents of the NOTICE file are for informational purposes only and do not
|
109
|
+
modify the License. You may add Your own attribution notices within Derivative
|
110
|
+
Works that You distribute, alongside or as an addendum to the NOTICE text from
|
111
|
+
the Work, provided that such additional attribution notices cannot be
|
112
|
+
construed as modifying the License.
|
113
|
+
|
114
|
+
You may add Your own copyright statement to Your modifications and may provide
|
115
|
+
additional or different license terms and conditions for use, reproduction, or
|
116
|
+
distribution of Your modifications, or for any such Derivative Works as a
|
117
|
+
whole, provided Your use, reproduction, and distribution of the Work otherwise
|
118
|
+
complies with the conditions stated in this License.
|
119
|
+
|
120
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any
|
121
|
+
Contribution intentionally submitted for inclusion in the Work by You to the
|
122
|
+
Licensor shall be under the terms and conditions of this License, without any
|
123
|
+
additional terms or conditions. Notwithstanding the above, nothing herein
|
124
|
+
shall supersede or modify the terms of any separate license agreement you may
|
125
|
+
have executed with Licensor regarding such Contributions.
|
126
|
+
|
127
|
+
6. Trademarks. This License does not grant permission to use the trade names,
|
128
|
+
trademarks, service marks, or product names of the Licensor, except as
|
129
|
+
required for reasonable and customary use in describing the origin of the Work
|
130
|
+
and reproducing the content of the NOTICE file.
|
131
|
+
|
132
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in
|
133
|
+
writing, Licensor provides the Work (and each Contributor provides its
|
134
|
+
Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
135
|
+
KIND, either express or implied, including, without limitation, any warranties
|
136
|
+
or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
137
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
138
|
+
appropriateness of using or redistributing the Work and assume any risks
|
139
|
+
associated with Your exercise of permissions under this License.
|
140
|
+
|
141
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in
|
142
|
+
tort (including negligence), contract, or otherwise, unless required by
|
143
|
+
applicable law (such as deliberate and grossly negligent acts) or agreed to in
|
144
|
+
writing, shall any Contributor be liable to You for damages, including any
|
145
|
+
direct, indirect, special, incidental, or consequential damages of any
|
146
|
+
character arising as a result of this License or out of the use or inability
|
147
|
+
to use the Work (including but not limited to damages for loss of goodwill,
|
148
|
+
work stoppage, computer failure or malfunction, or any and all other
|
149
|
+
commercial damages or losses), even if such Contributor has been advised of
|
150
|
+
the possibility of such damages.
|
151
|
+
|
152
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work
|
153
|
+
or Derivative Works thereof, You may choose to offer, and charge a fee for,
|
154
|
+
acceptance of support, warranty, indemnity, or other liability obligations
|
155
|
+
and/or rights consistent with this License. However, in accepting such
|
156
|
+
obligations, You may act only on Your own behalf and on Your sole
|
157
|
+
responsibility, not on behalf of any other Contributor, and only if You agree
|
158
|
+
to indemnify, defend, and hold each Contributor harmless for any liability
|
159
|
+
incurred by, or claims asserted against, such Contributor by reason of your
|
160
|
+
accepting any such warranty or additional liability.
|
161
|
+
|
162
|
+
END OF TERMS AND CONDITIONS
|
163
|
+
|
164
|
+
This software includes subcomponents with separate copyright notices and
|
165
|
+
license terms.
|
166
|
+
|
167
|
+
slugstring.rb
|
168
|
+
=============
|
169
|
+
|
170
|
+
Copyright (c) 2008-2010 Norman Clarke, Adrian Mugnolo and Emilio Tagua.
|
171
|
+
|
172
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
173
|
+
of this software and associated documentation files (the "Software"), to deal
|
174
|
+
in the Software without restriction, including without limitation the rights
|
175
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
176
|
+
copies of the Software, and to permit persons to whom the Software is
|
177
|
+
furnished to do so, subject to the following conditions:
|
178
|
+
|
179
|
+
The above copyright notice and this permission notice shall be included in all
|
180
|
+
copies or substantial portions of the Software.
|
181
|
+
|
182
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
183
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
184
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
185
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
186
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
187
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
188
|
+
SOFTWARE.
|
189
|
+
|
data/README.md
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# ASE (as-extensions)
|
2
|
+
|
3
|
+
## Purpose
|
4
|
+
|
5
|
+
This gem is built on top of ActiveSupport. It provides useful core
|
6
|
+
extensions, methods and modules.
|
7
|
+
|
8
|
+
## Warning
|
9
|
+
|
10
|
+
Using this gem will make some classes behave differently than what you
|
11
|
+
are used to. The Time and DateTime classes are especially concerned.
|
12
|
+
Read the RDoc if you use them.
|
13
|
+
|
14
|
+
## How To Install
|
15
|
+
|
16
|
+
1. Install Jeweler (http://github.com/technicalpickles/jeweler).
|
17
|
+
2. Clone the Git repository
|
18
|
+
3. Run:
|
19
|
+
rake build
|
20
|
+
gem install pkg/*.gem
|
21
|
+
4. Add "require 'as-extensions'" to your code.
|
22
|
+
|
23
|
+
## Copyright
|
24
|
+
|
25
|
+
Copyright (c) 2010-2011 Moodstocks SAS
|
data/ext/array.rb
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2010-2011 Moodstocks SAS
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# or in the LICENSE file which you should have received as part of
|
11
|
+
# this distribution.
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
18
|
+
#++
|
19
|
+
|
20
|
+
Array.class_eval do
|
21
|
+
|
22
|
+
# Boolean AND of elements
|
23
|
+
def band
|
24
|
+
inject{ |s,x| s && x }
|
25
|
+
end
|
26
|
+
|
27
|
+
# For compatibility purpose.
|
28
|
+
def bmap(&blk)
|
29
|
+
map(&blk).band
|
30
|
+
end
|
31
|
+
|
32
|
+
# Boolean OR of elements
|
33
|
+
def bor
|
34
|
+
inject{ |s,x| s || x }
|
35
|
+
end
|
36
|
+
|
37
|
+
# Call ASE::deepcompact(self)
|
38
|
+
def deepcompact
|
39
|
+
ASE::deepcompact(self)
|
40
|
+
end
|
41
|
+
|
42
|
+
def first=(x)
|
43
|
+
raise IndexError, 'empty array' if empty?
|
44
|
+
self[0] = x
|
45
|
+
end
|
46
|
+
|
47
|
+
def fpop
|
48
|
+
[last,init]
|
49
|
+
end
|
50
|
+
|
51
|
+
def fshift
|
52
|
+
[head,tail]
|
53
|
+
end
|
54
|
+
|
55
|
+
alias :head :first
|
56
|
+
alias :head= :first=
|
57
|
+
|
58
|
+
def init
|
59
|
+
self[0..-2]
|
60
|
+
end
|
61
|
+
|
62
|
+
def last=(x)
|
63
|
+
raise IndexError, 'empty array' if empty?
|
64
|
+
self[-1] = x
|
65
|
+
end
|
66
|
+
|
67
|
+
# Map a function (ie. Method object) to members
|
68
|
+
def map_f(f, *args)
|
69
|
+
self.map{ |x| f.call(self, *args) }
|
70
|
+
end
|
71
|
+
|
72
|
+
# Map a method of members to themselves
|
73
|
+
def map_m(sym, *args)
|
74
|
+
self.map{ |x| x.send(sym.to_sym, *args) }
|
75
|
+
end
|
76
|
+
|
77
|
+
# In-place version of map_m
|
78
|
+
def map_m!(sym, *args)
|
79
|
+
self.map!{ |x| x.send(sym.to_sym, *args) }
|
80
|
+
end
|
81
|
+
|
82
|
+
# Reverse map_m: members are arguments
|
83
|
+
def map_mr(sym, obj, *args)
|
84
|
+
self.map{ |x| obj.send(sym.to_sym, x, *args) }
|
85
|
+
end
|
86
|
+
|
87
|
+
# In-place version of map_mr
|
88
|
+
def map_mr!(sym, obj, *args)
|
89
|
+
self.map!{ |x| obj.send(sym.to_sym, x, *args) }
|
90
|
+
end
|
91
|
+
|
92
|
+
# Return a random element.
|
93
|
+
def pick(secure=false)
|
94
|
+
self[secure ? ActiveSupport::SecureRandom.random_number(size) : Kernel.rand(size)]
|
95
|
+
end
|
96
|
+
|
97
|
+
def tail
|
98
|
+
self[1..-1] || []
|
99
|
+
end
|
100
|
+
|
101
|
+
# See vsum for an example of what this does
|
102
|
+
# Another example: vapply(:to_a) transposes a matrix!
|
103
|
+
def vapply(x)
|
104
|
+
(head||[]).zip(*tail).map_m(x)
|
105
|
+
end
|
106
|
+
|
107
|
+
# Vector sum
|
108
|
+
# For instance: [[1,2],[1,3]].vsum => [2,5]
|
109
|
+
def vsum
|
110
|
+
vapply(:sum)
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
data/ext/boolean.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2010-2011 Moodstocks SAS
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# or in the LICENSE file which you should have received as part of
|
11
|
+
# this distribution.
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
18
|
+
#++
|
19
|
+
|
20
|
+
TrueClass.class_eval do
|
21
|
+
|
22
|
+
# Return 1
|
23
|
+
def to_i
|
24
|
+
1
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
FalseClass.class_eval do
|
30
|
+
|
31
|
+
# Return 0
|
32
|
+
def to_i
|
33
|
+
0
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
data/ext/datetime.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2010-2011 Moodstocks SAS
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# or in the LICENSE file which you should have received as part of
|
11
|
+
# this distribution.
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
18
|
+
#++
|
19
|
+
|
20
|
+
DateTime.class_eval do
|
21
|
+
|
22
|
+
alias :to_s_noase :to_s
|
23
|
+
# Convert to the standard ASE time string format
|
24
|
+
def to_s(format=nil)
|
25
|
+
if format.nil? then ASE::Time::to_string(self)
|
26
|
+
else to_s_noase(format) end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Get the number of seconds since Epoch
|
30
|
+
def to_epoch
|
31
|
+
ASE::Time::epoch(self)
|
32
|
+
end
|
33
|
+
|
34
|
+
# Convert to a Time object
|
35
|
+
def to_time
|
36
|
+
ASE::Time::to_time(self)
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
data/ext/dir.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2010-2011 Moodstocks SAS
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# or in the LICENSE file which you should have received as part of
|
11
|
+
# this distribution.
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
18
|
+
#++
|
19
|
+
|
20
|
+
Dir.class_eval do
|
21
|
+
|
22
|
+
# Iterate over files in directory with a specific extension.
|
23
|
+
def each_file(ext, &blk)
|
24
|
+
grep(/^[^.].*\.(#{ext})$/).each do |f|
|
25
|
+
yield "#{path}/#{f}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
data/ext/enumerable.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2010-2011 Moodstocks SAS
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# or in the LICENSE file which you should have received as part of
|
11
|
+
# this distribution.
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
18
|
+
#++
|
19
|
+
|
20
|
+
Enumerable.class_eval do
|
21
|
+
|
22
|
+
# When a method doesn't exist, convert to an Array
|
23
|
+
def method_missing(method_sym, *arguments, &block)
|
24
|
+
if !is_a?(Array) && respond_to?(:to_a) then to_a.send(method_sym, *arguments, &block)
|
25
|
+
else super end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
data/ext/file.rb
ADDED
data/ext/hash.rb
ADDED
@@ -0,0 +1,167 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2010-2011 Moodstocks SAS
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# or in the LICENSE file which you should have received as part of
|
11
|
+
# this distribution.
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
18
|
+
#++
|
19
|
+
|
20
|
+
Hash.class_eval do
|
21
|
+
|
22
|
+
class << self
|
23
|
+
|
24
|
+
# See Hash#normalize. This does the opposite.
|
25
|
+
def renormalize(array)
|
26
|
+
r = {}
|
27
|
+
array.each do |l|
|
28
|
+
s = r
|
29
|
+
l[0..-3].each do |x|
|
30
|
+
s = (s[x] ||= {})
|
31
|
+
end
|
32
|
+
if s[l[-2]].nil?
|
33
|
+
s[l[-2]] = l.last
|
34
|
+
elsif s[l[-2]].nil?.is_a?(Array)
|
35
|
+
s[l[-2]].push(l.last)
|
36
|
+
else
|
37
|
+
s[l[-2]] = [s[l[-2]], l.last]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
r
|
41
|
+
end
|
42
|
+
|
43
|
+
end # class << self
|
44
|
+
|
45
|
+
# Call ASE::deepcompact(self)
|
46
|
+
def deepcompact
|
47
|
+
ASE::deepcompact(self)
|
48
|
+
end
|
49
|
+
|
50
|
+
# Same logic as get! but for deleting values.
|
51
|
+
# Examples with h = {:a=>{:b=>{:c=>:d}}}:
|
52
|
+
# h.del!(:a, :b, :c) # => {:a=>{:b=>{}}}
|
53
|
+
# h.del!(:a, :b){|x| x[:c] == :z} # => {:a=>{:b=>{:c=>:d}}}
|
54
|
+
# h.del!(:a, :b){|x| x[:c] == :d} # => {:a=>{}}
|
55
|
+
def del!(*nodes, &blk)
|
56
|
+
if nodes.length == 1
|
57
|
+
self.delete(nodes.head) if ( (!block_given?) || blk.call(self[nodes.head]) )
|
58
|
+
else
|
59
|
+
( self.get!(*nodes.init) {{}} ).del!(nodes.last, &blk)
|
60
|
+
end
|
61
|
+
self
|
62
|
+
end
|
63
|
+
|
64
|
+
# Transforms a hash-based tree into an array of arrays
|
65
|
+
# Example input: {'a'=>'aa', 'b' => {'ba' => 'baa', 'bb' => 'bba'}}
|
66
|
+
# Example output: [["a", "aa"], ["b", "bb", "bba"], ["b", "ba", "baa"]]
|
67
|
+
def denormalize(trace=[])
|
68
|
+
r = Set.new
|
69
|
+
self.each_pair do |k,v|
|
70
|
+
if v.is_a?(Hash)
|
71
|
+
l = v.denormalize(trace + [k])
|
72
|
+
l.each do |x|
|
73
|
+
r.merge!(l) unless (l.respond_to?(:empty?) && l.empty?)
|
74
|
+
end
|
75
|
+
elsif v.is_a?(Array) || v.is_a?(::Set)
|
76
|
+
v.each do |x|
|
77
|
+
r.add(trace + [k, x])
|
78
|
+
end
|
79
|
+
else
|
80
|
+
r.add(trace + [k, v]) unless (v.respond_to?(:empty?) && v.empty?)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
r.to_a
|
84
|
+
end
|
85
|
+
|
86
|
+
def equiv?(x)
|
87
|
+
eq_canonize(self).inspect == eq_canonize(x).inspect
|
88
|
+
end
|
89
|
+
|
90
|
+
# Get a node in a recursive hash structure and create intermediate hashes if needed.
|
91
|
+
# Example:
|
92
|
+
# a = {}
|
93
|
+
# a.get!(:a, :b, :c) # => nil
|
94
|
+
# a # => {:a=>{:b=>{}}}
|
95
|
+
# You can pass a block to set the default value for the leaf:
|
96
|
+
# a = {}
|
97
|
+
# a.get!(:a, :b, :c) { :d } # => :d
|
98
|
+
# a # => {:a=>{:b=>{:c=>:d}}}
|
99
|
+
def get!(*nodes, &blk)
|
100
|
+
return self if (l = nodes.length) == 0
|
101
|
+
nh = nodes.head
|
102
|
+
if block_given?
|
103
|
+
if l == 1
|
104
|
+
self[nh] ||= yield
|
105
|
+
self[nh]
|
106
|
+
else
|
107
|
+
self[nh] ||= {}
|
108
|
+
self[nh].get!(*(nodes.tail), &blk)
|
109
|
+
end
|
110
|
+
else
|
111
|
+
if l == 1
|
112
|
+
self[nh]
|
113
|
+
else
|
114
|
+
self[nh] ||= {}
|
115
|
+
self[nh].get!(*(nodes.tail))
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
# Keep all keys except those in array x
|
121
|
+
def kexcept(*x)
|
122
|
+
reject{ |k,v| x.include?(k) }
|
123
|
+
end
|
124
|
+
|
125
|
+
# Keep only keys in array x
|
126
|
+
def konly(*x)
|
127
|
+
reject{ |k,v| !x.include?(k) }
|
128
|
+
end
|
129
|
+
|
130
|
+
def map_keys
|
131
|
+
self.inject(self.class.new){ |h,(k,v)| h.merge!( (yield k) => v ) }
|
132
|
+
end
|
133
|
+
|
134
|
+
def map_values
|
135
|
+
self.inject(self.class.new){ |h,(k,v)| h.merge!( k => (yield v) ) }
|
136
|
+
end
|
137
|
+
|
138
|
+
def map_values!
|
139
|
+
self.each_pair{ |k,v| self[k] = (yield v) }; self
|
140
|
+
end
|
141
|
+
|
142
|
+
# Same logic as get! but for setting values.
|
143
|
+
# Example:
|
144
|
+
# {}.set!(:a, :b, :c) { :d } # => {:a=>{:b=>{:c=>:d}}}
|
145
|
+
def set!(*nodes, &blk)
|
146
|
+
raise "Hash#set! takes a block and at least an argument." unless (block_given? && (l = nodes.length) > 0)
|
147
|
+
if l == 1 then self[nodes.head] = yield
|
148
|
+
else ( self.get!(*nodes.init) {{}} ).set!(nodes.last, &blk) end
|
149
|
+
self
|
150
|
+
end
|
151
|
+
|
152
|
+
# WARNING: breaks recent versions of Map
|
153
|
+
# def to_map
|
154
|
+
# self.is_a?(Map) ? self : Map(self)
|
155
|
+
# end
|
156
|
+
|
157
|
+
alias :to_sym :symbolize_keys
|
158
|
+
|
159
|
+
private
|
160
|
+
|
161
|
+
def eq_canonize(d) # helper for equiv?
|
162
|
+
if d.is_a?(Hash)
|
163
|
+
Map(d).inject(Map.new){ |h,(k,v)| h.set!(k){eq_canonize(v)} }.sort{ |x,y| x[0] <=> y[0] }
|
164
|
+
else d end
|
165
|
+
end
|
166
|
+
|
167
|
+
end
|
data/ext/io.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2010-2011 Moodstocks SAS
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# or in the LICENSE file which you should have received as part of
|
11
|
+
# this distribution.
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
18
|
+
#++
|
19
|
+
|
20
|
+
IO.class_eval do
|
21
|
+
|
22
|
+
# Read from a compressed IO stream
|
23
|
+
def gzread
|
24
|
+
ASE::IO::to_string(self, true)
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|