Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/y_support/unicode.rb,
lib/y_support/misc/respond_to.rb,
lib/y_support/misc/null_object.rb,
lib/y_support/misc/local_object.rb,
lib/y_support/misc/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.
Instance Method Summary collapse
-
#aA_blank(what_is_receiver = y_inspect) ⇒ Object
Fails with
ArgumentError
unless theActiveSupport
method#blank
returns true for the receiver. -
#aA_present(what_is_receiver = y_inspect) ⇒ Object
Fails with
ArgumentError
unless theActiveSupport
method #present returns true for the receiver. -
#aT(what_is_receiver = y_inspect, 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 = y_inspect) ⇒ Object
Fails with
TypeError
unless activesupport’s#blank
returns true for the receiver. -
#aT_complies(klass, what_is_receiver = y_inspect) ⇒ Object
(also: #aT_class_complies)
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 = y_inspect, 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 = y_inspect) ⇒ Object
(also: #aT_is_a)
Fails with
TypeError
unless the receiver is of the prescribed class. -
#aT_not(what_is_receiver = y_inspect, 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 = y_inspect, what_is_other = nil) ⇒ Object
Fails with
TypeError
unless the receiver, according to #== method, differs from to the argument. -
#aT_present(what_is_receiver = y_inspect) ⇒ Object
Fails with
TypeError
unless activesupport’s#present
returns true for the receiver. -
#aT_respond_to(method_name, what_is_receiver = y_inspect) ⇒ 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.
-
#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 aNullObject
. -
#Null(signature = nil) ⇒ Object
NullObject constructor.
-
#null_object?(signature = nil) ⇒ Boolean
(also: #null?)
Always false for ordinary objects, overriden in
NullObject
instances. -
#owns_subclass(name, of:, parametrized_by: {}, named: proc do |**params| param_str = params.empty? ? "" : "[#{params.pretty_print}]" "#{name}#{param_str}>" end, overwrite_existing_methods: true) ⇒ Object
New syntax for #param_class method for creating parametrized subclasses.
-
#param_class(hash, with: {}) ⇒ Object
Constructs heir classes (parametrized subclasses) of the supplied modules (classes) and makes them available under specified getters.
-
#param_class!(hash, with: {}) ⇒ Object
Like
#param_class
, but it shadows or overwrites existing methods colliding with the getters of the parametrized classes. -
#RespondTo(method) ⇒ Object
RespondTo constructor.
-
#set_attr_with_readers(hash) ⇒ Object
(also: #set_attr_with_selectors)
Assigns atrributes to the receiver object and makes them accessible via reader methods (aka. getter or selector methods).
-
#set_attr_with_readers!(hash) ⇒ Object
Like
#set_attr_with_readers
, but overloads existing methods if their names collied with the attributes to be set. -
#y_inspect(option = nil) ⇒ Object
Constructs the string “#selfself.class:#self”.
-
#Π(collection) ⇒ Object
Product.
-
#Σ(collection) ⇒ Object
Sum.
Instance Method Details
#aA_blank(what_is_receiver = y_inspect) ⇒ Object
Fails with ArgumentError
unless the ActiveSupport
method #blank
returns true for the receiver.
140 141 142 |
# File 'lib/y_support/typing/object/typing.rb', line 140 def aA_blank what_is_receiver=y_inspect tap { blank? or fail ArgumentError, "%s not blank".X!( what_is_receiver ) } end |
#aA_present(what_is_receiver = y_inspect) ⇒ Object
Fails with ArgumentError
unless the ActiveSupport
method #present returns true for the receiver.
147 148 149 150 151 |
# File 'lib/y_support/typing/object/typing.rb', line 147 def aA_present what_is_receiver=y_inspect tap { present? or fail ArgumentError, "%s not present".X!( what_is_receiver ) } end |
#aT(what_is_receiver = y_inspect, 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 |
# File 'lib/y_support/typing/object/typing.rb', line 51 def aT what_is_receiver=y_inspect, how_comply=nil, &b return tap { fail TypeError unless self } unless b return self if b.( self ) m = "%s fails " + ( how_comply ? "to #{how_comply}" : "its check" ) fail TypeError, m.X!( what_is_receiver ) end |
#aT_blank(what_is_receiver = y_inspect) ⇒ Object
Fails with TypeError
unless activesupport’s #blank
returns true for the receiver.
126 127 128 |
# File 'lib/y_support/typing/object/typing.rb', line 126 def aT_blank what_is_receiver=y_inspect tap { blank? or fail TypeError, "%s not blank".X!( what_is_receiver ) } end |
#aT_complies(klass, what_is_receiver = y_inspect) ⇒ Object Also known as: aT_class_complies
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).
87 88 89 90 |
# File 'lib/y_support/typing/object/typing.rb', line 87 def aT_complies klass, what_is_receiver=y_inspect return self if class_complies? klass fail TypeError, "%s does not comply with #{klass}".X!( what_is_receiver ) end |
#aT_equal(other, what_is_receiver = y_inspect, 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).
107 108 109 110 111 |
# File 'lib/y_support/typing/object/typing.rb', line 107 def aT_equal other, what_is_receiver=y_inspect, what_is_other=nil return self if self == other wo = what_is_other || "the prescribed value (#{other.y_inspect})" fail TypeError, "%s must be equal to %s".X!( [ what_is_receiver, wo ] ) end |
#aT_kind_of(klass, what_is_receiver = y_inspect) ⇒ 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).
77 78 79 80 |
# File 'lib/y_support/typing/object/typing.rb', line 77 def aT_kind_of klass, what_is_receiver=y_inspect return self if is_a? klass fail TypeError, "%s not a #{klass}".X!( what_is_receiver ) end |
#aT_not(what_is_receiver = y_inspect, 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.
67 68 69 70 71 72 |
# File 'lib/y_support/typing/object/typing.rb', line 67 def aT_not what_is_receiver=y_inspect, how_comply=nil, &b return tap { fail TypeError if self } unless b return self unless b.( self ) m = how_comply ? "%s must not #{how_comply}" : "%s fails its check" fail TypeError, m.X!( what_is_receiver ) end |
#aT_not_equal(other, what_is_receiver = y_inspect, 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).
117 118 119 120 121 |
# File 'lib/y_support/typing/object/typing.rb', line 117 def aT_not_equal other, what_is_receiver=y_inspect, what_is_other=nil return self unless self == other wo = what_is_other || "the prescribed value (#{other.y_inspect})" fail TypeError, "%s must not == %s".X!( [ what_is_receiver, wo ] ) end |
#aT_present(what_is_receiver = y_inspect) ⇒ Object
Fails with TypeError
unless activesupport’s #present
returns true for the receiver.
133 134 135 |
# File 'lib/y_support/typing/object/typing.rb', line 133 def aT_present what_is_receiver=y_inspect tap { present? or fail TypeError, "%s not present".X!( what_is_receiver ) } end |
#aT_respond_to(method_name, what_is_receiver = y_inspect) ⇒ 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).
96 97 98 99 100 |
# File 'lib/y_support/typing/object/typing.rb', line 96 def aT_respond_to method_name, what_is_receiver=y_inspect return self if respond_to? method_name fail TypeError, "%s does not respond to '#{method_name}'".X!( what_is_receiver ) 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/misc/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.
41 |
# File 'lib/y_support/misc/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/misc/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/misc/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/misc/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/misc/null_object.rb', line 83 def null_object? signature=nil; false end |
#owns_subclass(name, of:, parametrized_by: {}, named: proc do |**params| param_str = params.empty? ? "" : "[#{params.pretty_print}]" "#{name}#{param_str}>" end, overwrite_existing_methods: true) ⇒ Object
New syntax for #param_class method for creating parametrized subclasses.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/y_support/core_ext/object/misc.rb', line 92 def owns_subclass( name, of:, parametrized_by: {}, named: proc do |**params| param_str = params.empty? ? "" : "[#{params.pretty_print}]" "#{name}#{param_str}>" end, overwrite_existing_methods: true ) if overwrite_existing_methods then param_class!( { name => of }, with: parametrized_by ) else param_class( { name => of }, with: parametrized_by ) end # FIXME: This method cannot stay like this. It is not possible # for has_subclass method expecting a single class to subclass # to rely on param_class method which is intended for multiple # classes. Simplify simplify simplify. And change that SO # answer when done. end |
#param_class(hash, with: {}) ⇒ Object
Constructs heir classes (parametrized subclasses) of the supplied modules (classes) and makes them available under specified getters. Expects a hash of pairs { symbol: class }, and a hash of parameters with which to parametrize the modules (classes). The methods guards against collisions in the subclass getter symbols, rasing NameError
should these shadow or overwrite existing methods.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/y_support/core_ext/object/misc.rb', line 54 def param_class( hash, with: {} ) hash.each { |ß, m| case m when Class then parametrized_subclass = m.parametrize( with ) set_attr_with_readers( ß => parametrized_subclass ) when Module then heir_class = m.heir_class( with ) set_attr_with_readers( ß => heir_class ) else fail TypeError, "#{m} must be a module or a class!" end } return nil end |
#param_class!(hash, with: {}) ⇒ Object
Like #param_class
, but it shadows or overwrites existing methods colliding with the getters of the parametrized classes. See +#set_attr_with_readers!“ for full explanation of the shadowing / overwriting behavior.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/y_support/core_ext/object/misc.rb', line 74 def param_class!( hash, with: {} ) hash.each { |ß, m| case m when Class then parametrized_subclass = m.parametrize( with ) set_attr_with_readers!( ß => parametrized_subclass ) when Module then heir_class = m.heir_class( with ) set_attr_with_readers!( ß => heir_class ) else fail TypeError, "#{m} must be a module or a class!" end } return nil end |
#RespondTo(method) ⇒ Object
RespondTo constructor.
17 |
# File 'lib/y_support/misc/respond_to.rb', line 17 def RespondTo method; RespondTo.create method end |
#set_attr_with_readers(hash) ⇒ Object Also known as: set_attr_with_selectors
Assigns atrributes to the receiver object and makes them accessible via reader methods (aka. getter or selector methods). Raises ‘NameError` in case of name collisions with existing methods of the receiver object.
12 13 14 15 16 17 18 19 |
# File 'lib/y_support/core_ext/object/misc.rb', line 12 def set_attr_with_readers hash hash.each_pair { |ß, value| fail NameError, "Method \##{ß} already defined on " \ "#{self}! Use #set_attr_with_readers! to overload " \ "preexisting methods." if methods.include? ß set_attr_with_readers! ß => value } end |
#set_attr_with_readers!(hash) ⇒ Object
Like #set_attr_with_readers
, but overloads existing methods if their names collied with the attributes to be set. Overloading is performed in the following way: If the attribute reader is called without parameters, it acts as an attribute reader (as expected). If the user tries to invoke the attribute method with parameters (or a block), the message falls through to the colliding instance method. In this way, both methods are preserved – collision only happens if the colliding method took no parameters.
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/y_support/core_ext/object/misc.rb', line 32 def set_attr_with_readers! hash hash.each_pair { |ß, value| # puts "Setting @#{ß} of #{self} to #{value}." instance_variable_set "@#{ß}", value singleton_class.class_exec do define_method ß do |*args, &block| return instance_variable_get "@#{ß}" if args.empty? && block.nil? super *args, &block end end } end |
#y_inspect(option = nil) ⇒ Object
Constructs the string “#Object.selfself.class:#self”. Useful for inspection.
4 5 6 7 8 9 10 11 12 |
# File 'lib/y_support/core_ext/object/inspection.rb', line 4 def y_inspect( option=nil ) case option when :full then "#<#{y_inspect}>" when :short then "#{self.class.name.to_s.split( "::" ).last}:#{self}" else "#{self.class}:#{self}" end 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.
32 33 34 35 36 |
# File 'lib/y_support/unicode.rb', line 32 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.
22 23 24 25 26 |
# File 'lib/y_support/unicode.rb', line 22 def Σ( collection ) collection.reduce { |acc, element| acc + ( block_given? ? yield( element ) : element ) } end |