flippeur 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -22,17 +22,17 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- The basic idea is that a feature is, or isn't, available to a user of your webapp. So the logic determining whether or not a feature is available acts on a user.
25
+ The basic idea is that a feature is, or isn't, available to an actor (e.g. user or account) of your webapp.
26
26
 
27
- In the view and the controller layers, the user is obtained automatically by calling the controller's `current_person` method (currently and regrettably hardcoded). In the model layer you need to pass an appropriate user instance explicitly.
28
-
29
- (This is suboptimal and I'm exploring alternatives.)
27
+ In the view and the controller layers, the user is obtained automatically by sending your configured actor method to the controller. If you don't configure an actor method, it defaults to `:current_person`. In the model layer you need to pass an appropriate actor instance explicitly.
30
28
 
31
29
  First, setup your features. With Rails this would go in `config/initializers/flippeur.rb`.
32
30
 
33
31
  ```ruby
34
32
  Flippeur.setup do
35
33
 
34
+ actor :current_user
35
+
36
36
  feature :something_fancy do |user|
37
37
  user.is_fancy?
38
38
  end
@@ -44,7 +44,7 @@ Flippeur.setup do
44
44
  end
45
45
  ```
46
46
 
47
- Give each feature a name and a block. The block should return a truthy value if the feature is available to the user, and a falsey value otherwise.
47
+ Give each feature a name and a block. The block should return a truthy value if the feature is available to the actor and a falsey value otherwise.
48
48
 
49
49
  N.B. to return early from the block, use `next retvalue` instead of `return retvalue`. For example:
50
50
 
@@ -55,7 +55,7 @@ feature :add_widget do |user|
55
55
  end
56
56
  ```
57
57
 
58
- Second, use the helpers to determine whether or not features are available to your current user.
58
+ Second, use the helpers to determine whether or not features are available to your current actor.
59
59
 
60
60
  ### View
61
61
 
@@ -95,21 +95,26 @@ end
95
95
 
96
96
  ### Model
97
97
 
98
- Here you also need to pass a user instance to the helper. For example:
98
+ Here you also need to pass an actor instance to the helper. For example:
99
99
 
100
100
  ```ruby
101
+ Flippeur.setup do
102
+ actor :account
103
+ feature :something_fancy do |account|
104
+ account.id == 42
105
+ end
106
+ end
107
+
101
108
  class Project < ActiveRecord::Base
102
109
  belongs_to :account
103
110
  def do_something_fancy
104
- if feature? :something_fancy, account.users.first
111
+ if feature? :something_fancy, account
105
112
  # something fancy
106
113
  end
107
114
  end
108
115
  end
109
116
  ```
110
117
 
111
- This is suboptimal and should be revisited...
112
-
113
118
 
114
119
  ## See Also
115
120
 
@@ -7,8 +7,8 @@ module Flippeur
7
7
  @block = block
8
8
  end
9
9
 
10
- def available?(user)
11
- block.call user
10
+ def available?(actor)
11
+ block.call actor
12
12
  end
13
13
  end
14
14
  end
@@ -2,21 +2,21 @@ module Flippeur
2
2
 
3
3
  module ViewHelpers
4
4
  def feature?(name)
5
- available = Flippeur.find(name).available? @controller.current_person
5
+ available = Flippeur.find(name).available? @controller.send(Flippeur.actor)
6
6
  block_given? ? (yield if available) : available
7
7
  end
8
8
  end
9
9
 
10
10
  module ControllerHelpers
11
11
  def feature?(name)
12
- available = Flippeur.find(name).available? current_person
12
+ available = Flippeur.find(name).available? send(Flippeur.actor)
13
13
  block_given? ? (yield if available) : available
14
14
  end
15
15
  end
16
16
 
17
17
  module ModelHelpers
18
- def feature?(name, person)
19
- available = Flippeur.find(name).available? person
18
+ def feature?(name, actor)
19
+ available = Flippeur.find(name).available? actor
20
20
  block_given? ? (yield if available) : available
21
21
  end
22
22
  end
@@ -1,3 +1,3 @@
1
1
  module Flippeur
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
data/lib/flippeur.rb CHANGED
@@ -11,6 +11,11 @@ module Flippeur
11
11
  module_eval &block
12
12
  end
13
13
 
14
+ def self.actor(method_name = nil)
15
+ @actor = method_name if method_name
16
+ @actor
17
+ end
18
+
14
19
  def self.feature(name, &block)
15
20
  features[name] = Feature.new(name, &block)
16
21
  end
@@ -21,12 +26,17 @@ module Flippeur
21
26
 
22
27
  private
23
28
 
29
+ def self.default_actor
30
+ :current_person
31
+ end
32
+
24
33
  def self.features
25
34
  @features ||= Hash.new { |_,k| raise UnknownFeature, "Unknown feature: #{k}" }
26
35
  end
27
36
 
28
37
  def self.reset
29
38
  @features = nil
39
+ @actor = default_actor
30
40
  end
31
-
41
+
32
42
  end
@@ -0,0 +1,62 @@
1
+ require 'minitest/autorun'
2
+ require 'flippeur'
3
+ require 'flippeur/rails_helpers'
4
+
5
+ class ActorController
6
+ include Flippeur::ControllerHelpers
7
+
8
+ def current_person
9
+ @method = :current_person
10
+ end
11
+
12
+ def current_account
13
+ @method = :current_account
14
+ end
15
+
16
+ def method?
17
+ @method
18
+ end
19
+ end
20
+
21
+
22
+ class ActorView
23
+ include Flippeur::ViewHelpers
24
+ def initialize
25
+ @controller = OpenStruct.new(current_account: Object.new)
26
+ end
27
+ end
28
+
29
+
30
+ class ActorTest < MiniTest::Unit::TestCase
31
+
32
+ def setup
33
+ @controller = ActorController.new
34
+ @view = ActorView.new
35
+ end
36
+
37
+ def test_default_actor_controller
38
+ Flippeur.setup do
39
+ feature(:foo) { |actor| true }
40
+ end
41
+ @controller.feature?(:foo)
42
+ assert_equal :current_person, @controller.method?
43
+ end
44
+
45
+ def test_configured_actor_controller
46
+ Flippeur.setup do
47
+ actor :current_account
48
+ feature(:foo) { |actor| true }
49
+ end
50
+ @controller.feature?(:foo)
51
+ assert_equal :current_account, @controller.method?
52
+ end
53
+
54
+ def test_configured_actor_view
55
+ Flippeur.setup do
56
+ actor :current_account
57
+ feature(:foo) { |actor| true }
58
+ end
59
+ assert @view.feature?(:foo)
60
+ end
61
+
62
+ end
@@ -26,14 +26,14 @@ class ControllerHelpersTest < MiniTest::Unit::TestCase
26
26
 
27
27
  def test_available_feature_as_boolean
28
28
  Flippeur.setup do
29
- feature(:foo) { |user| true }
29
+ feature(:foo) { |actor| true }
30
30
  end
31
31
  assert @controller.feature?(:foo)
32
32
  end
33
33
 
34
34
  def test_available_feature_with_block
35
35
  Flippeur.setup do
36
- feature(:foo) { |user| true }
36
+ feature(:foo) { |actor| true }
37
37
  end
38
38
  block_called = false
39
39
  @controller.feature?(:foo) do
@@ -44,14 +44,14 @@ class ControllerHelpersTest < MiniTest::Unit::TestCase
44
44
 
45
45
  def test_unavailable_feature_as_boolean
46
46
  Flippeur.setup do
47
- feature(:foo) { |user| false }
47
+ feature(:foo) { |actor| false }
48
48
  end
49
49
  refute @controller.feature?(:foo)
50
50
  end
51
51
 
52
52
  def test_unavailable_feature_with_block
53
53
  Flippeur.setup do
54
- feature(:foo) { |user| false }
54
+ feature(:foo) { |actor| false }
55
55
  end
56
56
  block_called = false
57
57
  @controller.feature?(:foo) do
data/test/feature_test.rb CHANGED
@@ -19,14 +19,14 @@ class FeatureTest < MiniTest::Unit::TestCase
19
19
 
20
20
  def test_available_feature
21
21
  Flippeur.setup do
22
- feature(:foo) { |user| user.id == 42 }
22
+ feature(:foo) { |actor| actor.id == 42 }
23
23
  end
24
24
  assert Flippeur.find(:foo).available?( OpenStruct.new(id: 42) )
25
25
  end
26
26
 
27
27
  def test_unavailable_feature
28
28
  Flippeur.setup do
29
- feature(:foo) { |user| user.id == 42 }
29
+ feature(:foo) { |actor| actor.id == 42 }
30
30
  end
31
31
  refute Flippeur.find(:foo).available?( OpenStruct.new(id: 153) )
32
32
  end
@@ -23,14 +23,14 @@ class ModelHelpersTest < MiniTest::Unit::TestCase
23
23
 
24
24
  def test_available_feature_as_boolean
25
25
  Flippeur.setup do
26
- feature(:foo) { |user| true }
26
+ feature(:foo) { |actor| true }
27
27
  end
28
28
  assert @model.feature?(:foo, Object.new)
29
29
  end
30
30
 
31
31
  def test_available_feature_with_block
32
32
  Flippeur.setup do
33
- feature(:foo) { |user| true }
33
+ feature(:foo) { |actor| true }
34
34
  end
35
35
  block_called = false
36
36
  @model.feature?(:foo, Object.new) do
@@ -41,14 +41,14 @@ class ModelHelpersTest < MiniTest::Unit::TestCase
41
41
 
42
42
  def test_unavailable_feature_as_boolean
43
43
  Flippeur.setup do
44
- feature(:foo) { |user| false }
44
+ feature(:foo) { |actor| false }
45
45
  end
46
46
  refute @model.feature?(:foo, Object.new)
47
47
  end
48
48
 
49
49
  def test_unavailable_feature_with_block
50
50
  Flippeur.setup do
51
- feature(:foo) { |user| false }
51
+ feature(:foo) { |actor| false }
52
52
  end
53
53
  block_called = false
54
54
  @model.feature?(:foo, Object.new) do
@@ -26,14 +26,14 @@ class ViewHelpersTest < MiniTest::Unit::TestCase
26
26
 
27
27
  def test_available_feature_as_boolean
28
28
  Flippeur.setup do
29
- feature(:foo) { |user| true }
29
+ feature(:foo) { |actor| true }
30
30
  end
31
31
  assert @view.feature?(:foo)
32
32
  end
33
33
 
34
34
  def test_available_feature_with_block
35
35
  Flippeur.setup do
36
- feature(:foo) { |user| true }
36
+ feature(:foo) { |actor| true }
37
37
  end
38
38
  block_called = false
39
39
  @view.feature?(:foo) do
@@ -44,14 +44,14 @@ class ViewHelpersTest < MiniTest::Unit::TestCase
44
44
 
45
45
  def test_unavailable_feature_as_boolean
46
46
  Flippeur.setup do
47
- feature(:foo) { |user| false }
47
+ feature(:foo) { |actor| false }
48
48
  end
49
49
  refute @view.feature?(:foo)
50
50
  end
51
51
 
52
52
  def test_unavailable_feature_with_block
53
53
  Flippeur.setup do
54
- feature(:foo) { |user| false }
54
+ feature(:foo) { |actor| false }
55
55
  end
56
56
  block_called = false
57
57
  @view.feature?(:foo) do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flippeur
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-27 00:00:00.000000000 Z
12
+ date: 2013-12-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -45,6 +45,7 @@ files:
45
45
  - lib/flippeur/rails_helpers.rb
46
46
  - lib/flippeur/railtie.rb
47
47
  - lib/flippeur/version.rb
48
+ - test/actor_test.rb
48
49
  - test/controller_helpers_test.rb
49
50
  - test/feature_test.rb
50
51
  - test/model_helpers_test.rb
@@ -63,7 +64,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
63
64
  version: '0'
64
65
  segments:
65
66
  - 0
66
- hash: 1803748012281727158
67
+ hash: -2788601596674215805
67
68
  required_rubygems_version: !ruby/object:Gem::Requirement
68
69
  none: false
69
70
  requirements:
@@ -72,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
73
  version: '0'
73
74
  segments:
74
75
  - 0
75
- hash: 1803748012281727158
76
+ hash: -2788601596674215805
76
77
  requirements: []
77
78
  rubyforge_project:
78
79
  rubygems_version: 1.8.23
@@ -80,6 +81,7 @@ signing_key:
80
81
  specification_version: 3
81
82
  summary: Simple feature flipping.
82
83
  test_files:
84
+ - test/actor_test.rb
83
85
  - test/controller_helpers_test.rb
84
86
  - test/feature_test.rb
85
87
  - test/model_helpers_test.rb