mpatch 1.3.0 → 2.0.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.
- checksums.yaml +5 -13
- data/README.md +32 -0
- data/VERSION +1 -1
- data/lib/mpatch/array.rb +119 -117
- data/lib/mpatch/class.rb +68 -66
- data/lib/mpatch/file.rb +35 -37
- data/lib/mpatch/hash.rb +93 -91
- data/lib/mpatch/integer.rb +10 -8
- data/lib/mpatch/module.rb +13 -11
- data/lib/mpatch/object.rb +211 -196
- data/lib/mpatch/proc.rb +11 -9
- data/lib/mpatch/process.rb +11 -22
- data/lib/mpatch/random.rb +41 -32
- data/lib/mpatch/string.rb +65 -63
- data/lib/mpatch/yml.rb +11 -7
- data/lib/mpatch.rb +33 -0
- data/mpatch.gemspec +0 -2
- metadata +6 -24
- data/lib/mpatch/boolean.rb +0 -15
- data/lib/mpatch/exception.rb +0 -0
- data/lib/mpatch/kernel.rb +0 -10
- data/lib/mpatch/str2duck.rb +0 -1
checksums.yaml
CHANGED
@@ -1,15 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
data.tar.gz: !binary |-
|
6
|
-
YjliZTYxNTU5OTBjODZmMThiYTdkZDExMDU5OTk1NmNiOWYwNDI0YQ==
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e55cefa7d1160f216bcc28ef4b4f2bc0528e8313
|
4
|
+
data.tar.gz: 99d6f21dcb6f464b75350de5fc10cca80965a92a
|
7
5
|
SHA512:
|
8
|
-
metadata.gz:
|
9
|
-
|
10
|
-
MDAzNzIxZmVhZjU3ZGY4OGYzMDU4Y2Q5NGQ1OGZhM2I5YjFhM2Q5ZGMwMzll
|
11
|
-
YTExN2FlMzI5MWNiOTRlOGRkZGNmMmFiYWZlMTc0YmM3ZTllMTg=
|
12
|
-
data.tar.gz: !binary |-
|
13
|
-
NTNmY2VkYzY1YWM0ZDJmZGRjYzgzZjNjMjU1ODZkYjdjMWIxNGUzMzI2ZTUy
|
14
|
-
OGEyZWVkN2NkMWFkNmEwY2ZjZGIwYmMzMDZkZTkzNjYwNDZhMGMwYmYxM2U3
|
15
|
-
Njg5NjJmMTc5ZDlkMmUwMzc4YjVlNDg5YWQ5NTk2NjdmYzI0MzA=
|
6
|
+
metadata.gz: f889918a12d38cf8429f0017ed80872e35146d814864f6ead917759630593fec4abfb92471ca899acd3dd6b46e4e8983052cb2d604e6957e68261901ea8c8fcf
|
7
|
+
data.tar.gz: 7193ffa649afd9a1349b14febde2190945bc284bc00fb1bca190a41db5dd6b88149157c5b7cf6010571bf0234697330a6246d52ce670a6728c186186c424f667
|
data/README.md
CHANGED
@@ -2,3 +2,35 @@ mpatch
|
|
2
2
|
======
|
3
3
|
|
4
4
|
Monkey patch collection for advance helper functions
|
5
|
+
|
6
|
+
This project aim to give flexible methods to the developer by giving new inheritance to the base classes
|
7
|
+
|
8
|
+
for example
|
9
|
+
|
10
|
+
`ruby
|
11
|
+
|
12
|
+
require 'mpatch'
|
13
|
+
|
14
|
+
class Test
|
15
|
+
|
16
|
+
def initialize
|
17
|
+
|
18
|
+
@hello= "world"
|
19
|
+
@sup= "nothing"
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
puts Test.new.to_hash
|
26
|
+
#> {"hello" => "world", "sup" => "nothing"}
|
27
|
+
`
|
28
|
+
|
29
|
+
But there is a lot of method, for example for modules modules / subbmodules call that retunr modules under that namespace.
|
30
|
+
Lot of metaprogrammer stuff there too :)
|
31
|
+
|
32
|
+
please do enjoy :)
|
33
|
+
|
34
|
+
ps.:
|
35
|
+
|
36
|
+
* you can get each of the modules for only include to your custom class
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
2.0.0
|
data/lib/mpatch/array.rb
CHANGED
@@ -1,151 +1,153 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
1
|
+
module MPatch
|
2
|
+
module Array
|
3
|
+
|
4
|
+
# remove arguments or array of
|
5
|
+
# parameters from the main array
|
6
|
+
def trim(*args)
|
7
|
+
|
8
|
+
args.dup.each do |one_element|
|
9
|
+
if one_element.class <= ::Array
|
10
|
+
args.delete_at(args.index(one_element))
|
11
|
+
args= args+one_element
|
12
|
+
end
|
11
13
|
end
|
12
|
-
end
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
15
|
+
delete_array= self.class.new
|
16
|
+
args.each do |one_element|
|
17
|
+
index= self.index(one_element)
|
18
|
+
unless index.nil?
|
19
|
+
delete_array.push index
|
20
|
+
self.delete_at(index)
|
21
|
+
end
|
20
22
|
end
|
21
|
-
end
|
22
23
|
|
23
|
-
|
24
|
+
return self
|
24
25
|
|
25
|
-
|
26
|
-
|
27
|
-
# return index of the target element
|
28
|
-
def index_of(target_element)
|
29
|
-
array = self
|
30
|
-
hash = Hash[array.map.with_index.to_a]
|
31
|
-
return hash[target_element]
|
32
|
-
end
|
33
|
-
|
34
|
-
# remove n. element from the end
|
35
|
-
# and return a new object
|
36
|
-
def pinch n=1
|
37
|
-
return self[0..(self.count-(n+1))]
|
38
|
-
end
|
26
|
+
end
|
39
27
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
28
|
+
# return index of the target element
|
29
|
+
def index_of(target_element)
|
30
|
+
array = self
|
31
|
+
hash = ::Hash[array.map.with_index.to_a]
|
32
|
+
return hash[target_element]
|
45
33
|
end
|
46
|
-
return self
|
47
|
-
end
|
48
34
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
end
|
35
|
+
# remove n. element from the end
|
36
|
+
# and return a new object
|
37
|
+
def pinch n=1
|
38
|
+
return self[0..(self.count-(n+1))]
|
39
|
+
end
|
55
40
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
if self.include? element
|
62
|
-
return true
|
41
|
+
# remove n. element from the end
|
42
|
+
# and return the original object
|
43
|
+
def pinch! n=1
|
44
|
+
n.times do
|
45
|
+
self.pop
|
63
46
|
end
|
47
|
+
return self
|
64
48
|
end
|
65
|
-
return false
|
66
|
-
end
|
67
49
|
|
68
|
-
|
69
|
-
|
50
|
+
# return boolean by other array
|
51
|
+
# all element included or
|
52
|
+
# not in the target array
|
53
|
+
def contain?(oth_array)#anothere array
|
54
|
+
(oth_array & self) == oth_array
|
55
|
+
end
|
70
56
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
57
|
+
# return boolean by other array
|
58
|
+
# if any element included from
|
59
|
+
# the oth_array, return a true
|
60
|
+
def contain_any_of?(oth_array)
|
61
|
+
oth_array.each do |element|
|
62
|
+
if self.include? element
|
63
|
+
return true
|
64
|
+
end
|
65
|
+
end
|
66
|
+
return false
|
78
67
|
end
|
79
|
-
result
|
80
|
-
end
|
81
68
|
|
82
|
-
|
69
|
+
alias :contains_any_of? :contain_any_of?
|
70
|
+
alias :has_any_of? :contain_any_of?
|
83
71
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
return true
|
72
|
+
# do safe transpose
|
73
|
+
def safe_transpose
|
74
|
+
result = []
|
75
|
+
max_size = self.max { |a,b| a.size <=> b.size }.size
|
76
|
+
max_size.times do |i|
|
77
|
+
result[i] = self.class.new(self.first.size)
|
78
|
+
self.each_with_index { |r,j| result[i][j] = r[i] }
|
79
|
+
end
|
80
|
+
result
|
94
81
|
end
|
95
|
-
return false
|
96
|
-
end
|
97
|
-
|
98
|
-
alias :contains_element_of_class? :contain_element_of_class?
|
99
|
-
alias :has_element_of_class? :contain_element_of_class?
|
100
82
|
|
101
|
-
|
102
|
-
# *args => [:opts,:args]
|
103
|
-
def params_separation
|
83
|
+
alias :contains? :contain?
|
104
84
|
|
105
|
-
|
106
|
-
|
107
|
-
|
85
|
+
# return boolean
|
86
|
+
# if any element class is equal to th given class
|
87
|
+
# return a true , else false
|
88
|
+
def contain_element_of_class?(class_name)
|
89
|
+
target_array= self.map{|e| e.class }.uniq
|
90
|
+
if class_name.class != ::Class
|
91
|
+
raise ::ArgumentError, "Argument must be a Class!"
|
108
92
|
end
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
93
|
+
if target_array.include? class_name
|
94
|
+
return true
|
95
|
+
end
|
96
|
+
return false
|
97
|
+
end
|
113
98
|
|
114
|
-
|
99
|
+
alias :contains_element_of_class? :contain_element_of_class?
|
100
|
+
alias :has_element_of_class? :contain_element_of_class?
|
115
101
|
|
116
|
-
|
102
|
+
# generate params structure from array
|
103
|
+
# *args => [:opts,:args]
|
104
|
+
def params_separation
|
117
105
|
|
118
|
-
|
119
|
-
|
106
|
+
options= self.map { |element|
|
107
|
+
if element.class == ::Hash
|
108
|
+
element
|
109
|
+
end
|
110
|
+
}.uniq - [ nil ]
|
111
|
+
#options.each{|e| self.delete(e) }
|
112
|
+
arguments= self.dup - options
|
113
|
+
options= ::Hash[*options]
|
120
114
|
|
121
|
-
|
122
|
-
# return_array
|
123
|
-
def extract_class! class_name
|
115
|
+
return [options,arguments]
|
124
116
|
|
125
|
-
unless class_name.class <= Class
|
126
|
-
raise ArgumentError, "parameter must be a class name"
|
127
117
|
end
|
128
118
|
|
129
|
-
|
130
|
-
|
131
|
-
|
119
|
+
alias :separate_params :params_separation
|
120
|
+
alias :process_params :params_separation
|
121
|
+
|
122
|
+
# generate params structure from array
|
123
|
+
# return_array
|
124
|
+
def extract_class! class_name
|
125
|
+
|
126
|
+
unless class_name.class <= ::Class
|
127
|
+
raise ::ArgumentError, "parameter must be a class name"
|
132
128
|
end
|
133
|
-
}.uniq - [ nil ]
|
134
|
-
return_value.each{|e| self.delete(e) }
|
135
129
|
|
136
|
-
|
130
|
+
return_value= self.map { |element|
|
131
|
+
if element.class <= class_name
|
132
|
+
element
|
133
|
+
end
|
134
|
+
}.uniq - [ nil ]
|
135
|
+
return_value.each{|e| self.delete(e) }
|
137
136
|
|
138
|
-
|
137
|
+
return_value ||= self.class.new
|
139
138
|
|
140
|
-
|
141
|
-
alias :cut_class! :extract_class!
|
139
|
+
return return_value
|
142
140
|
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
141
|
+
end
|
142
|
+
alias :cut_class! :extract_class!
|
143
|
+
|
144
|
+
# generate params structure from array
|
145
|
+
# *args - options {}
|
146
|
+
def extract_options!
|
147
|
+
options= self.extract_class! ::Hash
|
148
|
+
return ::Hash[*options]
|
149
|
+
end
|
150
|
+
alias :extract_hash! :extract_options!
|
150
151
|
|
152
|
+
end
|
151
153
|
end
|
data/lib/mpatch/class.rb
CHANGED
@@ -1,92 +1,94 @@
|
|
1
|
-
|
1
|
+
module MPatch
|
2
|
+
class Class
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
# get singleton methods to target class without super class methods
|
5
|
+
def class_methods
|
6
|
+
self.methods - ::Object.methods
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
# bind a singleton method to a class object
|
10
|
+
def create_class_method(method,&block)
|
11
|
+
self.class_eval do
|
12
|
+
define_singleton_method method do |*args|
|
13
|
+
block.call *args
|
14
|
+
end
|
13
15
|
end
|
14
16
|
end
|
15
|
-
end
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
# create an instance method
|
19
|
+
def create_instance_method(method,&block)
|
20
|
+
self.class_eval do
|
21
|
+
define_method method do |*args|
|
22
|
+
block.call *args
|
23
|
+
end
|
22
24
|
end
|
23
25
|
end
|
24
|
-
end
|
25
26
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
27
|
+
# Iterates over all subclasses (direct and indirect)
|
28
|
+
def each_subclass
|
29
|
+
::ObjectSpace.each_object(::Class) { | candidate |
|
30
|
+
begin
|
31
|
+
yield candidate if candidate < self
|
32
|
+
rescue ::ArgumentError
|
33
|
+
# comparison of Class with Class failed (ArgumentError)
|
34
|
+
end
|
35
|
+
}
|
36
|
+
end
|
36
37
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
38
|
+
# Returns an Array of subclasses (direct and indirect)
|
39
|
+
def subclasses_all
|
40
|
+
ret = []
|
41
|
+
each_subclass {|c| ret.push c}
|
42
|
+
ret
|
43
|
+
end
|
43
44
|
|
44
|
-
|
45
|
+
alias :all_subclasses :subclasses_all
|
45
46
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
47
|
+
# Returns an Array of direct subclasses
|
48
|
+
def subclasses
|
49
|
+
ret = []
|
50
|
+
each_subclass {|c| ret.push(c) if c.superclass == self }
|
51
|
+
ret
|
52
|
+
end
|
53
|
+
alias :subclass :subclasses
|
53
54
|
|
54
|
-
|
55
|
-
|
55
|
+
# create singleton attribute
|
56
|
+
def class_attr_accessor(name)
|
56
57
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
58
|
+
### GET
|
59
|
+
begin
|
60
|
+
define_method name do
|
61
|
+
class_variable_get "@@#{name}"
|
62
|
+
end
|
61
63
|
end
|
62
|
-
end
|
63
64
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
65
|
+
### SET
|
66
|
+
begin
|
67
|
+
define_method "#{name}=" do |new_val|
|
68
|
+
class_variable_set "@@#{name}", new_val
|
69
|
+
end
|
68
70
|
end
|
69
|
-
end
|
70
71
|
|
71
|
-
|
72
|
+
end
|
72
73
|
|
73
|
-
|
74
|
-
|
74
|
+
# create class instance attribute
|
75
|
+
def instance_attr_accessor(name)
|
75
76
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
77
|
+
### GET
|
78
|
+
begin
|
79
|
+
define_method name do
|
80
|
+
instance_variable_get "@#{name}"
|
81
|
+
end
|
80
82
|
end
|
81
|
-
end
|
82
83
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
84
|
+
### SET
|
85
|
+
begin
|
86
|
+
define_method "#{name}=" do |new_val|
|
87
|
+
instance_variable_set "@#{name}", new_val
|
88
|
+
end
|
87
89
|
end
|
90
|
+
|
88
91
|
end
|
89
92
|
|
90
93
|
end
|
91
|
-
|
92
94
|
end
|
data/lib/mpatch/file.rb
CHANGED
@@ -1,33 +1,33 @@
|
|
1
|
-
|
1
|
+
module MPatch
|
2
|
+
module File
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
begin
|
4
|
+
# create a file, if not exsist create file, and dir if needed
|
5
|
+
def self.create(route_name ,filemod="w",string_data= ::String.new )
|
6
6
|
|
7
7
|
#file_name generate
|
8
|
-
if !route_name.to_s.split(
|
9
|
-
file_name = route_name.to_s.split(
|
8
|
+
if !route_name.to_s.split(self.class::SEPARATOR).last.nil? || route_name.to_s.split(self.class::SEPARATOR).last != ''
|
9
|
+
file_name = route_name.to_s.split(self.class::SEPARATOR).last
|
10
10
|
else
|
11
11
|
file_name = nil?
|
12
12
|
end
|
13
13
|
|
14
14
|
#path_way
|
15
15
|
begin
|
16
|
-
raise ArgumentError, "missing route_name: #{route_name}" if route_name.nil?
|
17
|
-
path =
|
18
|
-
path = path - [
|
16
|
+
raise ::ArgumentError, "missing route_name: #{route_name}" if route_name.nil?
|
17
|
+
path = self.class.expand_path(route_name).to_s.split(self.class::SEPARATOR)
|
18
|
+
path = path - [self.class.expand_path(route_name).to_s.split(self.class::SEPARATOR).last]
|
19
19
|
path.shift
|
20
20
|
end
|
21
21
|
|
22
22
|
#job
|
23
23
|
begin
|
24
|
-
if
|
24
|
+
if !::Dir.exists?(self.class::SEPARATOR+path.join(self.class::SEPARATOR))
|
25
25
|
|
26
|
-
at_now =
|
26
|
+
at_now = self.class::SEPARATOR
|
27
27
|
path.each do |dir_to_be_checked|
|
28
28
|
|
29
|
-
at_now += "#{dir_to_be_checked+
|
30
|
-
Dir.mkdir(at_now) if
|
29
|
+
at_now += "#{dir_to_be_checked+self.class::SEPARATOR}"
|
30
|
+
::Dir.mkdir(at_now) if !::Dir.exists?(at_now)
|
31
31
|
|
32
32
|
end
|
33
33
|
end
|
@@ -35,37 +35,35 @@ class File
|
|
35
35
|
|
36
36
|
# write data
|
37
37
|
begin
|
38
|
-
full_path = "#{
|
39
|
-
if
|
40
|
-
|
38
|
+
full_path = "#{self.class::SEPARATOR+path.join(self.class::SEPARATOR)+self.class::SEPARATOR}#{file_name}"
|
39
|
+
if self.class.exist? full_path
|
40
|
+
self.class.open(full_path,filemod).write string_data
|
41
41
|
else
|
42
|
-
|
42
|
+
self.class.new(full_path,filemod).write string_data
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
-
rescue Exception => ex
|
47
|
-
puts ex
|
48
46
|
end
|
49
|
-
end
|
50
47
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
48
|
+
# start read the file object on each line
|
49
|
+
# optionable an integer value to start read line at
|
50
|
+
# compatible with mac (i am linux user, so not tested)
|
51
|
+
def each_line_from(start_at=1,&block)
|
52
|
+
unless [::Integer,Fixnum,Bignum].include?(start_at.class)
|
53
|
+
raise ::ArgumentError, "invalid line index"
|
54
|
+
end
|
55
|
+
begin
|
56
|
+
line_num= 1
|
57
|
+
text= self.read
|
58
|
+
text.gsub!(/\r\n?/, "\n")
|
59
|
+
text.each_line do |*line|
|
60
|
+
if line_num >= start_at
|
61
|
+
block.call #*line
|
62
|
+
end
|
63
|
+
line_num += 1
|
65
64
|
end
|
66
|
-
line_num += 1
|
67
65
|
end
|
68
66
|
end
|
69
|
-
end
|
70
67
|
|
71
|
-
end
|
68
|
+
end
|
69
|
+
end
|