y_support 2.1.1 → 2.1.2
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 +4 -4
- data/lib/y_support/core_ext/object/inspection.rb +9 -0
- data/lib/y_support/core_ext/object.rb +1 -0
- data/lib/y_support/core_ext/string/misc.rb +7 -0
- data/lib/y_support/typing/object/typing.rb +50 -64
- data/lib/y_support/version.rb +1 -1
- data/test/typing_test.rb +25 -25
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: baadc96c9834da6e35bc3b2b9fc8406123c3c68c
|
4
|
+
data.tar.gz: e956e788aabb00f16ab6bef159b5900a00e945de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 06615d7a25a37c48818e8553c0d0f528397ec5035c04bd1fbd5b1a991b4646dbd928fe3f620b4b51cde6c226c5aacab1b4baab3e78495ca1935d662fe66bbb48
|
7
|
+
data.tar.gz: 9a235fe65a3a7b51f8e037fb0f50d49dfe5f14e75b74a4b8ff4fa98c6a7fe4beea0d9d19b0a71259ec0f560d0242f33a94466914f3fa57d0e23681aa93106eb9
|
@@ -77,4 +77,11 @@ class String
|
|
77
77
|
standardize
|
78
78
|
.to_sym
|
79
79
|
end
|
80
|
+
|
81
|
+
# Capitalizes a string and append an exclamation mark. Also allows optional
|
82
|
+
# argument for string interpolation. Handy for constructing error messages.
|
83
|
+
#
|
84
|
+
def X! arg=nil
|
85
|
+
arg.nil? ? capitalize + ?! : ( self % arg ).X!
|
86
|
+
end
|
80
87
|
end
|
@@ -1,6 +1,8 @@
|
|
1
1
|
#encoding: utf-8
|
2
2
|
|
3
3
|
require 'active_support/core_ext/object/blank'
|
4
|
+
require 'y_support/core_ext/object/inspection'
|
5
|
+
require 'y_support/core_ext/string/misc'
|
4
6
|
|
5
7
|
class Object
|
6
8
|
# === Support for typing by declaration
|
@@ -46,13 +48,12 @@ class Object
|
|
46
48
|
# the receiver (using #instance_exec method). If no block is given, it is
|
47
49
|
# checked, whether the object is truey.
|
48
50
|
#
|
49
|
-
def aT what_is_receiver=
|
51
|
+
def aT what_is_receiver=insp, how_comply=nil, &b
|
50
52
|
if block_given? then
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
tap { b.( self ) or fail TypeError, m }
|
53
|
+
if b.( self ) then self else
|
54
|
+
m = "%s fails " + how_comply ? "to #{how_comply}" : "its check"
|
55
|
+
fail TypeError, m.X!( what_is_receiver )
|
56
|
+
end
|
56
57
|
else self or fail TypeError end
|
57
58
|
end
|
58
59
|
|
@@ -65,107 +66,92 @@ class Object
|
|
65
66
|
# inside the singleton class of the receiver (using #instance_exec method). If
|
66
67
|
# no block is given, it is checked, whether the object is falsey.
|
67
68
|
#
|
68
|
-
def aT_not what_is_receiver=
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
69
|
+
def aT_not what_is_receiver=insp, how_comply=nil, &b
|
70
|
+
tap do
|
71
|
+
if block_given? then
|
72
|
+
if b.( self ) then
|
73
|
+
m = how_comply ? "%s must not #{how_comply}" : "%s fails its check"
|
74
|
+
fail TypeError, m.X!( what_is_receiver )
|
75
|
+
end
|
76
|
+
else fail TypeError if self end
|
77
|
+
end
|
76
78
|
end
|
77
79
|
|
78
80
|
# Fails with TypeError unless the receiver is of the prescribed class. Second
|
79
81
|
# optional argument customizes the error message (receiver description).
|
80
82
|
#
|
81
|
-
def aT_kind_of klass, what_is_receiver=
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
end
|
86
|
-
tap { kind_of? klass or fail TypeError, m }
|
83
|
+
def aT_kind_of klass, what_is_receiver=insp
|
84
|
+
tap do
|
85
|
+
is_a? klass or fail TypeError, "%s not a #{klass}".X!( what_is_receiver )
|
86
|
+
end
|
87
87
|
end
|
88
|
-
alias
|
88
|
+
alias aT_is_a aT_kind_of
|
89
89
|
|
90
90
|
# Fails with TypeError unless the receiver declares compliance with the
|
91
91
|
# given class, or is a descendant of that class. Second optional argument
|
92
92
|
# customizes the error message (receiver description).
|
93
93
|
#
|
94
|
-
def aT_class_complies klass, what_is_receiver=
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
end
|
99
|
-
tap { class_complies? klass or fail TypeError, m }
|
94
|
+
def aT_class_complies klass, what_is_receiver=insp
|
95
|
+
if class_complies? klass then
|
96
|
+
fail TypeError, "%s does not comply with #{klass}".X!( what_is_receiver )
|
97
|
+
else self end
|
100
98
|
end
|
101
99
|
|
102
100
|
# Fails with TypeError unless the receiver responds to the given
|
103
101
|
# method. Second optional argument customizes the error message (receiver
|
104
102
|
# description).
|
105
103
|
#
|
106
|
-
def aT_respond_to method_name, what_is_receiver=
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
tap { respond_to? method_name or fail TypeError, m }
|
104
|
+
def aT_respond_to method_name, what_is_receiver=insp
|
105
|
+
if respond_to? method_name then self else
|
106
|
+
m = "%s does not respond to method '#{method_name}'"
|
107
|
+
fail TypeError, m.X!( what_is_receiver )
|
108
|
+
end
|
112
109
|
end
|
113
|
-
alias
|
110
|
+
alias aT_responds_to aT_respond_to
|
114
111
|
|
115
112
|
# Fails with TypeError unless the receiver, according to #== method, is
|
116
113
|
# equal to the argument. Two more optional arguments customize the error
|
117
114
|
# message (receiver description and the description of the other object).
|
118
115
|
#
|
119
|
-
def aT_equal other, what_is_receiver=
|
120
|
-
|
121
|
-
"
|
122
|
-
|
123
|
-
|
124
|
-
tap { self == other or fail TypeError, m }
|
116
|
+
def aT_equal other, what_is_receiver=insp, what_is_other=nil
|
117
|
+
if self == other then self else
|
118
|
+
wo = what_is_other || "the prescribed value (#{other.insp})"
|
119
|
+
fail TypeError, "%s must be equal to %s".X!( [ what_is_receiver, wo ] )
|
120
|
+
end
|
125
121
|
end
|
126
122
|
|
127
123
|
# Fails with TypeError unless the receiver, according to #== method, differs
|
128
124
|
# from to the argument. Two more optional arguments customize the error
|
129
125
|
# message (receiver description and the description of the other object).
|
130
126
|
#
|
131
|
-
def aT_not_equal other, what_is_receiver=
|
132
|
-
|
133
|
-
"
|
134
|
-
|
135
|
-
|
136
|
-
tap { fail TypeError, m if self == other }
|
127
|
+
def aT_not_equal other, what_is_receiver=insp, what_is_other=nil
|
128
|
+
if self == other
|
129
|
+
wo = what_is_other || "the prescribed value (#{other.insp})"
|
130
|
+
fail TypeError, "%s must not == %s".X!( [ what_is_receiver, wo ] )
|
131
|
+
else self end
|
137
132
|
end
|
138
133
|
|
139
134
|
# Fails with TypeError unless the ActiveSupport method #blank returns true
|
140
135
|
# for the receiver.
|
141
136
|
#
|
142
|
-
def aT_blank what_is_receiver=
|
143
|
-
|
144
|
-
m = "#%s fails to be blank!" %
|
145
|
-
if what_is_receiver then what_is_receiver.to_s.capitalize else
|
146
|
-
"#{self.class} instance #{object_id}"
|
147
|
-
end
|
148
|
-
tap { blank? or fail TypeError, m }
|
137
|
+
def aT_blank what_is_receiver=insp
|
138
|
+
tap { blank? or fail TypeError, "%s not blank".X!( what_is_receiver ) }
|
149
139
|
end
|
150
140
|
|
151
141
|
# Fails with TypeError unless the ActiveSupport method #present returns true
|
152
142
|
# for the receiver.
|
153
143
|
#
|
154
|
-
def aT_present what_is_receiver=
|
155
|
-
|
156
|
-
if what_is_receiver then what_is_receiver.to_s.capitalize else
|
157
|
-
"#{self.class} instance #{object_id}"
|
158
|
-
end
|
159
|
-
tap { present? or fail TypeError, m }
|
144
|
+
def aT_present what_is_receiver=insp
|
145
|
+
tap { present? or fail TypeError, "%s not present".X!( what_is_receiver ) }
|
160
146
|
end
|
161
147
|
|
162
148
|
private
|
163
149
|
|
150
|
+
# Some objects do not have accessible singleton class. This method returns
|
151
|
+
# the singleton class for those object, which have a singleton class, and
|
152
|
+
# self.class for others.
|
153
|
+
#
|
164
154
|
def singleton_class_or_class
|
165
|
-
begin
|
166
|
-
self.singleton_class
|
167
|
-
rescue TypeError
|
168
|
-
self.class
|
169
|
-
end
|
155
|
+
begin; self.singleton_class; rescue TypeError; self.class end
|
170
156
|
end
|
171
157
|
end
|
data/lib/y_support/version.rb
CHANGED
data/test/typing_test.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#! /
|
1
|
+
#! /Usr/Bin/ruby
|
2
2
|
#encoding: utf-8
|
3
3
|
|
4
4
|
require 'test/unit'
|
@@ -38,31 +38,31 @@ class TypingTest < Test::Unit::TestCase
|
|
38
38
|
end
|
39
39
|
|
40
40
|
should "have #aT raising TypeError if block falsey" do
|
41
|
-
assert_raise
|
41
|
+
assert_raise TypeError do 0.aT "yada yada" do |rcvr| rcvr == 1 end end
|
42
42
|
assert_nothing_raised do 0.aT "yada yada" do |rcvr| rcvr == 0 end end
|
43
43
|
assert_equal( "hello",
|
44
44
|
"hello".aT( "have 4 unique letters" ) { |str|
|
45
45
|
str.each_char.map { |e| e }.uniq.join.size == 4
|
46
46
|
} )
|
47
47
|
assert_nothing_raised do 2.aT &:even? end
|
48
|
-
assert_raise
|
49
|
-
assert_raise
|
48
|
+
assert_raise TypeError do 3.aT &:even? end
|
49
|
+
assert_raise TypeError do nil.aT end
|
50
50
|
end
|
51
51
|
|
52
52
|
should "have #aT_not raising TypeError if block truey" do
|
53
|
-
assert_raise
|
53
|
+
assert_raise TypeError do 0.aT_not { |rcvr| rcvr < 1 } end
|
54
54
|
assert_nothing_raised do 1.aT_not { |rcvr| rcvr == 2 } end
|
55
55
|
assert_equal( "hello", "hello".aT_not( "have x" ) { |rcvr| rcvr.include? 'x' } )
|
56
56
|
assert_nothing_raised do 3.aT_not &:even? end
|
57
|
-
assert_raise
|
58
|
-
assert_raise
|
57
|
+
assert_raise TypeError do 2.aT_not &:even? end
|
58
|
+
assert_raise TypeError do "".aT_not end
|
59
59
|
end
|
60
60
|
|
61
|
-
should "have #aT_kind_of, alias #aT_is_a
|
62
|
-
assert_raise
|
61
|
+
should "have #aT_kind_of, alias #aT_is_a TypeError enforcers" do
|
62
|
+
assert_raise TypeError do :o.aT_kind_of Numeric end
|
63
63
|
assert_nothing_raised do 0.aT_kind_of Numeric end
|
64
64
|
assert_equal( "hello", "hello".aT_kind_of( String ) )
|
65
|
-
assert_raise
|
65
|
+
assert_raise TypeError do :o.aT_is_a Numeric end
|
66
66
|
assert_nothing_raised do 0.aT_is_a Numeric end
|
67
67
|
assert_equal( "hello", "hello".aT_is_a( String ) )
|
68
68
|
end
|
@@ -71,42 +71,42 @@ class TypingTest < Test::Unit::TestCase
|
|
71
71
|
Koko = Class.new; Pipi = Class.new
|
72
72
|
koko = Koko.new; pipi = Pipi.new
|
73
73
|
pipi.declare_class_compliance! koko.class
|
74
|
-
assert_raise
|
74
|
+
assert_raise TypeError do koko.aT_class_complies pipi.class end
|
75
75
|
assert_nothing_raised do koko.aT_class_complies koko.class end
|
76
76
|
assert_nothing_raised do pipi.aT_class_complies pipi.class end
|
77
77
|
assert_nothing_raised do pipi.aT_class_complies koko.class end
|
78
|
-
assert_raise
|
78
|
+
assert_raise TypeError do koko.aT_class_complies Pipi end
|
79
79
|
assert_nothing_raised do pipi.aT_class_complies Pipi end
|
80
80
|
assert_nothing_raised do pipi.aT_class_complies Koko end
|
81
81
|
assert_equal koko, koko.aT_class_complies( Koko )
|
82
82
|
end
|
83
83
|
|
84
|
-
should "have #aT_respond_to
|
85
|
-
assert_raise
|
84
|
+
should "have #aT_respond_to assertion" do
|
85
|
+
assert_raise TypeError do :o.aT_respond_to :each end
|
86
86
|
assert_nothing_raised do {}.aT_respond_to :each end
|
87
87
|
assert_equal( [:hello], [:hello].aT_respond_to( :each ) )
|
88
88
|
end
|
89
89
|
|
90
90
|
should "have #aT_equal enforcer" do
|
91
|
-
assert_raise
|
91
|
+
assert_raise TypeError do 0.aT_equal 1 end
|
92
92
|
assert_nothing_raised do 1.aT_equal 2.0/2.0 end
|
93
93
|
assert_equal( "hello", "hello".aT_equal( " hello ".strip ) )
|
94
94
|
end
|
95
95
|
|
96
96
|
should "have #aT_not_equal enforcer" do
|
97
|
-
assert_raise
|
97
|
+
assert_raise TypeError do 1.aT_not_equal 1.0 end
|
98
98
|
assert_nothing_raised do 7.aT_not_equal 42 end
|
99
99
|
assert_equal( "hello", "hello".aT_not_equal( "goodbye" ) )
|
100
100
|
end
|
101
101
|
|
102
102
|
should "have #aT_blank enforcer" do
|
103
|
-
assert_raise
|
103
|
+
assert_raise TypeError do "x".aT_blank end
|
104
104
|
assert_nothing_raised do ["", []].each{|e| e.aT_blank } end
|
105
105
|
assert_equal( {}, {}.aT_blank )
|
106
106
|
end
|
107
107
|
|
108
108
|
should "have #aT_present enforcer" do
|
109
|
-
assert_raise
|
109
|
+
assert_raise TypeError do nil.aT_present end
|
110
110
|
assert_nothing_raised do 0.aT_present end
|
111
111
|
assert_equal( "hello", "hello".aT_present )
|
112
112
|
end
|
@@ -114,34 +114,34 @@ class TypingTest < Test::Unit::TestCase
|
|
114
114
|
|
115
115
|
context "Enumerable" do
|
116
116
|
should "have #aT_all enforcer" do
|
117
|
-
assert_raise
|
117
|
+
assert_raise TypeError do [1, 2, 7].aT_all { |e| e < 5 } end
|
118
118
|
assert_nothing_raised do [1, 2, 4].aT_all { |e| e < 5 } end
|
119
119
|
end
|
120
120
|
|
121
121
|
should "have #aT_all_kind_of enforcer" do
|
122
|
-
assert_raise
|
122
|
+
assert_raise TypeError do [1.0, 2.0, :a].aT_all_kind_of Numeric end
|
123
123
|
assert_nothing_raised do [1.0, 2.0, 3].aT_all_kind_of Numeric end
|
124
124
|
end
|
125
125
|
|
126
126
|
should "have #aT_all_comply class compliance enforcer" do
|
127
|
-
assert_raise
|
127
|
+
assert_raise TypeError do [1.0, 2.0, :a].aT_all_comply Numeric end
|
128
128
|
assert_nothing_raised do [1.0, 2.0, 3].aT_all_comply Numeric end
|
129
129
|
end
|
130
130
|
|
131
131
|
should "have #aT_all_numeric enforcer" do
|
132
|
-
assert_raise
|
132
|
+
assert_raise TypeError do [:a].aT_all_numeric end
|
133
133
|
assert_nothing_raised do [1, 2.0].aT_all_numeric end
|
134
134
|
end
|
135
135
|
|
136
136
|
should "have #aT_subset_of enforcer" do
|
137
|
-
assert_raise
|
137
|
+
assert_raise TypeError do [6].aT_subset_of [*0..5] end
|
138
138
|
assert_nothing_raised do [1,2].aT_subset_of [*0..5] end
|
139
139
|
end
|
140
140
|
end # context Enumerable
|
141
141
|
|
142
142
|
context "Array" do
|
143
143
|
should "have #aT_includes (alias #aT_include) enforcer" do
|
144
|
-
assert_raise
|
144
|
+
assert_raise TypeError do [1, 2, 4].aT_includes 3 end
|
145
145
|
assert_nothing_raised do [1, 2, 4].aT_includes( 4 ).aT_include( 4 ) end
|
146
146
|
assert_equal [6, 7], [6, 7].aT_includes( 6 )
|
147
147
|
assert_equal [6, 7], [6, 7].aT_include( 6 )
|
@@ -160,7 +160,7 @@ class TypingTest < Test::Unit::TestCase
|
|
160
160
|
assert_equal true, a.merge_synonym_keys!( :k, :o, :t )
|
161
161
|
assert_equal( { a: 'a', b: 'b', k: 'k' }, a )
|
162
162
|
old = a.dup
|
163
|
-
assert_raise
|
163
|
+
assert_raise TypeError do a.merge_synonym_keys!( :a, :b ) end
|
164
164
|
assert_equal old, a
|
165
165
|
assert_equal true, a.merge_synonym_keys!( :c, :b )
|
166
166
|
assert_equal( { a: 'a', c: 'b', k: 'k' }, a )
|
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.1.
|
4
|
+
version: 2.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- boris
|
@@ -67,6 +67,7 @@ files:
|
|
67
67
|
- lib/y_support/core_ext/numeric.rb
|
68
68
|
- lib/y_support/core_ext/numeric/misc.rb
|
69
69
|
- lib/y_support/core_ext/object.rb
|
70
|
+
- lib/y_support/core_ext/object/inspection.rb
|
70
71
|
- lib/y_support/core_ext/object/misc.rb
|
71
72
|
- lib/y_support/core_ext/string.rb
|
72
73
|
- lib/y_support/core_ext/string/misc.rb
|