featurevisor-openfeature 1.1.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ac21a86ef0432191f6a12f262dd00b524f520a0e5a1cd88bf2029b0428842c46
4
- data.tar.gz: 9add287e8f085ee1b446db5ccf5c577a2f4758a78be61ca36dc0af4793b9f8ab
3
+ metadata.gz: 3a8495a304439dace1b52ba45bd2a7e9b4947cf482a179af80b9a3c8bb1f8e2c
4
+ data.tar.gz: 3473b28bac62de80fd0950c895d1a1a912919752cb99007cf04f922702bd6bb1
5
5
  SHA512:
6
- metadata.gz: f0ea87c62a2cfbe51bd00c5d3d75b2fb1f26f2989d5df8cfe44f52fba840374673c439960855441783074c61e531185d11decd393838cc7da485740467e53e3e
7
- data.tar.gz: d93238543cd2e2bef46ed2d2bc6c56ce4ca6bf8fc35211a837f1523d1e14c7bcbb016a7aea95d2b1057e2d09632d949c928d098495d0315a38fc868fdb640ae4
6
+ metadata.gz: f325bd259bd59b4e8ae6a3ef7bb6b3b48d534bbfcaaf7e2212f1bacc9b23e334bcc1e54b7bd017c8adcd4ed09340e56345ad89798a7d4c7cbcf72f521169dd23
7
+ data.tar.gz: 101e2d9eab3c39afea5cb634e294107606cf500db85040916ecaf0c570b66ea7c47b09773a7dfa11c09604f17ad858c99c712b48854500483a044710d7a7feec
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2025 Fahad Heylaal (https://fahad19.com)
3
+ Copyright (c) 2026 Fahad Heylaal (https://fahad19.com)
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -19,8 +19,9 @@ This SDK is compatible with Featurevisor v3 projects and v2 datafiles.
19
19
  - [Getting variation](#getting-variation)
20
20
  - [Getting variables](#getting-variables)
21
21
  - [Type specific methods](#type-specific-methods)
22
- - [Getting all evaluations](#getting-all-evaluations)
23
- - [Sticky](#sticky)
22
+ - [Getting global variables](#getting-global-variables)
23
+ - [Getting aggregate evaluations](#getting-aggregate-evaluations)
24
+ - [Sticky features and variables](#sticky-features-and-variables)
24
25
  - [Initialize with sticky](#initialize-with-sticky)
25
26
  - [Set sticky afterwards](#set-sticky-afterwards)
26
27
  - [Setting datafile](#setting-datafile)
@@ -36,7 +37,7 @@ This SDK is compatible with Featurevisor v3 projects and v2 datafiles.
36
37
  - [Events](#events)
37
38
  - [`datafile_set`](#datafile_set)
38
39
  - [`context_set`](#context_set)
39
- - [`sticky_set`](#sticky_set)
40
+ - [`sticky_features_set` and `sticky_variables_set`](#sticky_features_set-and-sticky_variables_set)
40
41
  - [`error`](#error)
41
42
  - [Modules](#modules)
42
43
  - [Defining a module](#defining-a-module)
@@ -89,6 +90,8 @@ f = Featurevisor.create_featurevisor(
89
90
 
90
91
  Most applications only need this factory and the returned `Featurevisor::Instance`. Public extension and observability APIs include modules, diagnostics, events, and the datafile structures accepted by the factory.
91
92
 
93
+ Concurrent evaluations are safe after an instance is configured. Do not mutate or close the same instance concurrently with evaluations. Serialize calls to `set_datafile`, `set_context`, `set_sticky_features`, `set_sticky_variables`, `add_module`, `remove_module`, and `close`. Module, event, and diagnostic callbacks must synchronize mutable state that they capture.
94
+
92
95
  ## Initialization
93
96
 
94
97
  The SDK can be initialized by passing [datafile](https://featurevisor.com/docs/building-datafiles/) content directly:
@@ -293,12 +296,24 @@ f.get_variable_json(feature_key, variable_key, context = {})
293
296
 
294
297
  Type specific methods do not coerce values. `get_variable_integer` returns `nil` for the string `"1"`, and boolean getters return `nil` for non-boolean values.
295
298
 
296
- ## Getting all evaluations
299
+ ## Getting global variables
300
+
301
+ Global variables use the same methods with a single variable key:
302
+
303
+ ```ruby
304
+ message = f.get_variable_string('welcomeMessage', context)
305
+ value = f.get_variable('checkoutSettings', context)
306
+ evaluation = f.evaluate_variable('checkoutSettings', context)
307
+ ```
308
+
309
+ Global variables resolve sticky values first, then required features, then the first matching override, and finally their default value.
310
+
311
+ ## Getting aggregate evaluations
297
312
 
298
313
  You can get evaluations of all features available in the SDK instance:
299
314
 
300
315
  ```ruby
301
- all_evaluations = f.get_all_evaluations({})
316
+ all_evaluations = f.get_feature_evaluations({})
302
317
 
303
318
  puts all_evaluations
304
319
  # {
@@ -319,11 +334,17 @@ puts all_evaluations
319
334
 
320
335
  This is handy especially when you want to pass all evaluations from a backend application to the frontend.
321
336
 
322
- ## Sticky
337
+ Global variable values are available separately:
338
+
339
+ ```ruby
340
+ global_variables = f.get_variable_evaluations({})
341
+ ```
342
+
343
+ ## Sticky features and variables
323
344
 
324
345
  For the lifecycle of the SDK instance in your application, you can set some features with sticky values, meaning that they will not be evaluated against the fetched [datafile](https://featurevisor.com/docs/building-datafiles/):
325
346
 
326
- Sticky values belong to an SDK or child instance. Evaluation options do not accept sticky overrides; use `spawn(context, sticky: ...)` when a child needs its own sticky state.
347
+ Sticky values belong to an SDK or child instance. Feature and global variable sticky values are independent.
327
348
 
328
349
  ### Initialize with sticky
329
350
 
@@ -331,7 +352,7 @@ Sticky values belong to an SDK or child instance. Evaluation options do not acce
331
352
  require 'featurevisor'
332
353
 
333
354
  f = Featurevisor.create_featurevisor(
334
- sticky: {
355
+ sticky_features: {
335
356
  myFeatureKey: {
336
357
  enabled: true,
337
358
  # optional
@@ -343,6 +364,9 @@ f = Featurevisor.create_featurevisor(
343
364
  anotherFeatureKey: {
344
365
  enabled: false
345
366
  }
367
+ },
368
+ sticky_variables: {
369
+ welcomeMessage: 'Welcome back'
346
370
  }
347
371
  )
348
372
  ```
@@ -354,7 +378,7 @@ Once initialized with sticky features, the SDK will look for values there first
354
378
  You can also set sticky features after the SDK is initialized:
355
379
 
356
380
  ```ruby
357
- f.set_sticky({
381
+ f.set_sticky_features({
358
382
  myFeatureKey: {
359
383
  enabled: true,
360
384
  variation: 'treatment',
@@ -366,6 +390,8 @@ f.set_sticky({
366
390
  enabled: false
367
391
  }
368
392
  }, true) # replace existing sticky features (false by default)
393
+
394
+ f.set_sticky_variables({ welcomeMessage: 'Welcome back' }, true)
369
395
  ```
370
396
 
371
397
  ## Setting datafile
@@ -391,7 +417,7 @@ By default, `set_datafile(datafile)` merges the incoming datafile into the SDK's
391
417
  - `segments` are merged, with incoming entries overriding existing ones
392
418
  - `features` are merged, with incoming entries overriding existing ones
393
419
 
394
- This means you can call `set_datafile` more than once with different datafiles, and the SDK instance accumulates their features and segments together.
420
+ This means you can call `set_datafile` more than once with different datafiles, and the SDK instance accumulates their features, segments, and global variables together.
395
421
 
396
422
  ### Replacing
397
423
 
@@ -543,15 +569,19 @@ unsubscribe = f.on('context_set') do |event|
543
569
  end
544
570
  ```
545
571
 
546
- ### `sticky_set`
572
+ ### `sticky_features_set` and `sticky_variables_set`
547
573
 
548
574
  ```ruby
549
- unsubscribe = f.on('sticky_set') do |event|
575
+ feature_unsubscribe = f.on('sticky_features_set') do |event|
550
576
  replaced = event[:replaced] # true if sticky features got replaced
551
577
  features = event[:features] # list of all affected feature keys
552
578
 
553
579
  puts 'Sticky features set'
554
580
  end
581
+
582
+ variable_unsubscribe = f.on('sticky_variables_set') do |event|
583
+ variables = event[:variables]
584
+ end
555
585
  ```
556
586
 
557
587
  ### `error`
@@ -581,11 +611,14 @@ evaluation = f.evaluate_variation(feature_key, context = {})
581
611
 
582
612
  # variable
583
613
  evaluation = f.evaluate_variable(feature_key, variable_key, context = {})
614
+
615
+ # global variable
616
+ global_evaluation = f.evaluate_variable(variable_key, context = {})
584
617
  ```
585
618
 
586
619
  The returned object will always contain the following properties:
587
620
 
588
- - `feature_key`: the feature key
621
+ - `feature_key`: the feature key when evaluating a feature
589
622
  - `reason`: the reason how the value was evaluated
590
623
 
591
624
  And optionally these properties depending on whether you are evaluating a feature variation or a variable:
@@ -604,6 +637,8 @@ And optionally these properties depending on whether you are evaluating a featur
604
637
 
605
638
  Modules allow you to intercept the evaluation process and customize SDK behavior.
606
639
 
640
+ For feature evaluations, all `before` callbacks run in registration order, followed by all `before_evaluation` callbacks. After evaluation and caller defaults, all `after_evaluation` callbacks run, followed by all `after` callbacks. Global variable evaluations use only `before_evaluation` and `after_evaluation`. Required feature checks run through the complete module pipeline, and transformed defaults are preserved.
641
+
607
642
  ### Defining a module
608
643
 
609
644
  A module is a simple hash with a unique recommended `name` and optional lifecycle functions:
@@ -637,6 +672,9 @@ my_custom_module = {
637
672
  options
638
673
  },
639
674
 
675
+ # unified callback for feature and global variable evaluations
676
+ before_evaluation: ->(options) { options },
677
+
640
678
  # after evaluation
641
679
  after: ->(evaluation, options) {
642
680
  reason = evaluation[:reason]
@@ -646,6 +684,9 @@ my_custom_module = {
646
684
  end
647
685
  },
648
686
 
687
+ # unified callback for feature and global variable evaluations
688
+ after_evaluation: ->(evaluation, options) { evaluation },
689
+
649
690
  # configure bucket key
650
691
  bucket_key: ->(options) {
651
692
  # return custom bucket key
@@ -697,6 +738,8 @@ f.remove_module('my-custom-module')
697
738
 
698
739
  ## Child instance
699
740
 
741
+ A child snapshots the parent keys that exist when it is spawned. Child values win for those keys. Parent keys introduced later are still inherited. Calling `close` removes both child-owned listeners and subscriptions delegated to the parent.
742
+
700
743
  When dealing with purely client-side applications, it is understandable that there is only one user involved, like in browser or mobile applications.
701
744
 
702
745
  But when using Featurevisor SDK in server-side applications, where a single server instance can handle multiple user requests simultaneously, it is important to isolate the context for each request.
@@ -716,14 +759,19 @@ Now you can pass the child instance where your individual request is being handl
716
759
  is_enabled = child_f.is_enabled('my_feature')
717
760
  variation = child_f.get_variation('my_feature')
718
761
  variable_value = child_f.get_variable('my_feature', 'my_variable')
762
+ global_value = child_f.get_variable('welcomeMessage')
719
763
  ```
720
764
 
721
765
  Similar to parent SDK, child instances also support several additional methods:
722
766
 
723
767
  - `set_context`
724
- - `set_sticky`
768
+ - `set_sticky_features`
769
+ - `set_sticky_variables`
770
+ - `evaluate_flag`
725
771
  - `is_enabled`
772
+ - `evaluate_variation`
726
773
  - `get_variation`
774
+ - `evaluate_variable`
727
775
  - `get_variable`
728
776
  - `get_variable_boolean`
729
777
  - `get_variable_string`
@@ -732,7 +780,8 @@ Similar to parent SDK, child instances also support several additional methods:
732
780
  - `get_variable_array`
733
781
  - `get_variable_object`
734
782
  - `get_variable_json`
735
- - `get_all_evaluations`
783
+ - `get_feature_evaluations`
784
+ - `get_variable_evaluations`
736
785
  - `on`
737
786
  - `close`
738
787
 
@@ -821,7 +870,7 @@ The provider currently requires Ruby 3.4 or newer because that is the minimum ve
821
870
  Install the provider:
822
871
 
823
872
  ```ruby
824
- gem "featurevisor-openfeature", "~> 1.1"
873
+ gem "featurevisor-openfeature", "~> 3.0"
825
874
  ```
826
875
 
827
876
  It installs the matching `featurevisor` gem and the official `openfeature-sdk` dependency. The provider and base SDK deliberately share the same version, and the provider requires that exact Featurevisor version.
@@ -845,9 +894,9 @@ enabled = client.fetch_boolean_value(
845
894
  )
846
895
  ```
847
896
 
848
- Use `checkout` for a flag, `checkout:variation` for its variation, and `checkout:title` for its `title` variable. Boolean variables use the boolean resolver. Arrays, hashes, and JSON variables use the object resolver.
897
+ Use `checkout` for a flag, `checkout:variation` for its variation, `checkout:title` for its `title` variable, and `variable:welcomeMessage` for a global variable. Boolean variables use the boolean resolver. Arrays, hashes, and JSON variables use the object resolver.
849
898
 
850
- OpenFeature's targeting key maps to `userId` by default. `targeting_key_field`, `key_separator`, and `variation_key` can customize the mapping.
899
+ OpenFeature's targeting key maps to `userId` by default. `targeting_key_field`, `key_separator`, `variation_key`, and `global_variable_prefix` can customize the mapping. The global variable prefix defaults to `variable` and cannot contain the separator.
851
900
 
852
901
  You can pass any Featurevisor initialization options directly to the provider. These options are used to create the Featurevisor instance owned by the provider:
853
902
 
@@ -901,7 +950,7 @@ The build produces `featurevisor-VERSION.gem` and `featurevisor-openfeature-VERS
901
950
  - Run `bundle install`
902
951
  - Push commit to `main` branch
903
952
  - Wait for CI to complete
904
- - Tag the release with the same version number, for example `v1.1.0`
953
+ - Tag the release with the same version number, for example `v3.0.0`
905
954
  - The workflow verifies that the tag matches the shared version
906
955
  - The workflow publishes `featurevisor` first, followed by `featurevisor-openfeature`
907
956
 
@@ -11,13 +11,15 @@ module Featurevisor
11
11
 
12
12
  attr_reader :metadata, :featurevisor
13
13
 
14
- def initialize(options = {}, featurevisor: nil, targeting_key_field: "userId", key_separator: ":", variation_key: "variation", on_track: nil, **featurevisor_options)
14
+ def initialize(options = {}, featurevisor: nil, targeting_key_field: "userId", key_separator: ":", variation_key: "variation", global_variable_prefix: "variable", on_track: nil, **featurevisor_options)
15
15
  raise ArgumentError, "options must be a Hash" unless options.is_a?(Hash)
16
16
 
17
17
  @metadata = Provider::ProviderMetadata.new(name: "Featurevisor").freeze
18
18
  @targeting_key_field = targeting_key_field.empty? ? "userId" : targeting_key_field
19
19
  @key_separator = key_separator.empty? ? ":" : key_separator
20
20
  @variation_key = variation_key.empty? ? "variation" : variation_key
21
+ @global_variable_prefix = global_variable_prefix.empty? ? "variable" : global_variable_prefix
22
+ raise ArgumentError, "global_variable_prefix cannot contain key_separator" if @global_variable_prefix.include?(@key_separator)
21
23
  @on_track = on_track
22
24
  @datafile_error = nil
23
25
  @shutdown = false
@@ -86,7 +88,10 @@ module Featurevisor
86
88
  targeting_key = evaluation_context&.targeting_key
87
89
  context[@targeting_key_field] = targeting_key if targeting_key && !targeting_key.empty?
88
90
 
89
- if selector.nil? || selector.empty?
91
+ if feature_key == @global_variable_prefix && selector && !selector.empty?
92
+ evaluation = featurevisor.evaluate_variable(selector, context)
93
+ value = evaluation[:variable_value]
94
+ elsif selector.nil? || selector.empty?
90
95
  return type_mismatch(flag_key, default_value, expected_type) unless expected_type == :boolean
91
96
  evaluation = featurevisor.evaluate_flag(feature_key, context)
92
97
  value = evaluation[:enabled]
@@ -96,7 +101,8 @@ module Featurevisor
96
101
  else
97
102
  evaluation = featurevisor.evaluate_variable(feature_key, selector, context)
98
103
  value = evaluation[:variable_value]
99
- if evaluation.dig(:variable_schema, :type) == "json" && value.is_a?(String)
104
+ variable_type = evaluation.dig(:variable_schema, :type) || evaluation.dig(:variable, :type)
105
+ if variable_type == "json" && value.is_a?(String)
100
106
  begin
101
107
  value = JSON.parse(value)
102
108
  rescue JSON::ParserError
@@ -127,10 +133,10 @@ module Featurevisor
127
133
 
128
134
  def metadata_for(evaluation)
129
135
  metadata = {
130
- "featureKey" => evaluation[:feature_key],
131
136
  "featurevisorReason" => evaluation[:reason],
132
137
  "schemaVersion" => featurevisor.get_schema_version
133
138
  }
139
+ metadata["featureKey"] = evaluation[:feature_key] unless evaluation[:feature_key].nil?
134
140
  metadata["revision"] = featurevisor.get_revision if featurevisor.get_revision
135
141
  {
136
142
  variable_key: "variableKey",
@@ -138,7 +144,9 @@ module Featurevisor
138
144
  bucket_key: "bucketKey",
139
145
  bucket_value: "bucketValue",
140
146
  force_index: "forceIndex",
141
- variable_override_index: "variableOverrideIndex"
147
+ variable_override_index: "variableOverrideIndex",
148
+ variable_override_key: "variableOverrideKey",
149
+ variable_override_path: "variableOverridePath"
142
150
  }.each do |key, metadata_key|
143
151
  metadata[metadata_key] = evaluation[key] unless evaluation[key].nil?
144
152
  end
@@ -149,7 +157,7 @@ module Featurevisor
149
157
  return Provider::Reason::ERROR if %w[feature_not_found variable_not_found no_variations error].include?(value)
150
158
  return Provider::Reason::TARGETING_MATCH if %w[required forced sticky rule variable_override_variation variable_override_rule].include?(value)
151
159
  return Provider::Reason::SPLIT if value == "allocated"
152
- return Provider::Reason::DISABLED if %w[disabled variation_disabled variable_disabled].include?(value)
160
+ return Provider::Reason::DISABLED if %w[disabled variation_disabled variable_disabled required_features_unmet].include?(value)
153
161
  Provider::Reason::DEFAULT
154
162
  end
155
163
 
@@ -162,7 +170,10 @@ module Featurevisor
162
170
  def error_message(evaluation)
163
171
  return evaluation[:error].message if evaluation[:error].respond_to?(:message)
164
172
  return %(Feature "#{evaluation[:feature_key]}" was not found) if evaluation[:reason] == "feature_not_found"
165
- return %(Variable "#{evaluation[:variable_key]}" was not found for feature "#{evaluation[:feature_key]}") if evaluation[:reason] == "variable_not_found"
173
+ if evaluation[:reason] == "variable_not_found"
174
+ return %(Variable "#{evaluation[:variable_key]}" was not found) unless evaluation[:feature_key]
175
+ return %(Variable "#{evaluation[:variable_key]}" was not found for feature "#{evaluation[:feature_key]}")
176
+ end
166
177
  return %(Feature "#{evaluation[:feature_key]}" has no variations) if evaluation[:reason] == "no_variations"
167
178
  "Featurevisor evaluation failed"
168
179
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: featurevisor-openfeature
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fahad Heylaal
@@ -15,14 +15,14 @@ dependencies:
15
15
  requirements:
16
16
  - - '='
17
17
  - !ruby/object:Gem::Version
18
- version: 1.1.0
18
+ version: 3.0.0
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - '='
24
24
  - !ruby/object:Gem::Version
25
- version: 1.1.0
25
+ version: 3.0.0
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: openfeature-sdk
28
28
  requirement: !ruby/object:Gem::Requirement