mpatch 1.3.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/mpatch/object.rb CHANGED
@@ -1,268 +1,283 @@
1
- class Object
1
+ module MPatch
2
+ module Object
2
3
 
3
- # basic validations for those who fear the DUCK!
4
- def must_be class_name
5
- if class_name.class == Class
6
- begin
7
- if self.class != class_name
8
- raise ArgumentError, "invalid parameter given: #{self}"
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
- end
11
- else
12
- begin
13
- if self != class_name
14
- raise ArgumentError, "invalid parameter given: #{self}"
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
- # The hidden singleton lurks behind everyone
22
- def metaclass; class << self; self; end; end
34
+ # The hidden singleton lurks behind everyone
35
+ def metaclass; class << self; self; end; end
23
36
 
24
- # extend the metaclass with an instance eval
25
- def meta_eval &blk; metaclass.instance_eval &blk; end
37
+ # extend the metaclass with an instance eval
38
+ def meta_eval &blk; metaclass.instance_eval &blk; end
26
39
 
27
- # Adds methods to a metaclass
28
- def meta_def name, &blk
29
- meta_eval { define_method name, &blk }
30
- end
40
+ # Adds methods to a metaclass
41
+ def meta_def name, &blk
42
+ meta_eval { define_method name, &blk }
43
+ end
31
44
 
32
- # Defines an instance method within a class
33
- def class_def name, &blk
34
- class_eval { define_method name, &blk }
35
- end
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
- # constantize object
38
- def constantize
39
- camel_cased_word= self.to_s
40
- names = camel_cased_word.split('::')
41
- names.shift if names.empty? || names.first.empty?
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
- constant = Object
44
- names.each do |name|
45
- constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
46
- end
47
- constant
48
- end unless method_defined? :constantize
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
- if input.class == Array
61
- input.count.times do |counter|
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[counter]= input[counter].gsub(key,value)
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
- # return value
81
- return input
93
+ # return value
94
+ return input
95
+ end
82
96
  end
83
- end
84
97
 
85
- # each for any object
86
- def each_universal(&block)
87
- case self.class.to_s.downcase
88
- when "hash"
89
- self.each do |key,value|
90
- block.call(key,value)
91
- end
92
- when "array"
93
- self.each do |one_element|
94
- block.call(self.index(one_element),one_element)
95
- end
96
- else
97
- block.call nil,self
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
- # map an object => experiment
102
- def map_object(symbol_key="$type")
114
+ # map an object => experiment
115
+ def map_object(symbol_key="$type")
103
116
 
104
- stage = self
105
- do_later = Hash.new
106
- samples = Hash.new
107
- relations = Hash.new
108
- main_object = nil
117
+ stage = self
118
+ do_later = ::Hash.new
119
+ samples = ::Hash.new
120
+ relations = ::Hash.new
121
+ main_object = nil
109
122
 
110
- loop do
123
+ loop do
111
124
 
112
- # processing
113
- begin
125
+ # processing
126
+ begin
114
127
 
115
- tmp_key = String.new
116
- tmp_hash= Hash.new
117
- stage.each_universal do |key,value|
128
+ tmp_key = ::String.new
129
+ tmp_hash= ::Hash.new
130
+ stage.each_universal do |key,value|
118
131
 
119
- if value.class.to_s.downcase == "string"
132
+ if value.class.to_s.downcase == "string"
120
133
 
121
- if key== symbol_key
122
- tmp_key= value
123
- main_object ||= value
124
- else
125
- tmp_hash[key]= value
126
- end
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
- else
141
+ else
129
142
 
130
- value.each_universal do |key_sub,value_sub|
131
- if key_sub == symbol_key && tmp_key != String.new
132
- child_property = Hash.new
133
- child_property['type']= value_sub
134
- child_property['class']= value.class.to_s
135
- relations[tmp_key]= child_property
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
- do_later[key_sub]= value_sub
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
- if tmp_key != String.new && tmp_hash != Hash.new
144
- samples[tmp_key]=tmp_hash
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
- # finish
150
- begin
151
- break if do_later == Hash.new
152
- stage= do_later
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
- return {:samples => samples,
159
- :relations => relations,
160
- :main => main_object
161
- }
178
+ # is class?
179
+ def class?
180
+ self.class == ::Class
181
+ end
162
182
 
