flagship 0.4.0 → 0.5.0

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: b5e0f2c9053b747cfc441a1e3ba8f31a5453d00d
4
- data.tar.gz: ba52d5627d54d495773f47649bb8333a37793dbe
3
+ metadata.gz: 8fb44a30b820db71bbf1edc824a3543a0e0f107e
4
+ data.tar.gz: 13d3196cd2ee42b7d1b7b6a4d9a09c966cf66e26
5
5
  SHA512:
6
- metadata.gz: cb5353d961b9a00391af1e0e42028b9bc432772073743d0f4ebb291a91342a3f7aa99bd555b234fee50128bcd85ae9bd0daa44e8fb7ad86e3b502390f21c8c46
7
- data.tar.gz: 8db9f8a90a544de056952bccc6ab0b88dd2189029b40d62c1320a40a0eb99f499a54e4a488a95879bada243f8e7532ea774a9920d85938b33c56c4895b284a56
6
+ metadata.gz: 3a564740caca6345d3190830913d9d4e3b2e1ea060312bd36471d65e68335506043c1568aa2f653241c028318bd03655aad4cecd3e2aade9552cf8f14f90f5a9
7
+ data.tar.gz: ba3b294b5bc9cf320b5210b7c54084107bee59c08e6107ac48ff958e9009dce6611dd7501ff6fe234ba60af82acbfbe956fb669c82f96e8e3471ab9b1b18b636
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Change Log
2
2
 
