mpatch 2.3.0 → 2.4.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.
data/lib/mpatch/object.rb CHANGED
@@ -1,166 +1,175 @@
1
- module MPatch::Include
2
- module Object
1
+ module MPatch
3
2
 
4
- def boolean?
5
- !!self == self
6
- end
3
+ module Include
7
4
 
8
- def true?
9
- self == true
10
- end
5
+ module Object
11
6
 
12
- def false?
13
- self == false
14
- end
7
+ def boolean?
8
+ !!self == self
9
+ end
15
10
 
16
- def class?
17
- self.class == ::Class
18
- end
11
+ def true?
12
+ self == true
13
+ end
19
14
 
20
- # basic validations for those who fear the DUCK!
21
- def must_be class_name
22
- if class_name.class == ::Class
23
- begin
24
- if self.class != class_name
25
- raise ::ArgumentError, "invalid parameter given: #{self}"
15
+ def false?
16
+ self == false
17
+ end
18
+
19
+ def class?
20
+ self.class == ::Class
21
+ end
22
+
23
+ # basic validations for those who fear the DUCK!
24
+ def must_be class_name
25
+ if class_name.class == ::Class
26
+ begin
27
+ if self.class != class_name
28
+ raise ::ArgumentError, "invalid parameter given: #{self}"
29
+ end
26
30
  end
27
- end
28
- else
29
- begin
30
- if self != class_name
31
- raise ::ArgumentError, "invalid parameter given: #{self}"
31
+ else
32
+ begin
33
+ if self != class_name
34
+ raise ::ArgumentError, "invalid parameter given: #{self}"
35
+ end
32
36
  end
33
37
  end
38
+ return self
34
39
  end
35
- return self
36
- end
37
40
 
38
- # The hidden singleton lurks behind everyone
39
- def metaclass; class << self; self; end; end
41
+ # The hidden singleton lurks behind everyone
42
+ def metaclass; class << self; self; end; end
40
43
 
41
- # extend the metaclass with an instance eval
42
- def meta_eval &blk; metaclass.instance_eval &blk; end
44
+ # extend the metaclass with an instance eval
45
+ def meta_eval &blk; metaclass.instance_eval &blk; end
43
46
 
44
- # Adds methods to a metaclass
45
- def meta_def name, &blk
46
- meta_eval { define_method name, &blk }
47
- end
47
+ # Adds methods to a metaclass
48
+ def meta_def name, &blk
49
+ meta_eval { define_method name, &blk }
50
+ end
48
51
 
49
- # Defines an instance method within a class
50
- def class_def name, &blk
51
- class_eval { define_method name, &blk }
52
- end
52
+ # Defines an instance method within a class
53
+ def class_def name, &blk
54
+ class_eval { define_method name, &blk }
55
+ end
53
56
 
54
- # constantize object
55
- def constantize
57
+ # constantize object
58
+ def constantize
56
59
 
57
- camel_cased_word= self.to_s
58
- names = camel_cased_word.split('::')
59
- names.shift if names.empty? || names.first.empty?
60
+ camel_cased_word= self.to_s
61
+ names = camel_cased_word.split('::')
62
+ names.shift if names.empty? || names.first.empty?
60
63
 
61
- constant = ::Object
62
- names.each do |name|
63
- constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
64
+ constant = ::Object
65
+ names.each do |name|
66
+ constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
67
+ end
68
+ constant
64
69
  end
65
- constant
66
- end
67
-
68
- #convert class instance instance variables into a hash object
69
- def convert_to_hash
70
70
 
71
- unless self.class.class <= ::Class
72
- super
73
- #raise ::NoMethodError, "undefined method `to_hash' for #{self.inspect}"
74
- end
71
+ #convert class instance instance variables into a hash object
72
+ def convert_to_hash
75
73
 
76
- tmp_hash= ::Hash.new()
74
+ unless self.class.class <= ::Class
75
+ super
76
+ #raise ::NoMethodError, "undefined method `to_hash' for #{self.inspect}"
77
+ end
77
78
 
