Sha256: 057d25d30ea3acf0d6b1214b723eb73078ca7b8c11359df6546e023dad89b7d7

Contents?: true

Size: 1.9 KB

Versions: 2

Compression:

Stored size: 1.9 KB

Contents

module WarningSigns
  class Handler
    attr_accessor :behavior, :environments, :except, :only, :source

    def self.from_hash(hash)
      new(**hash.symbolize_keys)
    end

    def initialize(
      behavior: "ignore",
      environment: nil,
      except: [],
      only: [],
      source: "any",
      environments: []
    )
      @behavior = behavior.to_s.downcase.inquiry
      @except = except
      @only = only
      @environments = environments.map { Environment.new(**_1.symbolize_keys) }
      if environment.present?
        @environments << Environment.new(environment: environment, behavior: behavior)
      end
      @source = source.to_s.downcase.inquiry
      raise InvalidHandlerError unless valid?
    end

    def valid?
      except.empty? || only.empty?
    end

    def known_source?(deprecation_source)
      deprecation_source.in?(%w[ruby rails])
    end

    def match?(deprecation)
      source_match?(deprecation.source) &&
        pattern_match?(deprecation.message)
    end

    def pattern_match?(message)
      only_match?(message) && except_match?(message)
    end

    def source_match?(deprecation_source)
      return known_source?(deprecation_source) if source.any?
      source == deprecation_source
    end

    def only_match?(message)
      return true if only.empty?
      only.any? do |only_pattern|
        message.include?(only_pattern)
      end
    end

    def except_match?(message)
      return true if except.empty?
      except.none? do |only_pattern|
        message.include?(only_pattern)
      end
    end

    def enabled_for_ruby?
      source.ruby? || source.all?
    end

    def enabled_for_rails?
      source.rails? || source.all?
    end

    def environment(current: nil)
      current ||= Rails.env
      environments.find { _1.environment == current } ||
        environments.find { _1.environment.all? } ||
        environments.find { _1.environment.other? }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
warning_signs-0.1.0 lib/warning_signs/handler.rb
warning_signs-0.0.1 lib/warning_signs/handler.rb