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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c623135aae2815fd50d9fc441039ea15246b18ec
4
- data.tar.gz: b3c7b0e2085623d4144fa9d7819c9413cd9d1a6e
3
+ metadata.gz: 9e7c3554d0b668a729d67e66d02ff58306080757
4
+ data.tar.gz: 925b0d2ad87a9d5bf06af86a99f5dcfa152a61c2
5
5
  SHA512:
6
- metadata.gz: 727a7aed54b1bcbdc912059c5a37d388c6746a697ed389e1bc3e43d942533988da8c7ad569d3ac73f0f1764a1222450092502e01c2a1f3bcc68d7b3f1bd76148
7
- data.tar.gz: c8cfe09d50fee1df7bd1917bfb8965867657635d3e7136ab907a0cbbe0cb99fe6850d60d9c139f39ee75f437a78b2601ac7bcf0be985f9767e83a2372c083095
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 parametrize **parameters, &block
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
- # Sets a constant to a value if this has not been previously defined.
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 const_set_if_not_defined( const, value )
5
- const_set( const, value ) unless const_defined? const
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
- # Redefines a constant without warning.
9
- #
10
- def const_reset!( const, value )
11
- send :remove_const, const if const_defined? const
12
- const_set( const, value )
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 chain **hash, &block
21
- hash.each_pair { |mapped_method_symbol, original_method_symbol|
22
- define_method mapped_method_symbol do |*args, &b|
23
- block.( send original_method_symbol, *args, &b )
24
- end
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
- end
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 )
@@ -1,4 +1,4 @@
1
1
  module YSupport
2
- VERSION = "2.1.16"
2
+ VERSION = "2.1.17"
3
3
  DEBUG = false
4
4
  end
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.16
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: 2014-12-04 00:00:00.000000000 Z
11
+ date: 2015-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport