jakewendt-ruby_extension 1.0.6
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/README.rdoc +71 -0
- data/lib/jakewendt-ruby_extension.rb +1 -0
- data/lib/ruby_extension.rb +8 -0
- data/lib/ruby_extension/array.rb +142 -0
- data/lib/ruby_extension/hash.rb +55 -0
- data/lib/ruby_extension/integer.rb +26 -0
- data/lib/ruby_extension/nil_class.rb +28 -0
- data/lib/ruby_extension/object.rb +68 -0
- data/lib/ruby_extension/string.rb +35 -0
- data/rails/init.rb +1 -0
- metadata +76 -0
data/README.rdoc
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
= RubyExtension (Gem / Rails Plugin)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
== Usage
|
|
5
|
+
|
|
6
|
+
config.gem 'jakewendt-ruby_extension',
|
|
7
|
+
:source => 'http://rubygems.org'
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
== ToDo
|
|
11
|
+
|
|
12
|
+
* add legitimate text to Rakefile
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
== Notes
|
|
16
|
+
|
|
17
|
+
Minor additions to some Ruby core classes.
|
|
18
|
+
(This may be better as a gem.)
|
|
19
|
+
|
|
20
|
+
Array
|
|
21
|
+
arrange(new_array_index=[])
|
|
22
|
+
drop_blanks
|
|
23
|
+
capitalize
|
|
24
|
+
capitalize!
|
|
25
|
+
downcase
|
|
26
|
+
average
|
|
27
|
+
median
|
|
28
|
+
first_index
|
|
29
|
+
to_boolean / to_b
|
|
30
|
+
true? (explicitly so)
|
|
31
|
+
false? (explicitly so)
|
|
32
|
+
true_xor_false?
|
|
33
|
+
|
|
34
|
+
Hash
|
|
35
|
+
delete_keys_matching!(regex)
|
|
36
|
+
delete_keys!(*keys)
|
|
37
|
+
dig(*keys)
|
|
38
|
+
|
|
39
|
+
String
|
|
40
|
+
to_params_hash
|
|
41
|
+
uniq
|
|
42
|
+
|
|
43
|
+
NilClass
|
|
44
|
+
split(*args)
|
|
45
|
+
include?(*args)
|
|
46
|
+
|
|
47
|
+
Integer
|
|
48
|
+
factorial
|
|
49
|
+
|
|
50
|
+
Object
|
|
51
|
+
to_boolean
|
|
52
|
+
true? (explicitly so)
|
|
53
|
+
false? (explicitly so)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
== Gemified with Jeweler
|
|
58
|
+
|
|
59
|
+
vi Rakefile
|
|
60
|
+
rake version:write
|
|
61
|
+
|
|
62
|
+
rake version:bump:patch
|
|
63
|
+
rake version:bump:minor
|
|
64
|
+
rake version:bump:major
|
|
65
|
+
|
|
66
|
+
rake gemspec
|
|
67
|
+
|
|
68
|
+
rake install
|
|
69
|
+
rake release
|
|
70
|
+
|
|
71
|
+
Copyright (c) 2010 [Jake Wendt], released under the MIT license
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'ruby_extension'
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# I need to be more explicit about which files to require
|
|
2
|
+
# because I have files of the same name in RAILS_ROOT/lib
|
|
3
|
+
require "ruby_extension/object"
|
|
4
|
+
require "ruby_extension/integer"
|
|
5
|
+
require "ruby_extension/array"
|
|
6
|
+
require "ruby_extension/string"
|
|
7
|
+
require "ruby_extension/hash"
|
|
8
|
+
require "ruby_extension/nil_class"
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
module RubyExtension # :nodoc:
|
|
2
|
+
module Array # :nodoc:
|
|
3
|
+
def self.included(base)
|
|
4
|
+
# base.extend(ClassMethods)
|
|
5
|
+
base.instance_eval do
|
|
6
|
+
include InstanceMethods
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# module ClassMethods # :nodoc:
|
|
11
|
+
# end
|
|
12
|
+
|
|
13
|
+
module InstanceMethods
|
|
14
|
+
|
|
15
|
+
# ['a','b','c'].arrange([2,0,1]) => ['c','a','b']
|
|
16
|
+
def arrange(new_array_index=[])
|
|
17
|
+
new_array = self.dup
|
|
18
|
+
new_array_index.each_with_index do |index,new_index|
|
|
19
|
+
new_array[new_index] = self[index]
|
|
20
|
+
end
|
|
21
|
+
new_array
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Remove all "blank?" items for the array
|
|
25
|
+
def drop_blanks!
|
|
26
|
+
delete_if{|a|a.blank?}
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Return capitlized versions of each item in the array
|
|
30
|
+
def capitalize
|
|
31
|
+
collect(&:capitalize)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Capitalize each item in the array and return it
|
|
35
|
+
def capitalize!
|
|
36
|
+
each_with_index do |element,index|
|
|
37
|
+
self[index] = element.capitalize
|
|
38
|
+
end
|
|
39
|
+
self
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Return downcased versions of each item in the array
|
|
43
|
+
def downcase
|
|
44
|
+
collect(&:downcase)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Return the average digitized value of the array
|
|
48
|
+
def average
|
|
49
|
+
if self.length > 0
|
|
50
|
+
# sum defined in activesupport/lib/active_support/core_ext/enumerable.rb
|
|
51
|
+
self.digitize.sum.to_f / self.length
|
|
52
|
+
else
|
|
53
|
+
nil
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Return the median digitized value of the array
|
|
58
|
+
def median
|
|
59
|
+
if self.length > 0
|
|
60
|
+
sorted_values = self.digitize.sort
|
|
61
|
+
length = sorted_values.length
|
|
62
|
+
if length.odd?
|
|
63
|
+
sorted_values[length/2]
|
|
64
|
+
else
|
|
65
|
+
( sorted_values[length/2] + sorted_values[-1+length/2] ).to_f / 2
|
|
66
|
+
end
|
|
67
|
+
else
|
|
68
|
+
nil
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Return a copy of the array with values at the
|
|
73
|
+
# given indexes swapped.
|
|
74
|
+
def swap_indexes(i,j)
|
|
75
|
+
new_array = self.dup
|
|
76
|
+
new_array[i],new_array[j] = self[j],self[i]
|
|
77
|
+
new_array
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Swap the values of an array at the given indexes
|
|
81
|
+
# and return it
|
|
82
|
+
def swap_indexes!(i,j)
|
|
83
|
+
self[i],self[j] = self[j],self[i]
|
|
84
|
+
self
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# Convert all items in the array to_f
|
|
88
|
+
def numericize
|
|
89
|
+
collect(&:to_f)
|
|
90
|
+
end
|
|
91
|
+
alias_method :digitize, :numericize
|
|
92
|
+
|
|
93
|
+
# def first_index(value = nil) # either way works
|
|
94
|
+
|
|
95
|
+
# return the first index of the array
|
|
96
|
+
# with a value matching that given
|
|
97
|
+
def first_index(value = nil, &block)
|
|
98
|
+
using_block = block_given?
|
|
99
|
+
each_with_index do |element,index|
|
|
100
|
+
return index if (using_block && yield(element)) || (value == element)
|
|
101
|
+
end
|
|
102
|
+
return nil
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def to_boolean
|
|
106
|
+
!empty? && all?{|v| v.to_boolean }
|
|
107
|
+
end
|
|
108
|
+
# alias_method :true?, :to_boolean
|
|
109
|
+
alias_method :to_b, :to_boolean
|
|
110
|
+
|
|
111
|
+
# [].true?
|
|
112
|
+
# => false
|
|
113
|
+
# [true].true?
|
|
114
|
+
# => true
|
|
115
|
+
# [true,false].true?
|
|
116
|
+
# => true
|
|
117
|
+
# [false].true?
|
|
118
|
+
# => false
|
|
119
|
+
def true?
|
|
120
|
+
!empty? && any?{|v| v.true? }
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def false?
|
|
124
|
+
!empty? && any?{|v| v.false? }
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# I need to work on this one ...
|
|
128
|
+
def true_xor_false?
|
|
129
|
+
# self.include?('true') ^ self.include?('false') ^
|
|
130
|
+
# self.include?(true) ^ self.include?(false)
|
|
131
|
+
contains_true = contains_false = false
|
|
132
|
+
each {|v|
|
|
133
|
+
# ( v.to_boolean ) ? contains_true = true : contains_false = true
|
|
134
|
+
eval("contains_#{v.to_boolean}=true")
|
|
135
|
+
}
|
|
136
|
+
contains_true ^ contains_false
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
Array.send(:include, RubyExtension::Array)
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
module RubyExtension # :nodoc:
|
|
2
|
+
module Hash # :nodoc:
|
|
3
|
+
def self.included(base)
|
|
4
|
+
# base.extend(ClassMethods)
|
|
5
|
+
base.instance_eval do
|
|
6
|
+
include InstanceMethods
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# module ClassMethods # :nodoc:
|
|
11
|
+
# end
|
|
12
|
+
|
|
13
|
+
module InstanceMethods
|
|
14
|
+
|
|
15
|
+
# delete all keys matching the given regex
|
|
16
|
+
# and return the new hash
|
|
17
|
+
def delete_keys_matching!(regex)
|
|
18
|
+
self.keys.each do |k|
|
|
19
|
+
if k.to_s =~ Regexp.new(regex)
|
|
20
|
+
self.delete(k)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
self
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# delete all keys in the given array
|
|
27
|
+
# and return the new hash
|
|
28
|
+
def delete_keys!(*keys)
|
|
29
|
+
keys.each do |k|
|
|
30
|
+
self.delete(k)
|
|
31
|
+
end
|
|
32
|
+
self
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# params.dig('study_events',se.id.to_s,'eligible')
|
|
36
|
+
def dig(*args)
|
|
37
|
+
if args.length > 0 && self.keys.include?(args.first)
|
|
38
|
+
key = args.shift
|
|
39
|
+
if args.length > 0
|
|
40
|
+
if self[key].is_a?(Hash)
|
|
41
|
+
self[key].dig(*args)
|
|
42
|
+
else
|
|
43
|
+
nil
|
|
44
|
+
end
|
|
45
|
+
else
|
|
46
|
+
self[key]
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
Hash.send( :include, RubyExtension::Hash )
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
module RubyExtension # :nodoc:
|
|
2
|
+
module Integer # :nodoc:
|
|
3
|
+
def self.included(base)
|
|
4
|
+
# base.extend(ClassMethods)
|
|
5
|
+
base.instance_eval do
|
|
6
|
+
include InstanceMethods
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# module ClassMethods # :nodoc:
|
|
11
|
+
# end
|
|
12
|
+
|
|
13
|
+
module InstanceMethods
|
|
14
|
+
|
|
15
|
+
# Return n!
|
|
16
|
+
def factorial
|
|
17
|
+
f = n = self
|
|
18
|
+
f *= n -= 1 while( n > 1 )
|
|
19
|
+
return f
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
Integer.send( :include, RubyExtension::Integer )
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
module RubyExtension # :nodoc:
|
|
2
|
+
module NilClass # :nodoc:
|
|
3
|
+
def self.included(base)
|
|
4
|
+
# base.extend(ClassMethods)
|
|
5
|
+
base.instance_eval do
|
|
6
|
+
include InstanceMethods
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# module ClassMethods # :nodoc:
|
|
11
|
+
# end
|
|
12
|
+
|
|
13
|
+
module InstanceMethods
|
|
14
|
+
|
|
15
|
+
# Return an empty array when attempting to split nil
|
|
16
|
+
def split(*args)
|
|
17
|
+
[]
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def include?(*args)
|
|
21
|
+
false
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
NilClass.send( :include, RubyExtension::NilClass )
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
module RubyExtension # :nodoc:
|
|
2
|
+
module Object # :nodoc:
|
|
3
|
+
def self.included(base)
|
|
4
|
+
# base.extend(ClassMethods)
|
|
5
|
+
base.instance_eval do
|
|
6
|
+
include InstanceMethods
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# module ClassMethods # :nodoc:
|
|
11
|
+
# end
|
|
12
|
+
|
|
13
|
+
module InstanceMethods
|
|
14
|
+
|
|
15
|
+
# originally from ActiveSupport::Callbacks::Callback
|
|
16
|
+
# needs modified to actually work the way I'd like
|
|
17
|
+
# needs tests
|
|
18
|
+
# x(:some_method)
|
|
19
|
+
# x(Proc.new(....))
|
|
20
|
+
# x(lambda{...})
|
|
21
|
+
# def evaluate_method(method, *args, &block)
|
|
22
|
+
def evaluate_method(method, *args, &block)
|
|
23
|
+
case method
|
|
24
|
+
when Symbol
|
|
25
|
+
# I don't quite understand the shift (it fails)
|
|
26
|
+
# object = args.shift
|
|
27
|
+
# object.send(method, *args, &block)
|
|
28
|
+
send(method, *args, &block)
|
|
29
|
+
when String
|
|
30
|
+
eval(method, args.first.instance_eval { binding })
|
|
31
|
+
when Proc, Method
|
|
32
|
+
# fails
|
|
33
|
+
method.call(*args, &block)
|
|
34
|
+
else
|
|
35
|
+
if method.respond_to?(kind)
|
|
36
|
+
method.send(kind, *args, &block)
|
|
37
|
+
else
|
|
38
|
+
raise ArgumentError,
|
|
39
|
+
"Callbacks must be a symbol denoting the method to call, a string to be evaluated, " +
|
|
40
|
+
"a block to be invoked, or an object responding to the callback method."
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
alias_method :x, :evaluate_method
|
|
45
|
+
|
|
46
|
+
def to_boolean
|
|
47
|
+
# return [true, 'true', 1, '1', 't'].include?(
|
|
48
|
+
return ![nil, false, 'false', 0, '0', 'f'].include?(
|
|
49
|
+
( self.is_a?(String) ) ? self.downcase : self )
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# looking for an explicit true
|
|
53
|
+
def true?
|
|
54
|
+
return [true, 'true', 1, '1', 't'].include?(
|
|
55
|
+
( self.is_a?(String) ) ? self.downcase : self )
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# looking for an explicit false (not nil)
|
|
59
|
+
def false?
|
|
60
|
+
return [false, 'false', 0, '0', 'f'].include?(
|
|
61
|
+
( self.is_a?(String) ) ? self.downcase : self )
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
Object.send(:include, RubyExtension::Object)
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module RubyExtension # :nodoc:
|
|
2
|
+
module String # :nodoc:
|
|
3
|
+
def self.included(base)
|
|
4
|
+
# base.extend(ClassMethods)
|
|
5
|
+
base.instance_eval do
|
|
6
|
+
include InstanceMethods
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
# module ClassMethods # :nodoc:
|
|
11
|
+
# end
|
|
12
|
+
|
|
13
|
+
module InstanceMethods
|
|
14
|
+
|
|
15
|
+
# Convert a query string like that in a URL
|
|
16
|
+
# to a Hash
|
|
17
|
+
def to_params_hash
|
|
18
|
+
h = HashWithIndifferentAccess.new
|
|
19
|
+
self.split('&').each do |p|
|
|
20
|
+
(k,v) = p.split('=',2)
|
|
21
|
+
h[k] = URI.decode(v)
|
|
22
|
+
end
|
|
23
|
+
return h
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Return self
|
|
27
|
+
def uniq
|
|
28
|
+
self
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
String.send( :include, RubyExtension::String )
|
data/rails/init.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'jakewendt-ruby_extension'
|
metadata
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: jakewendt-ruby_extension
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
hash: 27
|
|
5
|
+
prerelease:
|
|
6
|
+
segments:
|
|
7
|
+
- 1
|
|
8
|
+
- 0
|
|
9
|
+
- 6
|
|
10
|
+
version: 1.0.6
|
|
11
|
+
platform: ruby
|
|
12
|
+
authors:
|
|
13
|
+
- Jake
|
|
14
|
+
autorequire:
|
|
15
|
+
bindir: bin
|
|
16
|
+
cert_chain: []
|
|
17
|
+
|
|
18
|
+
date: 2011-08-10 00:00:00 -07:00
|
|
19
|
+
default_executable:
|
|
20
|
+
dependencies: []
|
|
21
|
+
|
|
22
|
+
description: longer description of your gem
|
|
23
|
+
email: github@jake.otherinbox.com
|
|
24
|
+
executables: []
|
|
25
|
+
|
|
26
|
+
extensions: []
|
|
27
|
+
|
|
28
|
+
extra_rdoc_files:
|
|
29
|
+
- README.rdoc
|
|
30
|
+
files:
|
|
31
|
+
- lib/jakewendt-ruby_extension.rb
|
|
32
|
+
- lib/ruby_extension.rb
|
|
33
|
+
- lib/ruby_extension/array.rb
|
|
34
|
+
- lib/ruby_extension/hash.rb
|
|
35
|
+
- lib/ruby_extension/integer.rb
|
|
36
|
+
- lib/ruby_extension/nil_class.rb
|
|
37
|
+
- lib/ruby_extension/object.rb
|
|
38
|
+
- lib/ruby_extension/string.rb
|
|
39
|
+
- rails/init.rb
|
|
40
|
+
- README.rdoc
|
|
41
|
+
has_rdoc: true
|
|
42
|
+
homepage: http://github.com/jakewendt/ruby_extension
|
|
43
|
+
licenses: []
|
|
44
|
+
|
|
45
|
+
post_install_message:
|
|
46
|
+
rdoc_options: []
|
|
47
|
+
|
|
48
|
+
require_paths:
|
|
49
|
+
- lib
|
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
51
|
+
none: false
|
|
52
|
+
requirements:
|
|
53
|
+
- - ">="
|
|
54
|
+
- !ruby/object:Gem::Version
|
|
55
|
+
hash: 3
|
|
56
|
+
segments:
|
|
57
|
+
- 0
|
|
58
|
+
version: "0"
|
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
60
|
+
none: false
|
|
61
|
+
requirements:
|
|
62
|
+
- - ">="
|
|
63
|
+
- !ruby/object:Gem::Version
|
|
64
|
+
hash: 3
|
|
65
|
+
segments:
|
|
66
|
+
- 0
|
|
67
|
+
version: "0"
|
|
68
|
+
requirements: []
|
|
69
|
+
|
|
70
|
+
rubyforge_project:
|
|
71
|
+
rubygems_version: 1.6.2
|
|
72
|
+
signing_key:
|
|
73
|
+
specification_version: 3
|
|
74
|
+
summary: one-line summary of your gem
|
|
75
|
+
test_files: []
|
|
76
|
+
|