Module: Naught::Conversions

Defined in:
lib/naught/conversions.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(null_class) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/naught/conversions.rb', line 3

def self.included(null_class)
  unless class_variable_defined?(:@@included) && @@included
    @@null_class = null_class
    @@null_equivs = null_class::NULL_EQUIVS
    @@included = true
  end
  super
end

Instance Method Details

#Actual(object = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/naught/conversions.rb', line 45

def Actual(object = nil)
  object = yield if block_given?
  case object
  when NullObjectTag
    nil
  else
    object
  end
end

#Just(object = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/naught/conversions.rb', line 35

def Just(object = nil)
  object = yield if block_given?
  case object
  when NullObjectTag, *@@null_equivs
    fail(ArgumentError.new("Null value: #{object.inspect}"))
  else
    object
  end
end

#Maybe(object = nil) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/naught/conversions.rb', line 23

def Maybe(object = nil)
  object = yield if block_given?
  case object
  when NullObjectTag
    object
  when *@@null_equivs
    @@null_class.get(:caller => caller(1))
  else
    object
  end
end

#Null(object = :nothing_passed) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/naught/conversions.rb', line 12

def Null(object = :nothing_passed)
  case object
  when NullObjectTag
    object
  when :nothing_passed, *@@null_equivs
    @@null_class.get(:caller => caller(1))
  else
    fail(ArgumentError.new("#{object.inspect} is not null!"))
  end
end