subflag-rails 0.2.0 → 0.4.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.
data/lib/subflag/rails.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "open_feature/sdk"
4
- require "subflag"
5
4
 
6
5
  require_relative "rails/version"
7
6
  require_relative "rails/configuration"
@@ -27,14 +26,25 @@ module Subflag
27
26
 
28
27
  # Configure Subflag for Rails
29
28
  #
30
- # @example
29
+ # @example Using Subflag Cloud (SaaS)
31
30
  # Subflag::Rails.configure do |config|
31
+ # config.backend = :subflag
32
32
  # config.api_key = Rails.application.credentials.subflag_api_key
33
33
  # config.user_context do |user|
34
34
  # { targeting_key: user.id.to_s, email: user.email, plan: user.plan }
35
35
  # end
36
36
  # end
37
37
  #
38
+ # @example Using ActiveRecord (self-hosted)
39
+ # Subflag::Rails.configure do |config|
40
+ # config.backend = :active_record
41
+ # end
42
+ #
43
+ # @example Using Memory (testing)
44
+ # Subflag::Rails.configure do |config|
45
+ # config.backend = :memory
46
+ # end
47
+ #
38
48
  # @yield [Configuration]
39
49
  def configure
40
50
  yield(configuration)
@@ -48,25 +58,86 @@ module Subflag
48
58
  @client ||= Client.new
49
59
  end
50
60
 
61
+ # Access the current provider instance
62
+ #
63
+ # Useful for the Memory backend where you can set flags directly:
64
+ # Subflag::Rails.provider.set(:my_flag, true)
65
+ #
66
+ # @return [Object] The current OpenFeature provider
67
+ def provider
68
+ @provider
69
+ end
70
+
71
+ # Prefetch all flags for a user/context in a single API call
72
+ #
73
+ # Call this early in a request to fetch all flags at once.
74
+ # Subsequent flag lookups will use the cached values.
75
+ #
76
+ # @param user [Object, nil] The user object for targeting
77
+ # @param context [Hash, nil] Additional context attributes
78
+ # @return [Array<Hash>] Array of prefetched flag results (for inspection)
79
+ #
80
+ # @example Prefetch in a controller
81
+ # before_action :prefetch_flags
82
+ #
83
+ # def prefetch_flags
84
+ # Subflag::Rails.prefetch_flags(user: current_user)
85
+ # end
86
+ #
87
+ def prefetch_flags(user: nil, context: nil)
88
+ client.prefetch_all(user: user, context: context)
89
+ end
90
+
51
91
  # Reset configuration (primarily for testing)
52
92
  def reset!
53
93
  @configuration = Configuration.new
54
94
  @client = nil
95
+ @provider = nil
55
96
  end
56
97
 
57
98
  private
58
99
 
59
100
  def setup_provider
60
- return unless configuration.api_key
101
+ @provider = build_provider
102
+ return unless @provider
103
+
104
+ OpenFeature::SDK.configure do |config|
105
+ config.set_provider(@provider)
106
+ end
107
+ end
108
+
109
+ def build_provider
110
+ case configuration.backend
111
+ when :subflag
112
+ build_subflag_provider
113
+ when :active_record
114
+ build_active_record_provider
115
+ when :memory
116
+ build_memory_provider
117
+ else
118
+ raise ArgumentError, "Unknown backend: #{configuration.backend}. Use :subflag, :active_record, or :memory"
119
+ end
120
+ end
121
+
122
+ def build_subflag_provider
123
+ return nil unless configuration.api_key
61
124
 
62
- provider = ::Subflag::Provider.new(
125
+ require_relative "rails/backends/subflag_provider"
126
+ Backends::SubflagProvider.new(
63
127
  api_key: configuration.api_key,
64
128
  api_url: configuration.api_url
65
129
  )
130
+ end
66
131
 
67
- OpenFeature::SDK.configure do |config|
68
- config.set_provider(provider)
69
- end
132
+ def build_active_record_provider
133
+ require_relative "rails/backends/active_record_provider"
134
+ require_relative "rails/models/flag"
135
+ Backends::ActiveRecordProvider.new
136
+ end
137
+
138
+ def build_memory_provider
139
+ require_relative "rails/backends/memory_provider"
140
+ Backends::MemoryProvider.new
70
141
  end
71
142
  end
72
143
  end
@@ -92,5 +163,20 @@ module Subflag
92
163
  def flags(user: nil, context: nil)
93
164
  Rails::FlagAccessor.new(user: user, context: context)
94
165
  end
166
+
167
+ # Prefetch all flags for a user/context in a single API call
168
+ #
169
+ # @param user [Object, nil] The user object for targeting
170
+ # @param context [Hash, nil] Additional context attributes
171
+ # @return [Array<Hash>] Array of prefetched flag results
172
+ #
173
+ # @example
174
+ # Subflag.prefetch_flags(user: current_user)
175
+ # # Subsequent lookups use cache
176
+ # Subflag.flags(user: current_user).new_feature?(default: false)
177
+ #
178
+ def prefetch_flags(user: nil, context: nil)
179
+ Rails.prefetch_flags(user: user, context: context)
180
+ end
95
181
  end
96
182
  end
metadata CHANGED
@@ -1,22 +1,22 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: subflag-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Subflag
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-12-01 00:00:00.000000000 Z
11
+ date: 2025-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: subflag-openfeature-provider
14
+ name: openfeature-sdk
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0.1'
19
+ version: '0.3'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
22
  version: '1.0'
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: '0.1'
29
+ version: '0.3'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '1.0'
@@ -170,9 +170,9 @@ dependencies:
170
170
  - - ">="
171
171
  - !ruby/object:Gem::Version
172
172
  version: '6.1'
173
- description: Rails integration for Subflag feature flags. Get typed values (boolean,
174
- string, integer, double, object) with user targeting. Includes generators, view
175
- helpers, and ActiveRecord context integration.
173
+ description: Feature flags for Rails with pluggable backends. Use Subflag Cloud (SaaS),
174
+ ActiveRecord (self-hosted), or Memory (testing). Get typed values (boolean, string,
175
+ integer, double, object) with the same API regardless of backend.
176
176
  email:
177
177
  - support@subflag.com
178
178
  executables: []
@@ -183,15 +183,20 @@ files:
183
183
  - LICENSE.txt
184
184
  - README.md
185
185
  - lib/generators/subflag/install_generator.rb
186
- - lib/generators/subflag/templates/initializer.rb
186
+ - lib/generators/subflag/templates/create_subflag_flags.rb.tt
187
+ - lib/generators/subflag/templates/initializer.rb.tt
187
188
  - lib/subflag-rails.rb
188
189
  - lib/subflag/rails.rb
190
+ - lib/subflag/rails/backends/active_record_provider.rb
191
+ - lib/subflag/rails/backends/memory_provider.rb
192
+ - lib/subflag/rails/backends/subflag_provider.rb
189
193
  - lib/subflag/rails/client.rb
190
194
  - lib/subflag/rails/configuration.rb
191
195
  - lib/subflag/rails/context_builder.rb
192
196
  - lib/subflag/rails/evaluation_result.rb
193
197
  - lib/subflag/rails/flag_accessor.rb
194
198
  - lib/subflag/rails/helpers.rb
199
+ - lib/subflag/rails/models/flag.rb
195
200
  - lib/subflag/rails/railtie.rb
196
201
  - lib/subflag/rails/request_cache.rb
197
202
  - lib/subflag/rails/test_helpers.rb