mpatch 2.1.3 → 2.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3ddc83c1cd8c64d47ffd74479721725f1b25bbd1
4
- data.tar.gz: 71deb2c36f7fe500198968406eca19949644db5d
3
+ metadata.gz: 2cbc8843bcb428bdd3ee7119c19e00bf08f251cf
4
+ data.tar.gz: 53a12a8e35f450aa25a1bb4d9ccf06c05e40e9cf
5
5
  SHA512:
6
- metadata.gz: f67da0a53c848fe41f2aa80fee1353881d167249d8704f36de4989a785696cc59f244c9c9322c7f62be553ac19f1069b21a0d200d4018a96f3c1228eeaba0632
7
- data.tar.gz: 2c88448ce1100cdf89338b222ccb2bafb4853cf4acc3e9ce2106bdb93e23af2a119bc381fa9c5a70780a2a1f41a0777c3afbee135be54db755c590b66ddba04f
6
+ metadata.gz: f32a8a90dc4ed94ca5a0198c73fb5ab7467ac82ad245744b3fd4b0be784a2b850f9ad5732fdce0f7c6f04caa83e3e365201cf3d478a28df20ebb4dff9a5cd9d1
7
+ data.tar.gz: 440b60f05e81d06e009b0114abf37a5aa4f148943cca44e25740f4af8382f032517c0044c71f03e765a20c8410ba2a2366296821e15a6d48e5a711c5b0f09fee
data/README.md CHANGED
@@ -26,11 +26,54 @@ puts Test.new.to_hash
26
26
  #> {"hello" => "world", "sup" => "nothing"}
27
27
  `
28
28
 
29
+ The module give you tools in your hand for inheritance handle.
30
+ For example:
31
+
32
+ `ruby
33
+ puts Models::MongoidClassName.mixin_ancestors.include? Mongoid::Document
34
+ #> true
35
+
36
+ puts Mongoid::Document.inherited_by.inspect
37
+ #> [MongoidClassName]
38
+
39
+ class Test
40
+
41
+ end
42
+
43
+ module Hello
44
+ class World < Test
45
+ end
46
+ end
47
+
48
+ puts Test.inherited_by.inspect
49
+ #> [Hello::World]
50
+
51
+ module ParentModule
52
+
53
+ end
54
+
55
+ module TargetModule
56
+ include ParentModule
57
+ end
58
+
59
+ module SomeModuleThatInclude
60
+ include TargetModule
61
+ end
62
+
63
+ class SomeClassThatInclude
64
+ include TargetModule
65
+ end
66
+
67
+ puts TargetModule.inherited_by.inspect
68
+ #>[SomeClassThatInclude, SomeModuleThatInclude]
69
+
70
+ puts TargetModule.inherited_by(class).inspect
71
+ #>[SomeClassThatInclude]
72
+
73
+ `
74
+
75
+
29
76
  But there is a lot of method, for example for modules modules / subbmodules call that retunr modules under that namespace.
30
77
  Lot of metaprogrammer stuff there too :)
31
78
 
32
79
  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
- 2.1.3
1
+ 2.2.1
@@ -0,0 +1,44 @@
1
+ module MPatch
2
+ module ClassAndModule
3
+ def mixin_ancestors(include_ancestors=true)
4
+ ancestors.take_while {|a| include_ancestors || a != superclass }.
5
+ select {|ancestor| ancestor.instance_of?( ::Module ) }
6
+ end
7
+
8
+ def inherited_by *args
9
+
10
+ if args.empty?
11
+ args.push(::Class)
12
+ args.push(::Module)
13
+ end
14
+
15
+ return_array= []
16
+ args.each do |type_name|
17
+
18
+ ::ObjectSpace.each_object(type_name) do |candidate|
19
+ begin
20
+
21
+ if !return_array.include?(candidate) && candidate != self
22
+ case self.class.to_s
23
+
24
+ when "Module"
25
+ return_array.push candidate if candidate.mixin_ancestors.include?(self)
26
+
27
+ when "Class"
28
+ return_array.push candidate if candidate < self
29
+
30
+ end
31
+
32
+ end
33
+
34
+ rescue ::ArgumentError, ::NoMethodError
35
+ end
36
+ end
37
+
38
+ end
39
+ return_array
40
+
41
+ end
42
+
43
+ end
44
+ end
data/lib/mpatch/random.rb CHANGED
@@ -12,23 +12,23 @@ module MPatch
12
12
  end
13
13
 
14
14
  def integer(length= 3)
15
- self.class.rand(length)
15
+ self.rand(length)
16
16
  end
17
17
 
18
18
  def boolean
19
- self.class.rand(2) == 1
19
+ self.rand(2) == 1
20
20
  end
21
21
 
22
22
  def time from = Time.at(1114924812), to = Time.now
23
- self.class.rand(from..to)
23
+ self.rand(from..to)
24
24
  end
25
25
 
26
26
  def date from = Time.at(1114924812), to = Time.now
27
- self.class.rand(from..to).to_date
27
+ self.rand(from..to).to_date
28
28
  end
29
29
 
30
30
  def datetime from = Time.at(1114924812), to = Time.now
31
- self.class.rand(from..to).to_datetime
31
+ self.rand(from..to).to_datetime
32
32
  end
33
33
 
34
34
  end
data/lib/mpatch.rb CHANGED
@@ -3,6 +3,10 @@ module MPatch
3
3
 
4
4
  Dir.glob(File.join(File.absolute_path(File.dirname(__FILE__)),"mpatch","**","*.{rb,ru}")).each{|e|require e}
5
5
 
6
+ [ MPatch::Module, MPatch::Class ].each do |module_name|
7
+ module_name.__send__ :include, MPatch::ClassAndModule
8
+ end
9
+
6
10
  [
7
11
  MPatch::Module,
8
12
  MPatch::Class,
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.1.3
4
+ version: 2.2.1
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-03-21 00:00:00.000000000 Z
11
+ date: 2014-03-26 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
@@ -27,6 +27,7 @@ files:
27
27
  - lib/mpatch.rb
28
28
  - lib/mpatch/array.rb
29
29
  - lib/mpatch/class.rb
30
+ - lib/mpatch/class_and_module.rb
30
31
  - lib/mpatch/hash.rb
31
32
  - lib/mpatch/integer.rb
32
33
  - lib/mpatch/module.rb