mpatch 1.3.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- N2RkMzUzNmY4ZmRiN2EyZDk3ODhmNzVhZjcxZjc4OTg5YjYzN2IwZA==
5
- data.tar.gz: !binary |-
6
- YjliZTYxNTU5OTBjODZmMThiYTdkZDExMDU5OTk1NmNiOWYwNDI0YQ==
2
+ SHA1:
3
+ metadata.gz: e55cefa7d1160f216bcc28ef4b4f2bc0528e8313
4
+ data.tar.gz: 99d6f21dcb6f464b75350de5fc10cca80965a92a
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- ZDgyZDk5NWFmN2I3YTU5ODdhYzA1OTQwZTg2NDMzNzQ4OWE3OWMyNTY1NTIz
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.3.0
1
+ 2.0.0
data/lib/mpatch/array.rb CHANGED
@@ -1,151 +1,153 @@
1
- class Array
2
-
3
- # remove arguments or array of
4
- # parameters from the main array
5
- def trim(*args)
6
-
7
- args.dup.each do |one_element|
8
- if one_element.class == Array
9
- args.delete_at(args.index(one_element))
10
- args= args+one_element
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
- delete_array= Array.new
15
- args.each do |one_element|
16
- index= self.index(one_element)
17
- unless index.nil?
18
- delete_array.push index
19
- self.delete_at(index)
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
- return self
24
+ return self
24
25
 
25
- end
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
- # remove n. element from the end
41
- # and return the original object
42
- def pinch! n=1
43
- n.times do
44
- self.pop
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
- # return boolean by other array
50
- # all element included or
51
- # not in the target array
52
- def contain?(oth_array)#anothere array
53
- (oth_array & self) == oth_array
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
- # return boolean by other array
57
- # if any element included from
58
- # the oth_array, return a true
59
- def contain_any_of?(oth_array)
60
- oth_array.each do |element|
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
- alias :contains_any_of? :contain_any_of?
69
- alias :has_any_of? :contain_any_of?
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
- # do safe transpose
72
- def safe_transpose
73
- result = []
74
- max_size = self.max { |a,b| a.size <=> b.size }.size
75
- max_size.times do |i|
76
- result[i] = Array.new(self.first.size)
77
- self.each_with_index { |r,j| result[i][j] = r[i] }
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
- alias :contains? :contain?
69
+ alias :contains_any_of? :contain_any_of?
70
+ alias :has_any_of? :contain_any_of?
83
71
 
84
- # return boolean
85
- # if any element class is equal to th given class
86
- # return a true , else false
87
- def contain_element_of_class?(class_name)
88
- target_array= self.map{|e| e.class }.uniq
89
- if class_name.class != Class
90
- raise ArgumentError, "Argument must be a Class!"
91
- end
92
- if target_array.include? class_name
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
- # generate params structure from array
102
- # *args => [:opts,:args]
103
- def params_separation
83
+ alias :contains? :contain?
104
84
 
105
- options= self.map { |element|
106
- if element.class == Hash
107
- element
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
- }.uniq - [ nil ]
110
- #options.each{|e| self.delete(e) }
111
- arguments= self.dup - options
112
- options= Hash[*options]
93
+ if target_array.include? class_name
94
+ return true
95
+ end
96
+ return false
97
+ end
113
98
 
114
- return [options,arguments]
99
+ alias :contains_element_of_class? :contain_element_of_class?
100
+ alias :has_element_of_class? :contain_element_of_class?
115
101
 
116
- end
102
+ # generate params structure from array
103
+ # *args => [:opts,:args]
104
+ def params_separation
117
105
 