163
- end
183
+ # convert class instance instance variables into a hash object
184
+ def to_hash
164
185
 
165
- # is class?
166
- def class?
167
- self.class == Class
168
- end
186
+ unless self.class.class <= ::Class
187
+ raise ::NoMethodError, "undefined method `to_hash' for #{self.inspect}"
188
+ end
169
189
 
170
- # convert class instance instance variables into a hash object
171
- def convert_to_hash
190
+ tmp_hash= ::Hash.new()
172
191
 
173
- unless self.class.class == Class
174
- raise NoMethodError, "undefined method `to_hash' for #{self.inspect}"
175
- end
192
+ self.instance_variables.each do|var|
193
+ tmp_hash[var.to_s.delete("@")] = self.instance_variable_get(var)
194
+ end
176
195
 
177
- tmp_hash= Hash.new()
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
- return tmp_hash
198
+ end
183
199
 
184
- end
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
- # this will check that the class is
187
- # defined or not in the runtime memory
188
- def class_exists?
189
- klass = Module.const_get(self)
190
- return klass.is_a?(Class)
191
- rescue NameError
192
- return false
193
- end
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
- # This will convert a symbol or string and format to be a valid
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
- class_name= self.to_s
236
+ unless self.class <= ::Class
221
237
 
222
- unless self.class == Class
238
+ class_name= class_name[0].upcase+class_name[1..class_name.length]
239
+ %w[ _ . : / ].each do |one_sym|
223
240
 
224
- class_name= class_name[0].upcase+class_name[1..class_name.length]
225
- %w[ _ . : / ].each do |one_sym|
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
- end
236
-
237
- create_attribute = Proc.new do |*args|
251
+ create_attribute = ::Proc.new do |*args|
238
252
 
239
- end
253
+ end
240
254
 
241
- unless class_name.class_exists?
255
+ unless class_name.class_exists?
242
256
 
243
- Object.const_set(
244
- class_name,
245
- Class.new
246
- )
257
+ self.class.const_set(
258
+ class_name,
259
+ ::Class.new
260
+ )
247
261
 
248
- end
262
+ end
249
263
 
250
264
 
251
- class_name.constantize.class_eval do
252
- attributes.each do |one_attribute|
253
- attr_accessor one_attribute.to_s.to_sym
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
- return true
273
+ return true
260
274
 
261
- end
275
+ end
262
276
 
263
- alias :create_attributes :to_class
264
- alias :map_sample :map_object
265
- alias :each_univ :each_universal
266
- alias :fnr :find_and_replace
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
- class Proc
1
+ module MPatch
2
+ module Proc
2
3
 
3
- # sugar syntax for proc * operator
4
- # a = ->(x){x+1}
5
- # b = ->(x){x*10}
6
- # c = b*a
7
- # c.call(1) #=> 20
8
- def *(other)
9
- Proc.new { |*args| self[*other[*args]] }
10
- end unless method_defined? :*
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
@@ -1,28 +1,17 @@
1
- module Process
1
+ module MPatch
2
+ module Process
2
3
 
3
- # return a string obj that include the memory usage info
4
- def self.memory_usage
4
+ # return a string obj that include the memory usage info
5
+ def self.memory_usage
5
6
 
6
- begin
7
- return `pmap #{self.pid}`.lines.to_a(
8
- ).last.chomp.scan(/ *\w* *(\w+)/)[0][0]
9
- rescue NoMethodError
10
- return nil
11
- end
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
- class RND
2
- def self.string(length= 7,amount=1,hyphen= " ")
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
- # alias in Random from RND
29
- begin
30
- (RND.singleton_methods-Object.instance_methods).each do |one_method_sym|
31
- Random.class_eval do
32
- define_singleton_method one_method_sym do |*args|
33
- RND.__send__(one_method_sym,*args)
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