corral 0.1.0 → 0.1.1
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 +19 -2
- data/lib/corral/feature.rb +20 -0
- data/lib/corral/helpers.rb +11 -0
- data/lib/corral/version.rb +1 -1
- data/lib/corral.rb +38 -37
- data/spec/corral_spec.rb +28 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d16e636469afb7e4a608fd6f34d8c479c9d7b366
|
4
|
+
data.tar.gz: 0d11cd106cc36d68273322e7f0c2c3e5c2bea527
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f3fb14dfebbdeb58615fde4e71838a7b81205091260b21db78e3b6da791909adacbf867ecd37229e3537101af47eaeed4e7fc0bda314931b6e8339efa28c0fda
|
7
|
+
data.tar.gz: 78029aad67dc1a06fb947003de11c422b25a177fd4912eff4e2876497c0ba82a690fafcb9a2ec50bb2f1b9748a4f416842ff1e8f84f9a77e292762a1639849d3
|
data/README.md
CHANGED
@@ -22,21 +22,38 @@ Put this somewhere like: `config/initializers/corral.rb`
|
|
22
22
|
```ruby
|
23
23
|
include Corral
|
24
24
|
|
25
|
-
|
25
|
+
# Pass an environment to use the `in` option...
|
26
|
+
Corral.corral(Rails.env) do
|
26
27
|
disable :torpedoes, when: -> { true }
|
27
|
-
disable :fun_and_games,
|
28
|
+
disable :fun_and_games, in: [:staging, :production]
|
28
29
|
disable :cupcakes, if: ->(person) { person == "Bryan" }
|
29
30
|
end
|
30
31
|
```
|
31
32
|
|
32
33
|
("when" and "if" mean the same thing. Use whichever makes you happy.)
|
33
34
|
|
35
|
+
If you don't mind polluting your global namespace and want to more
|
36
|
+
easily refer to these methods, you can include `Corral::Helpers`
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
include Corral::Helpers
|
40
|
+
|
41
|
+
corral(Rails.env) do
|
42
|
+
disable :caching, in: [:development, :test, :staging]
|
43
|
+
disable :crying, when: ->(user) { user.birthday == Date.today }
|
44
|
+
end
|
45
|
+
```
|
34
46
|
|
35
47
|
## Usage
|
36
48
|
|
37
49
|
```ruby
|
50
|
+
# If you've included Corral::Helpers
|
38
51
|
fire! if enabled?(:torpedoes)
|
39
52
|
sulk if disabled?(:cupcakes, "Bryan") # And I don't even *like* sweets!
|
53
|
+
|
54
|
+
# If you've only included Corral
|
55
|
+
fire! if Corral.enabled?(:torpedoes)
|
56
|
+
sulk if Corral.disabled?(:cupcakes, "Bryan")
|
40
57
|
```
|
41
58
|
|
42
59
|
## Installation
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Corral
|
2
|
+
class Feature
|
3
|
+
attr_reader :condition
|
4
|
+
|
5
|
+
def initialize(feature, condition, disabled)
|
6
|
+
@feature = feature
|
7
|
+
@condition = condition
|
8
|
+
@disabled = disabled
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.push(name, condition, disabled = true)
|
12
|
+
@features ||= {}
|
13
|
+
@features[name] = new(name, condition, disabled)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.get(name)
|
17
|
+
(@features ||= {})[name]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/corral/version.rb
CHANGED
data/lib/corral.rb
CHANGED
@@ -1,53 +1,54 @@
|
|
1
1
|
require "corral/version"
|
2
|
+
require "corral/feature"
|
3
|
+
require "corral/helpers"
|
2
4
|
|
3
5
|
module Corral
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
def initialize(feature, condition, disabled)
|
8
|
-
@feature = feature
|
9
|
-
@condition = condition
|
10
|
-
@disabled = disabled
|
11
|
-
end
|
6
|
+
def self.environment
|
7
|
+
@environment
|
8
|
+
end
|
12
9
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
end
|
10
|
+
def self.environment=(env)
|
11
|
+
@environment = env.to_sym
|
12
|
+
end
|
17
13
|
|
18
|
-
|
19
|
-
|
20
|
-
|
14
|
+
def self.corral(env = nil, &block)
|
15
|
+
self.environment = env.to_s
|
16
|
+
gather_features(&block)
|
21
17
|
end
|
22
18
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
19
|
+
def self.disable(feature, options = {})
|
20
|
+
environments = options[:in] and
|
21
|
+
return environment_override(feature, *environments)
|
22
|
+
|
23
|
+
condition = options[:when] || options[:if]
|
27
24
|
|
28
|
-
|
29
|
-
|
25
|
+
if condition && !condition.respond_to?(:call)
|
26
|
+
raise "'when' or 'if' condition must be a callable object"
|
30
27
|
end
|
31
28
|
|
32
|
-
|
33
|
-
|
34
|
-
return false unless feature && (condition = feature.condition)
|
29
|
+
Feature.push(feature, condition)
|
30
|
+
end
|
35
31
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
32
|
+
def self.disabled?(feature, *arguments)
|
33
|
+
!self.enabled?(feature, *arguments)
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.enabled?(feature, *arguments)
|
37
|
+
(feature = Feature.get(feature)) && (condition = feature.condition) or
|
38
|
+
return false
|
42
39
|
|
43
|
-
|
44
|
-
|
40
|
+
!condition.call(*arguments)
|
41
|
+
end
|
45
42
|
|
46
|
-
|
47
|
-
raise "'when' or 'if' condition must be a callable object"
|
48
|
-
end
|
43
|
+
private
|
49
44
|
|
50
|
-
|
51
|
-
|
45
|
+
def self.gather_features(&block)
|
46
|
+
instance_eval(&block)
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.environment_override(feature, *environments)
|
50
|
+
envs = environments.map(&:to_sym)
|
51
|
+
condition = -> { envs.any? { |env| env == self.environment } }
|
52
|
+
Feature.push(feature, condition)
|
52
53
|
end
|
53
54
|
end
|
data/spec/corral_spec.rb
CHANGED
@@ -10,6 +10,34 @@ describe Corral do
|
|
10
10
|
end
|
11
11
|
|
12
12
|
context "when given known features" do
|
13
|
+
context "when passed an environment in an 'in' option" do
|
14
|
+
context "when the environment matches the condition" do
|
15
|
+
before do
|
16
|
+
corral("development") do
|
17
|
+
disable :caching, in: :development
|
18
|
+
disable "expiry_headers", in: :development
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
it "disables the feature" do
|
23
|
+
disabled?(:caching).should be_true
|
24
|
+
disabled?(:expiry_headers).should be_true
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "when the environment does not match any of the conditions" do
|
29
|
+
before do
|
30
|
+
corral("development") do
|
31
|
+
disable :debug_mode, in: [:production, :test]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
it "does not disable the feature" do
|
36
|
+
disabled?(:debug_mode).should be_false
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
13
41
|
context "when given callable 'when' or 'if' options" do
|
14
42
|
context "when the conditions are true" do
|
15
43
|
before do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: corral
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bryan Woods
|
@@ -53,6 +53,8 @@ files:
|
|
53
53
|
- Rakefile
|
54
54
|
- corral.gemspec
|
55
55
|
- lib/corral.rb
|
56
|
+
- lib/corral/feature.rb
|
57
|
+
- lib/corral/helpers.rb
|
56
58
|
- lib/corral/version.rb
|
57
59
|
- spec/corral_spec.rb
|
58
60
|
- spec/spec_helper.rb
|