78
- self.instance_variables.each do|var|
79
- tmp_hash[var.to_s.delete("@")] = self.instance_variable_get(var)
80
- end
79
+ tmp_hash= ::Hash.new()
81
80
 
82
- return tmp_hash
81
+ self.instance_variables.each do|var|
82
+ tmp_hash[var.to_s.delete("@")] = self.instance_variable_get(var)
83
+ end
83
84
 
84
- end
85
+ return tmp_hash
85
86
 
86
- # this will check that the class is
87
- # defined or not in the runtime memory
88
- def class_exists?
89
- klass = ::Module.const_get(self)
90
- return klass.is_a?(::Class)
91
- rescue ::NameError
92
- return false
93
- end
87
+ end
94
88
 
95
- # This will convert a symbol or string and format to be a valid
96
- # constant name and create from it a class with instance attribute accessors
97
- # Best use is when you get raw data in string from external source
98
- # and you want make them class objects
99
- #
100
- # :hello_world.to_class(:test)
101
- # HelloWorld.to_class(:sup)
102
- # hw_var = HelloWorld.new
103
- # hw_var.sup = "Fine thanks!"
104
- # hw_var.test = 5
105
- #
106
- # puts hw_var.test
107
- #
108
- # #> produce 5 :Integer
109
- #
110
- # you can also use this formats
111
- # :HelloWorld , "hello.world",
112
- # "hello/world", "Hello::World",
113
- # "hello:world"...
114
- def convert_to_class(*attributes)
115
-
116
- unless self.class <= ::Symbol || self.class <= ::String || self.class <= ::Class
117
- raise ::ArgumentError, "object must be symbol or string to make able build class to it"
89
+ # this will check that the class is
90
+ # defined or not in the runtime memory
91
+ def class_exists?
92
+ klass = ::Module.const_get(self)
93
+ return klass.is_a?(::Class)
94
+ rescue ::NameError
95
+ return false
118
96
  end
119
97
 
120
- class_name= self.to_s
98
+ # This will convert a symbol or string and format to be a valid
99
+ # constant name and create from it a class with instance attribute accessors
100
+ # Best use is when you get raw data in string from external source
101
+ # and you want make them class objects
102
+ #
103
+ # :hello_world.to_class(:test)
104
+ # HelloWorld.to_class(:sup)
105
+ # hw_var = HelloWorld.new
106
+ # hw_var.sup = "Fine thanks!"
107
+ # hw_var.test = 5
108
+ #
109
+ # puts hw_var.test
110
+ #
111
+ # #> produce 5 :Integer
112
+ #
113
+ # you can also use this formats
114
+ # :HelloWorld , "hello.world",
115
+ # "hello/world", "Hello::World",
116
+ # "hello:world"...
117
+ def convert_to_class(*attributes)
118
+
119
+ unless self.class <= ::Symbol || self.class <= ::String || self.class <= ::Class
120
+ raise ::ArgumentError, "object must be symbol or string to make able build class to it"
121
+ end
122
+
123
+ class_name= self.to_s
121
124
 
122
- unless self.class <= ::Class
125
+ unless self.class <= ::Class
123
126
 
124
- class_name= class_name[0].upcase+class_name[1..class_name.length]
125
- %w[ _ . : / ].each do |one_sym|
127
+ class_name= class_name[0].upcase+class_name[1..class_name.length]
128
+ %w[ _ . : / ].each do |one_sym|
129
+
130
+ loop do
131
+ index_nmb= class_name.index(one_sym)
132
+ break if index_nmb.nil?
133
+ class_name[index_nmb..index_nmb+1]= class_name[index_nmb+1].upcase
134
+ end
126
135
 
127
- loop do
128
- index_nmb= class_name.index(one_sym)
129
- break if index_nmb.nil?
130
- class_name[index_nmb..index_nmb+1]= class_name[index_nmb+1].upcase
131
136
  end
132
137
 
133
138
  end
134
139
 
