Sha256: f4db22627c109de2bfa10a09bbbc085d3467d121f925c4f0ba1794a0e91f3ea4

Contents?: true

Size: 1.12 KB

Versions: 3

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module Dolos
  class Result
  end

  class Success < Result
    attr_reader :value, :length, :captures

    def initialize(value, length, captures = [])
      @value = value
      @length = length
      # @captures = captures || value
      @captures = captures
    end

    def capture!
      if value.is_a?(Array)
        value.each do |v|
          captures << v
        end
      else
        captures << value
      end

      Success.new(value, length, captures)
    end

    def inspect
      "Success(value: '#{value}',length: #{length}, capture: '#{captures}')"
    end

    def success?
      true
    end

    def failure?
      false
    end
  end

  class Failure < Result
    attr_reader :message, :committed

    def initialize(message, committed)
      @message = message
      @committed = committed
    end

    def inspect
      [
        "Failure",
        "message: #{message}",
        "committed: #{committed}"
      ].join("\n")
    end

    def map
      self
    end

    def success?
      false
    end

    def failure?
      true
    end

    def captures
      []
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dolos-0.1.2 lib/dolos/result.rb
dolos-0.1.1 lib/dolos/result.rb
dolos-0.1.0 lib/dolos/result.rb