Sha256: b5a70acc7aaec9b221d61ca55ad28f94a1164d62c869cb7a0708fe99b4211cc4

Contents?: true

Size: 1.16 KB

Versions: 5

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true
module Might
  # Marker module
  module Result
  end

  # Represents fetching failure
  class Failure
    include Result

    # @param errors [<String>]
    def initialize(errors)
      @errors = errors
    end

    # @param errors [<String>]
    attr_reader :errors

    # @return [true]
    def failure?
      !success?
    end

    # @return [false]
    def success?
      false
    end

    # @raise [NotImplementedError]
    def get
      fail NotImplementedError, <<-MESSAGE.strip_heredoc
        #{self.class} does not respond to #get. You should explicitly check for
        #success? before calling #get.
      MESSAGE
    end

    # @yield given block
    def get_or_else
      yield
    end
  end

  # Represents fetching success
  class Success
    include Result

    # @param value [ActiveRecord::Relation]
    def initialize(value)
      @value = value
    end

    # @return [false]
    def failure?
      !success?
    end

    # @return [true]
    def success?
      true
    end

    # @return [ActiveRecord::Relation]
    def get
      @value
    end

    # @return [ActiveRecord::Relation]
    def get_or_else
      @value
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
might-0.7.6 lib/might/result.rb
might-0.7.5 lib/might/result.rb
might-0.7.4 lib/might/result.rb
might-0.7.3 lib/might/result.rb
might-0.7.2 lib/might/result.rb