y_support 2.0.23 → 2.0.24
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/array.rb +1 -0
- data/lib/y_support/core_ext/enumerable.rb +1 -0
- data/lib/y_support/core_ext/hash.rb +1 -0
- data/lib/y_support/core_ext/module/misc.rb +12 -0
- data/lib/y_support/core_ext/numeric.rb +1 -0
- data/lib/y_support/core_ext/object/misc.rb +19 -25
- data/lib/y_support/core_ext/object.rb +1 -0
- data/lib/y_support/core_ext/string.rb +1 -0
- data/lib/y_support/core_ext/symbol.rb +1 -0
- data/lib/y_support/version.rb +1 -1
- data/test/misc_test.rb +12 -12
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d8afafc604de2cef2f7c7e22e744db08f5472efe
|
4
|
+
data.tar.gz: 8fbfc61eec9b5dd2f323f7d6d27123146bffe66b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d712d8892d2a4691bc6226591022aac9c5140d1251b8aa85d7cd03ab0603a0d163b04771c2d90d413e85c74123f5790f96ca781dc1131b1f14e23206b2868c64
|
7
|
+
data.tar.gz: b4a40eb0127ceed35b879a3fb04944f68e315cb0623a45c1e185bec8acab5e37c41b9c592cbb0b3d38488065bd2bc337b7613f9fcb01f161046cbf66e632000a
|
@@ -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,31 +1,25 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
|
3
3
|
class Object
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
-
#
|
16
|
-
#
|
17
|
-
#
|
18
|
-
#
|
19
|
-
def
|
20
|
-
hash.each {
|
21
|
-
|
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
|
data/lib/y_support/version.rb
CHANGED
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 #
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
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 "
|
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
|
|