y_support 2.0.23 → 2.0.24

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: 0143322533b38e137de87f30d4d5d94021a0e211
4
- data.tar.gz: f086f7e148dbd589c5f4007e689c4519c09c80b9
3
+ metadata.gz: d8afafc604de2cef2f7c7e22e744db08f5472efe
4
+ data.tar.gz: 8fbfc61eec9b5dd2f323f7d6d27123146bffe66b
5
5
  SHA512:
6
- metadata.gz: 602b5759bd46e3138145086c168cb695a0433f6efa3d114d4437186eec2b16507167326bbbb3247d058d1e69f0deae3b8d1e16a3b1d60bd4fbe127bd24ff66fc
7
- data.tar.gz: 6473a5334642f9d423495aa02e925532dc7e1a44074d4a4b8d41760756084bb0401686a75b1a371f577927e655dcf2dc4758647111321136eebf368e0f7db5e3
6
+ metadata.gz: d712d8892d2a4691bc6226591022aac9c5140d1251b8aa85d7cd03ab0603a0d163b04771c2d90d413e85c74123f5790f96ca781dc1131b1f14e23206b2868c64
7
+ data.tar.gz: b4a40eb0127ceed35b879a3fb04944f68e315cb0623a45c1e185bec8acab5e37c41b9c592cbb0b3d38488065bd2bc337b7613f9fcb01f161046cbf66e632000a
@@ -1 +1,2 @@
1
+ require 'y_support'
1
2
  require 'y_support/core_ext/array/misc'
@@ -1 +1,2 @@
1
+ require 'y_support'
1
2
  require 'y_support/core_ext/enumerable/misc'
@@ -1 +1,2 @@
1
+ require 'y_support'
1
2
  require 'y_support/core_ext/hash/misc'
@@ -1,4 +1,16 @@
1
1
  #encoding: utf-8
2
2
 
3
3
  class Module
4
+ # Sets a constant to a value if this has not been previously defined.
5
+ #
6
+ def const_set_if_not_defined( const, value )
7
+ const_set( const, value ) unless const_defined? const
8
+ end
9
+
10
+ # Redefines a constant without warning.
11
+ #
12
+ def const_reset!( const, value )
13
+ send :remove_const, const if const_defined? const
14
+ const_set( const, value )
15
+ end
4
16
  end
@@ -1 +1,2 @@
1
+ require 'y_support'
1
2
  require 'y_support/core_ext/numeric/misc'
@@ -1,31 +1,25 @@
1
- # -*- coding: utf-8 -*-
1
+ # encoding: utf-8
2
2
 
3
3
  class Object
4
- def const_set_if_not_defined( const, value )
5
- mod = self.is_a?(Module) ? self : self.singleton_class
6
- mod.const_set( const, value ) unless mod.const_defined?( const )
7
- end
8
-
9
- def const_redefine_without_warning( const, value )
10
- mod = self.is_a?(Module) ? self : self.singleton_class
11
- mod.send(:remove_const, const) if mod.const_defined?( const )
12
- mod.const_set( const, value )
4
+ # Assigns prescribed atrributes to the object and makes them accessible with
5
+ # getter (reader) methods. Optional argument +:overwrite_methods+ enables the
6
+ # readers to overwrite existing methods.
7
+ #
8
+ def set_attr_with_readers( overwrite_methods: false, **hash )
9
+ hash.each_pair { |symbol, value|
10
+ instance_variable_set "@#{symbol}", value
11
+ fail NameError, "Method \##{symbol} already defined!" if
12
+ methods.include? symbol unless overwrite_methods == true
13
+ singleton_class.class_exec { attr_reader symbol }
14
+ }
13
15
  end
14
16
 
15
- # Create public attributes (ie. with readers) and initialize them with
16
- # prescribed values. Takes a hash of { symbol => value } pairs. Existing methods
17
- # are not overwritten by the new getters, unless option :overwrite_methods
18
- # is set to true.
19
- def singleton_set_attr_with_readers( hash, oo = {} )
20
- hash.each { |key, val|
21
- key = key.aE_respond_to( :to_sym, "key of the attr hash" ).to_sym
22
- instance_variable_set( "@#{key}", val )
23
- if oo[:overwrite_methods] then ⓒ.module_exec { attr_reader key }
24
- elsif methods.include? key
25
- raise "Attempt to add \##{key} getter failed: " +
26
- "method \##{key} already defined."
27
- else ⓒ.module_exec { attr_reader key } end
28
- }
17
+ # Expects a hash of pairs { name: class }, and a hash of parameters. Creates
18
+ # subclasses parametrized with the supplied parameters as the object attributes
19
+ # and makes them accessible under the supplied names (as reader methods).
20
+ #
21
+ def parametrizes hash, with: (fail ArgumentError, "No parameters!")
22
+ hash.each { |ß, ç| set_attr_with_readers ß => ç.parametrize( **with ) }
23
+ return nil
29
24
  end
30
- alias :ⓒ_set_attr_w_readers :singleton_set_attr_with_readers
31
25
  end
@@ -1 +1,2 @@
1
+ require 'y_support'
1
2
  require 'y_support/core_ext/object/misc'
@@ -1 +1,2 @@
1
+ require 'y_support'
1
2
  require 'y_support/core_ext/string/misc'
@@ -1 +1,2 @@
1
+ require 'y_support'
1
2
  require 'y_support/core_ext/symbol/misc'
@@ -1,3 +1,3 @@
1
1
  module YSupport
2
- VERSION = "2.0.23"
2
+ VERSION = "2.0.24"
3
3
  end
data/test/misc_test.rb CHANGED
@@ -9,17 +9,12 @@ describe Object do
9
9
  require 'y_support/core_ext/object'
10
10
  end
11
11
 
12
- it "should have #const_set_if_not_defined" do
13
- ( = Object.new ).const_set_if_not_defined :KOKO, 42
14
- assert_equal 42, ◉.singleton_class::KOKO
15
- ◉.const_set_if_not_defined :KOKO, 43
16
- assert_equal 42, ◉.singleton_class::KOKO
17
- end
18
-
19
- it "should have #const_redef_without_warning" do
20
- ( ◉ = Object.new ).const_set_if_not_defined :KOKO, 42
21
- ◉.const_redefine_without_warning :KOKO, 43
22
- assert_equal 43, ◉.singleton_class::KOKO
12
+ it "should have #parametrizes" do
13
+ o = Object.new
14
+ o.parametrizes( { Array: Array, foo: Hash }, with: { mother: o } )
15
+ assert o.Array < Array
16
+ o.Array.mother.must_equal( o )
17
+ o.foo.mother.must_equal( o )
23
18
  end
24
19
  end
25
20
 
@@ -29,7 +24,12 @@ describe Module do
29
24
  require 'y_support/core_ext/module'
30
25
  end
31
26
 
32
- it "presently has no extensions" do
27
+ it "has #const_set_if_not_defined and #const_reset!" do
28
+ m = Module.new
29
+ m.const_set_if_not_defined :Foo, 42
30
+ m::Foo.must_equal 42
31
+ m.const_reset! :Foo, 43
32
+ m::Foo.must_equal 43
33
33
  end
34
34
  end
35
35
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: y_support
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.23
4
+ version: 2.0.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - boris