Sha256: 2c32cc64d3ed38fb3b964e31218bd137c2e043863320753e29ab71420fc48eb2

Contents?: true

Size: 1.74 KB

Versions: 9

Compression:

Stored size: 1.74 KB

Contents

module Bugsnag
  class FeatureFlag
    # Get the name of this feature flag
    #
    # @return [String]
    attr_reader :name

    # Get the variant of this feature flag
    #
    # @return [String, nil]
    attr_reader :variant

    # @param name [String] The name of this feature flags
    # @param variant [String, nil] An optional variant for this flag
    def initialize(name, variant = nil)
      @name = name
      @variant = coerce_variant(variant)
    end

    def ==(other)
      self.class == other.class && @name == other.name && @variant == other.variant
    end

    def hash
      [@name, @variant].hash
    end

    # Convert this flag to a hash
    #
    # @example With no variant
    #   { "featureFlag" => "name" }
    #
    # @example With a variant
    #   { "featureFlag" => "name", "variant" => "variant" }
    #
    # @return [Hash{String => String}]
    def to_h
      if @variant.nil?
        { "featureFlag" => @name }
      else
        { "featureFlag" => @name, "variant" => @variant }
      end
    end

    # Check if this flag is valid, i.e. has a name that's a String and a variant
    # that's either nil or a String
    #
    # @return [Boolean]
    def valid?
      @name.is_a?(String) &&
        !@name.empty? &&
        (@variant.nil? || @variant.is_a?(String))
    end

    private

    # Coerce this variant into a valid value (String or nil)
    #
    # If the variant is not already a string or nil, we use #to_s to coerce it.
    # If #to_s raises, the variant will be set to nil
    #
    # @param variant [Object]
    # @return [String, nil]
    def coerce_variant(variant)
      if variant.nil? || variant.is_a?(String)
        variant
      else
        variant.to_s
      end
    rescue StandardError
      nil
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
bugsnag-6.27.1 lib/bugsnag/feature_flag.rb
bugsnag-6.26.4 lib/bugsnag/feature_flag.rb
bugsnag-6.26.3 lib/bugsnag/feature_flag.rb
bugsnag-6.26.2 lib/bugsnag/feature_flag.rb
bugsnag-6.26.1 lib/bugsnag/feature_flag.rb
bugsnag-6.26.0 lib/bugsnag/feature_flag.rb
bugsnag-6.25.2 lib/bugsnag/feature_flag.rb
bugsnag-6.25.1 lib/bugsnag/feature_flag.rb
bugsnag-6.25.0 lib/bugsnag/feature_flag.rb