diff --git a/Gemfile.lock b/Gemfile.lock index f5eebe2..84a1853 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - flagsmith (3.0.0) + flagsmith (3.0.1) faraday faraday-retry faraday_middleware @@ -29,8 +29,8 @@ GEM faraday-em_synchrony (1.0.0) faraday-excon (1.1.0) faraday-httpclient (1.0.1) - faraday-multipart (1.0.3) - multipart-post (>= 1.2, < 3) + faraday-multipart (1.0.4) + multipart-post (~> 2) faraday-net_http (1.0.1) faraday-net_http_persistent (1.2.0) faraday-patron (1.0.0) @@ -40,7 +40,7 @@ GEM faraday (~> 1.0) gem-release (2.2.0) method_source (1.0.0) - multipart-post (2.1.1) + multipart-post (2.2.0) parallel (1.20.1) parser (3.0.0.0) ast (~> 2.4.1) diff --git a/example/config/initializers/flagsmith.rb b/example/config/initializers/flagsmith.rb index caeee22..935db43 100644 --- a/example/config/initializers/flagsmith.rb +++ b/example/config/initializers/flagsmith.rb @@ -2,8 +2,6 @@ enable_local_evaluation: true, environment_refresh_interval_seconds: 60, default_flag_handler: lambda { |feature_name| - Flagsmith::Flag.new( - feature_name: feature_name, enabled: false, value: {}.to_json, feature_id: nil - ) + Flagsmith::Flags::DefaultFlag.new(enabled: false, value: {}.to_json) } ) diff --git a/lib/flagsmith.rb b/lib/flagsmith.rb index c4e1316..4375e56 100644 --- a/lib/flagsmith.rb +++ b/lib/flagsmith.rb @@ -14,8 +14,7 @@ require 'flagsmith/sdk/errors' require 'flagsmith/sdk/intervals' require 'flagsmith/sdk/pooling_manager' -require 'flagsmith/sdk/models/flag' -require 'flagsmith/sdk/models/flags/collection' +require 'flagsmith/sdk/models/flags' require 'flagsmith/sdk/instance_methods' require 'flagsmith/engine/core' diff --git a/lib/flagsmith/sdk/models/flag.rb b/lib/flagsmith/sdk/models/flag.rb deleted file mode 100644 index d221a15..0000000 --- a/lib/flagsmith/sdk/models/flag.rb +++ /dev/null @@ -1,62 +0,0 @@ -# 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 diff --git a/lib/flagsmith/sdk/models/flags/collection.rb b/lib/flagsmith/sdk/models/flags.rb similarity index 61% rename from lib/flagsmith/sdk/models/flags/collection.rb rename to lib/flagsmith/sdk/models/flags.rb index 8dad5d1..8cfb10e 100644 --- a/lib/flagsmith/sdk/models/flags/collection.rb +++ b/lib/flagsmith/sdk/models/flags.rb @@ -4,7 +4,79 @@ module Flagsmith module Flags class NotFound < StandardError; end - # Flag Collection + class BaseFlag + include Comparable + + attr_reader :enabled, :value, :default + + def initialize(enabled:, value:, default:) + @enabled = enabled + @value = value + @default = default + end + + def enabled? + enabled + end + + alias is_default default + end + + class DefaultFlag < BaseFlag + def initialize(enabled:, value:) + super(enabled: enabled, value: value, default: true) + end + end + + class Flag < BaseFlag + + attr_reader :feature_name, :feature_id + + def initialize(feature_name:, enabled:, value:, feature_id:) + super(enabled: enabled, value: value, default: false) + @feature_name = feature_name + @feature_id = feature_id + end + + 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 + class Collection include Enumerable @@ -75,7 +147,7 @@ class << self def from_api(json_data, **args) to_flag_object = lambda { |json_flag, acc| acc[normalize_key(json_flag.dig(:feature, :name))] = - Flagsmith::Flag.from_api(json_flag) + Flagsmith::Flags::Flag.from_api(json_flag) } new( @@ -87,7 +159,7 @@ def from_api(json_data, **args) def from_feature_state_models(feature_states, identity_id: nil, **args) to_flag_object = lambda { |feature_state, acc| acc[normalize_key(feature_state.feature.name)] = - Flagsmith::Flag.from_feature_state_model(feature_state, identity_id) + Flagsmith::Flags::Flag.from_feature_state_model(feature_state, identity_id) } new( diff --git a/lib/flagsmith/version.rb b/lib/flagsmith/version.rb index c1236f5..793c2d4 100644 --- a/lib/flagsmith/version.rb +++ b/lib/flagsmith/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module Flagsmith - VERSION = '3.0.0' + VERSION = '3.0.1' end diff --git a/spec/engine-test-data b/spec/engine-test-data index 933f2ba..81e3037 160000 --- a/spec/engine-test-data +++ b/spec/engine-test-data @@ -1 +1 @@ -Subproject commit 933f2ba7aa6430797afc2d053530cfd005b461f6 +Subproject commit 81e3037fbd3fcf6debcede424706fdbeb1b0c0a6