Sha256: 77ffce633f1e534d2d46ca5fdf62b615c75d27fefe51ff6c56541c2e2619c6f7

Contents?: true

Size: 848 Bytes

Versions: 2

Compression:

Stored size: 848 Bytes

Contents

class FastCache::Maybe

  def self.nothing
    self.new
  end

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

  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

  def anything?
    !@nothing
  end

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

  def nothing?
    @nothing
  end
  alias empty? nothing?
  alias blank? nothing?

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

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

  def void
    @nothing = true
    @value = nil
    self
  end
  alias clear! void

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
binary42-fastcache-0.2 lib/fastcache/util/maybe.rb
binary42-fastcache-0.3 lib/fastcache/util/maybe.rb