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
data/lib/mpatch/object.rb
CHANGED
@@ -1,268 +1,283 @@
|
|
1
|
-
|
1
|
+
module MPatch
|
2
|
+
module Object
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
def boolean?
|
5
|
+
!!self == self
|
6
|
+
end
|
7
|
+
|
8
|
+
def true?
|
9
|
+
self == true
|
10
|
+
end
|
11
|
+
|
12
|
+
def false?
|
13
|
+
self == false
|
14
|
+
end
|
15
|
+
|
16
|
+
# basic validations for those who fear the DUCK!
|
17
|
+
def must_be class_name
|
18
|
+
if class_name.class == ::Class
|
19
|
+
begin
|
20
|
+
if self.class != class_name
|
21
|
+
raise ::ArgumentError, "invalid parameter given: #{self}"
|
22
|
+
end
|
9
23
|
end
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
24
|
+
else
|
25
|
+
begin
|
26
|
+
if self != class_name
|
27
|
+
raise ::ArgumentError, "invalid parameter given: #{self}"
|
28
|
+
end
|
15
29
|
end
|
16
30
|
end
|
31
|
+
return self
|
17
32
|
end
|
18
|
-
return self
|
19
|
-
end unless method_defined? :must_be
|
20
33
|
|
21
|
-
|
22
|
-
|
34
|
+
# The hidden singleton lurks behind everyone
|
35
|
+
def metaclass; class << self; self; end; end
|
23
36
|
|
24
|
-
|
25
|
-
|
37
|
+
# extend the metaclass with an instance eval
|
38
|
+
def meta_eval &blk; metaclass.instance_eval &blk; end
|
26
39
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
40
|
+
# Adds methods to a metaclass
|
41
|
+
def meta_def name, &blk
|
42
|
+
meta_eval { define_method name, &blk }
|
43
|
+
end
|
31
44
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
45
|
+
# Defines an instance method within a class
|
46
|
+
def class_def name, &blk
|
47
|
+
class_eval { define_method name, &blk }
|
48
|
+
end
|
36
49
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
50
|
+
# constantize object
|
51
|
+
def constantize
|
52
|
+
camel_cased_word= self.to_s
|
53
|
+
names = camel_cased_word.split('::')
|
54
|
+
names.shift if names.empty? || names.first.empty?
|
42
55
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
# find and replace object in object
|
51
|
-
def find_and_replace(input,*params)
|
52
|
-
params=Hash[*params]
|
53
|
-
# some default values
|
54
|
-
begin
|
55
|
-
#None!
|
56
|
-
end
|
57
|
-
# Do the find and replace
|
58
|
-
begin
|
56
|
+
constant = self.class
|
57
|
+
names.each do |name|
|
58
|
+
constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
|
59
|
+
end
|
60
|
+
constant
|
61
|
+
end unless method_defined? :constantize
|
59
62
|
|
60
|
-
|
61
|
-
|
63
|
+
# find and replace object in object
|
64
|
+
def find_and_replace(input,*params)
|
65
|
+
params=Hash[*params]
|
66
|
+
# some default values
|
67
|
+
begin
|
68
|
+
#None!
|
69
|
+
end
|
70
|
+
# Do the find and replace
|
71
|
+
begin
|
72
|
+
|
73
|
+
if input.class <= ::Array
|
74
|
+
input.count.times do |counter|
|
75
|
+
params.each do |key,value|
|
76
|
+
input[counter]= input[counter].gsub(key,value)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
elsif input.class <= ::String
|
62
80
|
params.each do |key,value|
|
63
|
-
input
|
81
|
+
input= input.gsub(key,value)
|
82
|
+
end
|
83
|
+
elsif input.class <= ::Integer
|
84
|
+
params.each do |key,value|
|
85
|
+
input= input.to_s.gsub(key,value).to_i
|
86
|
+
end
|
87
|
+
elsif input.class <= ::Float
|
88
|
+
params.each do |key,value|
|
89
|
+
input= input.to_s.gsub(key,value).to_f
|
64
90
|
end
|
65
91
|
end
|
66
|
-
elsif input.class == String
|
67
|
-
params.each do |key,value|
|
68
|
-
input= input.gsub(key,value)
|
69
|
-
end
|
70
|
-
elsif input.class == Integer
|
71
|
-
params.each do |key,value|
|
72
|
-
input= input.to_s.gsub(key,value).to_i
|
73
|
-
end
|
74
|
-
elsif input.class == Float
|
75
|
-
params.each do |key,value|
|
76
|
-
input= input.to_s.gsub(key,value).to_f
|
77
|
-
end
|
78
|
-
end
|
79
92
|
|
80
|
-
|
81
|
-
|
93
|
+
# return value
|
94
|
+
return input
|
95
|
+
end
|
82
96
|
end
|
83
|
-
end
|
84
97
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
+
# each for any object
|
99
|
+
def each_universal(&block)
|
100
|
+
case self.class.to_s.downcase
|
101
|
+
when "hash"
|
102
|
+
self.each do |key,value|
|
103
|
+
block.call(key,value)
|
104
|
+
end
|
105
|
+
when "array"
|
106
|
+
self.each do |one_element|
|
107
|
+
block.call(self.index(one_element),one_element)
|
108
|
+
end
|
109
|
+
else
|
110
|
+
block.call nil,self
|
111
|
+
end
|
98
112
|
end
|
99
|
-
end
|
100
113
|
|
101
|
-
|
102
|
-
|
114
|
+
# map an object => experiment
|
115
|
+
def map_object(symbol_key="$type")
|
103
116
|
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
117
|
+
stage = self
|
118
|
+
do_later = ::Hash.new
|
119
|
+
samples = ::Hash.new
|
120
|
+
relations = ::Hash.new
|
121
|
+
main_object = nil
|
109
122
|
|
110
|
-
|
123
|
+
loop do
|
111
124
|
|
112
|
-
|
113
|
-
|
125
|
+
# processing
|
126
|
+
begin
|
114
127
|
|
115
|
-
|
116
|
-
|
117
|
-
|
128
|
+
tmp_key = ::String.new
|
129
|
+
tmp_hash= ::Hash.new
|
130
|
+
stage.each_universal do |key,value|
|
118
131
|
|
119
|
-
|
132
|
+
if value.class.to_s.downcase == "string"
|
120
133
|
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
134
|
+
if key== symbol_key
|
135
|
+
tmp_key= value
|
136
|
+
main_object ||= value
|
137
|
+
else
|
138
|
+
tmp_hash[key]= value
|
139
|
+
end
|
127
140
|
|
128
|
-
|
141
|
+
else
|
129
142
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
143
|
+
value.each_universal do |key_sub,value_sub|
|
144
|
+
if key_sub == symbol_key && tmp_key != ::String.new
|
145
|
+
child_property = ::Hash.new
|
146
|
+
child_property['type']= value_sub
|
147
|
+
child_property['class']= value.class.to_s
|
148
|
+
relations[tmp_key]= child_property
|
149
|
+
end
|
150
|
+
do_later[key_sub]= value_sub
|
136
151
|
end
|
137
|
-
|
152
|
+
|
138
153
|
end
|
154
|
+
end
|
139
155
|
|
156
|
+
if tmp_key != ::String.new && tmp_hash != ::Hash.new
|
157
|
+
samples[tmp_key]=tmp_hash
|
140
158
|
end
|
159
|
+
|
141
160
|
end
|
142
161
|
|
143
|
-
|
144
|
-
|
162
|
+
# finish
|
163
|
+
begin
|
164
|
+
break if do_later == ::Hash.new
|
165
|
+
stage= do_later
|
166
|
+
do_later = ::Hash.new
|
145
167
|
end
|
146
168
|
|
147
169
|
end
|
148
170
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
do_later = Hash.new
|
154
|
-
end
|
171
|
+
return {:samples => samples,
|
172
|
+
:relations => relations,
|
173
|
+
:main => main_object
|
174
|
+
}
|
155
175
|
|
156
176
|
end
|
157
177
|
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
178
|
+
# is class?
|
179
|
+
def class?
|
180
|
+
self.class == ::Class
|
181
|
+
end
|
162
182
|
|
163
|
-
|
183
|
+
# convert class instance instance variables into a hash object
|
184
|
+
def to_hash
|
164
185
|
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
end
|
186
|
+
unless self.class.class <= ::Class
|
187
|
+
raise ::NoMethodError, "undefined method `to_hash' for #{self.inspect}"
|
188
|
+
end
|
169
189
|
|
170
|
-
|
171
|
-
def convert_to_hash
|
190
|
+
tmp_hash= ::Hash.new()
|
172
191
|
|
173
|
-
|
174
|
-
|
175
|
-
|
192
|
+
self.instance_variables.each do|var|
|
193
|
+
tmp_hash[var.to_s.delete("@")] = self.instance_variable_get(var)
|
194
|
+
end
|
176
195
|
|
177
|
-
|
178
|
-
self.instance_variables.each do|var|
|
179
|
-
tmp_hash[var.to_s.delete("@")] = self.instance_variable_get(var)
|
180
|
-
end
|
196
|
+
return tmp_hash
|
181
197
|
|
182
|
-
|
198
|
+
end
|
183
199
|
|
184
|
-
|
200
|
+
# this will check that the class is
|
201
|
+
# defined or not in the runtime memory
|
202
|
+
def class_exists?
|
203
|
+
klass = ::Module.const_get(self)
|
204
|
+
return klass.is_a?(::Class)
|
205
|
+
rescue ::NameError
|
206
|
+
return false
|
207
|
+
end
|
185
208
|
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
209
|
+
# This will convert a symbol or string and format to be a valid
|
210
|
+
# constant name and create from it a class with instance attribute accessors
|
211
|
+
# Best use is when you get raw data in string from external source
|
212
|
+
# and you want make them class objects
|
213
|
+
#
|
214
|
+
# :hello_world.to_class(:test)
|
215
|
+
# HelloWorld.to_class(:sup)
|
216
|
+
# hw_var = HelloWorld.new
|
217
|
+
# hw_var.sup = "Fine thanks!"
|
218
|
+
# hw_var.test = 5
|
219
|
+
#
|
220
|
+
# puts hw_var.test
|
221
|
+
#
|
222
|
+
# #> produce 5 :Integer
|
223
|
+
#
|
224
|
+
# you can also use this formats
|
225
|
+
# :HelloWorld , "hello.world",
|
226
|
+
# "hello/world", "Hello::World",
|
227
|
+
# "hello:world"...
|
228
|
+
def to_class(*attributes)
|
229
|
+
|
230
|
+
unless self.class <= ::Symbol || self.class <= ::String || self.class <= ::Class
|
231
|
+
raise ::ArgumentError, "object must be symbol or string to make able build class to it"
|
232
|
+
end
|
194
233
|
|
195
|
-
|
196
|
-
# constant name and create from it a class with instance attribute accessors
|
197
|
-
# Best use is when you get raw data in string from external source
|
198
|
-
# and you want make them class objects
|
199
|
-
#
|
200
|
-
# :hello_world.to_class(:test)
|
201
|
-
# HelloWorld.to_class(:sup)
|
202
|
-
# hw_var = HelloWorld.new
|
203
|
-
# hw_var.sup = "Fine thanks!"
|
204
|
-
# hw_var.test = 5
|
205
|
-
#
|
206
|
-
# puts hw_var.test
|
207
|
-
#
|
208
|
-
# #> produce 5 :Integer
|
209
|
-
#
|
210
|
-
# you can also use this formats
|
211
|
-
# :HelloWorld , "hello.world",
|
212
|
-
# "hello/world", "Hello::World",
|
213
|
-
# "hello:world"...
|
214
|
-
def to_class(*attributes)
|
215
|
-
|
216
|
-
unless self.class == Symbol || self.class == String || self.class == Class
|
217
|
-
raise ArgumentError, "object must be symbol or string to make able build class to it"
|
218
|
-
end
|
234
|
+
class_name= self.to_s
|
219
235
|
|
220
|
-
|
236
|
+
unless self.class <= ::Class
|
221
237
|
|
222
|
-
|
238
|
+
class_name= class_name[0].upcase+class_name[1..class_name.length]
|
239
|
+
%w[ _ . : / ].each do |one_sym|
|
223
240
|
|
224
|
-
|
225
|
-
|
241
|
+
loop do
|
242
|
+
index_nmb= class_name.index(one_sym)
|
243
|
+
break if index_nmb.nil?
|
244
|
+
class_name[index_nmb..index_nmb+1]= class_name[index_nmb+1].upcase
|
245
|
+
end
|
226
246
|
|
227
|
-
loop do
|
228
|
-
index_nmb= class_name.index(one_sym)
|
229
|
-
break if index_nmb.nil?
|
230
|
-
class_name[index_nmb..index_nmb+1]= class_name[index_nmb+1].upcase
|
231
247
|
end
|
232
248
|
|
233
249
|
end
|
234
250
|
|
235
|
-
|
236
|
-
|
237
|
-
create_attribute = Proc.new do |*args|
|
251
|
+
create_attribute = ::Proc.new do |*args|
|
238
252
|
|
239
|
-
|
253
|
+
end
|
240
254
|
|
241
|
-
|
255
|
+
unless class_name.class_exists?
|
242
256
|
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
257
|
+
self.class.const_set(
|
258
|
+
class_name,
|
259
|
+
::Class.new
|
260
|
+
)
|
247
261
|
|
248
|
-
|
262
|
+
end
|
249
263
|
|
250
264
|
|
251
|
-
|
252
|
-
|
253
|
-
|
265
|
+
class_name.constantize.class_eval do
|
266
|
+
attributes.each do |one_attribute|
|
267
|
+
attr_accessor one_attribute.to_s.to_sym
|
268
|
+
end
|
254
269
|
end
|
255
|
-
end
|
256
270
|
|
257
271
|
|
258
272
|
|
259
|
-
|
273
|
+
return true
|
260
274
|
|
261
|
-
|
275
|
+
end
|
262
276
|
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
277
|
+
alias :create_attributes :to_class
|
278
|
+
alias :map_sample :map_object
|
279
|
+
alias :each_univ :each_universal
|
280
|
+
alias :fnr :find_and_replace
|
267
281
|
|
282
|
+
end
|
268
283
|
end
|
data/lib/mpatch/proc.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
-
|
1
|
+
module MPatch
|
2
|
+
module Proc
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
4
|
+
# sugar syntax for proc * operator
|
5
|
+
# a = ->(x){x+1}
|
6
|
+
# b = ->(x){x*10}
|
7
|
+
# c = b*a
|
8
|
+
# c.call(1) #=> 20
|
9
|
+
def *(other)
|
10
|
+
self.class.new { |*args| self[*other[*args]] }
|
11
|
+
end
|
11
12
|
|
13
|
+
end
|
12
14
|
end
|
data/lib/mpatch/process.rb
CHANGED
@@ -1,28 +1,17 @@
|
|
1
|
-
module
|
1
|
+
module MPatch
|
2
|
+
module Process
|
2
3
|
|
3
|
-
|
4
|
-
|
4
|
+
# return a string obj that include the memory usage info
|
5
|
+
def self.memory_usage
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
end
|
7
|
+
begin
|
8
|
+
return `pmap #{self.pid}`.lines.to_a(
|
9
|
+
).last.chomp.scan(/ *\w* *(\w+)/)[0][0]
|
10
|
+
rescue ::NoMethodError
|
11
|
+
return nil
|
12
|
+
end
|
14
13
|
|
14
|
+
end
|
15
15
|
|
16
|
-
def self.daemonize
|
17
|
-
File.create Application.pid,'a+'
|
18
|
-
File.create Application.log,'a+'
|
19
|
-
File.create Application.daemon_stderr,'a+'
|
20
|
-
Daemon.start fork,
|
21
|
-
Application.pid,
|
22
|
-
Application.log,
|
23
|
-
Application.daemon_stderr
|
24
|
-
end
|
25
|
-
def self.stop
|
26
|
-
Daemon.stop
|
27
16
|
end
|
28
17
|
end
|
data/lib/mpatch/random.rb
CHANGED
@@ -1,37 +1,46 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
amount_container= Array.new
|
4
|
-
amount.times do
|
5
|
-
mrg= String.new
|
6
|
-
mrg= (0...length).map{ ('a'..'z').to_a[rand(26)] }.join
|
7
|
-
amount_container.push mrg
|
8
|
-
end
|
9
|
-
return amount_container.join(hyphen)
|
10
|
-
end
|
11
|
-
def self.integer(length= 3)
|
12
|
-
Random.rand(length)
|
13
|
-
end
|
14
|
-
def self.boolean
|
15
|
-
rand(2) == 1
|
16
|
-
end
|
17
|
-
def self.time from = Time.at(1114924812), to = Time.now
|
18
|
-
rand(from..to)
|
19
|
-
end
|
20
|
-
def self.date from = Time.at(1114924812), to = Time.now
|
21
|
-
rand(from..to).to_date
|
22
|
-
end
|
23
|
-
def self.datetime from = Time.at(1114924812), to = Time.now
|
24
|
-
rand(from..to).to_datetime
|
25
|
-
end
|
26
|
-
end
|
1
|
+
module MPatch
|
2
|
+
module Random
|
27
3
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
4
|
+
def string(length= 7,amount=1,hyphen= " ")
|
5
|
+
amount_container= []
|
6
|
+
amount.times do
|
7
|
+
mrg= ""
|
8
|
+
mrg= (0...length).map{ ('a'..'z').to_a[rand(26)] }.join
|
9
|
+
amount_container.push mrg
|
34
10
|
end
|
11
|
+
return amount_container.join(hyphen)
|
12
|
+
end
|
13
|
+
|
14
|
+
def integer(length= 3)
|
15
|
+
self.class.rand(length)
|
35
16
|
end
|
17
|
+
|
18
|
+
def boolean
|
19
|
+
self.class.rand(2) == 1
|
20
|
+
end
|
21
|
+
|
22
|
+
def time from = Time.at(1114924812), to = Time.now
|
23
|
+
self.class.rand(from..to)
|
24
|
+
end
|
25
|
+
|
26
|
+
def date from = Time.at(1114924812), to = Time.now
|
27
|
+
self.class.rand(from..to).to_date
|
28
|
+
end
|
29
|
+
|
30
|
+
def datetime from = Time.at(1114924812), to = Time.now
|
31
|
+
self.class.rand(from..to).to_datetime
|
32
|
+
end
|
33
|
+
|
36
34
|
end
|
37
35
|
end
|
36
|
+
|
37
|
+
## alias in Random from RND
|
38
|
+
#begin
|
39
|
+
# (RND.singleton_methods-Object.instance_methods).each do |one_method_sym|
|
40
|
+
# Random.class_eval do
|
41
|
+
# define_singleton_method one_method_sym do |*args|
|
42
|
+
# RND.__send__(one_method_sym,*args)
|
43
|
+
# end
|
44
|
+
# end
|
45
|
+
# end
|
46
|
+
#end
|