Sha256: e4fdae06a7aba072156ebb7240f0f689d524165706501c84f6cf0b75db43ac0d

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

module Option

  def option(thing)
    thing.nil? ? none : some(thing)
  end

  def some(thing)
    Some.new(thing)
  end

  def none(thing=nil)
    None.new(thing)
  end

  class Some

    include Comparable

    def initialize(content)
      @content = content
      raise(Exception,'some cannot be nil') if @content.nil?
    end

    def <=>(object)
      self.state <=> object.state
    end

    def get
      @content
    end

    def value
      get
    end

    def empty?
      @content.empty?
    end

    def defined?
      !empty?
    end

    def get_or_else(item)
      blank? ? item : @content
    end

    def get_or_nil
      blank? ? nil : @content
    end

    def get_or_throw(exception)
      blank? ? raise(exception) : @content
    end

    def to_seq
      sequence(self)
    end

    def contains(item)
     value == item
    end

    def exists?(predicate)

    end

    def join(target_sequence)
     sequence(value) << target_sequence
    end
    alias + join
    alias << join

    protected
    def state
      @content
    end

    private

    def blank?
      @content.respond_to?(:empty?) ? empty? : !self
    end

  end

  class None

    include Comparable

    def initialize(content)
      @content = content
    end

    def <=>(object)
      self.state <=> object.state
    end

    protected

    def state
      @content
    end

  end


end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
totally_lazy-0.0.4 lib/option.rb
totally_lazy-0.0.3 lib/option.rb
totally_lazy-0.0.2 lib/option.rb