Class: NullObject

Inherits:
Object show all
Defined in:
lib/y_support/misc/null_object.rb

Overview

Null object pattern implementation in YSupport. apart from the expected null object behavior (such as returning self in response to almost all messages), this null object instances can carry a signature specified by the user upon creation, which can serve to hint the origin of the null object. (This signature is opional, default is nil.)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(null_object_signature = nil) ⇒ NullObject

Signature can be given as an optional argument upon initialization.



16
17
18
# File 'lib/y_support/misc/null_object.rb', line 16

def initialize null_object_signature=nil
  @null_object_signature = null_object_signature 
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(ß, *args, &block) ⇒ Object

:nodoc:



70
71
72
# File 'lib/y_support/misc/null_object.rb', line 70

def method_missing ß, *args, &block      # :nodoc:
  self
end

Instance Attribute Details

#null_object_signatureObject (readonly)

Returns the value of attribute null_object_signature.



12
13
14
# File 'lib/y_support/misc/null_object.rb', line 12

def null_object_signature
  @null_object_signature
end

Instance Method Details

#==(other) ⇒ Object

True if and only if the other object is a NullObject with same signature.



65
66
67
68
# File 'lib/y_support/misc/null_object.rb', line 65

def == other
  other.is_a?( self.class ) &&
    other.null_object_signature == null_object_signature
end

#blank?Boolean

Always true.

Returns:

  • (Boolean)


61
# File 'lib/y_support/misc/null_object.rb', line 61

def blank?; true end

#empty?Boolean

Always true.

Returns:

  • (Boolean)


57
# File 'lib/y_support/misc/null_object.rb', line 57

def empty?; true end

#inspectObject

Inspection string.



41
# File 'lib/y_support/misc/null_object.rb', line 41

def inspect; to_s end

#null_object?(signature = nil) ⇒ Boolean Also known as: null?

Inquirer whether an object is a NullObject. Again, optional signature argument can be given to distinguish between different null objects.

Returns:

  • (Boolean)


23
24
25
# File 'lib/y_support/misc/null_object.rb', line 23

def null_object? signature=nil
  null_object_signature == signature
end

#present?Boolean

Always false.

Returns:

  • (Boolean)


53
# File 'lib/y_support/misc/null_object.rb', line 53

def present?; false end

#respond_to?(ß, *args, &block) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


74
75
76
# File 'lib/y_support/misc/null_object.rb', line 74

def respond_to? ß, *args, &block         # :nodoc:
  true
end

#to_aObject

Empty array.



30
# File 'lib/y_support/misc/null_object.rb', line 30

def to_a; [] end

#to_fObject

Float zero.



45
# File 'lib/y_support/misc/null_object.rb', line 45

def to_f; 0.0 end

#to_iObject

Integer zero.



49
# File 'lib/y_support/misc/null_object.rb', line 49

def to_i; 0 end

#to_sObject

Description string.



34
35
36
37
# File 'lib/y_support/misc/null_object.rb', line 34

def to_s
  sgn = null_object_signature
  sgn.nil? ? "#<NullObject>" : "#<NullObject #{sgn}>"
end