gorillib 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/LICENSE.textile +81 -0
- data/README.textile +153 -0
- data/Rakefile +26 -0
- data/VERSION +1 -0
- data/fiddle/hubahuba.rb +62 -0
- data/gorillib.gemspec +142 -0
- data/lib/gorillib/array/compact_blank.rb +17 -0
- data/lib/gorillib/array/extract_options.rb +29 -0
- data/lib/gorillib/base.rb +7 -0
- data/lib/gorillib/datetime/flat.rb +29 -0
- data/lib/gorillib/datetime/parse.rb +21 -0
- data/lib/gorillib/enumerable/sum.rb +38 -0
- data/lib/gorillib/hash/compact.rb +31 -0
- data/lib/gorillib/hash/deep_merge.rb +16 -0
- data/lib/gorillib/hash/keys.rb +42 -0
- data/lib/gorillib/hash/reverse_merge.rb +26 -0
- data/lib/gorillib/hash/slice.rb +53 -0
- data/lib/gorillib/hash/zip.rb +10 -0
- data/lib/gorillib/logger/log.rb +14 -0
- data/lib/gorillib/metaprogramming/aliasing.rb +43 -0
- data/lib/gorillib/metaprogramming/cattr_accessor.rb +79 -0
- data/lib/gorillib/metaprogramming/class_attribute.rb +90 -0
- data/lib/gorillib/metaprogramming/delegation.rb +146 -0
- data/lib/gorillib/metaprogramming/mattr_accessor.rb +61 -0
- data/lib/gorillib/metaprogramming/remove_method.rb +11 -0
- data/lib/gorillib/metaprogramming/singleton_class.rb +8 -0
- data/lib/gorillib/object/blank.rb +89 -0
- data/lib/gorillib/some.rb +12 -0
- data/lib/gorillib/string/constantize.rb +21 -0
- data/lib/gorillib/string/human.rb +52 -0
- data/lib/gorillib/string/inflections.rb +78 -0
- data/lib/gorillib/string/truncate.rb +33 -0
- data/lib/gorillib.rb +1 -0
- data/spec/blank_spec.rb +86 -0
- data/spec/gorillib_spec.rb +7 -0
- data/spec/rcov.opts +6 -0
- data/spec/spec.opts +4 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/spec_tasks.rake +15 -0
- data/test/abstract_unit.rb +25 -0
- data/test/array/compact_blank_test.rb +33 -0
- data/test/array/extract_options_test.rb +39 -0
- data/test/datetime/flat_test.rb +0 -0
- data/test/datetime/parse_test.rb +0 -0
- data/test/enumerable/sum_test.rb +50 -0
- data/test/hash/compact_test.rb +38 -0
- data/test/hash/deep_merge_test.rb +30 -0
- data/test/hash/keys_test.rb +110 -0
- data/test/hash/reverse_merge_test.rb +20 -0
- data/test/hash/slice_test.rb +47 -0
- data/test/hash/zip_test.rb +0 -0
- data/test/logger/log_test.rb +0 -0
- data/test/metaprogramming/aliasing_test.rb +188 -0
- data/test/metaprogramming/cattr_accessor_test.rb +38 -0
- data/test/metaprogramming/class_attribute_test.rb +73 -0
- data/test/metaprogramming/delegation_test.rb +166 -0
- data/test/metaprogramming/mattr_accessor_test.rb +40 -0
- data/test/metaprogramming/singleton_class_test.rb +9 -0
- data/test/object/blank_test.rb +22 -0
- data/test/string/constantize_test.rb +30 -0
- data/test/string/human_test.rb +65 -0
- data/test/string/inflections_test.rb +57 -0
- data/test/string/inflector_test_cases.rb +50 -0
- data/test/string/truncate_test.rb +37 -0
- metadata +199 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/LICENSE.textile
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
h1(gemheader). Gorillib license
|
2
|
+
|
3
|
+
|
4
|
+
The gorillib code is __Copyright (c) 2011 Infochimps, Inc__
|
5
|
+
|
6
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
|
7
|
+
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
|
10
|
+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an **AS IS** BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
|
11
|
+
|
12
|
+
h1. Apache License
|
13
|
+
|
14
|
+
Apache License
|
15
|
+
Version 2.0, January 2004
|
16
|
+
http://www.apache.org/licenses/
|
17
|
+
|
18
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
19
|
+
|
20
|
+
h2. 1. Definitions.
|
21
|
+
|
22
|
+
* **License** shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
23
|
+
* **Licensor** shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
24
|
+
* **Legal Entity** shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, **control** means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
25
|
+
* **You** (or **Your**) shall mean an individual or Legal Entity exercising permissions granted by this License.
|
26
|
+
* **Source** form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
27
|
+
* **Object** form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
28
|
+
* **Work** shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
29
|
+
* **Derivative Works** shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
30
|
+
* **Contribution** shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, **submitted** means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
31
|
+
* **Contributor** shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
32
|
+
|
33
|
+
h2. 2. Grant of Copyright License.
|
34
|
+
|
35
|
+
Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
36
|
+
|
37
|
+
|
38
|
+
h2. 3. Grant of Patent License.
|
39
|
+
|
40
|
+
Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
41
|
+
|
42
|
+
h2. 4. Redistribution.
|
43
|
+
|
44
|
+
You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
45
|
+
|
46
|
+
# You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
47
|
+
# You must cause any modified files to carry prominent notices stating that You changed the files; and
|
48
|
+
# You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
49
|
+
# If the Work includes a __NOTICE__ text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
50
|
+
|
51
|
+
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
52
|
+
|
53
|
+
h2. 5. Submission of Contributions.
|
54
|
+
|
55
|
+
Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
56
|
+
|
57
|
+
h2. 6. Trademarks.
|
58
|
+
|
59
|
+
This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
60
|
+
|
61
|
+
h2. 7. Disclaimer of Warranty.
|
62
|
+
|
63
|
+
Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an **AS IS** BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
64
|
+
|
65
|
+
h2. 8. Limitation of Liability.
|
66
|
+
|
67
|
+
In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
68
|
+
|
69
|
+
h2. 9. Accepting Warranty or Additional Liability.
|
70
|
+
|
71
|
+
While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
72
|
+
|
73
|
+
END OF TERMS AND CONDITIONS
|
74
|
+
|
75
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
|
76
|
+
|
77
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
78
|
+
|
79
|
+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an **AS IS** BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
|
80
|
+
|
81
|
+
<notextile></div></notextile>
|
data/README.textile
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
h2. Gorillib: infochimps' lightweight subset of ruby convenience methods
|
2
|
+
|
3
|
+
* No creep: include only what you need
|
4
|
+
* No dependencies unless audaciously advertised.
|
5
|
+
* Compatibile with active_record and extlib
|
6
|
+
* NO COMMITS UNLESS JACOB SAYS OK
|
7
|
+
|
8
|
+
h3. require 'gorillib'
|
9
|
+
|
10
|
+
* require 'gorrillib/base'
|
11
|
+
|
12
|
+
h3. require 'gorillib/base'
|
13
|
+
|
14
|
+
requires the following libraries:
|
15
|
+
|
16
|
+
* gorillib/object/blank
|
17
|
+
* gorillib/array/extract_options
|
18
|
+
* gorillib/hash/reverse_merge
|
19
|
+
* gorillib/hash/compact
|
20
|
+
* gorillib/array/compact
|
21
|
+
|
22
|
+
h3. require 'gorillib/some'
|
23
|
+
|
24
|
+
requires @gorillib/base@ and the following additional libraries:
|
25
|
+
|
26
|
+
* set
|
27
|
+
* time
|
28
|
+
* date
|
29
|
+
* gorillib/enumerable/sum
|
30
|
+
* gorillib/datetime/flat
|
31
|
+
* gorillib/datetime/parse_safely
|
32
|
+
* gorillib/hash/zip
|
33
|
+
* gorillib/hash/slice
|
34
|
+
* gorillib/hash/keys
|
35
|
+
* gorillib/metaprogramming/singleton_class
|
36
|
+
* gorillib/metaprogramming/remove_method
|
37
|
+
* gorillib/metaprogramming/class_attribute
|
38
|
+
* gorillib/metaprogramming/cattr_accessor
|
39
|
+
|
40
|
+
---------------------------------------------------------------------------
|
41
|
+
|
42
|
+
h3. gorillib/array
|
43
|
+
|
44
|
+
* *gorrillib/array/extract_options*
|
45
|
+
- Array extract_options!
|
46
|
+
- Hash extractable_options? (helper method)
|
47
|
+
* *gorrillib/array/compact_blank*
|
48
|
+
- Array compact_blank, compact_blank!
|
49
|
+
|
50
|
+
h3. gorillib/datetime
|
51
|
+
|
52
|
+
* *gorillib/datetime/flat*
|
53
|
+
- Date, Time to_flat
|
54
|
+
* *gorillib/datetime/parse*
|
55
|
+
- Date, Time parse_safely
|
56
|
+
|
57
|
+
h3. gorillib/enumerable
|
58
|
+
|
59
|
+
* *gorillib/enumerable/sum*
|
60
|
+
- Enumerable sum
|
61
|
+
|
62
|
+
h3. gorillib/hash
|
63
|
+
|
64
|
+
* *gorillib/hash/compact*
|
65
|
+
- Hash compact, compact!, compact_blank, compact_blank!
|
66
|
+
* *gorrilib/hash/deep_merge*
|
67
|
+
- Hash deep_merge, deep_merge!
|
68
|
+
* *gorillib/hash/keys*
|
69
|
+
- Hash stringify_keys, stringify_keys!, symbolize_keys, symbolize_keys!
|
70
|
+
- Hash assert_valid_keys
|
71
|
+
* *gorillib/hash/reverse_merge*
|
72
|
+
- Hash reverse_merge, reverse_merge!
|
73
|
+
* *gorillib/hash/slice*
|
74
|
+
- Hash slice, slice!, extract!
|
75
|
+
* *gorillib/hash/zip*
|
76
|
+
- Hash Hash.zip
|
77
|
+
|
78
|
+
h3. gorillib/logger
|
79
|
+
|
80
|
+
* *gorillib/logger/log*
|
81
|
+
- Unless the top-level constant ::Log has been defined, opens a new Logger to STDERR and assigns it to ::Log
|
82
|
+
|
83
|
+
h3. gorillib/metaprogramming
|
84
|
+
|
85
|
+
* *gorillib/metaprogramming/aliasing*
|
86
|
+
- alias_method_chain
|
87
|
+
* *gorillib/metaprogramming/class_attribute*
|
88
|
+
- Class class_attribute
|
89
|
+
* *gorillib/metaprogramming/remove_method* _required with class_attribute_
|
90
|
+
- Module remove_possible_method, redefine_method
|
91
|
+
* *gorillib/metaprogramming/singleton_class* _required with class_attribute_
|
92
|
+
- Kernel singleton_class
|
93
|
+
* *gorillib/metaprogramming/cattr_accessor*
|
94
|
+
- Class# cattr_reader, cattr_writer, cattr_accessor
|
95
|
+
* *gorillib/metaprogramming/mattr_accessor*
|
96
|
+
- Class# mattr_reader, mattr_writer, mattr_accessor
|
97
|
+
* *gorillib/metaprogramming/delegation*
|
98
|
+
- Module# delegate
|
99
|
+
|
100
|
+
h3. gorillib/object
|
101
|
+
|
102
|
+
* *gorillib/object/blank*
|
103
|
+
- Object blank?, present? (and specialized for all other classes)
|
104
|
+
|
105
|
+
h3. gorillib/string
|
106
|
+
|
107
|
+
* *gorillib/string/constantize*
|
108
|
+
- String constantize
|
109
|
+
* *gorillib/string/inflections*:
|
110
|
+
- String camelize
|
111
|
+
- String snakeize
|
112
|
+
- String underscore
|
113
|
+
- String demodulize
|
114
|
+
* *gorillib/string/human*
|
115
|
+
- String titleize
|
116
|
+
- String humainze
|
117
|
+
- Array as_sentence
|
118
|
+
* *gorillib/string/truncate*
|
119
|
+
- String truncate
|
120
|
+
|
121
|
+
---------------------------------------------------------------------------
|
122
|
+
|
123
|
+
h2. Maybe and No
|
124
|
+
|
125
|
+
h4. Maybe
|
126
|
+
|
127
|
+
* *Mash*
|
128
|
+
* *Receiver*
|
129
|
+
* *Struct*
|
130
|
+
* *gorillib/string/escaping*: _DEPENDENCIES_: htmlentities, addressable/uri
|
131
|
+
- String xml_escape
|
132
|
+
- String url_escape
|
133
|
+
- String escape_regexp, unescape_regexp
|
134
|
+
* *extlib/module/find_const*
|
135
|
+
- Module find_const
|
136
|
+
|
137
|
+
h4. No
|
138
|
+
|
139
|
+
* String classify -- this singularizes. You want camelize unless you're in ActiveSupport
|
140
|
+
* Object tap isn't necessary -- included in 1.8.7+
|
141
|
+
* Symbol to_proc isn't necessary -- included in 1.8.7+
|
142
|
+
* Class class_inheritable_attribute -- use class_attribute instead
|
143
|
+
* Object to_flat on anything but Time and Date -- poorly-defined
|
144
|
+
* Object returning -- deprecated in favor of #tap
|
145
|
+
|
146
|
+
---------------------------------------------------------------------------
|
147
|
+
|
148
|
+
h2. Credits & Copyright
|
149
|
+
|
150
|
+
Most of this code is ripped from active_support and extlib -- their license
|
151
|
+
carries over. Everything else is Copyright (c) 2011 Infochimps. See LICENSE.txt
|
152
|
+
for further details.
|
153
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
require 'jeweler'
|
5
|
+
Jeweler::Tasks.new do |gem|
|
6
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
7
|
+
gem.name = "gorillib"
|
8
|
+
gem.homepage = "http://infochimps.com/labs"
|
9
|
+
gem.license = "MIT"
|
10
|
+
gem.summary = %Q{include only what you need. No dependencies, no creep}
|
11
|
+
gem.description = %Q{Gorillib: infochimps lightweight subset of ruby convenience methods}
|
12
|
+
gem.email = "coders@infochimps.org"
|
13
|
+
gem.authors = ["Infochimps"]
|
14
|
+
|
15
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
16
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
17
|
+
# gem.add_runtime_dependency 'jabber4r', '> 0.1'
|
18
|
+
# gem.add_development_dependency 'rspec', '> 1.2.3'
|
19
|
+
gem.add_development_dependency "yard", "~> 0.6.0"
|
20
|
+
gem.add_development_dependency "jeweler", "~> 1.5.2"
|
21
|
+
gem.add_development_dependency "rcov", ">= 0"
|
22
|
+
end
|
23
|
+
Jeweler::RubygemsDotOrgTasks.new
|
24
|
+
|
25
|
+
require 'yard'
|
26
|
+
YARD::Rake::YardocTask.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.2
|
data/fiddle/hubahuba.rb
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
|
2
|
+
module Enumerable
|
3
|
+
def map_with_index
|
4
|
+
result = []
|
5
|
+
self.each_with_index do |elt, idx|
|
6
|
+
result << yield(elt, idx)
|
7
|
+
end
|
8
|
+
result
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
https://github.com/github/hubahuba/tree/master/lib
|
13
|
+
|
14
|
+
|
15
|
+
class Object
|
16
|
+
# Metaid == a few simple metaclass helper
|
17
|
+
# (See http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html.)
|
18
|
+
# The hidden singleton lurks behind everyone
|
19
|
+
def metaclass() class << self; self end end
|
20
|
+
def meta_eval(&blk) metaclass.instance_eval(&blk) end
|
21
|
+
|
22
|
+
# Adds methods to a metaclass
|
23
|
+
def meta_def(name, &blk)
|
24
|
+
meta_eval { define_method(name, &blk) }
|
25
|
+
end
|
26
|
+
|
27
|
+
# Defines an instance method within a class
|
28
|
+
def class_def(name, &blk)
|
29
|
+
class_eval { define_method(name, &blk) }
|
30
|
+
end
|
31
|
+
|
32
|
+
##
|
33
|
+
# if ''.not.blank?
|
34
|
+
# http://blog.jayfields.com/2007/08/ruby-adding-not-method-for-readability.html
|
35
|
+
define_method :not do
|
36
|
+
Not.new(self)
|
37
|
+
end
|
38
|
+
|
39
|
+
class Not
|
40
|
+
private *instance_methods.select { |m| m !~ /(^__|^\W|^binding$)/ }
|
41
|
+
|
42
|
+
def initialize(subject)
|
43
|
+
@subject = subject
|
44
|
+
end
|
45
|
+
|
46
|
+
def method_missing(sym, *args, &blk)
|
47
|
+
!@subject.send(sym,*args,&blk)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
##
|
52
|
+
# @person ? @person.name : nil
|
53
|
+
# vs
|
54
|
+
# @person.try(:name)
|
55
|
+
def try(method)
|
56
|
+
send method if respond_to? method
|
57
|
+
end
|
58
|
+
|
59
|
+
def class_attr_accessor(*attrs)
|
60
|
+
metaclass.send(:attr_accessor, *attrs)
|
61
|
+
end
|
62
|
+
end
|
data/gorillib.gemspec
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{gorillib}
|
8
|
+
s.version = "0.0.2"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Infochimps"]
|
12
|
+
s.date = %q{2011-04-02}
|
13
|
+
s.description = %q{Gorillib: infochimps lightweight subset of ruby convenience methods}
|
14
|
+
s.email = %q{coders@infochimps.org}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.textile",
|
17
|
+
"README.textile"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".rspec",
|
22
|
+
"LICENSE.textile",
|
23
|
+
"README.textile",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"fiddle/hubahuba.rb",
|
27
|
+
"gorillib.gemspec",
|
28
|
+
"lib/gorillib.rb",
|
29
|
+
"lib/gorillib/array/compact_blank.rb",
|
30
|
+
"lib/gorillib/array/extract_options.rb",
|
31
|
+
"lib/gorillib/base.rb",
|
32
|
+
"lib/gorillib/datetime/flat.rb",
|
33
|
+
"lib/gorillib/datetime/parse.rb",
|
34
|
+
"lib/gorillib/enumerable/sum.rb",
|
35
|
+
"lib/gorillib/hash/compact.rb",
|
36
|
+
"lib/gorillib/hash/deep_merge.rb",
|
37
|
+
"lib/gorillib/hash/keys.rb",
|
38
|
+
"lib/gorillib/hash/reverse_merge.rb",
|
39
|
+
"lib/gorillib/hash/slice.rb",
|
40
|
+
"lib/gorillib/hash/zip.rb",
|
41
|
+
"lib/gorillib/logger/log.rb",
|
42
|
+
"lib/gorillib/metaprogramming/aliasing.rb",
|
43
|
+
"lib/gorillib/metaprogramming/cattr_accessor.rb",
|
44
|
+
"lib/gorillib/metaprogramming/class_attribute.rb",
|
45
|
+
"lib/gorillib/metaprogramming/delegation.rb",
|
46
|
+
"lib/gorillib/metaprogramming/mattr_accessor.rb",
|
47
|
+
"lib/gorillib/metaprogramming/remove_method.rb",
|
48
|
+
"lib/gorillib/metaprogramming/singleton_class.rb",
|
49
|
+
"lib/gorillib/object/blank.rb",
|
50
|
+
"lib/gorillib/some.rb",
|
51
|
+
"lib/gorillib/string/constantize.rb",
|
52
|
+
"lib/gorillib/string/human.rb",
|
53
|
+
"lib/gorillib/string/inflections.rb",
|
54
|
+
"lib/gorillib/string/truncate.rb",
|
55
|
+
"spec/blank_spec.rb",
|
56
|
+
"spec/gorillib_spec.rb",
|
57
|
+
"spec/rcov.opts",
|
58
|
+
"spec/spec.opts",
|
59
|
+
"spec/spec_helper.rb",
|
60
|
+
"spec/spec_tasks.rake",
|
61
|
+
"test/abstract_unit.rb",
|
62
|
+
"test/array/compact_blank_test.rb",
|
63
|
+
"test/array/extract_options_test.rb",
|
64
|
+
"test/datetime/flat_test.rb",
|
65
|
+
"test/datetime/parse_test.rb",
|
66
|
+
"test/enumerable/sum_test.rb",
|
67
|
+
"test/hash/compact_test.rb",
|
68
|
+
"test/hash/deep_merge_test.rb",
|
69
|
+
"test/hash/keys_test.rb",
|
70
|
+
"test/hash/reverse_merge_test.rb",
|
71
|
+
"test/hash/slice_test.rb",
|
72
|
+
"test/hash/zip_test.rb",
|
73
|
+
"test/logger/log_test.rb",
|
74
|
+
"test/metaprogramming/aliasing_test.rb",
|
75
|
+
"test/metaprogramming/cattr_accessor_test.rb",
|
76
|
+
"test/metaprogramming/class_attribute_test.rb",
|
77
|
+
"test/metaprogramming/delegation_test.rb",
|
78
|
+
"test/metaprogramming/mattr_accessor_test.rb",
|
79
|
+
"test/metaprogramming/singleton_class_test.rb",
|
80
|
+
"test/object/blank_test.rb",
|
81
|
+
"test/string/constantize_test.rb",
|
82
|
+
"test/string/human_test.rb",
|
83
|
+
"test/string/inflections_test.rb",
|
84
|
+
"test/string/inflector_test_cases.rb",
|
85
|
+
"test/string/truncate_test.rb"
|
86
|
+
]
|
87
|
+
s.homepage = %q{http://infochimps.com/labs}
|
88
|
+
s.licenses = ["MIT"]
|
89
|
+
s.require_paths = ["lib"]
|
90
|
+
s.rubygems_version = %q{1.3.7}
|
91
|
+
s.summary = %q{include only what you need. No dependencies, no creep}
|
92
|
+
s.test_files = [
|
93
|
+
"spec/blank_spec.rb",
|
94
|
+
"spec/gorillib_spec.rb",
|
95
|
+
"spec/spec_helper.rb",
|
96
|
+
"test/abstract_unit.rb",
|
97
|
+
"test/array/compact_blank_test.rb",
|
98
|
+
"test/array/extract_options_test.rb",
|
99
|
+
"test/datetime/flat_test.rb",
|
100
|
+
"test/datetime/parse_test.rb",
|
101
|
+
"test/enumerable/sum_test.rb",
|
102
|
+
"test/hash/compact_test.rb",
|
103
|
+
"test/hash/deep_merge_test.rb",
|
104
|
+
"test/hash/keys_test.rb",
|
105
|
+
"test/hash/reverse_merge_test.rb",
|
106
|
+
"test/hash/slice_test.rb",
|
107
|
+
"test/hash/zip_test.rb",
|
108
|
+
"test/logger/log_test.rb",
|
109
|
+
"test/metaprogramming/aliasing_test.rb",
|
110
|
+
"test/metaprogramming/cattr_accessor_test.rb",
|
111
|
+
"test/metaprogramming/class_attribute_test.rb",
|
112
|
+
"test/metaprogramming/delegation_test.rb",
|
113
|
+
"test/metaprogramming/mattr_accessor_test.rb",
|
114
|
+
"test/metaprogramming/singleton_class_test.rb",
|
115
|
+
"test/object/blank_test.rb",
|
116
|
+
"test/string/constantize_test.rb",
|
117
|
+
"test/string/human_test.rb",
|
118
|
+
"test/string/inflections_test.rb",
|
119
|
+
"test/string/inflector_test_cases.rb",
|
120
|
+
"test/string/truncate_test.rb"
|
121
|
+
]
|
122
|
+
|
123
|
+
if s.respond_to? :specification_version then
|
124
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
125
|
+
s.specification_version = 3
|
126
|
+
|
127
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
128
|
+
s.add_development_dependency(%q<yard>, ["~> 0.6.0"])
|
129
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
|
130
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
131
|
+
else
|
132
|
+
s.add_dependency(%q<yard>, ["~> 0.6.0"])
|
133
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
134
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
135
|
+
end
|
136
|
+
else
|
137
|
+
s.add_dependency(%q<yard>, ["~> 0.6.0"])
|
138
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
139
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'gorillib/object/blank'
|
2
|
+
|
3
|
+
class Array
|
4
|
+
#
|
5
|
+
# remove all key-value pairs where the value is blank
|
6
|
+
#
|
7
|
+
def compact_blank
|
8
|
+
reject{|val| val.blank? }
|
9
|
+
end unless method_defined?(:compact_blank)
|
10
|
+
|
11
|
+
#
|
12
|
+
# Replace the array with its compact_blank'ed self
|
13
|
+
#
|
14
|
+
def compact_blank!
|
15
|
+
replace(compact_blank)
|
16
|
+
end unless method_defined?(:compact_blank!)
|
17
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class Hash
|
2
|
+
# By default, only instances of Hash itself are extractable.
|
3
|
+
# Subclasses of Hash may implement this method and return
|
4
|
+
# true to declare themselves as extractable. If a Hash
|
5
|
+
# is extractable, Array#extract_options! pops it from
|
6
|
+
# the Array when it is the last element of the Array.
|
7
|
+
def extractable_options?
|
8
|
+
instance_of?(Hash)
|
9
|
+
end unless method_defined?(:extractable_options?)
|
10
|
+
end
|
11
|
+
|
12
|
+
class Array
|
13
|
+
# Extracts options from a set of arguments. Removes and returns the last
|
14
|
+
# element in the array if it's a hash, otherwise returns a blank hash.
|
15
|
+
#
|
16
|
+
# def options(*args)
|
17
|
+
# args.extract_options!
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# options(1, 2) # => {}
|
21
|
+
# options(1, 2, :a => :b) # => {:a=>:b}
|
22
|
+
def extract_options!
|
23
|
+
if last.is_a?(Hash) && last.extractable_options?
|
24
|
+
pop
|
25
|
+
else
|
26
|
+
{}
|
27
|
+
end
|
28
|
+
end unless method_defined?(:extract_options!)
|
29
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class Time
|
2
|
+
# strftime() format to flatten a date
|
3
|
+
FLAT_FORMAT = "%Y%m%d%H%M%S" unless defined?(FLAT_FORMAT)
|
4
|
+
|
5
|
+
# Flatten
|
6
|
+
def to_flat
|
7
|
+
utc.strftime(FLAT_FORMAT)
|
8
|
+
end unless method_defined?(:to_flat)
|
9
|
+
end
|
10
|
+
|
11
|
+
class DateTime < Date
|
12
|
+
# strftime() format to flatten a date
|
13
|
+
FLAT_FORMAT = "%Y%m%d%H%M%S" unless defined?(FLAT_FORMAT)
|
14
|
+
|
15
|
+
# Flatten
|
16
|
+
def to_flat
|
17
|
+
strftime(FLAT_FORMAT)
|
18
|
+
end unless method_defined?(:to_flat)
|
19
|
+
end
|
20
|
+
|
21
|
+
class Date
|
22
|
+
# strftime() format to flatten a date
|
23
|
+
FLAT_FORMAT = "%Y%m%d" unless defined?(FLAT_FORMAT)
|
24
|
+
|
25
|
+
# Flatten
|
26
|
+
def to_flat
|
27
|
+
strftime(FLAT_FORMAT)
|
28
|
+
end unless method_defined?(:to_flat)
|
29
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class Time
|
2
|
+
#
|
3
|
+
# Parses the time but never fails.
|
4
|
+
# Return value is always in the UTC time zone.
|
5
|
+
#
|
6
|
+
# A flattened datetime -- a 14-digit YYYYmmddHHMMMSS -- is fixed to the UTC
|
7
|
+
# time zone by parsing it as YYYYmmddHHMMMSSZ <- 'Z' at end
|
8
|
+
#
|
9
|
+
def self.parse_safely dt
|
10
|
+
return nil if dt.blank?
|
11
|
+
begin
|
12
|
+
case
|
13
|
+
when dt.is_a?(Time) then dt.utc
|
14
|
+
when (dt.to_s =~ /\A\d{14}\z/) then parse(dt.to_s+'Z', true)
|
15
|
+
else parse(dt.to_s, true).utc
|
16
|
+
end
|
17
|
+
rescue StandardError => e
|
18
|
+
Log.debug e
|
19
|
+
end
|
20
|
+
end unless method_defined?(:parse_safely)
|
21
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Enumerable
|
2
|
+
# Calculates a sum from the elements. Examples:
|
3
|
+
#
|
4
|
+
# payments.sum { |p| p.price * p.tax_rate }
|
5
|
+
# payments.sum(&:price)
|
6
|
+
#
|
7
|
+
# The latter is a shortcut for:
|
8
|
+
#
|
9
|
+
# payments.inject(0) { |sum, p| sum + p.price }
|
10
|
+
#
|
11
|
+
# It can also calculate the sum without the use of a block.
|
12
|
+
#
|
13
|
+
# [5, 15, 10].sum # => 30
|
14
|
+
# ["foo", "bar"].sum # => "foobar"
|
15
|
+
# [[1, 2], [3, 1, 5]].sum => [1, 2, 3, 1, 5]
|
16
|
+
#
|
17
|
+
# The default sum of an empty list is zero. You can override this default:
|
18
|
+
#
|
19
|
+
# [].sum(Payment.new(0)) { |i| i.amount } # => Payment.new(0)
|
20
|
+
#
|
21
|
+
def sum(identity = 0, &block)
|
22
|
+
if block_given?
|
23
|
+
map(&block).sum(identity)
|
24
|
+
else
|
25
|
+
inject { |sum, element| sum + element } || identity
|
26
|
+
end
|
27
|
+
end unless method_defined?(:sum)
|
28
|
+
end
|
29
|
+
|
30
|
+
class Range #:nodoc:
|
31
|
+
# Optimize range sum to use arithmetic progression if a block is not given and
|
32
|
+
# we have a range of numeric values.
|
33
|
+
def sum(identity = 0)
|
34
|
+
return super if block_given? || !(first.instance_of?(Integer) && last.instance_of?(Integer))
|
35
|
+
actual_last = exclude_end? ? (last - 1) : last
|
36
|
+
(actual_last - first + 1) * (actual_last + first) / 2
|
37
|
+
end unless method_defined?(:sum)
|
38
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'gorillib/object/blank'
|
2
|
+
|
3
|
+
class Hash
|
4
|
+
#
|
5
|
+
# remove all key-value pairs where the value is nil
|
6
|
+
#
|
7
|
+
def compact
|
8
|
+
reject{|key,val| val.nil? }
|
9
|
+
end unless method_defined?(:compact)
|
10
|
+
|
11
|
+
#
|
12
|
+
# Replace the hash with its compacted self
|
13
|
+
#
|
14
|
+
def compact!
|
15
|
+
replace(compact)
|
16
|
+
end unless method_defined?(:compact!)
|
17
|
+
|
18
|
+
#
|
19
|
+
# remove all key-value pairs where the value is blank
|
20
|
+
#
|
21
|
+
def compact_blank
|
22
|
+
reject{|key,val| val.blank? }
|
23
|
+
end unless method_defined?(:compact_blank)
|
24
|
+
|
25
|
+
#
|
26
|
+
# Replace the hash with its compact_blank'ed self
|
27
|
+
#
|
28
|
+
def compact_blank!
|
29
|
+
replace(compact_blank)
|
30
|
+
end unless method_defined?(:compact_blank!)
|
31
|
+
end
|