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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 619742d68fba82f665cb0f3e35ba09acf35e5ceb
4
- data.tar.gz: 493acbc382bde9159cd33ac345818351bf64ea7a
3
+ metadata.gz: d16e636469afb7e4a608fd6f34d8c479c9d7b366
4
+ data.tar.gz: 0d11cd106cc36d68273322e7f0c2c3e5c2bea527
5
5
  SHA512:
6
- metadata.gz: 79e9d8fd56a693200e2964d7c40ca4429ebea1449f94b471cc21a324b85e39745c32ddcb8b7918a5a18e961a20a9490f032579b1778291a61b46f84a5a433605
7
- data.tar.gz: 3553a67dffe46df119d4b1bad3569b8c3e6aa17cf8206685ce6327f684400cde0ee7a060ba0816faa5093b32d76324987465dc4e401e46d2e7b4fc5e9672faaf
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
- corral do
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, when: -> { Rails.env.production? }
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
@@ -0,0 +1,11 @@
1
+ module Corral
2
+ module Helpers
3
+ %w(corral disable enabled? disabled?).each do |name|
4
+ class_eval <<-EOS
5
+ def #{name}(*args, &block)
6
+ Corral.#{name}(*args, &block)
7
+ end
8
+ EOS
9
+ end
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module Corral
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
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
- class Feature
5
- attr_reader :condition
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
- def self.push(name, condition, disabled = true)
14
- @features ||= {}
15
- @features[name] = new(name, condition, disabled)
16
- end
10
+ def self.environment=(env)
11
+ @environment = env.to_sym
12
+ end
17
13
 
18
- def self.get(name)
19
- (@features ||= {})[name]
20
- end
14
+ def self.corral(env = nil, &block)
15
+ self.environment = env.to_s
16
+ gather_features(&block)
21
17
  end
22
18
 
23
- module Helpers
24
- def corral(&block)
25
- instance_eval(&block)
26
- end
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
- def disabled?(feature, argument = nil)
29
- !enabled?(feature, argument)
25
+ if condition && !condition.respond_to?(:call)
26
+ raise "'when' or 'if' condition must be a callable object"
30
27
  end
31
28
 
32
- def enabled?(feature, argument = nil)
33
- feature = Feature.get(feature)
34
- return false unless feature && (condition = feature.condition)
29
+ Feature.push(feature, condition)
30
+ end
35
31
 
36
- if argument
37
- !condition.call(argument)
38
- else
39
- !condition.call
40
- end
41
- end
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
- def disable(feature, options = {})
44
- condition = options[:when] || options[:if]
40
+ !condition.call(*arguments)
41
+ end
45
42
 
46
- if condition && !condition.respond_to?(:call)
47
- raise "'when' or 'if' condition must be a callable object"
48
- end
43
+ private
49
44
 
50
- Feature.push(feature, condition)
51
- end
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.0
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