mpatch 2.9.0 → 2.11.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0777613055d38431a72ccfb7b0ff1691cdec65a1
4
- data.tar.gz: b9d3d3b92578dd730ba5fb56c882ff8bda44619b
3
+ metadata.gz: cec135221ac3f2a5c694c8d93fe4a8765e3bca68
4
+ data.tar.gz: ac1e24c549d1c98cff57023453529513838a4efd
5
5
  SHA512:
6
- metadata.gz: d4facd8f4c5dfd1c18f48ed363d84192c14e9cd9863015c2d9a60ea268e96a1c8e79e2b5c94cae3d0938267ea0f83d7a64fd862d83b23ed24a5725e1828c086d
7
- data.tar.gz: b9a51fd5843ea09ca99c25515086cf486e1d09cc9ff1ff186a50d30982be90a8fd43e63cde5fade3e293595f2e7b649c51f3020b5e7a63d695e1af713acc416e
6
+ metadata.gz: c2bd6ff235e20003cc427d16a8f3efd475bd2d250ca3921373cad8e90ca421c6ce3c883755f8b5cf138cbcffb08aa0dff68a0f0cd186a301250e06580d8f9e1f
7
+ data.tar.gz: 7a5d7383f5ea92faddf35238a180f6b978c29c644766dd39d936b3331695dae9101343ec9179c7a2bce667c05aef25cf35041d7a0ec7098ded7b4bb04024c3c0
data/README.md CHANGED
@@ -21,27 +21,41 @@ This will result in lot of helper methods, and metaprograming tricks
21
21
  ### example with Class methods
22
22
 
23
23
  ```ruby
24
+
24
25
  require 'mpatch'
25
26
 
26
- class Test
27
+ class Test2
28
+ @@test= "asd"
29
+ end
30
+
31
+ class Test < Test2
27
32
 
28
33
  def initialize
29
34
 
30
35
  @hello= "world"
31
- @sup= "nothing"
36
+ @no = "yes"
32
37
 
33
38
  end
34
39
 
35
40
  end
36
41
 
37
- puts Test.new.convert_to_hash
38
- #> {"hello" => "world", "sup" => "nothing"}
42
+ puts Test2.inherited_by.inspect
43
+ # [Test]
44
+
45
+ puts Test.conv2hash
46
+ # {"test"=>"asd"}
47
+
48
+ puts Test.new.conv2hash
49
+ # {"hello"=>"world", "no"=>"yes"}
50
+
51
+
39
52
  ```
40
53
 
41
54
  The module give you tools in your hand for inheritance handle.
42
55
  For example:
43
56
 