3
+ ## [0.5.0] - 2017-01-07
4
+
5
+ - Documented about helper methods [#21](https://github.com/yuya-takeyama/flagship/pull/21)
6
+ - Helper methods can be specified as a `Symbol` [#21](https://github.com/yuya-takeyama/flagship/pull/21)
7
+ - Helper methods are extended [#22](https://github.com/yuya-takeyama/flagship/pull/22)
8
+
3
9
  ## [0.4.0] - 2016-12-23
4
10
 
5
11
  ### Added
data/README.md CHANGED
@@ -147,6 +147,59 @@ Flagship.define :blog do
147
147
  end
148
148
  ```
149
149
 
150
+ ## Helper methods
151
+
152
+ You can define helpers as normal methods with `def`. Methods can be used within blocks, procs, or as symbolic names for if statements to tidy up your code.
153
+
154
+ ```rb
155
+ Flagship.define :blog do
156
+ def is_author(comment, user)
157
+ comment.author == user
158
+ end
159
+
160
+ def can_view_comment(context)
161
+ context.current_user.moderator?
162
+ end
163
+
164
+ enable :comment, if: :can_view_comment
165
+ enable :comment_deletion, if: ->(context) { is_author(context.comment, context.current_user) }
166
+ end
167
+ ```
168
+
169
+ To share helpers, you can simply include them as modules.
170
+
171
+ ```rb
172
+ module FlagHelpers
173
+ def is_author(context)
174
+ context.comment.author == context.current_user
175
+ end
176
+ end
177
+
178
+ Flagship.define :development do
179
+ include FlagHelpers
180
+ enable :delete, if: :is_author
181
+ end
182
+
183
+ Flagship.define :production do
184
+ include FlagHelpers
185
+ enable :delete, if: :is_author
186
+ end
187
+ ```
188
+
189
+ And you can also extend helper methods from base flagset.
190
+
191
+ ```rb
192
+ Flagship.define :base do
193
+ def is_author(context)
194
+ context.comment.author == context.current_user
195
+ end
196
+ end
197
+
198
+ Flagship.define :production do
199
+ enable :delete, if: :is_author
200
+ end
201
+ ```
202
+
150
203
  ## Development
151
204
 
152
205
  After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/lib/flagship.rb CHANGED
@@ -9,46 +9,48 @@ require "flagship/flagsets_container"
9
9
  module Flagship
10
10
  class NoFlagsetSelectedError < ::StandardError; end
11
11
 
12
- def self.define(key, options = {}, &block)
13
- context = self.default_context
14
- base = options[:extend] ? self.get_flagset(options[:extend]) : nil
15
- self.default_flagsets_container.add ::Flagship::Dsl.new(key, context, base, &block).flagset
16
- end
17
-
18
- def self.enabled?(key)
19
- self.current_flagset.enabled?(key)
20
- end
21
-
22
- def self.set_context(key, value)
23
- self.default_context.__set(key, value)
24
- end
25
-
26
- def self.select_flagset(key)
27
- @@current_flagset = self.default_flagsets_container.get(key)
28
- end
29
-
30
- def self.features
31
- self.current_flagset.features
32
- end
33
-
34
- def self.get_flagset(key)
35
- self.default_flagsets_container.get(key)
36
- end
37
-
38
- def self.default_flagsets_container
39
- @@default_flagsts_container ||= ::Flagship::FlagsetsContainer.new
40
- end
41
-
42
- def self.current_flagset
43
- @@current_flagset or raise NoFlagsetSelectedError.new('No flagset is selected')
44
- end
45
-
46
- def self.default_context
47
- @@default_context ||= ::Flagship::Context.new
48
- end
49
-
50
- def self.clear_state
51
- @@default_flagsts_container = nil
52
- @@current_flagset = nil
12
+ class << self
13
+ def define(key, options = {}, &block)
14
+ context = self.default_context
15
+ base = options[:extend] ? self.get_flagset(options[:extend]) : nil
16
+ default_flagsets_container.add ::Flagship::Dsl.new(key, context, base, &block).flagset
17
+ end
18
+
19
+ def enabled?(key)
20
+ current_flagset.enabled?(key)
21
+ end
22
+
23
+ def set_context(key, value)
24
+ default_context.__set(key, value)
25
+ end
26
+
27
+ def select_flagset(key)
28
+ @current_flagset = default_flagsets_container.get(key)
29
+ end
30
+
31
+ def features
32
+ current_flagset.features
33
+ end
34
+
35
+ def get_flagset(key)
36
+ default_flagsets_container.get(key)
37
+ end
38
+
39
+ def default_flagsets_container
40
+ @default_flagsts_container ||= ::Flagship::FlagsetsContainer.new
41
+ end
42
+
43
+ def current_flagset
44
+ @current_flagset or raise NoFlagsetSelectedError.new('No flagset is selected')
45
+ end
46
+
47
+ def default_context
48
+ @default_context ||= ::Flagship::Context.new
49
+ end
50
+
51
+ def clear_state
52
+ @default_flagsts_container = nil
53
+ @current_flagset = nil
54
+ end
53
55
  end
54
56
  end
data/lib/flagship/dsl.rb CHANGED
@@ -11,14 +11,26 @@ class Flagship::Dsl
11
11
  @definition = block
12
12
  @base_tags = {}
13
13
 
14
+ if @base
15
+ @base.helper_methods.each do |method|
16
+ define_singleton_method(method.name, &method)
17
+ end
18
+ end
19
+
14
20
  instance_eval(&@definition)
15
21
 
16
- @flagset = ::Flagship::Flagset.new(@key, @features, @base)
22
+ helper_methods = singleton_methods.map { |sym| method(sym) }
23
+ @flagset = ::Flagship::Flagset.new(@key, @features, @base, helper_methods)
17
24
  end
18
25
 
19
26
  def enable(key, opts = {})
20
27
  tags = opts.dup
21
28
  condition = tags.delete(:if)
29
+ # convert to proc
30
+ if condition.is_a?(Symbol)
31
+ sym = condition
32
+ condition = ->(context) { method(sym).call(context) }
33
+ end
22
34
 
23
35
  if condition
24
36
  @features[key] = ::Flagship::Feature.new(key, condition, @context, @base_tags.merge(tags))
@@ -49,4 +61,8 @@ class Flagship::Dsl
49
61
  def disabled?(key)
50
62
  @flagset.disabled?(key)
51
63
  end
64
+
65
+ def include(mod)
66
+ extend mod
67
+ end
52
68
  end
@@ -1,13 +1,14 @@
1
1
  class Flagship::Flagset
2
- attr_reader :key
2
+ attr_reader :key, :helper_methods
3
3
 
4
4
  class UndefinedFlagError < ::StandardError; end
5
5
 
6
- def initialize(key, features_hash, base = nil)
6
+ def initialize(key, features_hash, base = nil, helper_methods = [])
7
7
  @key = key
8
8
  @features = base ?
9
9
  extend_features(features_hash, base) :
10
10
  features_hash
11
+ @helper_methods = helper_methods
11
12
  end
12
13
 
13
14
  def enabled?(key)
@@ -1,3 +1,3 @@
1
1
  module Flagship
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flagship
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuya Takeyama
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-22 00:00:00.000000000 Z
11
+ date: 2017-01-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -98,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
98
  version: '0'
99
99
  requirements: []
100
100
  rubyforge_project:
101
- rubygems_version: 2.5.2
101
+ rubygems_version: 2.6.8
102
102
  signing_key:
103
103
  specification_version: 4
104
104
  summary: Ship/unship features using flags defined with declarative DSL