135
- end
136
-
137
- create_attribute = ::Proc.new do |*args|
140
+ create_attribute = ::Proc.new do |*args|
138
141
 
139
- end
142
+ end
140
143
 
141
- unless class_name.class_exists?
144
+ unless class_name.class_exists?
142
145
 
143
- self.class.const_set(
144
- class_name,
145
- ::Class.new
146
- )
146
+ self.class.const_set(
147
+ class_name,
148
+ ::Class.new
149
+ )
147
150
 
148
- end
151
+ end
149
152
 
150
153
 
151
- class_name.constantize.class_eval do
152
- attributes.each do |one_attribute|
153
- attr_accessor one_attribute.to_s.to_sym
154
+ class_name.constantize.class_eval do
155
+ attributes.each do |one_attribute|
156
+ attr_accessor one_attribute.to_s.to_sym
157
+ end
154
158
  end
155
- end
156
159
 
157
160
 
158
161
 
159
- return true
162
+ return true
160
163
 
161
- end
164
+ end
162
165
 
163
- alias :create_attributes :convert_to_class
166
+ alias :create_attributes :convert_to_class
167
+
168
+ end
164
169
 
165
170
  end
171
+
172
+ require File.join 'mpatch','injector'
173
+
174
+
166
175
  end
data/lib/mpatch/proc.rb CHANGED
@@ -1,14 +1,23 @@
1
- module MPatch::Include
2
- module Proc
3
-
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]] }
1
+ module MPatch
2
+
3
+ module Include
4
+
5
+ module Proc
6
+
7
+ # sugar syntax for proc * operator
8
+ # a = ->(x){x+1}
9
+ # b = ->(x){x*10}
10
+ # c = b*a
11
+ # c.call(1) #=> 20
12
+ def *(other)
13
+ self.class.new { |*args| self[*other[*args]] }
14
+ end
15
+
11
16
  end
12
17
 
13
18
  end
19
+
20
+ require File.join 'mpatch','injector'
21
+
22
+
14
23
  end
@@ -1,17 +1,26 @@
1
- module MPatch::Extend
2
- module Process
1
+ module MPatch
3
2
 
4
- # return a string obj that include the memory usage info
5
- def memory_usage
3
+ module Extend
4
+
5
+ module Process
6
+
7
+ # return a string obj that include the memory usage info
8
+ def memory_usage
9
+
10
+ begin
11
+ return `pmap #{self.pid}`.lines.to_a(
12
+ ).last.chomp.scan(/ *\w* *(\w+)/)[0][0]
13
+ rescue ::NoMethodError
14
+ return nil
15
+ end
6
16
 
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
17
  end
13
18
 
14
19
  end
15
20
 
16
21
  end
22
+
23
+ require File.join 'mpatch','injector'
24
+
25
+
17
26
  end
data/lib/mpatch/random.rb CHANGED
@@ -1,37 +1,46 @@
1
- module MPatch::Extend
2
- module Random
3
-
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
1
+ module MPatch
2
+
3
+ module Extend
4
+
5
+ module Random
6
+
7
+ def string(length= 7,amount=1,hyphen= " ")
8
+ amount_container= []
9
+ amount.times do
10
+ mrg= ""
11
+ mrg= (0...length).map{ ('a'..'z').to_a[rand(26)] }.join
12
+ amount_container.push mrg
13
+ end
14
+ return amount_container.join(hyphen)
10
15
  end
11
- return amount_container.join(hyphen)
12
- end
13
16
 
14
- def integer(length= 3)
15
- self.rand(length)
16
- end
17
+ def integer(length= 3)
18
+ self.rand(length)
19
+ end
17
20
 
18
- def boolean
19
- self.rand(2) == 1
20
- end
21
+ def boolean
22
+ self.rand(2) == 1
23
+ end
21
24
 
22
- def time from = Time.at(1114924812), to = Time.now
23
- self.rand(from..to)
24
- end
25
+ def time from = Time.at(1114924812), to = Time.now
26
+ self.rand(from..to)
27
+ end
25
28
 
