flipper 1.4.2 → 1.5.0.rc2
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 +4 -4
- data/README.md +70 -10
- data/lib/flipper/adapter.rb +24 -0
- data/lib/flipper/adapter_builder.rb +12 -1
- data/lib/flipper/adapters/actor_limit.rb +4 -3
- data/lib/flipper/adapters/cache_base.rb +17 -0
- data/lib/flipper/adapters/dual_write.rb +12 -0
- data/lib/flipper/adapters/failover.rb +15 -0
- data/lib/flipper/adapters/failsafe.rb +16 -0
- data/lib/flipper/adapters/http/client.rb +7 -3
- data/lib/flipper/adapters/http/redacted_debug_output.rb +70 -0
- data/lib/flipper/adapters/http.rb +42 -8
- data/lib/flipper/adapters/instrumented.rb +28 -0
- data/lib/flipper/adapters/memoizable.rb +21 -5
- data/lib/flipper/adapters/memory.rb +63 -31
- data/lib/flipper/adapters/poll.rb +51 -6
- data/lib/flipper/adapters/pstore.rb +15 -0
- data/lib/flipper/adapters/read_only.rb +1 -1
- data/lib/flipper/adapters/strict.rb +17 -1
- data/lib/flipper/adapters/sync/feature_synchronizer.rb +3 -1
- data/lib/flipper/adapters/sync/synchronizer.rb +20 -1
- data/lib/flipper/adapters/sync.rb +8 -0
- data/lib/flipper/adapters/wrapper.rb +7 -0
- data/lib/flipper/cli.rb +87 -22
- data/lib/flipper/cloud/configuration.rb +149 -19
- data/lib/flipper/cloud/dsl.rb +6 -0
- data/lib/flipper/cloud/memory_adapter.rb +43 -0
- data/lib/flipper/cloud/message_verifier.rb +1 -0
- data/lib/flipper/cloud/middleware.rb +8 -3
- data/lib/flipper/cloud/migrate.rb +3 -1
- data/lib/flipper/cloud/routes.rb +24 -2
- data/lib/flipper/cloud/shared_memory.rb +111 -0
- data/lib/flipper/cloud/telemetry.rb +56 -28
- data/lib/flipper/cloud/url_validator.rb +26 -0
- data/lib/flipper/cloud.rb +22 -4
- data/lib/flipper/configuration.rb +86 -3
- data/lib/flipper/dsl.rb +47 -3
- data/lib/flipper/engine.rb +64 -8
- data/lib/flipper/errors.rb +18 -0
- data/lib/flipper/export.rb +4 -0
- data/lib/flipper/exporters/json/export.rb +6 -0
- data/lib/flipper/exporters/json/v1.rb +5 -9
- data/lib/flipper/expression/constant.rb +6 -0
- data/lib/flipper/expression.rb +105 -1
- data/lib/flipper/expressions/comparable.rb +5 -0
- data/lib/flipper/expressions/feature_enabled.rb +11 -7
- data/lib/flipper/expressions/number.rb +2 -0
- data/lib/flipper/expressions/percentage.rb +3 -1
- data/lib/flipper/expressions/percentage_of_actors.rb +9 -0
- data/lib/flipper/expressions/random.rb +2 -0
- data/lib/flipper/expressions/sem_ver.rb +24 -0
- data/lib/flipper/expressions/time.rb +9 -1
- data/lib/flipper/feature.rb +153 -43
- data/lib/flipper/feature_check_context.rb +19 -1
- data/lib/flipper/gates/actor.rb +4 -1
- data/lib/flipper/gates/expression.rb +17 -2
- data/lib/flipper/gates/group.rb +1 -1
- data/lib/flipper/gates/percentage_of_actors.rb +19 -2
- data/lib/flipper/gates/percentage_of_time.rb +4 -1
- data/lib/flipper/groups.rb +66 -0
- data/lib/flipper/instrumentation/log_subscriber.rb +6 -1
- data/lib/flipper/instrumentation/subscriber.rb +21 -2
- data/lib/flipper/middleware/memoizer.rb +5 -2
- data/lib/flipper/named_configuration.rb +194 -0
- data/lib/flipper/named_proxy.rb +46 -0
- data/lib/flipper/poller.rb +39 -17
- data/lib/flipper/sem_ver.rb +84 -0
- data/lib/flipper/spec/shared_adapter_specs.rb +40 -0
- data/lib/flipper/sync_version.rb +61 -0
- data/lib/flipper/test_help.rb +50 -0
- data/lib/flipper/typecast.rb +15 -0
- data/lib/flipper/types/group.rb +2 -2
- data/lib/flipper/version.rb +1 -1
- data/lib/flipper.rb +183 -65
- data/lib/generators/flipper/setup_generator.rb +18 -0
- data/lib/generators/flipper/table_prefix.rb +44 -0
- data/lib/generators/flipper/templates/initializer.rb +7 -0
- data/lib/generators/flipper/templates/update/migrations/01_create_flipper_tables.rb.erb +13 -7
- data/lib/generators/flipper/templates/update/migrations/02_change_flipper_gates_value_to_text.rb.erb +12 -7
- data/lib/generators/flipper/update_generator.rb +3 -0
- metadata +42 -171
- data/.codeclimate.yml +0 -3
- data/.github/FUNDING.yml +0 -1
- data/.github/dependabot.yml +0 -6
- data/.github/workflows/ci.yml +0 -110
- data/.github/workflows/examples.yml +0 -105
- data/.github/workflows/release.yml +0 -54
- data/.rspec +0 -1
- data/.superset/config.json +0 -4
- data/CLAUDE.md +0 -93
- data/CODE_OF_CONDUCT.md +0 -46
- data/Dockerfile +0 -27
- data/Gemfile +0 -41
- data/Rakefile +0 -54
- data/benchmark/enabled_ips.rb +0 -10
- data/benchmark/enabled_multiple_actors_ips.rb +0 -20
- data/benchmark/enabled_profile.rb +0 -20
- data/benchmark/instrumentation_ips.rb +0 -21
- data/benchmark/typecast_ips.rb +0 -27
- data/docker-compose.yml +0 -37
- data/docs/DockerCompose.md +0 -18
- data/docs/README.md +0 -1
- data/docs/images/banner.jpg +0 -0
- data/docs/images/flipper_cloud.png +0 -0
- data/examples/api/basic.ru +0 -18
- data/examples/api/custom_memoized.ru +0 -36
- data/examples/api/memoized.ru +0 -42
- data/examples/basic.rb +0 -19
- data/examples/cloud/app.ru +0 -12
- data/examples/cloud/backoff_policy.rb +0 -13
- data/examples/cloud/basic.rb +0 -22
- data/examples/cloud/cloud_setup.rb +0 -20
- data/examples/cloud/forked.rb +0 -36
- data/examples/cloud/import.rb +0 -17
- data/examples/cloud/poll_interval/README.md +0 -111
- data/examples/cloud/poll_interval/client.rb +0 -108
- data/examples/cloud/poll_interval/server.rb +0 -98
- data/examples/cloud/threaded.rb +0 -33
- data/examples/configuring_default.rb +0 -19
- data/examples/dsl.rb +0 -57
- data/examples/enabled_for_actor.rb +0 -37
- data/examples/expressions.rb +0 -237
- data/examples/group.rb +0 -38
- data/examples/group_dynamic_lookup.rb +0 -75
- data/examples/group_with_members.rb +0 -83
- data/examples/importing.rb +0 -29
- data/examples/individual_actor.rb +0 -28
- data/examples/instrumentation.rb +0 -42
- data/examples/instrumentation_last_accessed_at.rb +0 -38
- data/examples/memoizing.rb +0 -35
- data/examples/mirroring.rb +0 -59
- data/examples/percentage_of_actors.rb +0 -31
- data/examples/percentage_of_actors_enabled_check.rb +0 -35
- data/examples/percentage_of_actors_group.rb +0 -35
- data/examples/percentage_of_time.rb +0 -27
- data/examples/strict.rb +0 -18
- data/flipper-cloud.gemspec +0 -19
- data/flipper.gemspec +0 -42
- data/package-lock.json +0 -41
- data/package.json +0 -10
- data/spec/fixtures/environment.rb +0 -1
- data/spec/fixtures/feature.json +0 -31
- data/spec/fixtures/flipper_pstore_1679087600.json +0 -46
- data/spec/flipper/actor_spec.rb +0 -56
- data/spec/flipper/adapter_builder_spec.rb +0 -72
- data/spec/flipper/adapter_spec.rb +0 -166
- data/spec/flipper/adapters/actor_limit_spec.rb +0 -75
- data/spec/flipper/adapters/dual_write_spec.rb +0 -82
- data/spec/flipper/adapters/failover_spec.rb +0 -141
- data/spec/flipper/adapters/failsafe_spec.rb +0 -58
- data/spec/flipper/adapters/http/client_spec.rb +0 -61
- data/spec/flipper/adapters/http_spec.rb +0 -596
- data/spec/flipper/adapters/instrumented_spec.rb +0 -167
- data/spec/flipper/adapters/memoizable_spec.rb +0 -377
- data/spec/flipper/adapters/memory_spec.rb +0 -36
- data/spec/flipper/adapters/operation_logger_spec.rb +0 -128
- data/spec/flipper/adapters/poll_spec.rb +0 -41
- data/spec/flipper/adapters/pstore_spec.rb +0 -16
- data/spec/flipper/adapters/read_only_spec.rb +0 -100
- data/spec/flipper/adapters/strict_spec.rb +0 -122
- data/spec/flipper/adapters/sync/feature_synchronizer_spec.rb +0 -247
- data/spec/flipper/adapters/sync/interval_synchronizer_spec.rb +0 -33
- data/spec/flipper/adapters/sync/synchronizer_spec.rb +0 -175
- data/spec/flipper/adapters/sync_spec.rb +0 -213
- data/spec/flipper/cli_spec.rb +0 -217
- data/spec/flipper/cloud/configuration_spec.rb +0 -251
- data/spec/flipper/cloud/dsl_spec.rb +0 -82
- data/spec/flipper/cloud/message_verifier_spec.rb +0 -104
- data/spec/flipper/cloud/middleware_spec.rb +0 -289
- data/spec/flipper/cloud/migrate_spec.rb +0 -160
- data/spec/flipper/cloud/telemetry/backoff_policy_spec.rb +0 -107
- data/spec/flipper/cloud/telemetry/metric_spec.rb +0 -87
- data/spec/flipper/cloud/telemetry/metric_storage_spec.rb +0 -58
- data/spec/flipper/cloud/telemetry/submitter_spec.rb +0 -145
- data/spec/flipper/cloud/telemetry_spec.rb +0 -208
- data/spec/flipper/cloud_spec.rb +0 -186
- data/spec/flipper/configuration_spec.rb +0 -50
- data/spec/flipper/dsl_spec.rb +0 -346
- data/spec/flipper/engine_spec.rb +0 -374
- data/spec/flipper/export_spec.rb +0 -13
- data/spec/flipper/exporter_spec.rb +0 -16
- data/spec/flipper/exporters/json/export_spec.rb +0 -60
- data/spec/flipper/exporters/json/v1_spec.rb +0 -33
- data/spec/flipper/expression/builder_spec.rb +0 -248
- data/spec/flipper/expression_spec.rb +0 -188
- data/spec/flipper/expressions/all_spec.rb +0 -15
- data/spec/flipper/expressions/any_spec.rb +0 -15
- data/spec/flipper/expressions/boolean_spec.rb +0 -15
- data/spec/flipper/expressions/equal_spec.rb +0 -24
- data/spec/flipper/expressions/greater_than_or_equal_to_spec.rb +0 -28
- data/spec/flipper/expressions/greater_than_spec.rb +0 -28
- data/spec/flipper/expressions/less_than_or_equal_to_spec.rb +0 -28
- data/spec/flipper/expressions/less_than_spec.rb +0 -32
- data/spec/flipper/expressions/not_equal_spec.rb +0 -15
- data/spec/flipper/expressions/now_spec.rb +0 -11
- data/spec/flipper/expressions/number_spec.rb +0 -21
- data/spec/flipper/expressions/percentage_of_actors_spec.rb +0 -20
- data/spec/flipper/expressions/percentage_spec.rb +0 -15
- data/spec/flipper/expressions/property_spec.rb +0 -13
- data/spec/flipper/expressions/random_spec.rb +0 -9
- data/spec/flipper/expressions/string_spec.rb +0 -11
- data/spec/flipper/expressions/time_spec.rb +0 -29
- data/spec/flipper/feature_check_context_spec.rb +0 -65
- data/spec/flipper/feature_spec.rb +0 -1227
- data/spec/flipper/gate_spec.rb +0 -39
- data/spec/flipper/gate_values_spec.rb +0 -103
- data/spec/flipper/gates/actor_spec.rb +0 -7
- data/spec/flipper/gates/boolean_spec.rb +0 -74
- data/spec/flipper/gates/expression_spec.rb +0 -190
- data/spec/flipper/gates/group_spec.rb +0 -62
- data/spec/flipper/gates/percentage_of_actors_spec.rb +0 -131
- data/spec/flipper/gates/percentage_of_time_spec.rb +0 -37
- data/spec/flipper/identifier_spec.rb +0 -12
- data/spec/flipper/instrumentation/log_subscriber_spec.rb +0 -122
- data/spec/flipper/instrumentation/statsd_subscriber_spec.rb +0 -95
- data/spec/flipper/instrumenters/memory_spec.rb +0 -43
- data/spec/flipper/instrumenters/noop_spec.rb +0 -27
- data/spec/flipper/middleware/memoizer_spec.rb +0 -518
- data/spec/flipper/middleware/setup_env_spec.rb +0 -94
- data/spec/flipper/model/active_record_spec.rb +0 -72
- data/spec/flipper/poller_spec.rb +0 -390
- data/spec/flipper/registry_spec.rb +0 -140
- data/spec/flipper/serializers/gzip_spec.rb +0 -13
- data/spec/flipper/serializers/json_spec.rb +0 -13
- data/spec/flipper/typecast_spec.rb +0 -232
- data/spec/flipper/types/actor_spec.rb +0 -132
- data/spec/flipper/types/boolean_spec.rb +0 -23
- data/spec/flipper/types/group_spec.rb +0 -112
- data/spec/flipper/types/percentage_of_actors_spec.rb +0 -5
- data/spec/flipper/types/percentage_of_time_spec.rb +0 -5
- data/spec/flipper/types/percentage_spec.rb +0 -46
- data/spec/flipper_integration_spec.rb +0 -783
- data/spec/flipper_spec.rb +0 -422
- data/spec/spec_helper.rb +0 -112
- data/spec/support/actor_names.yml +0 -1
- data/spec/support/descriptions.yml +0 -1
- data/spec/support/fail_on_output.rb +0 -8
- data/spec/support/fake_backoff_policy.rb +0 -15
- data/spec/support/fake_udp_socket.rb +0 -27
- data/spec/support/skippable.rb +0 -18
- data/spec/support/spec_helpers.rb +0 -111
- data/test/adapters/actor_limit_test.rb +0 -20
- data/test/adapters/memory_test.rb +0 -9
- data/test/adapters/pstore_test.rb +0 -17
- data/test/test_helper.rb +0 -7
- data/test_rails/generators/flipper/setup_generator_test.rb +0 -69
- data/test_rails/generators/flipper/update_generator_test.rb +0 -96
- data/test_rails/helper.rb +0 -31
- data/test_rails/system/test_help_test.rb +0 -52
data/lib/flipper.rb
CHANGED
|
@@ -1,12 +1,29 @@
|
|
|
1
1
|
require "forwardable"
|
|
2
|
+
require "flipper/groups"
|
|
2
3
|
|
|
3
4
|
module Flipper
|
|
5
|
+
# Included before `extend self` so Flipper.register and friends exist on
|
|
6
|
+
# Rubies that don't propagate later includes, and `include Flipper` keeps
|
|
7
|
+
# providing the group methods.
|
|
8
|
+
include Groups
|
|
4
9
|
extend self
|
|
5
10
|
extend Forwardable
|
|
6
11
|
|
|
12
|
+
# Private: Valid named instance names (they double as Flipper.<name>).
|
|
13
|
+
NAMED_INSTANCE_NAME = /\A[a-z_][a-z0-9_]*\z/
|
|
14
|
+
|
|
7
15
|
# Private: The namespace for all instrumented events.
|
|
8
16
|
InstrumentationNamespace = :flipper
|
|
9
17
|
|
|
18
|
+
# Public: Feature keys starting with this are reserved for data Flipper
|
|
19
|
+
# stores next to features, like the sync version.
|
|
20
|
+
RESERVED_KEY_PREFIX = "__flipper".freeze
|
|
21
|
+
|
|
22
|
+
# Public: Is this feature key reserved for Flipper's own data?
|
|
23
|
+
def reserved_key?(key)
|
|
24
|
+
key.to_s.start_with?(RESERVED_KEY_PREFIX)
|
|
25
|
+
end
|
|
26
|
+
|
|
10
27
|
# Public: Start here. Given an adapter returns a handy DSL to all the flipper
|
|
11
28
|
# goodness. To see supported options, check out dsl.rb.
|
|
12
29
|
def new(adapter, options = {})
|
|
@@ -31,8 +48,9 @@ module Flipper
|
|
|
31
48
|
|
|
32
49
|
# Public: Sets Flipper::Configuration instance.
|
|
33
50
|
def configuration=(configuration)
|
|
34
|
-
# need to reset flipper
|
|
51
|
+
# need to reset flipper instances if configuration changes
|
|
35
52
|
self.instance = nil
|
|
53
|
+
Thread.current[:__flipper_named_instances__] = nil
|
|
36
54
|
@configuration = configuration
|
|
37
55
|
end
|
|
38
56
|
|
|
@@ -43,16 +61,102 @@ module Flipper
|
|
|
43
61
|
#
|
|
44
62
|
# Returns Flipper::DSL instance.
|
|
45
63
|
def instance
|
|
46
|
-
Thread.current[:flipper_instance] ||=
|
|
64
|
+
Thread.current[:flipper_instance] ||= begin
|
|
65
|
+
flipper = configuration.default
|
|
66
|
+
claim_instance_owner(flipper, Flipper)
|
|
67
|
+
flipper
|
|
68
|
+
end
|
|
47
69
|
end
|
|
48
70
|
|
|
49
71
|
# Public: Set the flipper instance. It is most common to use the
|
|
50
72
|
# Configuration#default to set this instance, but for things like the test
|
|
51
73
|
# environment, this writer is actually useful.
|
|
52
74
|
def instance=(flipper)
|
|
75
|
+
claim_instance_owner(flipper, Flipper) if flipper
|
|
53
76
|
Thread.current[:flipper_instance] = flipper
|
|
54
77
|
end
|
|
55
78
|
|
|
79
|
+
# Public: Returns a stable proxy for a configured named instance. Each
|
|
80
|
+
# configured name is also available as Flipper.<name>.
|
|
81
|
+
#
|
|
82
|
+
# CrossAppFlipper = Flipper.named(:cross_app)
|
|
83
|
+
# CrossAppFlipper.enabled?(:chat, person)
|
|
84
|
+
def named(name)
|
|
85
|
+
name = normalize_named_instance_name(name)
|
|
86
|
+
named_configuration(name)
|
|
87
|
+
@named_instance_proxies.compute_if_absent(name) { NamedProxy.new(name) }
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Internal: Returns the current named configuration or raises if missing.
|
|
91
|
+
def named_configuration(name)
|
|
92
|
+
configured = find_named_configuration(name)
|
|
93
|
+
return configured if configured
|
|
94
|
+
|
|
95
|
+
raise NamedInstanceNotFound, "Named instance #{normalize_named_instance_name(name).inspect} has not been configured"
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Internal: Named configurations, or none for custom configuration objects
|
|
99
|
+
# that don't support named instances.
|
|
100
|
+
def named_configurations(config = configuration)
|
|
101
|
+
config.respond_to?(:named_configurations) ? config.named_configurations : []
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# Internal: Returns the per-thread DSL for a configured named instance.
|
|
105
|
+
def named_instance(name)
|
|
106
|
+
configured = named_configuration(name)
|
|
107
|
+
instances = Thread.current[:__flipper_named_instances__] ||= {}
|
|
108
|
+
cached = instances[name]
|
|
109
|
+
|
|
110
|
+
if cached && cached[:configuration].equal?(configured) && cached[:version] == configured.version
|
|
111
|
+
cached[:instance]
|
|
112
|
+
else
|
|
113
|
+
loop do
|
|
114
|
+
version = configured.version
|
|
115
|
+
instance = configured.default
|
|
116
|
+
next unless version == configured.version
|
|
117
|
+
|
|
118
|
+
instances[name] = {
|
|
119
|
+
configuration: configured,
|
|
120
|
+
version: version,
|
|
121
|
+
instance: instance,
|
|
122
|
+
}
|
|
123
|
+
return instance
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# Internal: Reset named DSL caches for the current thread.
|
|
129
|
+
def reset_named_instances
|
|
130
|
+
Thread.current[:__flipper_named_instances__] = nil
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Internal: Normalize and validate a named instance name.
|
|
134
|
+
def normalize_named_instance_name(name)
|
|
135
|
+
unless name.is_a?(String) || name.is_a?(Symbol)
|
|
136
|
+
raise InvalidNamedInstanceName, "Named instance name must be a String or Symbol"
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
normalized = name.to_s
|
|
140
|
+
unless NAMED_INSTANCE_NAME.match?(normalized)
|
|
141
|
+
raise InvalidNamedInstanceName, "Named instance #{name.inspect} must use lowercase snake case"
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
normalized.to_sym
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# Internal: Reject names that Flipper.<name> could never reach because
|
|
148
|
+
# Flipper already responds to them.
|
|
149
|
+
def validate_named_instance_name!(name)
|
|
150
|
+
if singleton_class.method_defined?(name) || singleton_class.private_method_defined?(name)
|
|
151
|
+
raise InvalidNamedInstanceName, "Named instance #{name.inspect} conflicts with an existing Flipper method"
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
name
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
private :named_configuration, :named_configurations, :named_instance, :reset_named_instances,
|
|
158
|
+
:normalize_named_instance_name, :validate_named_instance_name!
|
|
159
|
+
|
|
56
160
|
# Public: All the methods delegated to instance. These should match the
|
|
57
161
|
# interface of Flipper::DSL.
|
|
58
162
|
def_delegators :instance,
|
|
@@ -65,7 +169,7 @@ module Flipper
|
|
|
65
169
|
:enable_percentage_of_time, :disable_percentage_of_time,
|
|
66
170
|
:features, :feature, :[], :preload, :preload_all,
|
|
67
171
|
:adapter, :adapter_stack, :add, :exist?, :remove, :import, :export,
|
|
68
|
-
:memoize=, :memoizing?, :read_only?,
|
|
172
|
+
:memoize=, :memoizing?, :preload?, :read_only?,
|
|
69
173
|
:sync, :sync_secret # For Flipper::Cloud. Will error for OSS Flipper.
|
|
70
174
|
|
|
71
175
|
def any(*args)
|
|
@@ -108,6 +212,10 @@ module Flipper
|
|
|
108
212
|
Expression.build({ Time: value })
|
|
109
213
|
end
|
|
110
214
|
|
|
215
|
+
def semver(value)
|
|
216
|
+
Expression.build({ SemVer: value })
|
|
217
|
+
end
|
|
218
|
+
|
|
111
219
|
def feature_enabled(name)
|
|
112
220
|
Expression.build({ FeatureEnabled: name })
|
|
113
221
|
end
|
|
@@ -116,67 +224,6 @@ module Flipper
|
|
|
116
224
|
feature_enabled(name).eq(false)
|
|
117
225
|
end
|
|
118
226
|
|
|
119
|
-
# Public: Use this to register a group by name.
|
|
120
|
-
#
|
|
121
|
-
# name - The Symbol name of the group.
|
|
122
|
-
# block - The block that should be used to determine if the group matches a
|
|
123
|
-
# given actor.
|
|
124
|
-
#
|
|
125
|
-
# Examples
|
|
126
|
-
#
|
|
127
|
-
# Flipper.register(:admins) { |actor|
|
|
128
|
-
# actor.respond_to?(:admin?) && actor.admin?
|
|
129
|
-
# }
|
|
130
|
-
#
|
|
131
|
-
# Returns a Flipper::Group.
|
|
132
|
-
# Raises Flipper::DuplicateGroup if the group is already registered.
|
|
133
|
-
def register(name, &block)
|
|
134
|
-
group = Types::Group.new(name, &block)
|
|
135
|
-
groups_registry.add(group.name, group)
|
|
136
|
-
group
|
|
137
|
-
rescue Registry::DuplicateKey
|
|
138
|
-
raise DuplicateGroup, "Group #{name.inspect} has already been registered"
|
|
139
|
-
end
|
|
140
|
-
|
|
141
|
-
# Public: Returns a Set of registered Types::Group instances.
|
|
142
|
-
def groups
|
|
143
|
-
groups_registry.values.to_set
|
|
144
|
-
end
|
|
145
|
-
|
|
146
|
-
# Public: Returns a Set of symbols where each symbol is a registered
|
|
147
|
-
# group name. If you just want the names, this is more efficient than doing
|
|
148
|
-
# `Flipper.groups.map(&:name)`.
|
|
149
|
-
def group_names
|
|
150
|
-
groups_registry.keys.to_set
|
|
151
|
-
end
|
|
152
|
-
|
|
153
|
-
# Public: Clears the group registry.
|
|
154
|
-
#
|
|
155
|
-
# Returns nothing.
|
|
156
|
-
def unregister_groups
|
|
157
|
-
groups_registry.clear
|
|
158
|
-
end
|
|
159
|
-
|
|
160
|
-
# Public: Check if a group exists
|
|
161
|
-
#
|
|
162
|
-
# Returns boolean
|
|
163
|
-
def group_exists?(name)
|
|
164
|
-
groups_registry.key?(name)
|
|
165
|
-
end
|
|
166
|
-
|
|
167
|
-
# Public: Fetches a group by name.
|
|
168
|
-
#
|
|
169
|
-
# name - The Symbol name of the group.
|
|
170
|
-
#
|
|
171
|
-
# Examples
|
|
172
|
-
#
|
|
173
|
-
# Flipper.group(:admins)
|
|
174
|
-
#
|
|
175
|
-
# Returns Flipper::Group.
|
|
176
|
-
def group(name)
|
|
177
|
-
groups_registry.get(name) || Types::Group.new(name)
|
|
178
|
-
end
|
|
179
|
-
|
|
180
227
|
# Internal: Registry of all groups_registry.
|
|
181
228
|
def groups_registry
|
|
182
229
|
@groups_registry ||= Registry.new
|
|
@@ -197,7 +244,10 @@ require 'flipper/adapters/memoizable'
|
|
|
197
244
|
require 'flipper/adapters/memory'
|
|
198
245
|
require 'flipper/adapters/strict'
|
|
199
246
|
require 'flipper/adapter_builder'
|
|
247
|
+
require 'flipper/registry'
|
|
200
248
|
require 'flipper/configuration'
|
|
249
|
+
require 'flipper/named_configuration'
|
|
250
|
+
require 'flipper/named_proxy'
|
|
201
251
|
require 'flipper/dsl'
|
|
202
252
|
require 'flipper/errors'
|
|
203
253
|
require 'flipper/feature'
|
|
@@ -208,7 +258,7 @@ require 'flipper/identifier'
|
|
|
208
258
|
require 'flipper/middleware/memoizer'
|
|
209
259
|
require 'flipper/middleware/setup_env'
|
|
210
260
|
require 'flipper/poller'
|
|
211
|
-
require 'flipper/
|
|
261
|
+
require 'flipper/sync_version'
|
|
212
262
|
require 'flipper/expression'
|
|
213
263
|
require 'flipper/type'
|
|
214
264
|
require 'flipper/types/actor'
|
|
@@ -220,4 +270,72 @@ require 'flipper/types/percentage_of_time'
|
|
|
220
270
|
require 'flipper/typecast'
|
|
221
271
|
require 'flipper/version'
|
|
222
272
|
|
|
273
|
+
# Eagerly initialize memoized module state while loading is still
|
|
274
|
+
# single-threaded (require/autoload hold a per-feature lock). This avoids a
|
|
275
|
+
# check-then-set race on the `||=` memoization if two threads were to first
|
|
276
|
+
# touch Flipper concurrently during a parallel boot.
|
|
277
|
+
module Flipper
|
|
278
|
+
@named_instance_proxies = Concurrent::Map.new
|
|
279
|
+
@instance_owner_mutex = Mutex.new
|
|
280
|
+
|
|
281
|
+
class << self
|
|
282
|
+
# Flipper.<name> returns the proxy for a configured named instance, the
|
|
283
|
+
# same object as Flipper.named(:<name>). It is resolved on each call, so
|
|
284
|
+
# it follows configuration changes without defining or removing methods.
|
|
285
|
+
def respond_to_missing?(method_name, include_private = false)
|
|
286
|
+
!named_accessor(method_name).nil? || super
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
private
|
|
290
|
+
|
|
291
|
+
def method_missing(method_name, *args, &block)
|
|
292
|
+
proxy = named_accessor(method_name) if args.empty? && block.nil?
|
|
293
|
+
proxy || super
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
def named_accessor(method_name)
|
|
297
|
+
return unless NAMED_INSTANCE_NAME.match?(method_name) && find_named_configuration(method_name)
|
|
298
|
+
|
|
299
|
+
@named_instance_proxies[method_name] || named(method_name)
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
# Nil when the name isn't configured or the configuration object doesn't
|
|
303
|
+
# support named instances.
|
|
304
|
+
def find_named_configuration(name)
|
|
305
|
+
config = configuration
|
|
306
|
+
config.named_configuration(name) if config.respond_to?(:named_configuration)
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
# Claims a DSL for the default instance (owner Flipper) or a named
|
|
310
|
+
# configuration. Returns false when the instance isn't a Flipper DSL.
|
|
311
|
+
def claim_instance_owner(instance, owner)
|
|
312
|
+
dsl = unwrap_flipper_dsl(instance)
|
|
313
|
+
return false unless dsl
|
|
314
|
+
|
|
315
|
+
@instance_owner_mutex.synchronize do
|
|
316
|
+
dsl.send(:claim_instance_owner, owner)
|
|
317
|
+
end
|
|
318
|
+
true
|
|
319
|
+
end
|
|
320
|
+
|
|
321
|
+
def unwrap_flipper_dsl(instance)
|
|
322
|
+
return instance if instance.is_a?(DSL)
|
|
323
|
+
return unless defined?(Delegator) && instance.is_a?(Delegator)
|
|
324
|
+
|
|
325
|
+
seen = Set.new
|
|
326
|
+
while instance.is_a?(Delegator)
|
|
327
|
+
return if seen.include?(instance.object_id)
|
|
328
|
+
|
|
329
|
+
seen.add(instance.object_id)
|
|
330
|
+
instance = instance.__getobj__
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
instance if instance.is_a?(DSL)
|
|
334
|
+
end
|
|
335
|
+
end
|
|
336
|
+
end
|
|
337
|
+
|
|
338
|
+
Flipper.configuration
|
|
339
|
+
Flipper.groups_registry
|
|
340
|
+
|
|
223
341
|
require "flipper/engine" if defined?(Rails)
|
|
@@ -23,6 +23,24 @@ module Flipper
|
|
|
23
23
|
configure_with_dotenv || configure_with_credentials
|
|
24
24
|
end
|
|
25
25
|
|
|
26
|
+
def show_cloud_upgrade
|
|
27
|
+
return if options[:token] || ENV["FLIPPER_CLOUD_TOKEN"] || behavior == :revoke
|
|
28
|
+
|
|
29
|
+
migrate_db = " bin/rails db:migrate\n" if defined?(Flipper::Adapters::ActiveRecord)
|
|
30
|
+
|
|
31
|
+
say <<~MSG
|
|
32
|
+
|
|
33
|
+
Flipper is installed and self-hosted: your app stores its own features.
|
|
34
|
+
|
|
35
|
+
For a hosted dashboard with audit history, environments, and teammates, try
|
|
36
|
+
Flipper Cloud (free plan, no token needed to start):
|
|
37
|
+
|
|
38
|
+
#{migrate_db} bundle exec flipper cloud migrate
|
|
39
|
+
|
|
40
|
+
https://www.flippercloud.io/docs/guides/migration
|
|
41
|
+
MSG
|
|
42
|
+
end
|
|
43
|
+
|
|
26
44
|
private
|
|
27
45
|
|
|
28
46
|
def configure_with_dotenv
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
require 'active_support/core_ext/string/inflections'
|
|
2
|
+
|
|
3
|
+
module Flipper
|
|
4
|
+
module Generators
|
|
5
|
+
module TablePrefix
|
|
6
|
+
def self.included(generator)
|
|
7
|
+
generator.class_option :table_prefix,
|
|
8
|
+
type: :string,
|
|
9
|
+
default: '',
|
|
10
|
+
desc: 'Prefix for the flipper_features and flipper_gates tables'
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
private
|
|
14
|
+
|
|
15
|
+
def feature_table_name
|
|
16
|
+
:"#{table_prefix}flipper_features"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def gate_table_name
|
|
20
|
+
:"#{table_prefix}flipper_gates"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def prefixed_migration_file_name(file_name)
|
|
24
|
+
file_name.sub('flipper', "#{table_prefix}flipper")
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def migration_class_name(class_name)
|
|
28
|
+
class_name.sub('Flipper', "#{table_prefix.camelize}Flipper")
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def feature_index_name
|
|
32
|
+
:"index_#{table_prefix}flipper_features_on_key" unless table_prefix.empty?
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def gate_index_name
|
|
36
|
+
:"index_#{table_prefix}flipper_gates_on_keys" unless table_prefix.empty?
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def table_prefix
|
|
40
|
+
options[:table_prefix].to_s
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -21,6 +21,13 @@ Rails.application.configure do
|
|
|
21
21
|
## Reconfigure Flipper to use the Memory adapter and disable Cloud in tests
|
|
22
22
|
# config.flipper.test_help = true
|
|
23
23
|
|
|
24
|
+
## Flipper Cloud is built into this gem. Set FLIPPER_CLOUD_TOKEN (or
|
|
25
|
+
## `flipper: cloud_token:` in credentials) and Flipper syncs with Cloud.
|
|
26
|
+
## New to Cloud? `bundle exec flipper cloud migrate` uploads your features and
|
|
27
|
+
## gives you a link to sign up. Already have a Cloud project? Run
|
|
28
|
+
## `bundle exec flipper cloud push` and paste your token to import them there.
|
|
29
|
+
## For more info, see https://www.flippercloud.io/docs/guides/migration
|
|
30
|
+
##
|
|
24
31
|
## The path that Flipper Cloud will use to sync features
|
|
25
32
|
# config.flipper.cloud_path = "_flipper"
|
|
26
33
|
|
|
@@ -1,22 +1,28 @@
|
|
|
1
|
-
|
|
1
|
+
<%
|
|
2
|
+
migration_class = migration_class_name('CreateFlipperTables')
|
|
3
|
+
feature_table = feature_table_name
|
|
4
|
+
gate_table = gate_table_name
|
|
5
|
+
feature_index = feature_index_name
|
|
6
|
+
gate_index = gate_index_name
|
|
7
|
+
%>class <%= migration_class %> < ActiveRecord::Migration<%= migration_version %>
|
|
2
8
|
def up
|
|
3
|
-
create_table
|
|
9
|
+
create_table <%= feature_table.inspect %> do |t|
|
|
4
10
|
t.string :key, null: false
|
|
5
11
|
t.timestamps null: false
|
|
6
12
|
end
|
|
7
|
-
add_index
|
|
13
|
+
add_index <%= feature_table.inspect %>, :key, unique: true<%= ", name: #{feature_index.inspect}" if feature_index %>
|
|
8
14
|
|
|
9
|
-
create_table
|
|
15
|
+
create_table <%= gate_table.inspect %> do |t|
|
|
10
16
|
t.string :feature_key, null: false
|
|
11
17
|
t.string :key, null: false
|
|
12
18
|
t.string :value
|
|
13
19
|
t.timestamps null: false
|
|
14
20
|
end
|
|
15
|
-
add_index
|
|
21
|
+
add_index <%= gate_table.inspect %>, [:feature_key, :key, :value], unique: true<%= ", name: #{gate_index.inspect}" if gate_index %>
|
|
16
22
|
end
|
|
17
23
|
|
|
18
24
|
def down
|
|
19
|
-
drop_table
|
|
20
|
-
drop_table
|
|
25
|
+
drop_table <%= gate_table.inspect %>
|
|
26
|
+
drop_table <%= feature_table.inspect %>
|
|
21
27
|
end
|
|
22
28
|
end
|
data/lib/generators/flipper/templates/update/migrations/02_change_flipper_gates_value_to_text.rb.erb
CHANGED
|
@@ -1,18 +1,23 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<%
|
|
4
|
+
migration_class = migration_class_name('ChangeFlipperGatesValueToText')
|
|
5
|
+
gate_table = gate_table_name
|
|
6
|
+
gate_index = gate_index_name
|
|
7
|
+
%>class <%= migration_class %> < ActiveRecord::Migration<%= migration_version %>
|
|
4
8
|
def up
|
|
5
9
|
# Ensure this incremental update migration is idempotent
|
|
6
|
-
return unless connection.column_exists?
|
|
10
|
+
return unless connection.column_exists? <%= gate_table.inspect %>, :value, :string
|
|
7
11
|
|
|
8
|
-
|
|
9
|
-
|
|
12
|
+
gate_index = connection.indexes(<%= gate_table.inspect %>).find do |index|
|
|
13
|
+
index.columns == %w[feature_key key value]
|
|
10
14
|
end
|
|
11
|
-
|
|
12
|
-
|
|
15
|
+
remove_index <%= gate_table.inspect %>, name: gate_index.name if gate_index
|
|
16
|
+
change_column <%= gate_table.inspect %>, :value, :text
|
|
17
|
+
add_index <%= gate_table.inspect %>, [:feature_key, :key, :value], unique: true, length: { value: 255 }<%= ", name: #{gate_index.inspect}" if gate_index %>
|
|
13
18
|
end
|
|
14
19
|
|
|
15
20
|
def down
|
|
16
|
-
change_column
|
|
21
|
+
change_column <%= gate_table.inspect %>, :value, :string
|
|
17
22
|
end
|
|
18
23
|
end
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require 'rails/generators'
|
|
4
4
|
require 'rails/generators/active_record'
|
|
5
|
+
require 'generators/flipper/table_prefix'
|
|
5
6
|
|
|
6
7
|
module Flipper
|
|
7
8
|
module Generators
|
|
@@ -11,6 +12,7 @@ module Flipper
|
|
|
11
12
|
#
|
|
12
13
|
class UpdateGenerator < Rails::Generators::Base
|
|
13
14
|
include ActiveRecord::Generators::Migration
|
|
15
|
+
include Flipper::Generators::TablePrefix
|
|
14
16
|
|
|
15
17
|
TEMPLATES = File.join(File.dirname(__FILE__), 'templates/update')
|
|
16
18
|
source_paths << TEMPLATES
|
|
@@ -21,6 +23,7 @@ module Flipper
|
|
|
21
23
|
migration_templates = Dir.children(File.join(TEMPLATES, 'migrations')).sort
|
|
22
24
|
migration_templates.each do |template_file|
|
|
23
25
|
destination_file = template_file.match(/^\d*_(.*\.rb)/)[1] # 01_create_flipper_tables.rb.erb => create_flipper_tables.rb
|
|
26
|
+
destination_file = prefixed_migration_file_name(destination_file)
|
|
24
27
|
migration_template "migrations/#{template_file}", File.join(db_migrate_path, destination_file), skip: true
|
|
25
28
|
end
|
|
26
29
|
end
|