Sha256: d06b0b25d6cc817eb864c69dbdf660884134138ea140fed0b49ba789bb6a4b9e

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

module JsonStatham
  class InvalidRatioError < StandardError
  end

  class Result
    attr_reader :parser, :config

    def self.call(parser)
      new(parser).call
    end

    def initialize(parser)
      Validation.check_object_class(parser, [JsonStatham::Parser])

      @parser = parser
      @config = JsonStatham.config
    end

    def call
      ensure_valid_ratio

      self
    end

    def current_duration
      return unless observed?

      parser.observer.duration
    end

    def observed?
      !!parser.observer
    end

    def previous_duration
      parser.previous_duration
    end

    def previous_duration?
      !!previous_duration
    end

    def success?
      parser.valid?
    end

    def failure?
      !success?
    end

    def ratio
      return 0 unless observed?

      current_duration.fdiv(previous_duration)
    end

    def raise_ratio
      config.raise_ratio
    end

    private

    def ensure_valid_ratio
      return unless config.raise_on_failure? && previous_duration? && ratio > raise_ratio

      raise JsonStatham::InvalidRatioError, "raise_ratio: #{raise_ratio}. current_ratio: #{ratio}"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
json_statham-0.1.3 lib/json_statham/result.rb
json_statham-0.1.2 lib/json_statham/result.rb