corral 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d16e636469afb7e4a608fd6f34d8c479c9d7b366
4
- data.tar.gz: 0d11cd106cc36d68273322e7f0c2c3e5c2bea527
3
+ metadata.gz: c593e012df1b9d81b10115e9c06fef4fb12c6ea8
4
+ data.tar.gz: de278173c63e93b84215eedef1624ad61350b6b9
5
5
  SHA512:
6
- metadata.gz: f3fb14dfebbdeb58615fde4e71838a7b81205091260b21db78e3b6da791909adacbf867ecd37229e3537101af47eaeed4e7fc0bda314931b6e8339efa28c0fda
7
- data.tar.gz: 78029aad67dc1a06fb947003de11c422b25a177fd4912eff4e2876497c0ba82a690fafcb9a2ec50bb2f1b9748a4f416842ff1e8f84f9a77e292762a1639849d3
6
+ metadata.gz: 80b4c8bdc3065a437c6e655ffd05a04955ccac9a0043f83ca881ee4d93e16457c042bebd363b7f7df9e3c119efb095033095370b31a2920ea45462fb8c60c720
7
+ data.tar.gz: 55e0c876711a8053fa58a4516890982e94618f0c803f50d3720330f80784834b9ead22ff7ac2d2e845a9b04284d66146059642f52895b9e4834c993f934bc0cf
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Corral
2
2
 
3
+ [![Code Climate](https://codeclimate.com/github/bryanwoods/corral.png)](https://codeclimate.com/github/bryanwoods/corral)
4
+
3
5
  ![Corral](http://cl.ly/image/0Q1E2Z451v2a/corral.jpg)
4
6
 
5
7
  Use Corral to disable certain features in your application.
@@ -40,7 +42,7 @@ include Corral::Helpers
40
42
 
41
43
  corral(Rails.env) do
42
44
  disable :caching, in: [:development, :test, :staging]
43
- disable :crying, when: ->(user) { user.birthday == Date.today }
45
+ enable :crying, when: ->(user) { user.birthday == Date.today }
44
46
  end
45
47
  ```
46
48
 
@@ -1,6 +1,6 @@
1
1
  module Corral
2
2
  class Feature
3
- attr_reader :condition
3
+ attr_reader :condition, :disabled
4
4
 
5
5
  def initialize(feature, condition, disabled)
6
6
  @feature = feature
@@ -13,6 +13,14 @@ module Corral
13
13
  @features[name] = new(name, condition, disabled)
14
14
  end
15
15
 
16
+ def self.enable(name, condition)
17
+ push(name, condition, false)
18
+ end
19
+
20
+ def self.disable(name, condition)
21
+ push(name, condition, true)
22
+ end
23
+
16
24
  def self.get(name)
17
25
  (@features ||= {})[name]
18
26
  end
@@ -1,3 +1,3 @@
1
1
  module Corral
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
data/lib/corral.rb CHANGED
@@ -13,20 +13,15 @@ module Corral
13
13
 
14
14
  def self.corral(env = nil, &block)
15
15
  self.environment = env.to_s
16
- gather_features(&block)
16
+ instance_eval(&block)
17
17
  end
18
18
 
19
19
  def self.disable(feature, options = {})
20
- environments = options[:in] and
21
- return environment_override(feature, *environments)
22
-
23
- condition = options[:when] || options[:if]
24
-
25
- if condition && !condition.respond_to?(:call)
26
- raise "'when' or 'if' condition must be a callable object"
27
- end
20
+ flip_feature(feature, options.merge(enable: false))
21
+ end
28
22
 
29
- Feature.push(feature, condition)
23
+ def self.enable(feature, options = {})
24
+ flip_feature(feature, options.merge(enable: true))
30
25
  end
31
26
 
32
27
  def self.disabled?(feature, *arguments)
@@ -37,18 +32,48 @@ module Corral
37
32
  (feature = Feature.get(feature)) && (condition = feature.condition) or
38
33
  return false
39
34
 
40
- !condition.call(*arguments)
35
+ call_condition(feature, condition, *arguments)
41
36
  end
42
37
 
43
38
  private
44
39
 
45
- def self.gather_features(&block)
46
- instance_eval(&block)
47
- end
48
-
49
- def self.environment_override(feature, *environments)
40
+ def self.environment_override(feature, enable, *environments)
50
41
  envs = environments.map(&:to_sym)
51
42
  condition = -> { envs.any? { |env| env == self.environment } }
52
- Feature.push(feature, condition)
43
+ push_feature(enable, feature, condition)
44
+ end
45
+
46
+ def self.process_condition(options = {})
47
+ condition = options[:when] || options[:if]
48
+
49
+ (condition && !condition.respond_to?(:call)) and
50
+ raise "'when' or 'if' condition must be a callable object"
51
+
52
+ condition
53
+ end
54
+
55
+ def self.flip_feature(feature, options = {})
56
+ enable = options[:enable]
57
+ environments = options[:in] and
58
+ return environment_override(feature, enable, *environments)
59
+ condition = process_condition(options)
60
+
61
+ push_feature(enable, feature, condition)
62
+ end
63
+
64
+ def self.push_feature(enable, feature, condition)
65
+ if enable
66
+ Feature.enable(feature, condition)
67
+ else
68
+ Feature.disable(feature, condition)
69
+ end
70
+ end
71
+
72
+ def self.call_condition(feature, condition, *arguments)
73
+ if feature.disabled
74
+ !condition.call(*arguments)
75
+ else
76
+ condition.call(*arguments)
77
+ end
53
78
  end
54
79
  end
data/spec/corral_spec.rb CHANGED
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe Corral do
4
4
  include Corral::Helpers
5
5
 
6
- describe "#corral" do
6
+ describe ".corral" do
7
7
  context "when given a block" do
8
8
  it "yields the block" do
9
9
  corral { :cool_feature }.should == :cool_feature
@@ -15,13 +15,13 @@ describe Corral do
15
15
  before do
16
16
  corral("development") do
17
17
  disable :caching, in: :development
18
- disable "expiry_headers", in: :development
18
+ enable "expiry_headers", in: :development
19
19
  end
20
20
  end
21
21
 
22
22
  it "disables the feature" do
23
23
  disabled?(:caching).should be_true
24
- disabled?(:expiry_headers).should be_true
24
+ enabled?("expiry_headers").should be_true
25
25
  end
26
26
  end
27
27
 
@@ -241,4 +241,20 @@ describe Corral do
241
241
  end
242
242
  end
243
243
  end
244
+
245
+ describe "#enable" do
246
+ context "when the if or when conditions are true" do
247
+ before do
248
+ corral do
249
+ enable :always_on, if: -> { true }
250
+ enable :everything, when: -> { 1 + 1 == 2 }
251
+ end
252
+ end
253
+
254
+ it "enables the feature" do
255
+ enabled?(:always_on).should be_true
256
+ disabled?(:everything).should be_false
257
+ end
258
+ end
259
+ end
244
260
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: corral
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryan Woods
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-10 00:00:00.000000000 Z
11
+ date: 2014-03-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler