flippant 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a6778b1feeb68165dda564259e1d43a452631e01
4
+ data.tar.gz: 2e64a598a8da303f436dd2c98485fd02fac5a32a
5
+ SHA512:
6
+ metadata.gz: cef32740c7a1a25d33c91927815a187df6f4defa568c2a3505b297ce7588d2d7a9e441e9c8d6e6f52fafd167c56e83908db8b94a756a9bf4ef7d10d3cabab90a
7
+ data.tar.gz: 618a8147ddadf75b99266a236124cc4ec664baecdf5b571a0f3d3a8b9744e6580ddb680265f1e96e15e49e7c04908e4dfe00f85d59f0b7969fe7bfce3a77a1dc
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,2 @@
1
+ --color
2
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,44 @@
1
+ AllCops:
2
+ Exclude:
3
+ - "bin/*"
4
+ - "vendor/**/*"
5
+ TargetRubyVersion: 2.3
6
+
7
+ Style/FrozenStringLiteralComment:
8
+ EnforcedStyle: always
9
+ Exclude:
10
+ - "Gemfile*"
11
+ - "Rakefile"
12
+ - "*.gemspec"
13
+ - "spec/**/*"
14
+
15
+ Style/IfUnlessModifier:
16
+ Enabled: false
17
+
18
+ # Turn this on in the future
19
+ Style/Documentation:
20
+ Enabled: false
21
+
22
+ # Extend self preserves private methods
23
+ Style/ModuleFunction:
24
+ Enabled: false
25
+
26
+ Style/PercentLiteralDelimiters:
27
+ PreferredDelimiters:
28
+ '%i': '[]'
29
+ '%w': '[]'
30
+ '%W': '[]'
31
+
32
+ Style/StringLiterals:
33
+ EnforcedStyle: double_quotes
34
+
35
+ Style/SpaceInsideHashLiteralBraces:
36
+ EnforcedStyle: no_space
37
+ EnforcedStyleForEmptyBraces: no_space
38
+
39
+ Style/IndentArray:
40
+ Enabled: false
41
+
42
+ Metrics/BlockLength:
43
+ Exclude:
44
+ - "spec/**/*"
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in flippant-rb.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Parker Selbert
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,41 @@
1
+ # Flippant
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/flippant`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'flippant-rb'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install flippant-rb
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/flippant-rb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ require "bundler/setup"
2
+ require "bundler/gem_tasks"
3
+
4
+ begin
5
+ require "rspec/core/rake_task"
6
+ RSpec::Core::RakeTask.new(:spec)
7
+ rescue LoadError
8
+ puts "rspec not loaded"
9
+ end
10
+
11
+ begin
12
+ require "rubocop/rake_task"
13
+ RuboCop::RakeTask.new
14
+ rescue LoadError
15
+ puts "rubocop not loaded"
16
+ end
17
+
18
+ task default: [:rubocop, :spec]
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "flippant"
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/rspec ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ #
4
+ # This file was generated by Bundler.
5
+ #
6
+ # The application 'rspec' is installed as part of a gem, and
7
+ # this file is here to facilitate running it.
8
+ #
9
+
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
12
+ Pathname.new(__FILE__).realpath)
13
+
14
+ require "rubygems"
15
+ require "bundler/setup"
16
+
17
+ load Gem.bin_path("rspec-core", "rspec")
data/bin/rubocop ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ #
4
+ # This file was generated by Bundler.
5
+ #
6
+ # The application 'rubocop' is installed as part of a gem, and
7
+ # this file is here to facilitate running it.
8
+ #
9
+
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
12
+ Pathname.new(__FILE__).realpath)
13
+
14
+ require "rubygems"
15
+ require "bundler/setup"
16
+
17
+ load Gem.bin_path("rubocop", "rubocop")
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/flippant.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "flippant/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "flippant"
8
+ spec.version = Flippant::VERSION
9
+ spec.authors = ["Parker Selbert"]
10
+ spec.email = ["parker@sorentwo.com"]
11
+
12
+ spec.summary = "Fast feature toggling for applications"
13
+ spec.description = "Fast feature toggling for applications"
14
+ spec.homepage = "https://github.com/sorentwo/flippant-rb"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^spec/})
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.2"
27
+ spec.add_development_dependency "redis", "~> 3.3"
28
+ spec.add_development_dependency "rubocop", "~> 0.47"
29
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Flippant
4
+ module Adapter
5
+ class Memory
6
+ attr_reader :table
7
+
8
+ def initialize
9
+ clear
10
+ end
11
+
12
+ def add(feature)
13
+ table[feature.to_s] ||= {}
14
+ end
15
+
16
+ def remove(feature)
17
+ table.delete(feature)
18
+ end
19
+
20
+ def enable(feature, group, values = [])
21
+ fkey = feature.to_s
22
+ gkey = group.to_s
23
+ mutex = Mutex.new
24
+
25
+ mutex.synchronize do
26
+ table[fkey][gkey] ||= []
27
+ table[fkey][gkey] = (table[fkey][gkey] | values).sort
28
+ end
29
+ end
30
+
31
+ def disable(feature, group, values = [])
32
+ rules = table[feature.to_s]
33
+ mutex = Mutex.new
34
+
35
+ mutex.synchronize do
36
+ if values.any?
37
+ remove_values(rules, group, values)
38
+ else
39
+ remove_group(rules, group)
40
+ end
41
+ end
42
+ end
43
+
44
+ def enabled?(feature, actor, registered = Flippant.registered)
45
+ table[feature.to_s].any? do |group, values|
46
+ if (block = registered[group.to_s])
47
+ block.call(actor, values)
48
+ end
49
+ end
50
+ end
51
+
52
+ def exists?(feature, group = nil)
53
+ if group.nil?
54
+ table.key?(feature)
55
+ else
56
+ table.dig(feature, group.to_s)
57
+ end
58
+ end
59
+
60
+ def features(filter = nil)
61
+ if filter.nil?
62
+ table.keys.sort
63
+ else
64
+ table.select do |_, pairs|
65
+ pairs.any? { |(group, _)| group == filter.to_s }
66
+ end.keys.sort
67
+ end
68
+ end
69
+
70
+ def breakdown(actor = nil)
71
+ return table if actor.nil?
72
+
73
+ table.each_with_object({}) do |(feature, _), memo|
74
+ memo[feature] = enabled?(feature, actor)
75
+ end
76
+ end
77
+
78
+ def clear
79
+ @table = Hash.new { |hash, key| hash[key] = {} }
80
+ end
81
+
82
+ private
83
+
84
+ def remove_group(rules, to_remove)
85
+ rules.reject! { |(group, _)| group == to_remove.to_s }
86
+ end
87
+
88
+ def remove_values(rules, group, values)
89
+ rules[group] = (rules[group] - values)
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,129 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "redis"
4
+
5
+ module Flippant
6
+ module Adapter
7
+ class Redis
8
+ extend Forwardable
9
+
10
+ DEFAULT_KEY = "features"
11
+
12
+ attr_reader :client, :key, :serializer
13
+
14
+ def_delegators :serializer, :dump, :load
15
+
16
+ def initialize(client = ::Redis.current,
17
+ key = DEFAULT_KEY,
18
+ serializer = Flippant.serializer)
19
+ @client = client
20
+ @key = key
21
+ @serializer = serializer
22
+ end
23
+
24
+ def add(feature)
25
+ client.sadd(key, feature)
26
+ end
27
+
28
+ def remove(feature)
29
+ client.multi do
30
+ client.srem(key, feature)
31
+ client.del(namespace(feature))
32
+ end
33
+ end
34
+
35
+ def enable(feature, group, values = [])
36
+ add(feature)
37
+
38
+ change_values(namespace(feature), group) do |old|
39
+ (old | values).sort
40
+ end
41
+ end
42
+
43
+ def disable(feature, group, values = [])
44
+ namespaced = namespace(feature)
45
+
46
+ if values.any?
47
+ change_values(namespaced, group) do |old|
48
+ old - values
49
+ end
50
+ else
51
+ client.hdel(namespaced, group)
52
+ end
53
+
54
+ maybe_cleanup(feature)
55
+ end
56
+
57
+ def enabled?(feature, actor, registered = Flippant.registered)
58
+ client.hgetall(namespace(feature)).any? do |group, values|
59
+ if (block = registered[group])
60
+ block.call(actor, load(values))
61
+ end
62
+ end
63
+ end
64
+
65
+ def exists?(feature, group = nil)
66
+ if group.nil?
67
+ client.sismember(key, feature)
68
+ else
69
+ client.hexists(namespace(feature), group)
70
+ end
71
+ end
72
+
73
+ def features(filter = :all)
74
+ if filter == :all
75
+ client.smembers(key).sort
76
+ else
77
+ features(:all).select do |fkey|
78
+ client.hexists(namespace(fkey), filter)
79
+ end
80
+ end
81
+ end
82
+
83
+ def breakdown(actor = nil)
84
+ features(:all).each_with_object({}) do |fkey, memo|
85
+ memo[fkey] = actor.nil? ? feature_rules(fkey) : enabled?(fkey, actor)
86
+ end
87
+ end
88
+
89
+ def clear
90
+ client.smembers(key).each { |fkey| remove(fkey) }
91
+ end
92
+
93
+ private
94
+
95
+ def feature_rules(feature)
96
+ namespaced = namespace(feature)
97
+
98
+ client.hgetall(namespaced).each_with_object({}) do |(key, val), memo|
99
+ memo[key] = load(val)
100
+ end
101
+ end
102
+
103
+ def get_values(namespaced, group)
104
+ load(client.hget(namespaced, group)) || []
105
+ end
106
+
107
+ def maybe_cleanup(feature)
108
+ namespaced = namespace(feature)
109
+
110
+ client.srem(key, feature) if client.hkeys(namespaced).empty?
111
+ end
112
+
113
+ def namespace(feature)
114
+ "#{key}-#{feature}"
115
+ end
116
+
117
+ def change_values(namespaced, group)
118
+ client.watch(namespaced) do
119
+ old_values = get_values(namespaced, group)
120
+ new_values = yield(old_values)
121
+
122
+ client.multi do
123
+ client.hset(namespaced, group, dump(new_values))
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Flippant
4
+ class Registry
5
+ def initialize
6
+ clear
7
+ end
8
+
9
+ def register(group, fun = nil, &block)
10
+ table[group.to_s] = fun || block
11
+ end
12
+
13
+ def registered
14
+ table
15
+ end
16
+
17
+ def clear
18
+ @table = {}
19
+ end
20
+
21
+ private
22
+
23
+ attr_reader :table
24
+ end
25
+ end
File without changes
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Flippant
4
+ VERSION = "0.5.0"
5
+ end
data/lib/flippant.rb ADDED
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module Flippant
6
+ autoload :Registry, "flippant/registry"
7
+
8
+ module Adapter
9
+ autoload :Memory, "flippant/adapters/memory"
10
+ autoload :Redis, "flippant/adapters/redis"
11
+ end
12
+
13
+ extend Forwardable
14
+ extend self
15
+
16
+ attr_writer :adapter, :registry, :serializer
17
+
18
+ def_delegators :adapter,
19
+ :add,
20
+ :remove,
21
+ :enable,
22
+ :enabled?,
23
+ :exists?,
24
+ :disable,
25
+ :features,
26
+ :breakdown,
27
+ :clear
28
+
29
+ def_delegators :registry,
30
+ :register,
31
+ :registered,
32
+ :clear
33
+
34
+ def adapter
35
+ @adapter ||= Flippant::Adapter::Memory.new
36
+ end
37
+
38
+ def registry
39
+ @registry ||= Flippant::Registry.new
40
+ end
41
+
42
+ def serializer
43
+ @serializer ||= JSON
44
+ end
45
+
46
+ def configure
47
+ yield self
48
+ end
49
+
50
+ def clear(selection = nil)
51
+ case selection
52
+ when :features then adapter.clear
53
+ when :groups then registry.clear
54
+ else adapter.clear && registry.clear
55
+ end
56
+ end
57
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flippant
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Parker Selbert
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-24 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.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: redis
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.47'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.47'
83
+ description: Fast feature toggling for applications
84
+ email:
85
+ - parker@sorentwo.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".rubocop.yml"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/rspec
100
+ - bin/rubocop
101
+ - bin/setup
102
+ - flippant.gemspec
103
+ - lib/flippant.rb
104
+ - lib/flippant/adapters/memory.rb
105
+ - lib/flippant/adapters/redis.rb
106
+ - lib/flippant/registry.rb
107
+ - lib/flippant/rules.rb
108
+ - lib/flippant/version.rb
109
+ homepage: https://github.com/sorentwo/flippant-rb
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.5.1
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Fast feature toggling for applications
133
+ test_files: []