diff --git a/lib/ldclient-rb/impl/data_store.rb b/lib/ldclient-rb/impl/data_store.rb index e834e522..00043597 100644 --- a/lib/ldclient-rb/impl/data_store.rb +++ b/lib/ldclient-rb/impl/data_store.rb @@ -4,6 +4,56 @@ module LaunchDarkly module Impl module DataStore + + class DataKind + FEATURES = "features".freeze + SEGMENTS = "segments".freeze + + FEATURE_PREREQ_FN = lambda { |flag| (flag[:prerequisites] || []).map { |p| p[:key] } }.freeze + + attr_reader :namespace + attr_reader :priority + + # + # @param namespace [String] + # @param priority [Integer] + # + def initialize(namespace:, priority:) + @namespace = namespace + @priority = priority + end + + # + # Maintain the same behavior when these data kinds were standard ruby hashes. + # + # @param key [Symbol] + # @return [Object] + # + def [](key) + return priority if key == :priority + return namespace if key == :namespace + return get_dependency_keys_fn() if key == :get_dependency_keys + nil + end + + # + # Retrieve the dependency keys for a particular data kind. Right now, this is only defined for flags. + # + def get_dependency_keys_fn() + return nil unless @namespace == FEATURES + + FEATURE_PREREQ_FN + end + + def eql?(other) + namespace == other.namespace && priority == other.priority + end + + def hash + [namespace, priority].hash + end + end + class StatusProvider include LaunchDarkly::Interfaces::DataStore::StatusProvider diff --git a/lib/ldclient-rb/in_memory_store.rb b/lib/ldclient-rb/in_memory_store.rb index ad4f6e85..3cb9dc48 100644 --- a/lib/ldclient-rb/in_memory_store.rb +++ b/lib/ldclient-rb/in_memory_store.rb @@ -11,17 +11,10 @@ module LaunchDarkly # to ensure data consistency during non-atomic updates. # @private - FEATURES = { - namespace: "features", - priority: 1, # that is, features should be stored after segments - get_dependency_keys: lambda { |flag| (flag[:prerequisites] || []).map { |p| p[:key] } }, - }.freeze + FEATURES = Impl::DataStore::DataKind.new(namespace: "features", priority: 1).freeze # @private - SEGMENTS = { - namespace: "segments", - priority: 0, - }.freeze + SEGMENTS = Impl::DataStore::DataKind.new(namespace: "segments", priority: 0).freeze # @private ALL_KINDS = [FEATURES, SEGMENTS].freeze