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,
lib/y_support/core_ext/object/inspection.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 = insp, 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 = insp) ⇒ Object
Fails with TypeError unless the ActiveSupport method #blank returns true for the receiver.
-
#aT_class_complies(klass, what_is_receiver = insp) ⇒ 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 = insp, 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 = insp) ⇒ Object
(also: #aT_is_a)
Fails with TypeError unless the receiver is of the prescribed class.
-
#aT_not(what_is_receiver = insp, 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 = insp, what_is_other = nil) ⇒ Object
Fails with TypeError unless the receiver, according to #== method, differs from to the argument.
-
#aT_present(what_is_receiver = insp) ⇒ Object
Fails with TypeError unless the ActiveSupport method #present returns true for the receiver.
-
#aT_respond_to(method_name, what_is_receiver = insp) ⇒ 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.
-
#declare_class_compliance!(klass) ⇒ Object
Declaration of class compliance.
-
#declared_class_compliance ⇒ Object
Declared class compliance.
-
#InertRecorder(*args, &block) ⇒ Object
InertRecorder constructor.
-
#insp ⇒ Object
Constructs the string “#selfself.class:#self”.
-
#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.
-
#param_class(hash, with: (fail ArgumentError, "No parameters!")) ⇒ Object
Constructs parametrized subclasses of the supplied classes and makes them available under specified getters.
-
#param_class!(hash, with: (fail ArgumentError, "No parameters!")) ⇒ Object
Constructs parametrized subclasses of the supplied classes and makes them available under specified getters.
-
#RespondTo(method) ⇒ Object
RespondTo constructor.
-
#set_attr_with_readers(hash) ⇒ Object
Assigns prescribed atrributes to the object and makes them accessible with getter (reader) methods.
-
#set_attr_with_readers!(hash) ⇒ Object
Assigns prescribed atrributes to the object and makes them accessible with getter (reader) methods.
-
#try(receiver_NL_description = self, attempt_NL_description, &block) ⇒ Object
(also: #consciously)
Try method takes two textual arguments and one block.
-
#Π(collection) ⇒ Object
Product.
-
#Σ(collection) ⇒ Object
Sum.
Instance Method Details
#aT(what_is_receiver = insp, 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.
51 52 53 54 55 56 57 58 |
# File 'lib/y_support/typing/object/typing.rb', line 51 def aT what_is_receiver=insp, how_comply=nil, &b if block_given? then if b.( self ) then self else m = "%s fails " + how_comply ? "to #{how_comply}" : "its check" fail TypeError, m.X!( what_is_receiver ) end else self or fail TypeError end end |
#aT_blank(what_is_receiver = insp) ⇒ Object
Fails with TypeError unless the ActiveSupport method #blank returns true for the receiver.
137 138 139 |
# File 'lib/y_support/typing/object/typing.rb', line 137 def aT_blank what_is_receiver=insp tap { blank? or fail TypeError, "%s not blank".X!( what_is_receiver ) } end |
#aT_class_complies(klass, what_is_receiver = insp) ⇒ 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).
94 95 96 97 98 |
# File 'lib/y_support/typing/object/typing.rb', line 94 def aT_class_complies klass, what_is_receiver=insp if class_complies? klass then fail TypeError, "%s does not comply with #{klass}".X!( what_is_receiver ) else self end end |
#aT_equal(other, what_is_receiver = insp, 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).
116 117 118 119 120 121 |
# File 'lib/y_support/typing/object/typing.rb', line 116 def aT_equal other, what_is_receiver=insp, what_is_other=nil if self == other then self else wo = what_is_other || "the prescribed value (#{other.insp})" fail TypeError, "%s must be equal to %s".X!( [ what_is_receiver, wo ] ) end end |
#aT_kind_of(klass, what_is_receiver = insp) ⇒ 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).
83 84 85 86 87 |
# File 'lib/y_support/typing/object/typing.rb', line 83 def aT_kind_of klass, what_is_receiver=insp tap do is_a? klass or fail TypeError, "%s not a #{klass}".X!( what_is_receiver ) end end |
#aT_not(what_is_receiver = insp, 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.
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/y_support/typing/object/typing.rb', line 69 def aT_not what_is_receiver=insp, how_comply=nil, &b tap do if block_given? then if b.( self ) then m = how_comply ? "%s must not #{how_comply}" : "%s fails its check" fail TypeError, m.X!( what_is_receiver ) end else fail TypeError if self end end end |
#aT_not_equal(other, what_is_receiver = insp, 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).
127 128 129 130 131 132 |
# File 'lib/y_support/typing/object/typing.rb', line 127 def aT_not_equal other, what_is_receiver=insp, what_is_other=nil if self == other wo = what_is_other || "the prescribed value (#{other.insp})" fail TypeError, "%s must not == %s".X!( [ what_is_receiver, wo ] ) else self end end |
#aT_present(what_is_receiver = insp) ⇒ Object
Fails with TypeError unless the ActiveSupport method #present returns true for the receiver.
144 145 146 |
# File 'lib/y_support/typing/object/typing.rb', line 144 def aT_present what_is_receiver=insp tap { present? or fail TypeError, "%s not present".X!( what_is_receiver ) } end |
#aT_respond_to(method_name, what_is_receiver = insp) ⇒ 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).
104 105 106 107 108 109 |
# File 'lib/y_support/typing/object/typing.rb', line 104 def aT_respond_to method_name, what_is_receiver=insp if respond_to? method_name then self else m = "%s does not respond to method '#{method_name}'" fail TypeError, m.X!( what_is_receiver ) end end |
#class_compliance ⇒ Object
Class compliance (declared class compliance + ancestors).
24 25 26 |
# File 'lib/y_support/typing/object/typing.rb', line 24 def class_compliance singleton_class_or_class.compliance end |
#class_complies?(klass) ⇒ Boolean
Class compliance inquirer (declared compliance + class ancestors).
12 13 14 |
# File 'lib/y_support/typing/object/typing.rb', line 12 def class_complies?( klass ) singleton_class_or_class.complies? klass end |
#class_declares_compliance?(klass) ⇒ Boolean
Declared class compliance.
18 19 20 |
# File 'lib/y_support/typing/object/typing.rb', line 18 def class_declares_compliance?( klass ) singleton_class_or_class.declares_compliance? klass end |
#declare_class_compliance!(klass) ⇒ Object
Declaration of class compliance.
36 37 38 |
# File 'lib/y_support/typing/object/typing.rb', line 36 def declare_class_compliance!( klass ) singleton_class_or_class.declare_compliance! klass end |
#declared_class_compliance ⇒ Object
Declared class compliance.
30 31 32 |
# File 'lib/y_support/typing/object/typing.rb', line 30 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 |
#insp ⇒ Object
Constructs the string “#Object.selfself.class:#self”. Useful for inspection.
6 7 8 |
# File 'lib/y_support/core_ext/object/inspection.rb', line 6 def insp "#{self}:#{self.class}" end |
#local_object?(signature = nil) ⇒ Boolean Also known as: ℓ?
False for normal objects, overriden in the LocalObject class.
41 |
# File 'lib/y_support/local_object.rb', line 41 def local_object? signature=nil; false end |
#LocalObject(signature = caller_locations( 1, 1 )[0].label) ⇒ Object Also known as: L!
LocalObject constructor.
34 35 36 |
# File 'lib/y_support/local_object.rb', line 34 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 |
#param_class(hash, with: (fail ArgumentError, "No parameters!")) ⇒ Object
Constructs parametrized subclasses of the supplied classes and makes them available under specified getters. Expects a hash of pairs { reader_symbol: class }, and a hash of parameters, with which the class(es) is (are) parametrized. Raises NameError should any of the getters shadow / overwrite existing methods.
33 34 35 36 37 38 39 |
# File 'lib/y_support/core_ext/object/misc.rb', line 33 def param_class hash, with: (fail ArgumentError, "No parameters!") hash.each { |ß, ç| sub = ç.parametrize with set_attr_with_readers( ß => sub ) } return nil end |
#param_class!(hash, with: (fail ArgumentError, "No parameters!")) ⇒ Object
Constructs parametrized subclasses of the supplied classes and makes them available under specified getters. Expects a hash of pairs { reader_symbol: class }, and a hash of parameters, with which the class(es) is (are) parametrized. Shadows / overwrites existing methods.
46 47 48 49 50 51 52 |
# File 'lib/y_support/core_ext/object/misc.rb', line 46 def param_class! hash, with: (fail ArgumentError, "No parameters!") hash.each { |ß, ç| sub = ç.parametrize with set_attr_with_readers!( ß => sub ) } return nil end |
#RespondTo(method) ⇒ Object
RespondTo constructor.
19 |
# File 'lib/y_support/respond_to.rb', line 19 def RespondTo method; RespondTo.create method end |
#set_attr_with_readers(hash) ⇒ Object
Assigns prescribed atrributes to the object and makes them accessible with getter (reader) methods. Raises NameError should any of the getters shadow / overwrite existing methods.
10 11 12 13 14 15 |
# File 'lib/y_support/core_ext/object/misc.rb', line 10 def set_attr_with_readers hash hash.each_pair { |ß, value| fail NameError, "Method \##{ß} already defined!" if methods.include? ß set_attr_with_readers! ß => value } end |
#set_attr_with_readers!(hash) ⇒ Object
Assigns prescribed atrributes to the object and makes them accessible with getter (reader) methods. Shadows / overwrites existing methods.
20 21 22 23 24 25 |
# File 'lib/y_support/core_ext/object/misc.rb', line 20 def set_attr_with_readers! hash hash.each_pair { |symbol, value| instance_variable_set "@#{symbol}", value singleton_class.class_exec { attr_reader symbol } } end |
#try(receiver_NL_description = self, attempt_NL_description, &block) ⇒ Object Also known as: consciously
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
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.
31 32 33 34 35 |
# File 'lib/y_support/unicode.rb', line 31 def Π( collection ) collection.reduce { |acc, element| acc * ( block_given? ? yield( element ) : element ) } end |
#Σ(collection) ⇒ Object
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.
21 22 23 24 25 |
# File 'lib/y_support/unicode.rb', line 21 def Σ( collection ) collection.reduce { |acc, element| acc + ( block_given? ? yield( element ) : element ) } end |