118
- alias :separate_params :params_separation
119
- alias :process_params :params_separation
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
- # generate params structure from array
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
- return_value= self.map { |element|
130
- if element.class <= class_name
131
- element
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
- return_value ||= Array.new
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
- return return_value
137
+ return_value ||= self.class.new
139
138
 
140
- end
141
- alias :cut_class! :extract_class!
139
+ return return_value
142
140
 
143
- # generate params structure from array
144
- # *args - options {}
145
- def extract_options!
146
- options= self.extract_class! Hash
147
- return Hash[*options]
148
- end
149
- alias :extract_hash! :extract_options!
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
- class Class
1
+ module MPatch
2
+ class Class
2
3
 
3
- # get singleton methods to target class without super class methods
4
- def class_methods
5
- self.methods - Object.methods
6
- end
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
- # bind a singleton method to a class object
9
- def create_class_method(method,&block)
10
- self.class_eval do
11
- define_singleton_method method do |*args|
12
- block.call *args
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
- # create an instance method
18
- def create_instance_method(method,&block)
19
- self.class_eval do
20
- define_method method do |*args|
21
- block.call *args
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
- # Iterates over all subclasses (direct and indirect)
27
- def each_subclass
28
- ObjectSpace.each_object(Class) { | candidate |
29
- begin
30
- yield candidate if candidate < self
31
- rescue ArgumentError
32
- # comparison of Class with Class failed (ArgumentError)
33
- end
34
- }
35
- end
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
- # Returns an Array of subclasses (direct and indirect)
38
- def subclasses_all
39
- ret = []
40
- each_subclass {|c| ret.push c}
41
- ret
42
- end
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
- alias :all_subclasses :subclasses_all
45
+ alias :all_subclasses :subclasses_all
45
46
 
46
- # Returns an Array of direct subclasses
47
- def subclasses
48
- ret = []
49
- each_subclass {|c| ret.push(c) if c.superclass == self }
50
- ret
51
- end
52
- alias :subclass :subclasses
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
- # create singleton attribute
55
- def class_attr_accessor(name)
55
+ # create singleton attribute
56
+ def class_attr_accessor(name)
56
57
 
57
- ### GET
58
- begin
59
- define_method name do
60
- class_variable_get "@@#{name}"
58
+ ### GET
59
+ begin
60
+ define_method name do
61
+ class_variable_get "@@#{name}"
62
+ end
61
63
  end
62
- end
63
64
 
64
- ### SET
65
- begin
66
- define_method "#{name}=" do |new_val|
67
- class_variable_set "@@#{name}", new_val
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
- end
72
+ end
72
73
 
73
- # create class instance attribute
74
- def instance_attr_accessor(name)
74
+ # create class instance attribute
75
+ def instance_attr_accessor(name)
75
76
 
76
- ### GET
77
- begin
78
- define_method name do
79
- instance_variable_get "@#{name}"
77
+ ### GET
78
+ begin
79
+ define_method name do
80
+ instance_variable_get "@#{name}"
81
+ end
80
82
  end
81
- end
82
83
 
83
- ### SET
84
- begin
85
- define_method "#{name}=" do |new_val|
86
- instance_variable_set "@#{name}", new_val
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
- class File
1
+ module MPatch
2
+ module File
2
3
 
3
- # create a file, if not exsist create file, and dir if needed
4
- def self.create(route_name ,filemod="w",string_data=String.new)
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(File::SEPARATOR).last.nil? || route_name.to_s.split(File::SEPARATOR).last != ''
9
- file_name = route_name.to_s.split(File::SEPARATOR).last
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 = File.expand_path(route_name).to_s.split(File::SEPARATOR)
18
- path = path - [File.expand_path(route_name).to_s.split(File::SEPARATOR).last]
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 !Dir.exists?(File::SEPARATOR+path.join(File::SEPARATOR))
24
+ if !::Dir.exists?(self.class::SEPARATOR+path.join(self.class::SEPARATOR))
25
25
 
26
- at_now = File::SEPARATOR
26
+ at_now = self.class::SEPARATOR
27
27
  path.each do |dir_to_be_checked|
28
28
 
29
- at_now += "#{dir_to_be_checked+File::SEPARATOR}"
30
- Dir.mkdir(at_now) if !Dir.exists?(at_now)
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 = "#{File::SEPARATOR+path.join(File::SEPARATOR)+File::SEPARATOR}#{file_name}"
39
- if File.exist? full_path
40
- File.open(full_path,filemod).write string_data
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
- File.new(full_path,filemod).write string_data
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
- # start read the file object on each line
52
- # optionable an integer value to start read line at
53
- # compatible with mac (i am linux user, so not tested)
54
- def each_line_from(start_at=1,&block)
55
- unless [Integer,Fixnum,Bignum].include?(start_at.class)
56
- raise ArgumentError, "invalid line index"
57
- end
58
- begin
59
- line_num= 1
60
- text= self.read
61
- text.gsub!(/\r\n?/, "\n")
62
- text.each_line do |*line|
63
- if line_num >= start_at
64
- block.call #*line
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