26
- def date from = Time.at(1114924812), to = Time.now
27
- self.rand(from..to).to_date
28
- end
29
+ def date from = Time.at(1114924812), to = Time.now
30
+ self.rand(from..to).to_date
31
+ end
32
+
33
+ def datetime from = Time.at(1114924812), to = Time.now
34
+ self.rand(from..to).to_datetime
35
+ end
29
36
 
30
- def datetime from = Time.at(1114924812), to = Time.now
31
- self.rand(from..to).to_datetime
32
37
  end
33
38
 
34
39
  end
40
+
41
+ require File.join 'mpatch','injector'
42
+
43
+
35
44
  end
36
45
 
37
46
  ## alias in Random from RND
data/lib/mpatch/string.rb CHANGED
@@ -1,80 +1,89 @@
1
- module MPatch::Include
2
- module String
1
+ module MPatch
3
2
 
4
- # Find string in othere string
5
- def positions(oth_string)
3
+ module Include
6
4
 
7
- special_chrs=%w[# _ & < > @ $ . , -]+[*(0..9)]+[*("A".."Z")]+[*("a".."z")]
8
- loop do
9
- if oth_string.include? special_chrs[0]
10
- special_chrs.shift
11
- else
12
- break
5
+ module String
6
+
7
+ # Find string in othere string
8
+ def positions(oth_string)
9
+
10
+ special_chrs=%w[# _ & < > @ $ . , -]+[*(0..9)]+[*("A".."Z")]+[*("a".."z")]
11
+ loop do
12
+ if oth_string.include? special_chrs[0]
13
+ special_chrs.shift
14
+ else
15
+ break
16
+ end
13
17
  end
14
- end
15
18
 
16
- string=self
17
- return_array = []
18
- loop do
19
- break if string.index(oth_string).nil?
20
- range_value= ((string.index(oth_string))..(string.index(oth_string)+oth_string.length-1))
21
- return_array.push range_value
22
- [*range_value].each do |one_index|
23
- string[one_index]= special_chrs[0]
19
+ string=self
20
+ return_array = []
21
+ loop do
22
+ break if string.index(oth_string).nil?
23
+ range_value= ((string.index(oth_string))..(string.index(oth_string)+oth_string.length-1))
24
+ return_array.push range_value
25
+ [*range_value].each do |one_index|
26
+ string[one_index]= special_chrs[0]
27
+ end
24
28
  end
29
+
30
+ # return value
31
+ return return_array
25
32
  end
26
33
 
27
- # return value
28
- return return_array
29
- end
34
+ # Standard in rails. See official documentation
35
+ # [http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html]
36
+ def camelize(first_letter = :upper)
37
+ if first_letter == :upper
38
+ gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
39
+ else
40
+ self[0..0].downcase + camelize[1..-1]
41
+ end
42
+ end unless method_defined? :camelize
30
43
 
31
- # Standard in rails. See official documentation
32
- # [http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html]
33
- def camelize(first_letter = :upper)
34
- if first_letter == :upper
35
- gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
36
- else
37
- self[0..0].downcase + camelize[1..-1]
44
+ # Standard in rails. See official documentation
45
+ # [http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html]
46
+ def dasherize
47
+ gsub(/_/, '-')
48
+ end unless method_defined? :dasherize
49
+
50
+ # Standard in rails. See official documentation
51
+ # [http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html]
52
+ def demodulize
53
+ gsub(/^.*::/, '')
54
+ end unless method_defined? :demodulize
55
+
56
+ # Standard in rails. See official documentation
57
+ # [http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html]
58
+ def underscore
59
+ gsub(/::/, '/').
60
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
61
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
62
+ tr("-", "_").
63
+ downcase
64
+ end unless method_defined? :underscore
65
+
66
+ # Check that instance of String is start with an upper case or not
67
+ def capitalized?
68
+ self.match(/^[[:upper:]]/) ? true : false
38
69
  end
39
- end unless method_defined? :camelize
40
-
41
- # Standard in rails. See official documentation
42
- # [http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html]
43
- def dasherize
44
- gsub(/_/, '-')
45
- end unless method_defined? :dasherize
46
-
47
- # Standard in rails. See official documentation
48
- # [http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html]
49
- def demodulize
50
- gsub(/^.*::/, '')
51
- end unless method_defined? :demodulize
52
-
53
- # Standard in rails. See official documentation
54
- # [http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html]
55
- def underscore
56
- gsub(/::/, '/').
57
- gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
58
- gsub(/([a-z\d])([A-Z])/,'\1_\2').
59
- tr("-", "_").
60
- downcase
61
- end unless method_defined? :underscore
62
-
63
- # Check that instance of String is start with an upper case or not
64
- def capitalized?
65
- self.match(/^[[:upper:]]/) ? true : false
66
- end
67
70
 
68
- # return the number how often the str is with in the self
69
- # by default with \b regex border
70
- def frequency(str)
71
- begin
72
- if str.class == String
73
- str= '\b'+str+'\b'
71
+ # return the number how often the str is with in the self
72
+ # by default with \b regex border
73
+ def frequency(str)
74
+ begin
75
+ if str.class == String
76
+ str= '\b'+str+'\b'
77
+ end
74
78
  end
79
+ self.scan(/#{str}/).count
75
80
  end
76
- self.scan(/#{str}/).count
81
+
77
82
  end
78
83
 
79
84
  end
85
+
86
+ require File.join 'mpatch','injector'
87
+
88
+
80
89
  end
data/lib/mpatch/yml.rb CHANGED
@@ -1,15 +1,22 @@
1
1
  require 'yaml'
2
2
 
3
- module MPatch::Extend
3
+ module MPatch
4
4
 
5
- module YAML
6
- def save_file(file_path,config_hash)
7
- File.open(file_path, 'w+') {|f| f.write(config_hash.to_yaml) }
8
- end
5
+ module Extend
6
+
7
+ module YAML
8
+ def save_file(file_path,config_hash)
9
+ File.open(file_path, 'w+') {|f| f.write(config_hash.to_yaml) }
10
+ end
9
11
 
10
- def load_file(file_path)
11
- ::YAML.load(File.open(file_path))
12
+ def load_file(file_path)
13
+ ::YAML.load(File.open(file_path))
14
+ end
12
15
  end
16
+
13
17
  end
14
18
 
19
+ require File.join 'mpatch','injector'
20
+
21
+
15
22
  end
data/lib/mpatch.rb CHANGED
@@ -1,49 +1,6 @@
1
1
  #encoding: UTF-8
2
2
  module MPatch
3
3
 
4
- module Include;end
5
- module Extend;end
6
-
7
- Dir.glob(File.join(File.absolute_path(File.dirname(__FILE__)),"mpatch","**","*.{rb,ru}")).each{|e|require e}
8
- extend MPatch::Include::Module
9
-
10
- self.submodules.each do |module_name|
11
-
12
- method_name= module_name.to_s.split('::').last.downcase.to_s.to_sym
13
-
14
- module_name.__send__ :extend, MPatch::Include::Module
15
- module_name.submodules.each do |sub_module_name|
16
-
17
- constant= ::Object
18
- constant_name= sub_module_name.to_s.split('::').last
19
- array_of_target_constant= []
20
-
21
- case true
22
-
23
- when sub_module_name.to_s.include?('And')
24
- sub_module_name.to_s.split('::').last.split('And').each do |tag_module|
25
- array_of_target_constant.push tag_module
26
- end
27
-
28
- else
29
- array_of_target_constant.push constant_name
30
-
31
- end
32
-
33
- array_of_target_constant.each do |name|
34
-
35
- begin
36
- target_constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
37
- target_constant.__send__ method_name, sub_module_name
38
- rescue ::NoMethodError => ex
39
- STDERR.puts ex
40
- end
41
-
42
- end
43
-
44
- end
45
-
46
-
47
- end
4
+ require File.join 'mpatch','all'
48
5
 
49
6
  end