Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/y_support/typing.rb,
lib/y_support/try.rb,
lib/y_support/unicode.rb,
lib/y_support/respond_to.rb,
lib/y_support/null_object.rb,
lib/y_support/local_object.rb,
lib/y_support/inert_recorder.rb,
lib/y_support/core_ext/object/misc.rb,
lib/y_support/typing/object/typing.rb
Overview
Object class is patched with #LocalObject (alias L!) constructor, and #local_object?, alias #ℓ? inquirer.
Constant Summary collapse
- AErr =
Alias for ArgumentError
ArgumentError
- TErr =
Alias for TypeError
TypeError
Instance Method Summary collapse
-
#aT(what_is_receiver = nil, how_comply = nil, &b) ⇒ Object
This method takes a block and fails with TypeError, unless the receiver fullfills the block criterion.
-
#aT_blank(what_is_receiver = nil) ⇒ Object
Fails with TypeError unless the ActiveSupport method #blank returns true for the receiver.
-
#aT_class_complies(klass, what_is_receiver = nil) ⇒ Object
Fails with TypeError unless the receiver declares compliance with the given class, or is a descendant of that class.
-
#aT_equal(other, what_is_receiver = nil, what_is_other = nil) ⇒ Object
Fails with TypeError unless the receiver, according to #== method, is equal to the argument.
-
#aT_kind_of(klass, what_is_receiver = nil) ⇒ Object
(also: #aT_is_a)
Fails with TypeError unless the receiver is of the prescribed class.
-
#aT_not(what_is_receiver = nil, how_comply = nil, &b) ⇒ Object
This method takes a block and fails with TypeError, unless the receiver causes the supplied block to return falsey value.
-
#aT_not_equal(other, what_is_receiver = nil, what_is_other = nil) ⇒ Object
Fails with TypeError unless the receiver, according to #== method, differs from to the argument.
-
#aT_present(what_is_receiver = nil) ⇒ Object
Fails with TypeError unless the ActiveSupport method #present returns true for the receiver.
-
#aT_respond_to(method_name, what_is_receiver = nil) ⇒ Object
(also: #aT_responds_to)
Fails with TypeError unless the receiver responds to the given method.
-
#class_compliance ⇒ Object
Class compliance (declared class compliance + ancestors).
-
#class_complies?(klass) ⇒ Boolean
Class compliance inquirer (declared compliance + class ancestors).
-
#class_declares_compliance?(klass) ⇒ Boolean
Declared class compliance.
- #const_redefine_without_warning(const, value) ⇒ Object
- #const_set_if_not_defined(const, value) ⇒ Object
-
#declare_class_compliance!(klass) ⇒ Object
Declaration of class compliance.
-
#declared_class_compliance ⇒ Object
Declared class compliance.
-
#InertRecorder(*args, &block) ⇒ Object
InertRecorder constructor.
-
#local_object?(signature = nil) ⇒ Boolean
(also: #ℓ?)
False for normal objects, overriden in the LocalObject class.
-
#LocalObject(signature = caller_locations( 1, 1 )[0].label) ⇒ Object
(also: #L!)
LocalObject constructor.
-
#Maybe(object, null_object_signature = nil) ⇒ Object
Converts #nil?-positive objects to a NullObject.
-
#Null(signature = nil) ⇒ Object
NullObject constructor.
-
#null_object?(signature = nil) ⇒ Boolean
(also: #null?)
Always false for ordinary objects, overriden in NullObject instances.
-
#RespondTo(method) ⇒ Object
RespondTo constructor.
-
#singleton_set_attr_with_readers(hash, oo = {}) ⇒ Object
(also: #ⓒ_set_attr_w_readers)
Create public attributes (ie. with readers) and initialize them with prescribed values.
-
#try(receiver_NL_description = self, attempt_NL_description, &block) ⇒ Object
Try method takes two textual arguments and one block.
-
#∏(collection) ⇒ Object
(also: #Π)
Product.
-
#∑(collection) ⇒ Object
(also: #Σ)
Sum.
-
#√(number) ⇒ Object
Square root (proxy for Math.sqrt(x)).
Instance Method Details
#aT(what_is_receiver = nil, how_comply = nil, &b) ⇒ Object
This method takes a block and fails with TypeError, unless the receiver fullfills the block criterion. Optional arguments customize customize the error message. First optional argument describes the receiver, the second one describes the tested duck type. If the criterion block takes at least one argument, the receiver is passed to it. If the criterion block takes no arguments (arity 0), it is executed inside the singleton class of the receiver (using #instance_exec method). If no block is given, it is checked, whether the object is truey.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/y_support/typing/object/typing.rb', line 49 def aT what_is_receiver=nil, how_comply=nil, &b r = what_is_receiver ? what_is_receiver.to_s.capitalize : "#{self.class} instance #{object_id}" if block_given? m = "#{r} fails #{how_comply ? 'to %s' % how_comply : 'its duck type'}!" raise TErr, m unless ( b.arity == 0 ) ? instance_exec( &b ) : b.( self ) else raise TErr, m unless self end return self end |
#aT_blank(what_is_receiver = nil) ⇒ Object
Fails with TypeError unless the ActiveSupport method #blank returns true for the receiver.
150 151 152 153 154 155 156 |
# File 'lib/y_support/typing/object/typing.rb', line 150 def aT_blank what_is_receiver=nil r = what_is_receiver ? what_is_receiver.to_s.capitalize : "#{self.class} instance #{object_id}" m = "#{r} fails to be #blank?!" raise TErr, m unless blank? return self end |
#aT_class_complies(klass, what_is_receiver = nil) ⇒ Object
Fails with TypeError unless the receiver declares compliance with the given class, or is a descendant of that class. Second optional argument customizes the error message (receiver description).
100 101 102 103 104 105 106 |
# File 'lib/y_support/typing/object/typing.rb', line 100 def aT_class_complies klass, what_is_receiver=nil r = what_is_receiver ? what_is_receiver.to_s.capitalize : "#{self.class} instance #{object_id}" m = "#{r} does not comply or declare compliance with #{klass}!" raise TErr, m unless class_complies? klass return self end |
#aT_equal(other, what_is_receiver = nil, what_is_other = nil) ⇒ Object
Fails with TypeError unless the receiver, according to #== method, is equal to the argument. Two more optional arguments customize the error message (receiver description and the description of the other object).
125 126 127 128 129 130 131 132 |
# File 'lib/y_support/typing/object/typing.rb', line 125 def aT_equal other, what_is_receiver=nil, what_is_other=nil r = what_is_receiver ? what_is_receiver.to_s.capitalize : "#{self.class} instance #{object_id}" o = what_is_other || "the prescribed value (#{other.class})" m = "#{r} is not equal (==) to #{o}!" raise TErr, m unless self == other return self end |
#aT_kind_of(klass, what_is_receiver = nil) ⇒ Object Also known as: aT_is_a
Fails with TypeError unless the receiver is of the prescribed class. Second optional argument customizes the error message (receiver description).
87 88 89 90 91 92 93 |
# File 'lib/y_support/typing/object/typing.rb', line 87 def aT_kind_of klass, what_is_receiver=nil r = what_is_receiver ? what_is_receiver.to_s.capitalize : "#{self.class} instance #{object_id}" m = "#{r} is not a kind of #{klass}!" raise TErr, m unless kind_of? klass return self end |
#aT_not(what_is_receiver = nil, how_comply = nil, &b) ⇒ Object
This method takes a block and fails with TypeError, unless the receiver causes the supplied block to return falsey value. Optional arguments customize customize the error message. First optional argument describes the receiver, the second one describes the tested duck type. If the criterion block takes at least one argument (or more arguments), the receiver is passed to it. If the criterion block takes no arguments (arity 0), it is executed inside the singleton class of the receiver (using #instance_exec method). If no block is given, it is checked, whether the object is falsey.
70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/y_support/typing/object/typing.rb', line 70 def aT_not what_is_receiver=nil, how_comply=nil, &b r = what_is_receiver ? what_is_receiver.to_s.capitalize : "#{self.class} instance #{object_id}" if block_given? m = how_comply ? "#{r} must not #{how_comply}!" : "#{r} fails its duck type!" raise TErr, m if ( b.arity == 0 ) ? instance_exec( &b ) : b.( self ) else m = "#{r} is not falsey!" raise TErr, m if self end return self end |
#aT_not_equal(other, what_is_receiver = nil, what_is_other = nil) ⇒ Object
Fails with TypeError unless the receiver, according to #== method, differs from to the argument. Two more optional arguments customize the error message (receiver description and the description of the other object).
138 139 140 141 142 143 144 145 |
# File 'lib/y_support/typing/object/typing.rb', line 138 def aT_not_equal other, what_is_receiver=nil, what_is_other=nil r = what_is_receiver ? what_is_receiver.to_s.capitalize : "#{self.class} instance #{object_id}" o = what_is_other || "the prescribed value (#{other.class})" m = "#{r} fails to differ from #{o}!" raise TErr, m if self == other return self end |
#aT_present(what_is_receiver = nil) ⇒ Object
Fails with TypeError unless the ActiveSupport method #present returns true for the receiver.
161 162 163 164 165 166 167 |
# File 'lib/y_support/typing/object/typing.rb', line 161 def aT_present what_is_receiver=nil r = what_is_receiver ? what_is_receiver.to_s.capitalize : "#{self.class} instance #{object_id}" m = "#{r} fails to be #present?!" raise TErr, m unless present? return self end |
#aT_respond_to(method_name, what_is_receiver = nil) ⇒ Object Also known as: aT_responds_to
Fails with TypeError unless the receiver responds to the given method. Second optional argument customizes the error message (receiver description).
112 113 114 115 116 117 118 |
# File 'lib/y_support/typing/object/typing.rb', line 112 def aT_respond_to method_name, what_is_receiver=nil r = what_is_receiver ? what_is_receiver.to_s.capitalize : "#{self.class} instance #{object_id}" m = "#{r} does not respond to method '#{method_name}'!" raise TErr, m unless respond_to? method_name return self end |
#class_compliance ⇒ Object
Class compliance (declared class compliance + ancestors).
22 23 24 |
# File 'lib/y_support/typing/object/typing.rb', line 22 def class_compliance singleton_class_or_class.compliance end |
#class_complies?(klass) ⇒ Boolean
Class compliance inquirer (declared compliance + class ancestors).
10 11 12 |
# File 'lib/y_support/typing/object/typing.rb', line 10 def class_complies?( klass ) singleton_class_or_class.complies? klass end |
#class_declares_compliance?(klass) ⇒ Boolean
Declared class compliance.
16 17 18 |
# File 'lib/y_support/typing/object/typing.rb', line 16 def class_declares_compliance?( klass ) singleton_class_or_class.declares_compliance? klass end |
#const_redefine_without_warning(const, value) ⇒ Object
9 10 11 12 13 |
# File 'lib/y_support/core_ext/object/misc.rb', line 9 def const_redefine_without_warning( const, value ) mod = self.is_a?(Module) ? self : self.singleton_class mod.send(:remove_const, const) if mod.const_defined?( const ) mod.const_set( const, value ) end |
#const_set_if_not_defined(const, value) ⇒ Object
4 5 6 7 |
# File 'lib/y_support/core_ext/object/misc.rb', line 4 def const_set_if_not_defined( const, value ) mod = self.is_a?(Module) ? self : self.singleton_class mod.const_set( const, value ) unless mod.const_defined?( const ) end |
#declare_class_compliance!(klass) ⇒ Object
Declaration of class compliance.
34 35 36 |
# File 'lib/y_support/typing/object/typing.rb', line 34 def declare_class_compliance!( klass ) singleton_class_or_class.declare_compliance! klass end |
#declared_class_compliance ⇒ Object
Declared class compliance.
28 29 30 |
# File 'lib/y_support/typing/object/typing.rb', line 28 def declared_class_compliance singleton_class_or_class.declared_compliance end |
#InertRecorder(*args, &block) ⇒ Object
InertRecorder constructor.
50 |
# File 'lib/y_support/inert_recorder.rb', line 50 def InertRecorder *args, █ InertRecorder.new *args, &block end |
#local_object?(signature = nil) ⇒ Boolean Also known as: ℓ?
False for normal objects, overriden in the LocalObject class.
40 |
# File 'lib/y_support/local_object.rb', line 40 def local_object? signature=nil; false end |
#LocalObject(signature = caller_locations( 1, 1 )[0].label) ⇒ Object Also known as: L!
LocalObject constructor.
33 34 35 |
# File 'lib/y_support/local_object.rb', line 33 def LocalObject signature=caller_locations( 1, 1 )[0].label LocalObject.new signature end |
#Maybe(object, null_object_signature = nil) ⇒ Object
Converts #nil?-positive objects to a NullObject. Second optional argument specifies the signature of the null object to be created.
89 90 91 |
# File 'lib/y_support/null_object.rb', line 89 def Maybe object, null_object_signature=nil object.nil? ? NullObject.new( null_object_signature ) : object end |
#Null(signature = nil) ⇒ Object
NullObject constructor.
95 |
# File 'lib/y_support/null_object.rb', line 95 def Null( signature=nil ); NullObject.new signature end |
#null_object?(signature = nil) ⇒ Boolean Also known as: null?
Always false for ordinary objects, overriden in NullObject instances.
83 |
# File 'lib/y_support/null_object.rb', line 83 def null_object? signature=nil; false end |
#RespondTo(method) ⇒ Object
RespondTo constructor.
19 |
# File 'lib/y_support/respond_to.rb', line 19 def RespondTo method; RespondTo.create method end |
#singleton_set_attr_with_readers(hash, oo = {}) ⇒ Object Also known as: ⓒ_set_attr_w_readers
Create public attributes (ie. with readers) and initialize them with prescribed values. Takes a hash of { symbol => value } pairs. Existing methods are not overwritten by the new getters, unless option :overwrite_methods is set to true.
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/y_support/core_ext/object/misc.rb', line 19 def singleton_set_attr_with_readers( hash, oo = {} ) hash.each { |key, val| key = key.aE_respond_to( :to_sym, "key of the attr hash" ).to_sym instance_variable_set( "@#{key}", val ) if oo[:overwrite_methods] then ⓒ.module_exec { attr_reader key } elsif methods.include? key raise "Attempt to add \##{key} getter failed: " + "method \##{key} already defined." else ⓒ.module_exec { attr_reader key } end } end |
#try(receiver_NL_description = self, attempt_NL_description, &block) ⇒ Object
Try method takes two textual arguments and one block. The first (optional) argument is a natural language description of the method’s receiver (with #to_s of the receiver used by default). The second argument is a natural language description of the supplied block’s contract – in other words, what the supplied block tries to do. Finally, the block contains the code to perform the described risky action. Inside the block, #note
method is available, which builds up the context information for a good error message, should the risky action raise one.
127 128 129 130 131 |
# File 'lib/y_support/try.rb', line 127 def try receiver_NL_description=self, attempt_NL_description, &block Consciously::Try.new( object: receiver_NL_description, text: attempt_NL_description, &block ).__invoke__ end |
#∏(collection) ⇒ Object Also known as: Π
Product. The argument is expected to be a collection; block can be specified. Basically same as chaining .reduce( :* ) to the end; Π() notation can be more readable at times.
58 59 60 61 62 |
# File 'lib/y_support/unicode.rb', line 58 def ∏( collection ) collection.reduce { |acc, element| acc * ( block_given? ? yield( element ) : element ) } end |
#∑(collection) ⇒ Object Also known as: Σ
Sum. The argument is expected to be a collection; block can be specified. Basically same as chaining .reduce( :+ ) to the end; Σ() notation can be more readable at times.
47 48 49 50 51 |
# File 'lib/y_support/unicode.rb', line 47 def ∑( collection ) collection.reduce { |acc, element| acc + ( block_given? ? yield( element ) : element ) } end |
#√(number) ⇒ Object
Square root (proxy for Math.sqrt(x)).
41 |
# File 'lib/y_support/unicode.rb', line 41 def √( number ); Math.sqrt( number ) end |