Class: Null

Inherits:
Object show all
Defined in:
lib/functional-light-service/functional/null.rb

Overview

The simplest NullObject there can be

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(methods) ⇒ Null

Returns a new instance of Null.



34
35
36
# File 'lib/functional-light-service/functional/null.rb', line 34

def initialize(methods)
  @methods = methods
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object



47
48
49
50
51
# File 'lib/functional-light-service/functional/null.rb', line 47

def method_missing(m, *args)
  return self if respond_to?(m)

  super
end

Class Method Details

.==(other) ⇒ Object



28
29
30
# File 'lib/functional-light-service/functional/null.rb', line 28

def ==(other)
  other.respond_to?(:null?) && other.null?
end

.instanceObject



12
13
14
# File 'lib/functional-light-service/functional/null.rb', line 12

def instance
  @instance ||= new([])
end

.method_missing(m, *args) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/functional-light-service/functional/null.rb', line 4

def method_missing(m, *args)
  if m == :new
    super
  else
    Null.instance
  end
end

.mimic(klas) ⇒ Object



24
25
26
# File 'lib/functional-light-service/functional/null.rb', line 24

def mimic(klas)
  new(klas.instance_methods(false))
end

.null?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/functional-light-service/functional/null.rb', line 16

def null?
  true
end

.some?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/functional-light-service/functional/null.rb', line 20

def some?
  false
end

Instance Method Details

#==(other) ⇒ Object



71
72
73
# File 'lib/functional-light-service/functional/null.rb', line 71

def ==(other)
  other.respond_to?(:null?) && other.null?
end

#inspectObject



67
68
69
# File 'lib/functional-light-service/functional/null.rb', line 67

def inspect
  'Null'
end

#null?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/functional-light-service/functional/null.rb', line 53

def null?
  true
end

#respond_to?(m) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/functional-light-service/functional/null.rb', line 61

def respond_to?(m)
  return true if @methods.empty? || @methods.include?(m)

  super
end

#some?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/functional-light-service/functional/null.rb', line 57

def some?
  false
end

#to_aryObject



43
44
45
# File 'lib/functional-light-service/functional/null.rb', line 43

def to_ary
  []
end

#to_strObject

implicit conversions



39
40
41
# File 'lib/functional-light-service/functional/null.rb', line 39

def to_str
  ''
end