Sha256: e241656955775dc569c0de0593a31e3f6c4f044b90e2a1a56153a85aa6195e78

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module Flagsmith
  # Flag object
  class Flag
    include Comparable

    attr_reader :enabled, :value, :default, :feature_name, :feature_id

    def initialize(feature_name:, enabled:, value:, feature_id:, default: false)
      @feature_name = feature_name
      @feature_id = feature_id
      @enabled = enabled
      @value = value
      @default = default
    end

    def enabled?
      @enabled
    end

    alias is_default default

    def <=>(other)
      feature_name <=> other.feature_name
    end

    def [](key)
      to_h[key]
    end

    def to_h
      {
        feature_id: feature_id,
        feature_name: feature_name,
        value: value,
        enabled: enabled,
        default: default
      }
    end

    class << self
      def from_feature_state_model(feature_state_model, identity_id)
        new(
          enabled: feature_state_model.enabled,
          value: feature_state_model.get_value(identity_id),
          feature_name: feature_state_model.feature.name,
          feature_id: feature_state_model.feature.id
        )
      end

      def from_api(json_flag_data)
        new(
          enabled: json_flag_data[:enabled],
          value: json_flag_data[:feature_state_value] || json_flag_data[:value],
          feature_name: json_flag_data.dig(:feature, :name),
          feature_id: json_flag_data.dig(:feature, :id)
        )
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
flagsmith-3.0.0 lib/flagsmith/sdk/models/flag.rb