44
57
  ```ruby
58
+
45
59
  puts Models::MongoidClassName.mixin_ancestors.include? Mongoid::Document
46
60
  #> true
47
61
 
data/VERSION CHANGED
@@ -1,2 +1,2 @@
1
- 2.9.0
1
+ 2.11.0
2
2
 
data/examples/class.rb ADDED
@@ -0,0 +1,25 @@
1
+ require 'mpatch'
2
+
3
+ class Test2
4
+ @@test= "asd"
5
+ end
6
+
7
+ class Test < Test2
8
+
9
+ def initialize
10
+
11
+ @hello= "world"
12
+ @no = "yes"
13
+
14
+ end
15
+
16
+ end
17
+
18
+ puts Test2.inherited_by.inspect
19
+ # [Test]
20
+
21
+ puts Test.conv2hash
22
+ # {"test"=>"asd"}
23
+
24
+ puts Test.new.conv2hash
25
+ # {"hello"=>"world", "no"=>"yes"}
data/examples/hash.rb ADDED
@@ -0,0 +1,17 @@
1
+
2
+ require "mpatch"
3
+
4
+ x = {}
5
+ (1..10000).each do |i|
6
+ x["key#{i}"] = i
7
+ end
8
+
9
+ t=Time.now
10
+ x.map_hash{ |k,v| { k.to_sym => v.to_s } }
11
+ puts Time.now - t
12
+
13
+ var= {hello: "world",no: "yes"}
14
+ puts var
15
+
16
+ var= [[:hello, "world"],[:no, "yes"]].map_hash{|k,v| {k => v} }
17
+ puts var.inspect
data/lib/mpatch.rb CHANGED
@@ -1,6 +1,4 @@
1
1
  #encoding: UTF-8
2
2
  module MPatch
3
-
4
3
  require File.join 'mpatch','all'
5
-
6
4
  end
data/lib/mpatch/array.rb CHANGED
@@ -168,6 +168,42 @@ module MPatch
168
168
  end
169
169
  alias :extract_hash! :extract_options!
170
170
 
171
+ # map hash will work just alike map but instead of an array it will return a hash obj
172
+ #
173
+ # [:hello, "world",:world , "hello"].map_hash{|k,v| [ k , 123] }
174
+ # #> {:hello=>123, :world=>123}
175
+ #
176
+ # [:hello,"world",:world,"hello"].map_hash{|k,v| { k => 123 } }
177
+ # #> {:hello=>123, :world=>123}
178
+ #
179
+ # [[:hello,"world"],[:world,"hello"]].map_hash{ |ary| { ary[0] => ary[1] } }
180
+ # #> {:hello=>"world", :world=>"hello"}
181
+ #
182
+ def map_hash *args,&block
183
+
184
+ tmp_hash= ::Hash.new(*args)
185
+ self.map(&block).each do |hash|
186
+ case true
187
+
188
+ when hash.class <= ::Array
189
+ tmp_hash.deep_merge!(::Hash[*hash])
190
+
191
+ when hash.class <= ::Hash
192
+ tmp_hash.deep_merge!(hash)
193
+
194
+ else
195
+ raise ArgumentError,
196
+ "invalid input as last valie for #{__method__}: #{hash}/#{hash.class}"
197
+
198
+ end
199
+
200
+ end
201
+
202
+ return tmp_hash
203
+
204
+ end
205
+ alias :map2hash :map_hash
206
+
171
207
  end
172
208
 
173
209
  end
data/lib/mpatch/hash.rb CHANGED
@@ -93,23 +93,39 @@ module MPatch
93
93
  # {:hello=> "world",:world => "hello"}.map_hash{|k,v| { k => 123 } }
94
94
  # #> {:hello=>123, :world=>123}
95
95
  #
96
- def map_hash &block
96
+ def map_hash *args,&block
97
97
 
98
- tmp_hash= self.class.new
99
- map_hash_obj= self.map &block
100
- map_hash_obj.each do |hash|
98
+ tmp_hash= self.class.new(*args)
99
+ self.map(&block).each do |hash|
101
100
 
102
- if hash.class <= ::Array
103
- hash= self.class[*hash]
104
- end
101
+ case true
102
+
103
+ when hash.class <= ::Array
104
+ tmp_hash.deep_merge!( self.class[*hash] )
105
105
 
106
- tmp_hash.deep_merge!(hash)
106
+ when hash.class <= ::Hash
107
+ tmp_hash.deep_merge!(hash)
108
+
109
+ else
110
+ raise ArgumentError,
111
+ "invalid input as last valie for #{__method__}: #{hash}/#{hash.class}"
112
+
113
+ end
107
114
 
108
115
  end
109
116
 
110
117
  return tmp_hash
118
+
119
+ end
120
+ alias :map2hash :map_hash
121
+
122
+ def map_hash! *args,&block
123
+ self.replace(self.map_hash(*args,&block))
111
124
  end
112
125
 
126
+ alias :map2hash! :map_hash!
127
+
128
+
113
129
  # Fetch a nested hash value
114
130
  def value_by_keys(*attrs)
115
131
  attr_count = attrs.size
data/lib/mpatch/module.rb CHANGED
@@ -36,12 +36,14 @@ module MPatch
36
36
  begin
37
37
 
38
38
  if !return_array.include?(candidate) && candidate != self
39
- case self.class.to_s
40
39
 
41
- when "Module"
40
+ #> can't make subclass of Class so == is enough
41
+ case true
42
+
43
+ when self.class == ::Module
42
44
  return_array.push candidate if candidate.mixin_ancestors.include?(self)
43
45
 
44
- when "Class"
46
+ when self.class == ::Class
45
47
  return_array.push candidate if candidate < self
46
48
 
47
49
  end
@@ -49,11 +51,12 @@ module MPatch
49
51
  end
50
52
 
51
53
  rescue ::ArgumentError, ::NoMethodError
54
+
52
55
  end
53
56
  end
54
57
 
55
58
  end
56
- return_array
59
+ return return_array
57
60
 
58
61
  end
59
62
 
@@ -88,6 +91,23 @@ module MPatch
88
91
 
89
92
  end
90
93
 
94
+ def hello
95
+ "hello"
96
+ end
97
+
98
+ #convert class instance instance variables into a hash object
99
+ def convert_to_hash
100
+
101
+ tmp_hash= {}
102
+ self.class_variables.each do|var|
103
+ tmp_hash[var.to_s.delete("@@")] = self.class_variable_get(var)
104
+ end
105
+
106
+ return tmp_hash
107
+
108
+ end
109
+ alias :conv2hash :convert_to_hash
110
+
91
111
  end
92
112
 
93
113
  end
@@ -104,8 +124,6 @@ module MPatch
104
124
  end
105
125
 
106
126
  end
107
-
108
- alias :ci2s :convert_instance_methods_to_singleton_methods
109
127
  alias :instances2singletons :convert_instance_methods_to_singleton_methods
110
128
 
111
129
  end
data/lib/mpatch/object.rb CHANGED
@@ -66,6 +66,7 @@ module MPatch
66
66
  constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name)
67
67
  end
68
68
  constant
69
+
69
70
  end
70
71
 
71
72
  #convert class instance instance variables into a hash object
@@ -73,11 +74,9 @@ module MPatch
73
74
 
74
75
  unless self.class.class <= ::Class
75
76
  super
76
- #raise ::NoMethodError, "undefined method `to_hash' for #{self.inspect}"
77
77
  end
78
78
 
79
- tmp_hash= ::Hash.new()
80
-
79
+ tmp_hash= {}
81
80
  self.instance_variables.each do|var|
82
81
  tmp_hash[var.to_s.delete("@")] = self.instance_variable_get(var)
83
82
  end
@@ -85,6 +84,7 @@ module MPatch
85
84
  return tmp_hash
86
85
 
87
86
  end
87
+ alias :conv2hash :convert_to_hash
88
88
 
89
89
  # this will check that the class is
90
90
  # defined or not in the runtime memory
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mpatch
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.9.0
4
+ version: 2.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Luzsi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-01 00:00:00.000000000 Z
11
+ date: 2014-05-08 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: This is a collection of my Ruby monkey patches for making easer to use
14
14
  the basic classes
@@ -25,6 +25,8 @@ files:
25
25
  - create_folder.rb
26
26
  - dump/class_and_module.rb
27
27
  - examples/array.rb
28
+ - examples/class.rb
29
+ - examples/hash.rb
28
30
  - examples/privatize.rb
29
31
  - examples/test.rb
30
32
  - files.rb