y_support 2.1.1 → 2.1.2

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: cc599e5fe91dcb19ccd68fd32429f95b63ded715
4
- data.tar.gz: 2bc37581f0ccd3a5bbe9496ec289b0b883958dfc
3
+ metadata.gz: baadc96c9834da6e35bc3b2b9fc8406123c3c68c
4
+ data.tar.gz: e956e788aabb00f16ab6bef159b5900a00e945de
5
5
  SHA512:
6
- metadata.gz: 8c7f97c8a6d64c095ff12c9372b300c9a4a9bb5086537c3caee403e03b68a45dd1a0ddc8319b9bf1b94a9f31667f63d11a8d8cb846cd97b91707d79f54535976
7
- data.tar.gz: c70caa248db66904b1d1393c41fd48bee5abf70a9cc9a326ab766e62196975ee2b6b93de8267e30c5da950e877ad8c0a24461cc0a5dc1dab0c4224f8e3f16fa7
6
+ metadata.gz: 06615d7a25a37c48818e8553c0d0f528397ec5035c04bd1fbd5b1a991b4646dbd928fe3f620b4b51cde6c226c5aacab1b4baab3e78495ca1935d662fe66bbb48
7
+ data.tar.gz: 9a235fe65a3a7b51f8e037fb0f50d49dfe5f14e75b74a4b8ff4fa98c6a7fe4beea0d9d19b0a71259ec0f560d0242f33a94466914f3fa57d0e23681aa93106eb9
@@ -0,0 +1,9 @@
1
+ # encoding: utf-8
2
+
3
+ class Object
4
+ # Constructs the string "#{self.class}:#{self}". Useful for inspection.
5
+ #
6
+ def insp
7
+ "#{self.class}:#{self}"
8
+ end
9
+ end
@@ -1,2 +1,3 @@
1
1
  require 'y_support'
2
2
  require 'y_support/core_ext/object/misc'
3
+ require 'y_support/core_ext/object/inspection'
@@ -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=nil, how_comply=nil, &b
51
+ def aT what_is_receiver=insp, how_comply=nil, &b
50
52
  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 }
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=nil, how_comply=nil, &b
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
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=nil
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 }
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 :aT_is_a :aT_kind_of
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=nil
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 }
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=nil
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 }
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 :aT_responds_to :aT_respond_to
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=nil, what_is_other=nil
120
- r = what_is_receiver ? what_is_receiver.to_s.capitalize :
121
- "#{self.class} instance #{object_id}"
122
- o = what_is_other || "the prescribed value (#{other.class})"
123
- m = "%s is not equal (==) to %s!" % [r, o]
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=nil, what_is_other=nil
132
- r = what_is_receiver ? what_is_receiver.to_s.capitalize :
133
- "#{self.class} instance #{object_id}"
134
- o = what_is_other || "the prescribed value (#{other.class})"
135
- m = "%s fails to differ from %s!" % [r, o]
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=nil
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 }
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=nil
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 }
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
@@ -1,4 +1,4 @@
1
1
  module YSupport
2
- VERSION = "2.1.1"
2
+ VERSION = "2.1.2"
3
3
  DEBUG = false
4
4
  end
data/test/typing_test.rb CHANGED
@@ -1,4 +1,4 @@
1
- #! /usr/bin/ruby
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 TErr do 0.aT "yada yada" do |rcvr| rcvr == 1 end end
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 TErr do 3.aT &:even? end
49
- assert_raise TErr do nil.aT end
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 TErr do 0.aT_not { |rcvr| rcvr < 1 } end
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 TErr do 2.aT_not &:even? end
58
- assert_raise TErr do "".aT_not end
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 TErr enforcers" do
62
- assert_raise TErr do :o.aT_kind_of Numeric end
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 TErr do :o.aT_is_a Numeric end
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 TErr do koko.aT_class_complies pipi.class end
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 TErr do koko.aT_class_complies Pipi end
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 enforcer" do
85
- assert_raise TErr do :o.aT_respond_to :each end
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 TErr do 0.aT_equal 1 end
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 TErr do 1.aT_not_equal 1.0 end
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 TErr do "x".aT_blank end
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 TErr do nil.aT_present end
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 TErr do [1, 2, 7].aT_all { |e| e < 5 } end
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 TErr do [1.0, 2.0, :a].aT_all_kind_of Numeric end
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 TErr do [1.0, 2.0, :a].aT_all_comply Numeric end
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 TErr do [:a].aT_all_numeric end
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 TErr do [6].aT_subset_of [*0..5] end
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 TErr do [1, 2, 4].aT_includes 3 end
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 TErr do a.merge_synonym_keys!( :a, :b ) end
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.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