y_support 2.0.20 → 2.0.21
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/array/misc.rb +8 -1
- data/lib/y_support/typing/array/typing.rb +13 -6
- data/lib/y_support/typing/object/typing.rb +44 -51
- data/lib/y_support/version.rb +1 -1
- data/test/misc_test.rb +4 -0
- data/test/typing_test.rb +5 -5
- 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: 6981d4eb5fdec5cb5eaf09321d4081c39b65c4db
|
4
|
+
data.tar.gz: 472b3008961422d3823f0b5b654ca2ecfadbd4f1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c0fe48e25cd93a0b8abce16b12b227d408f1f2e99b5f0275abd13a56735ef78e7b303ad22f18fa9ff15e695f02cd050e3ae32cd3d2130786e9c479ffacc2549f
|
7
|
+
data.tar.gz: dbcfa5713ba17cbc1dc64e65f18ad31497faeb82d6943cead57f1504b9e78e1c3ef17bf61d9b18a1684ca8be7e0f224c21b935c16884e3abe2cb0fcc726265a6
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'matrix'
|
2
|
+
|
1
3
|
class Array
|
2
4
|
# Converts an array, whose elements are also arrays, to a hash. Head
|
3
5
|
# (position 0) of each array is made to point at the rest of the array
|
@@ -12,7 +14,6 @@ class Array
|
|
12
14
|
}
|
13
15
|
end
|
14
16
|
|
15
|
-
|
16
17
|
# Zips this array with another collection into a hash. If a block is given,
|
17
18
|
# it is applied to each element of the array to get the hash values.
|
18
19
|
#
|
@@ -79,6 +80,12 @@ class Array
|
|
79
80
|
l = last
|
80
81
|
l.delete( key ).tap { pop if l.empty? } if l.is_a? Hash
|
81
82
|
end
|
83
|
+
|
84
|
+
# Converts the array to a +Matrix#column_vector+.
|
85
|
+
#
|
86
|
+
def to_column_vector
|
87
|
+
Matrix.column_vector self
|
88
|
+
end
|
82
89
|
|
83
90
|
# TEST ME
|
84
91
|
# def pretty_inspect
|
@@ -6,12 +6,19 @@ class Array
|
|
6
6
|
# fails to include the specified element. An optional argument customizes the
|
7
7
|
# error message (element description).
|
8
8
|
#
|
9
|
-
def aT_includes element, what_is_element=nil
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
9
|
+
def aT_includes element, what_is_self="array", what_is_element=nil
|
10
|
+
m = "%s is absent from #{what_is_self}!" %
|
11
|
+
if what_is_element then what_is_element.to_s.capitalize else
|
12
|
+
"Element (#{element.class} instance)"
|
13
|
+
end
|
14
|
+
tap { include? element or fail TypeError, m }
|
15
15
|
end
|
16
16
|
alias :aT_include :aT_includes
|
17
|
+
|
18
|
+
# Fails with TypeError if the array contains duplicates (using +#uniq+).
|
19
|
+
#
|
20
|
+
def aT_uniq what_is_self="array"
|
21
|
+
m = "#{what_is_self.to_s.capitalize} non-uniq!"
|
22
|
+
tap { self == uniq or fail TypeError, m }
|
23
|
+
end
|
17
24
|
end
|
@@ -47,15 +47,13 @@ class Object
|
|
47
47
|
# checked, whether the object is truey.
|
48
48
|
#
|
49
49
|
def aT what_is_receiver=nil, how_comply=nil, &b
|
50
|
-
|
51
|
-
"#{
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
end
|
58
|
-
return self
|
50
|
+
if block_given? then
|
51
|
+
m = "%s fails #{how_comply ? 'to %s' % how_comply : 'its duck type'}!" %
|
52
|
+
if what_is_receiver then what_is_receiver.to_s.capitalize else
|
53
|
+
"#{self.class} instance #{object_id}"
|
54
|
+
end
|
55
|
+
tap { b.( self ) or fail TypeError, m }
|
56
|
+
else self or fail TypeError end
|
59
57
|
end
|
60
58
|
|
61
59
|
# This method takes a block and fails with TypeError, unless the receiver
|
@@ -68,28 +66,24 @@ class Object
|
|
68
66
|
# no block is given, it is checked, whether the object is falsey.
|
69
67
|
#
|
70
68
|
def aT_not what_is_receiver=nil, how_comply=nil, &b
|
71
|
-
|
72
|
-
"
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
else
|
78
|
-
m = "#{r} is not falsey!"
|
79
|
-
raise TErr, m if self
|
80
|
-
end
|
81
|
-
return self
|
69
|
+
if block_given? then
|
70
|
+
m = how_comply ? "%s must not #{how_comply}!" : "%s fails its duck type!"
|
71
|
+
m %= if what_is_receiver then what_is_receiver.to_s.capitalize else
|
72
|
+
"#{self.class} instance #{object_id}"
|
73
|
+
end
|
74
|
+
tap { fail TypeError, m if b.( self ) }
|
75
|
+
else tap { fail TypeError if self } end
|
82
76
|
end
|
83
77
|
|
84
78
|
# Fails with TypeError unless the receiver is of the prescribed class. Second
|
85
79
|
# optional argument customizes the error message (receiver description).
|
86
80
|
#
|
87
81
|
def aT_kind_of klass, what_is_receiver=nil
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
82
|
+
m = "%s is not a kind of #{klass}!" %
|
83
|
+
if what_is_receiver then what_is_receiver.to_s.capitalize else
|
84
|
+
"#{self.class} instance #{object_id}"
|
85
|
+
end
|
86
|
+
tap { kind_of? klass or fail TypeError, m }
|
93
87
|
end
|
94
88
|
alias :aT_is_a :aT_kind_of
|
95
89
|
|
@@ -98,11 +92,11 @@ class Object
|
|
98
92
|
# customizes the error message (receiver description).
|
99
93
|
#
|
100
94
|
def aT_class_complies klass, what_is_receiver=nil
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
95
|
+
m = "%s does not comply or declare compliance with #{klass}!" %
|
96
|
+
if what_is_receiver then what_is_receiver.to_s.capitalize else
|
97
|
+
"#{self.class} instance #{object_id}"
|
98
|
+
end
|
99
|
+
tap { class_complies? klass or fail TypeError, m }
|
106
100
|
end
|
107
101
|
|
108
102
|
# Fails with TypeError unless the receiver responds to the given
|
@@ -110,11 +104,11 @@ class Object
|
|
110
104
|
# description).
|
111
105
|
#
|
112
106
|
def aT_respond_to method_name, what_is_receiver=nil
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
107
|
+
m = "%s does not respond to method '#{method_name}'!" %
|
108
|
+
if what_is_receiver then what_is_receiver.to_s.capitalize else
|
109
|
+
"#{self.class} instance #{object_id}"
|
110
|
+
end
|
111
|
+
tap { respond_to? method_name or fail TypeError, m }
|
118
112
|
end
|
119
113
|
alias :aT_responds_to :aT_respond_to
|
120
114
|
|
@@ -126,9 +120,8 @@ class Object
|
|
126
120
|
r = what_is_receiver ? what_is_receiver.to_s.capitalize :
|
127
121
|
"#{self.class} instance #{object_id}"
|
128
122
|
o = what_is_other || "the prescribed value (#{other.class})"
|
129
|
-
m = "
|
130
|
-
|
131
|
-
return self
|
123
|
+
m = "%s is not equal (==) to %s!" % [r, o]
|
124
|
+
tap { self == other or fail TypeError, m }
|
132
125
|
end
|
133
126
|
|
134
127
|
# Fails with TypeError unless the receiver, according to #== method, differs
|
@@ -139,31 +132,31 @@ class Object
|
|
139
132
|
r = what_is_receiver ? what_is_receiver.to_s.capitalize :
|
140
133
|
"#{self.class} instance #{object_id}"
|
141
134
|
o = what_is_other || "the prescribed value (#{other.class})"
|
142
|
-
m = "
|
143
|
-
|
144
|
-
return self
|
135
|
+
m = "%s fails to differ from %s!" % [r, o]
|
136
|
+
tap { fail TypeError, m if self == other }
|
145
137
|
end
|
146
138
|
|
147
139
|
# Fails with TypeError unless the ActiveSupport method #blank returns true
|
148
140
|
# for the receiver.
|
149
141
|
#
|
150
142
|
def aT_blank what_is_receiver=nil
|
151
|
-
r =
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
143
|
+
r =
|
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 }
|
156
149
|
end
|
157
150
|
|
158
151
|
# Fails with TypeError unless the ActiveSupport method #present returns true
|
159
152
|
# for the receiver.
|
160
153
|
#
|
161
154
|
def aT_present what_is_receiver=nil
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
155
|
+
m = "%s fails to be present!" %
|
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 }
|
167
160
|
end
|
168
161
|
|
169
162
|
private
|
data/lib/y_support/version.rb
CHANGED
data/test/misc_test.rb
CHANGED
data/test/typing_test.rb
CHANGED
@@ -172,8 +172,8 @@ class TypingTest < Test::Unit::TestCase
|
|
172
172
|
old = a.dup
|
173
173
|
assert_nothing_raised do a.may_have :z end
|
174
174
|
assert_nothing_raised do a.has? :z end
|
175
|
-
assert_raises
|
176
|
-
assert_raises
|
175
|
+
assert_raises TypeError do a.may_have( :a, syn!: :b ) end
|
176
|
+
assert_raises TypeError do a.has?( :a, syn!: :b ) end
|
177
177
|
assert_equal false, a.has?( :z )
|
178
178
|
assert_equal nil, a.may_have( :z )
|
179
179
|
assert_equal false, a.has?( :z )
|
@@ -192,7 +192,7 @@ class TypingTest < Test::Unit::TestCase
|
|
192
192
|
a = { infile: 'a', csv_out_file: 'b', k: 'k', o: 'k', t: 'k' }
|
193
193
|
assert_respond_to a, :aT_has
|
194
194
|
old = a.dup
|
195
|
-
assert_raises
|
195
|
+
assert_raises TypeError do a.aT_has :z end
|
196
196
|
assert_nothing_raised do a.aT_has :infile end
|
197
197
|
assert_nothing_raised do a.aT_has :csv_out_file end
|
198
198
|
class TestClass; def initialize( args )
|
@@ -201,12 +201,12 @@ class TypingTest < Test::Unit::TestCase
|
|
201
201
|
args.aT_has :k
|
202
202
|
end end
|
203
203
|
assert_nothing_raised do TestClass.new a end
|
204
|
-
assert_raises
|
204
|
+
assert_raises TypeError do a.aT_has( :a, syn!: :b ) end
|
205
205
|
assert_equal "a", a.aT_has( :infile )
|
206
206
|
assert_equal "k", a.aT_has( :k, syn!: [:o, :t] )
|
207
207
|
assert_equal "b", a.aT_has( :c, syn!: :csv_out_file )
|
208
208
|
assert_equal( { infile: 'a', c: 'b', k: 'k' }, a )
|
209
|
-
assert_raises
|
209
|
+
assert_raises TypeError do a.aT_has(:c) {|val| val == 'c'} end
|
210
210
|
assert_nothing_raised do a.aT_has(:c) {|val| val == 'b'} end
|
211
211
|
end
|
212
212
|
end # context Hash
|
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.0.
|
4
|
+
version: 2.0.21
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- boris
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-07-
|
11
|
+
date: 2013-07-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|