Sha256: a51b3a83f9f0af37fdca233b3c2cebd307909abc1d1cdde98973ac744fe22e6b

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

class DMTool::Result
  include Comparable

  def initialize(success: false, failure: false, critical: false, symbol: :nil_result)
    @success = success
    @failure = failure
    @critical = critical
    @symbol = symbol
  end

  public

  Success         = DMTool::Result.new(success: true, failure: false, critical: false, symbol: :success)
  Failure         = DMTool::Result.new(success: false, failure: true, critical: false, symbol: :failure)
  NilResult       = DMTool::Result.new(success: false, failure: false, critical: false, symbol: :nil_result)
  CriticalSuccess = DMTool::Result.new(success: true, failure: false, critical: true, symbol: :critical_success)
  CriticalFailure = DMTool::Result.new(success: false, failure: true, critical: true, symbol: :critical_failure)

  attr_reader :success, :failure, :critical, :symbol

  def critical_success
    critical? && success?
  end

  def critical_failure
    critical? && failure?
  end

  def to_s
    "#{to_sym}"
  end

  def to_i
    val = 0
    val += 1 if success?
    val -=1 if failure?
    val *=2 if critical?
    val
  end

  def coerce(other)
    [other, self.to_i]
  end

  def +(other)
    self.to_i + other.to_i
  end

  def <=>(other)
    self.to_i <=> other.to_i
  end

  alias success? success
  alias failure? failure
  alias critical? critical
  alias critical_failure? critical_failure
  alias critical_success? critical_success
  alias to_sym symbol
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dmtool-0.0.1 lib/dmtool/result.rb