flagship 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c9f85fb317ee229493f115ec7c65647e75660a0f
4
+ data.tar.gz: 449021917bc8f499d51ae96a20eb76c3192bfcc3
5
+ SHA512:
6
+ metadata.gz: faffff4c99df31cf5b2e4addc170fe651274917b3d093fd6fdb84917dacbfc677b904fe0f336ae6238084acf62e356700a8b9243193ef8972a845e5f132006f3
7
+ data.tar.gz: d4d2b1cf9e573615f92616e71a1f29800f0dad42819724a9a97961b24e853b16357b5d5e260f25399ba80027b3542ea1d0517a60e37a968b2f47ca54505d9c97
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --require=spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.2.5
5
+ - 2.3.1
6
+
7
+ before_install: gem install bundler --no-document
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in flagship.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Yuya Takeyama
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,102 @@
1
+ # :flags: Flagship :ship:
2
+
3
+ Ship/unship features using flags defined with declarative DSL.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'flagship'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install flagship
20
+
21
+ ## Usage
22
+
23
+ ### Define and use a flagset
24
+
25
+ ```rb
26
+ Flagship.define :app do
27
+ enable :stable_feature
28
+ enable :experimental_feature, if: ->(context) { context.current_user.staff? }
29
+ disable :deprecate_feature
30
+ end
31
+
32
+ Flagship.set_flagset(:app)
33
+ ```
34
+
35
+ ### Branch with a flag
36
+
37
+ ```rb
38
+ if Flagship.enabled?(:some_feature)
39
+ # Implement the feature here
40
+ end
41
+ ```
42
+
43
+ ### Set context variables
44
+
45
+ Both of below can be called as `context.foo` from `:if` block.
46
+
47
+ ```rb
48
+ # Set a value
49
+ Flagship.set_context :foo, 'FOO'
50
+
51
+ # Set a lambda
52
+ Flagship.set_context :foo, ->(context) { 'FOO' }
53
+ ```
54
+
55
+ Or you can set a method too.
56
+
57
+ ```rb
58
+ Flagship.set_method :current_user, method(:current_user)
59
+ ```
60
+
61
+ ### Extend flagset
62
+
63
+ ```rb
64
+ Flagship.define :common do
65
+ enable :stable_feature
66
+ end
67
+
68
+ Flagship.define :development, extend: :common do
69
+ enable :experimental_feature
70
+ end
71
+
72
+ Flagship.define :production, extend: :common do
73
+ disable :experimental_feature
74
+ end
75
+
76
+ if Rails.env.production?
77
+ Flagset.set_flagset(:production)
78
+ else
79
+ Flagset.set_flagset(:development)
80
+ end
81
+ ```
82
+
83
+ ### Override flag with ENV
84
+
85
+ You can override flags with ENV named `FLAG_***`.
86
+
87
+ Assuming that there is a flag `:foo`, you can override it with ENV `FLAGSHIP_FOO=1`.
88
+
89
+ ## Development
90
+
91
+ 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.
92
+
93
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
94
+
95
+ ## Contributing
96
+
97
+ Bug reports and pull requests are welcome on GitHub at https://github.com/yuya-takeyama/flagship.
98
+
99
+
100
+ ## License
101
+
102
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "flagship"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/flagship.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'flagship/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "flagship"
8
+ spec.version = Flagship::VERSION
9
+ spec.authors = ["Yuya Takeyama"]
10
+ spec.email = ["sign.of.the.wolf.pentagram@gmail.com"]
11
+
12
+ spec.summary = %q{Ship/unship features using flags defined with declarative DSL}
13
+ spec.description = %q{Ship/unship features using flags defined with declarative DSL}
14
+ spec.homepage = "https://github.com/yuya-takeyama/flagship"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.13"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.5.0"
27
+ end
@@ -0,0 +1,27 @@
1
+ class Flagship::Context
2
+ def initialize
3
+ @values = {}
4
+ end
5
+
6
+ def __set(key, value)
7
+ @values[key.to_sym] = value
8
+ end
9
+
10
+ def method_missing(name, args = [], &block)
11
+ if @values.key?(name)
12
+ value = @values[name]
13
+
14
+ if value.respond_to?(:call)
15
+ value.call
16
+ else
17
+ value
18
+ end
19
+ else
20
+ super
21
+ end
22
+ end
23
+
24
+ def respond_to_missing?(name, include_private = false)
25
+ @values.key?(name) or super
26
+ end
27
+ end
@@ -0,0 +1,30 @@
1
+ class Flagship::Dsl
2
+ class InvalidOptionError < ::StandardError; end
3
+
4
+ def initialize(key, context, base = nil, &block)
5
+ @key = key
6
+ @context = context
7
+ @base = base
8
+ @flags = {}
9
+ @definition = block
10
+ end
11
+
12
+ def enable(key, opts = {})
13
+ if opts[:if]
14
+ @flags[key] = opts[:if]
15
+ else
16
+ @flags[key] = true
17
+ end
18
+ end
19
+
20
+ def disable(key, opts = {})
21
+ raise InvalidOptionError.new("Option :if is not available for #disable") if opts[:if]
22
+
23
+ @flags[key] = false
24
+ end
25
+
26
+ def flagset
27
+ instance_eval(&@definition)
28
+ ::Flagship::Flagset.new(@key, @flags, @context, @base)
29
+ end
30
+ end
@@ -0,0 +1,34 @@
1
+ class Flagship::Flagset
2
+ attr_reader :key, :flags
3
+
4
+ class UndefinedFlagError < ::StandardError; end
5
+
6
+ def initialize(key, flags, context, base = nil)
7
+ @key = key
8
+ @flags = base ? base.flags.merge(flags) : flags
9
+ @context = context
10
+ end
11
+
12
+ def enabled?(key, if: nil)
13
+ raise UndefinedFlagError.new("The flag :#{key} is not defined") unless @flags.key? key
14
+
15
+ env = ENV['FLAGSHIP_' + key.to_s.upcase]
16
+
17
+ if env
18
+ case env.downcase
19
+ when '1', 'true'
20
+ return true
21
+ when '0', 'false', ''
22
+ return false
23
+ end
24
+ end
25
+
26
+ flag = @flags[key]
27
+
28
+ if flag.respond_to?(:call)
29
+ !!@flags[key].call(@context)
30
+ else
31
+ !!@flags[key]
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,20 @@
1
+ class Flagship::FlagsetsContainer
2
+ class DuplicatedFlagsetError < ::StandardError; end
3
+ class UndefinedFlagsetError < ::StandardError; end
4
+
5
+ def initialize
6
+ @flagsets = {}
7
+ end
8
+
9
+ def add(flagset)
10
+ raise DuplicatedFlagsetError.new("Flagset :#{flagset.key} already exists") if @flagsets.key? flagset.key
11
+
12
+ @flagsets[flagset.key] = flagset
13
+ end
14
+
15
+ def get(key)
16
+ raise UndefinedFlagsetError.new("Flagset :#{key} does not exist") unless @flagsets.key? key
17
+
18
+ @flagsets[key]
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module Flagship
2
+ VERSION = "0.1.0"
3
+ end
data/lib/flagship.rb ADDED
@@ -0,0 +1,48 @@
1
+ require "flagship/version"
2
+ require "flagship/context"
3
+ require "flagship/dsl"
4
+ require "flagship/flagset"
5
+ require "flagship/flagsets_container"
6
+
7
+ module Flagship
8
+ class NoFlagsetSelectedError < ::StandardError; end
9
+
10
+ def self.define(key, options = {}, &block)
11
+ context = self.default_context
12
+ base = options[:extend] ? self.get_flagset(options[:extend]) : nil
13
+ self.default_flagsets_container.add ::Flagship::Dsl.new(key, context, base, &block).flagset
14
+ end
15
+
16
+ def self.enabled?(key)
17
+ self.current_flagset.enabled?(key)
18
+ end
19
+
20
+ def self.set_context(key, value)
21
+ self.default_context.__set(key, value)
22
+ end
23
+
24
+ def self.set_flagset(key)
25
+ @@current_flagset = self.default_flagsets_container.get(key)
26
+ end
27
+
28
+ def self.get_flagset(key)
29
+ self.default_flagsets_container.get(key)
30
+ end
31
+
32
+ def self.default_flagsets_container
33
+ @@default_flagsts_container ||= ::Flagship::FlagsetsContainer.new
34
+ end
35
+
36
+ def self.current_flagset
37
+ @@current_flagset or raise NoFlagsetSelectedError.new('No flagset is selected')
38
+ end
39
+
40
+ def self.default_context
41
+ @@default_context ||= ::Flagship::Context.new
42
+ end
43
+
44
+ def self.clear_state
45
+ @@default_flagsts_container = nil
46
+ @@current_flagset = nil
47
+ end
48
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flagship
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yuya Takeyama
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.5.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.5.0
55
+ description: Ship/unship features using flags defined with declarative DSL
56
+ email:
57
+ - sign.of.the.wolf.pentagram@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - flagship.gemspec
72
+ - lib/flagship.rb
73
+ - lib/flagship/context.rb
74
+ - lib/flagship/dsl.rb
75
+ - lib/flagship/flagset.rb
76
+ - lib/flagship/flagsets_container.rb
77
+ - lib/flagship/version.rb
78
+ homepage: https://github.com/yuya-takeyama/flagship
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.5.1
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Ship/unship features using flags defined with declarative DSL
102
+ test_files: []