flip-flop 0.0.3 → 0.0.5

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
  SHA1:
3
- metadata.gz: 402aff4b7c0a288f0f8f6bf862faa253a991cdeb
4
- data.tar.gz: 6344ef9e5da7ed8cfd854feaf488285f01f3bdef
3
+ metadata.gz: acc3152ed4ebd7b39e858887a7c13c5db472bec2
4
+ data.tar.gz: 5f57534081e131d55c7602534cbfeacacc04b261
5
5
  SHA512:
6
- metadata.gz: eb0ee52c53566d20e347126e2978c10606a0d04f460c426456fbe08d0bf115eeb9c261826148a3fd3668f66e23471a642dc68428da0a6e1b2207d6829adbd13e
7
- data.tar.gz: ecaf65cefa2a7d68e85093e1ea3d1593ce2d09ba301dcf3ccdec4eaff90f93adbb865d7d90ceed942d633085e915ac8aba6276c3de3e4a53d2a148bd1e0b66cb
6
+ metadata.gz: 8fc793de8e5a677db5c9e0bb0b73931ec212753959b6b6c126926cf78a0741b50a797bfe833b41e4bdcd7edbc09a925021fa8811fe4069c1f59923e38cb77b1d
7
+ data.tar.gz: 16a9c9d267af5fd68348bc2109b5f498279c159860a0d519b6ccde14e0e5156188f7d2ec7e2b7a4cec14de349260998ddce572e7e6d83e3d29afb53e7e38bf31
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- flip-flop (0.0.3)
4
+ flip-flop (0.0.5)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -55,4 +55,4 @@ DEPENDENCIES
55
55
  sqlite3
56
56
 
57
57
  BUNDLED WITH
58
- 1.12.4
58
+ 1.12.5
data/README.md CHANGED
@@ -62,9 +62,12 @@ for example:
62
62
  :value: :production
63
63
  :another_rails_env_example:
64
64
  :type: :rails_env
65
- :env:
65
+ :value:
66
66
  - :production
67
67
  - :test
