y_support 2.1.16 → 2.1.17
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/y_support/core_ext/class/misc.rb +8 -1
- data/lib/y_support/core_ext/module/misc.rb +29 -22
- data/lib/y_support/version.rb +1 -1
- data/test/misc_test.rb +8 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9e7c3554d0b668a729d67e66d02ff58306080757
|
4
|
+
data.tar.gz: 925b0d2ad87a9d5bf06af86a99f5dcfa152a61c2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 30a986280eca560719e42884223f74cb4bca22f56cdfe04a45a8063d4fb9530d713e3d7c87f1babc1fdda834110397d42f6a94c83ed3ba90fa45b4aa9c026ddd
|
7
|
+
data.tar.gz: cad946575666768b918b6b847e2fe7c5e00f9b7c8e2a9e40981eb42757df5e2a8ce1e2309949c25fd7cd706dd48bc937261de1dbd9f75c11935b082ec83193d9
|
@@ -4,7 +4,7 @@ class Class
|
|
4
4
|
# singleton method(s) named "symbol" be defined on the subclass, returning
|
5
5
|
# "value".
|
6
6
|
#
|
7
|
-
def
|
7
|
+
def parametrized_subclass **parameters, &block
|
8
8
|
Class.new( self ).tap do |subclass|
|
9
9
|
parameters.each_pair { |symbol, value|
|
10
10
|
subclass.define_singleton_method symbol do value end
|
@@ -13,4 +13,11 @@ class Class
|
|
13
13
|
subclass.class_exec &block if block
|
14
14
|
end
|
15
15
|
end
|
16
|
+
alias parametrize parametrized_subclass
|
17
|
+
|
18
|
+
# Method #heir_module is not applicable to classes, raises TypeError.
|
19
|
+
#
|
20
|
+
def heir_module
|
21
|
+
fail TypeError, "Method #heir_module is not applicable to classes!"
|
22
|
+
end
|
16
23
|
end
|
@@ -1,31 +1,38 @@
|
|
1
1
|
class Module
|
2
|
-
#
|
2
|
+
# Creates a module that inherits from the receiver and is parametrized
|
3
|
+
# with the given set of parameters. The parameters have form { symbol:
|
4
|
+
# value } and they cause singleton method(s) named "symbol" be defined
|
5
|
+
# on the heir, returning "value".
|
3
6
|
#
|
4
|
-
def
|
5
|
-
|
7
|
+
def heir_module **parameters, &block
|
8
|
+
s = self
|
9
|
+
Module.new { include s }.tap do |m|
|
10
|
+
parameters.each_pair { |symbol, value|
|
11
|
+
m.define_singleton_method symbol do value end
|
12
|
+
}
|
13
|
+
m.define_singleton_method inspect do s.inspect + "<" end
|
14
|
+
m.module_exec &block if block
|
15
|
+
end
|
6
16
|
end
|
7
17
|
|
8
|
-
#
|
9
|
-
#
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
end
|
14
|
-
|
15
|
-
# Defines a set of methods by applying the block on the return value of
|
16
|
-
# another set of methods. Accepts a hash of pairs { mapped_method_symbol =>
|
17
|
-
# original_method_symbol } and a block which to chain to the original
|
18
|
-
# method result.
|
18
|
+
# Creates a class, which is a subclass of a supplied class (defaults
|
19
|
+
# to Object if none supplied), and which also inherits from the receiver
|
20
|
+
# and is parametrized by the given set of parameters. The parameters have
|
21
|
+
# form { symbol: value } and they cause singleton method(s) named "symbol"
|
22
|
+
# be defined on the heir class, returning "value".
|
19
23
|
#
|
20
|
-
def
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
24
|
+
def heir_class mother=Object, **parameters, &block
|
25
|
+
s = self
|
26
|
+
Class.new( mother ) { include s }.tap do |c|
|
27
|
+
parameters.each_pair { |symbol, value|
|
28
|
+
c.define_singleton_method symbol do value end
|
29
|
+
}
|
30
|
+
# TODO: This line is controversial:
|
31
|
+
c.define_singleton_method inspect do s.inspect + "<" end
|
32
|
+
c.module_exec &block if block
|
33
|
+
end
|
26
34
|
end
|
27
|
-
|
28
|
-
class Module
|
35
|
+
|
29
36
|
# Sets a constant to a value if this has not been previously defined.
|
30
37
|
#
|
31
38
|
def const_set_if_not_defined( const, value )
|
data/lib/y_support/version.rb
CHANGED
data/test/misc_test.rb
CHANGED
@@ -25,6 +25,14 @@ describe Module do
|
|
25
25
|
|
26
26
|
it "has #const_set_if_not_defined and #const_reset!" do
|
27
27
|
m = Module.new
|
28
|
+
hm = m.heir_module( p1: 1, p2: 2 )
|
29
|
+
hm.ancestors[1].must_equal m
|
30
|
+
hm.p1.must_equal 1
|
31
|
+
hm.p2.must_equal 2
|
32
|
+
hc = m.heir_class( Array, q1: 1, q2: 2 )
|
33
|
+
hc.new.class.ancestors[1].must_equal m
|
34
|
+
hc.q1.must_equal 1
|
35
|
+
hc.q2.must_equal 2
|
28
36
|
m.const_set_if_not_defined :Foo, 42
|
29
37
|
m::Foo.must_equal 42
|
30
38
|
m.const_reset! :Foo, 43
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: y_support
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.17
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- boris
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-10-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|