class Collins::Option

Represents optional values. Instances of `Option` are either an instance of `Some` or `None` @note This is pretty much a straight rip off of the scala version @example

name = get_parameter("name")
upper = Option(name).map{|s| s.strip}.filter{|s|s.size > 0}.map{|s|s.upcase}
puts(upper.get_or_else(""))

Public Instance Methods

defined?() click to toggle source

@return [Boolean] True if the value is defined

# File lib/collins/option.rb, line 34
def defined?
  !empty?
end
empty?() click to toggle source

@return [Boolean] True if the value is undefined

# File lib/collins/option.rb, line 29
def empty?
  raise NotImplementedError.new("empty? not implemented")
end
exists?(&predicate) click to toggle source

Return true if non-empty and predicate is true for the value @return [Boolean] test passed

# File lib/collins/option.rb, line 90
def exists? &predicate
  !empty? && predicate.call(get)
end
filter(&predicate) click to toggle source

Convert to `None` if predicate fails

Returns this option if it is non-empty and applying the predicate to this options returns true. Otherwise return `None`.

@yieldparam [Object] predicate The current value @yieldreturn [Boolean] result of testing value @return [Option<Object>] `None` if predicate fails, or already `None`

# File lib/collins/option.rb, line 149
def filter &predicate
  if empty? || predicate.call(get) then
    self
  else
    None.new
  end
end
filter_not(&predicate) click to toggle source

Inverse of `filter` operation.

Returns this option if it is non-empty and applying the predicate to this option returns false. Otherwise return `None`.

@see filter @return [Option<Object>]

# File lib/collins/option.rb, line 164
def filter_not &predicate
  if empty? || !predicate.call(get) then
    self
  else
    None.new
  end
end
flat_map(&block) click to toggle source

Same as map, but flatten the results

This is useful when operating on an object that will return an `Option`.

@example

Option(15).flat_map {|i| Option(i).filter{|i2| i2 > 0}} == Some(15)

@see map @return [Option<Object>] Optional value

# File lib/collins/option.rb, line 128
def flat_map &block
  if empty? then
    None.new
  else
    res = block.call(get)
    if res.is_a?(Some) then
      res
    else
      Some.new(res)
    end
  end
end
foreach(&f) click to toggle source

Apply the block specified to the value if non-empty @return [NilClass]

# File lib/collins/option.rb, line 96
def foreach &f
  if self.defined? then
    f.call(get)
  end
  nil
end
get() click to toggle source

@return [Object] Value, if defined @raise [NameError] if value is undefined

# File lib/collins/option.rb, line 40
def get
  raise NotImplementedError.new("get not implemented")
end
get_or_else(*default) { || ... } click to toggle source

The value associated with this option, or the default

@example

# Raises an exception
Option(nil).get_or_else { raise Exception.new("Stuff") }
# Returns -1
Option("23").map {|i| i.to_i}.filter{|i| i > 25}.get_or_else -1

@param [Object] default A default value to use if the option value is undefined @yield [] Provide a default with a block instead of a parameter @return [Object] If None, default, otherwise the value

# File lib/collins/option.rb, line 55
def get_or_else *default
  if empty? then
    if block_given? then
      yield
    else
      default.first
    end
  else
    get
  end
end
map(&block) click to toggle source

If the option value is defined, apply the specified block to that value

@example

Option("15").map{|i| i.to_i}.get == 15

@yieldparam [Object] block The current value @yieldreturn [Object] The new value @return [Option<Object>] Optional value

# File lib/collins/option.rb, line 111
def map &block
  if empty? then
    None.new
  else
    Some.new(block.call(get))
  end
end
or_else(*default) { || ... } click to toggle source

Return this `Option` if non-empty, otherwise return the result of evaluating the default @example

Option(nil).or_else { "foo" } == Some("foo")

@return [Option<Object>]

# File lib/collins/option.rb, line 71
def or_else *default
  if empty? then
    res = if block_given? then
            yield
          else
            default.first
          end
    if res.is_a?(Option) then
      res
    else
      ::Collins::Option(res)
    end
  else
    self
  end
end