Skip to content

Commit

Permalink
Add BaseFlag and DefaultFlag classes (#14)
Browse files Browse the repository at this point in the history
* Add BaseFlag and DefaultFlag classes

* Add require_relative statements

* Add new line to terminate file

* tab size

* Fix ruby syntax

* Update engine test data

* Update example code

* Rationalise flags models to a single file
  • Loading branch information
matthewelwell authored Jun 9, 2022
1 parent 414ccf0 commit 9aceedb
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 71 deletions.
4 changes: 1 addition & 3 deletions example/config/initializers/flagsmith.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
)
3 changes: 1 addition & 2 deletions lib/flagsmith.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
62 changes: 0 additions & 62 deletions lib/flagsmith/sdk/models/flag.rb

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion spec/engine-test-data

0 comments on commit 9aceedb

Please sign in to comment.