Class: FastCache::Maybe
- Inherits:
-
Object
- Object
- FastCache::Maybe
- Defined in:
- lib/fastcache/util/maybe.rb
Class Method Summary collapse
Instance Method Summary collapse
- #anything? ⇒ Boolean
-
#initialize(*args) ⇒ Maybe
constructor
A new instance of Maybe.
- #join(other) ⇒ Object
- #nothing? ⇒ Boolean (also: #empty?, #blank?)
- #value ⇒ Object
- #value=(obj) ⇒ Object
- #void ⇒ Object (also: #clear!)
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 |
.nothing ⇒ Object
3 4 5 |
# File 'lib/fastcache/util/maybe.rb', line 3 def self.nothing self.new end |
Instance Method Details
#anything? ⇒ 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?
33 34 35 |
# File 'lib/fastcache/util/maybe.rb', line 33 def nothing? @nothing end |
#value ⇒ Object
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 |
#void ⇒ Object 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 |