Class: FastCache::Maybe

Inherits:
Object
  • Object
show all
Defined in:
lib/fastcache/util/maybe.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Maybe

Returns a new instance of Maybe.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/fastcache/util/maybe.rb', line 11

def initialize(*args)
  args.size < 2 or raise ArgumentError,
    "#{self.class.name}#initialize only takes 0 or 1 arguments. Given #{args.size}."
  if args.empty?
    @value = nil
    @nothing = true
  else
    @value = *args
    @nothing = false
  end
end

Class Method Details

.just(x) ⇒ Object



7
8
9
# File 'lib/fastcache/util/maybe.rb', line 7

def self.just(x)
  self.new(x)
end

.nothingObject



3
4
5
# File 'lib/fastcache/util/maybe.rb', line 3

def self.nothing
  self.new
end

Instance Method Details

#anything?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/fastcache/util/maybe.rb', line 23

def anything?
  !@nothing
end

#join(other) ⇒ Object



27
28
29
30
31
# File 'lib/fastcache/util/maybe.rb', line 27

def join(other)
  (other.nothing? || self.nothing?) ?
    Maybe.nothing :
    other
end

#nothing?Boolean Also known as: empty?, blank?

Returns:

  • (Boolean)


33
34
35
# File 'lib/fastcache/util/maybe.rb', line 33

def nothing?
  @nothing
end

#valueObject



39
40
41
42
43
44
45
# File 'lib/fastcache/util/maybe.rb', line 39

def value
  if anything?
    @value
  else
    raise 'No value set'
  end
end

#value=(obj) ⇒ Object



47
48
49
50
# File 'lib/fastcache/util/maybe.rb', line 47

def value=(obj)
  @nothing = false
  @value = obj
end

#voidObject Also known as: clear!



52
53
54
55
56
# File 'lib/fastcache/util/maybe.rb', line 52

def void
  @nothing = true
  @value = nil
  self
end