68
+ :group_example:
69
+ :type: :group
70
+ :value: :group_name
68
71
  ```
69
72
 
70
73
  Example Usage
@@ -118,6 +121,34 @@ Gates
118
121
  * `time_range` — enable a feature for the duration of a time range
119
122
  * `percentage_of_time` — enable a feature for a given percentage of checks
120
123
  * `rails_env` — enable a feature for a specified Rails environment
124
+ * `group` — enable a feature for a specified group of users (needs additional configuration)
125
+
126
+ Groups
127
+ ------
128
+
129
+ To use the group adapter you need to register a group. For Ruby on Rails this can be done in an initializer.
130
+ Groups are reusable, many features can be tied to a single group.
131
+
132
+ ```ruby
133
+ FlipFlop::Group.register(:administrators) do |user|
134
+ user.is_admin?
135
+ end
136
+ ```
137
+
138
+ Setup your feature definition. Here is an example using the YAML adapter:
139
+
140
+ ```yaml
141
+ ---
142
+ :awesome_admin_only_feature
143
+ :type: :group
144
+ :value: :administrators
145
+ ```
146
+
147
+ When checking if the feature is enabled for that group you can provide the actor (user).
148
+
149
+ ```ruby
150
+ FlipFlop.feature_enabled? :awesome_admin_only_feature, user
151
+ ```
121
152
 
122
153
  Adapters
123
154
  --------
@@ -1,6 +1,7 @@
1
1
  require 'flip-flop/gates'
2
2
  require 'flip-flop/adapters/memory'
3
3
  require 'flip-flop/railtie' if defined? Rails
4
+ require 'flip-flop/group'
4
5
 
5
6
  module FlipFlop
6
7
 
@@ -32,8 +33,8 @@ module FlipFlop
32
33
  #
33
34
  # Arguments:
34
35
  # feature_name (String/Symbol)
35
- def self.feature_enabled?(feature_name)
36
- get_instance.feature_enabled? feature_name.to_sym
36
+ def self.feature_enabled?(feature_name, actor=nil)
37
+ get_instance.feature_enabled? feature_name.to_sym, actor
37
38
  end
38
39
 
39
40
  class FlipFlop
@@ -63,8 +64,8 @@ module FlipFlop
63
64
  #
64
65
  # Returns:
65
66
  # boolean
66
- def feature_enabled?(feature_name)
67
- public_send feature_type(feature_name), feature_value(feature_name)
67
+ def feature_enabled?(feature_name, actor=nil)
68
+ public_send feature_type(feature_name), feature_value(feature_name), actor
68
69
  rescue
69
70
  false
70
71
  end
@@ -6,9 +6,9 @@ module FlipFlop
6
6
 
7
7
  # override this method to prevent the feature being loaded from the
8
8
  # database twice.
9
- def feature_enabled?(feature_name)
9
+ def feature_enabled?(feature_name, actor=nil)
10
10
  f = get_feature(feature_name)
11
- public_send f.gate_type, f.value
11
+ public_send f.gate_type, f.value, actor
12
12
  rescue
13
13
  false
14
14
  end
@@ -54,7 +54,7 @@ module FlipFlop
54
54
  def gate_type
55
55
  super.to_sym
56
56
  end
57
-
57
+
58
58
  end
59
59
  end
60
60
  end
@@ -2,40 +2,44 @@ require 'date'
2
2
 
3
3
  module FlipFlop
4
4
  module Gates
5
- def boolean(value)
5
+ def boolean(value, actor)
6
6
  value
7
7
  end
8
8
 
9
- def date_range(value)
9
+ def date_range(value, actor)
10
10
  value.begin < Date.today && value.end > Date.today
11
11
  end
12
12
 
13
- def until_date(value)
13
+ def until_date(value, actor)
14
14
  Date.today < value
15
15
  end
16
16
 
17
- def after_date(value)
17
+ def after_date(value, actor)
18
18
  Date.today > value
19
19
  end
20
20
 
21
- def time_range(value)
21
+ def time_range(value, actor)
22
22
  value.begin < Time.now.utc && value.end > Time.now.utc
23
23
  end
24
24
 
25
- def until_time(value)
25
+ def until_time(value, actor)
26
26
  Time.now.utc < value
27
27
  end
28
28
 
29
- def after_time(value)
29
+ def after_time(value, actor)
30
30
  Time.now.utc > value
31
31
  end
32
32
 
33
- def percentage_of_time(value)
33
+ def percentage_of_time(value, actor)
34
34
  rand < (value / 100.0)
35
35
  end
36
36
 
37
- def rails_env(value)
37
+ def rails_env(value, actor)
38
38
  value == Rails.env.to_sym || value.include?(Rails.env.to_sym)
39
39
  end
40
+
41
+ def group(group_name, actor)
42
+ Group.evaluate_group(group_name, actor)
43
+ end
40
44
  end
41
45
  end
@@ -0,0 +1,19 @@
1
+ module FlipFlop
2
+
3
+ class Group
4
+
5
+ @@groups = {}
6
+
7
+ def self.register group_name, &block
8
+ @@groups[group_name] = block
9
+ end
10
+
11
+ def self.evaluate_group group_name, actor
12
+ @@groups[group_name].call actor
13
+ rescue
14
+ false
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -1,3 +1,3 @@
1
1
  module FlipFlop
2
- VERSION = '0.0.3'
3
- end
2
+ VERSION = '0.0.5'
3
+ end
@@ -11,8 +11,8 @@ module FlipFlop
11
11
  #
12
12
  # Returns:
13
13
  # boolean
14
- def feature_enabled?(feature_name)
15
- ::FlipFlop::feature_enabled? feature_name
14
+ def feature_enabled?(feature_name, actor=nil)
15
+ ::FlipFlop::feature_enabled? feature_name, actor
16
16
  end
17
17
  end
18
18
  end
@@ -12,8 +12,20 @@ describe FlipFlop do
12
12
  end
13
13
 
14
14
  it 'should load from yaml file' do
15
- expect(FlipFlop::feature_enabled? :on).to be_truthy
16
- expect(FlipFlop::feature_enabled? :off).to be_falsey
15
+ expect(FlipFlop.feature_enabled? :on).to be_truthy
16
+ expect(FlipFlop.feature_enabled? :off).to be_falsey
17
+ end
18
+
19
+ it 'should still work for group gate' do
20
+ user1 = { group: "a" }
21
+ user2 = { group: "b" }
22
+
23
+ FlipFlop::Group.register(:group_name) do |actor|
24
+ actor[:group] == 'a'
25
+ end
26
+
27
+ expect(FlipFlop.feature_enabled? :group_example, user1).to be_truthy
28
+ expect(FlipFlop.feature_enabled? :group_example, user2).to be_falsey
17
29
  end
18
30
 
19
31
  end
@@ -150,4 +150,28 @@ describe DummyController do
150
150
  end
151
151
  end
152
152
 
153
+ context 'group gate' do
154
+ before :each do
155
+ ::FlipFlop::Group.register(:group_a) { |actor| actor[:user_group] == 'asdf' }
156
+ end
157
+
158
+ it 'should be able to execute logic specified when registering a group' do
159
+ ::FlipFlop::get_instance.set_feature(:awesomeness, :group, :group_a)
160
+ actor = { user_group: 'asdf' }
161
+ expect(subject.feature_enabled? :awesomeness, actor).to be_truthy
162
+ end
163
+
164
+ it 'should fail the feature check if the block doesnt pass' do
165
+ ::FlipFlop::get_instance.set_feature(:awesomeness, :group, :group_a)
166
+ actor = { user_group: 'qwert' }
167
+ expect(subject.feature_enabled? :awesomeness, actor).to be_falsey
168
+ end
169
+
170
+ it 'should fail if the group has not been defined' do
171
+ ::FlipFlop::get_instance.set_feature(:awesomeness, :group, :group_b)
172
+ actor = { user_group: 'not_there' }
173
+ expect(subject.feature_enabled? :awesomeness, actor).to be_falsey
174
+ end
175
+ end
176
+
153
177
  end
@@ -4,4 +4,7 @@
4
4
  :value: true
5
5
  :off:
6
6
  :type: :boolean
7
- :value: false
7
+ :value: false
8
+ :group_example:
9
+ :type: :group
10
+ :value: :group_name
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flip-flop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Kulyk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-23 00:00:00.000000000 Z
11
+ date: 2016-06-30 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Enable or disable features easily
14
14
  email:
@@ -17,6 +17,7 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - ".rspec"
20
21
  - Gemfile
21
22
  - Gemfile.lock
22
23
  - README.md
@@ -27,6 +28,7 @@ files:
27
28
  - lib/flip-flop/adapters/memory.rb
28
29
  - lib/flip-flop/adapters/yaml.rb
29
30
  - lib/flip-flop/gates.rb
31
+ - lib/flip-flop/group.rb
30
32
  - lib/flip-flop/railtie.rb
31
33
  - lib/flip-flop/version.rb
32
34
  - lib/flip-flop/view_helpers.rb
@@ -57,7 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
59
  version: '0'
58
60
  requirements: []
59
61
  rubyforge_project:
60
- rubygems_version: 2.5.1
62
+ rubygems_version: 2.4.8
61
63
  signing_key:
62
64
  specification_version: 4
63
65
  summary: Feature flipper