mongoid_feature_flags 0.0.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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/Guardfile +13 -0
- data/LICENSE +22 -0
- data/README.md +56 -0
- data/Rakefile +2 -0
- data/lib/mongoid_feature_flags/errors.rb +5 -0
- data/lib/mongoid_feature_flags/version.rb +5 -0
- data/lib/mongoid_feature_flags.rb +66 -0
- data/mongoid_feature_flags.gemspec +25 -0
- data/spec/lib/feature_flag_spec.rb +81 -0
- data/spec/models/family.rb +8 -0
- data/spec/models/person.rb +10 -0
- data/spec/models/possession.rb +8 -0
- data/spec/models/without_mongoid.rb +3 -0
- data/spec/spec_helper.rb +9 -0
- metadata +133 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# A sample Guardfile
|
2
|
+
# More info at https://github.com/guard/guard#readme
|
3
|
+
|
4
|
+
guard 'rspec', :version => 2 do
|
5
|
+
watch(%r{^spec/.+_spec\.rb$})
|
6
|
+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
7
|
+
watch('spec/spec_helper.rb') { "spec" }
|
8
|
+
end
|
9
|
+
|
10
|
+
guard 'bundler' do
|
11
|
+
watch("Gemfile")
|
12
|
+
watch("mongoid-feature-flags.gemspec")
|
13
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Chris Winslett
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# Mongoid::FeatureFlags
|
2
|
+
|
3
|
+
Out of the box simple usage of feature flags with Mongoid classes.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'mongoid-feature-flags'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install mongoid-feature-flags
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
If you would like to add features on persons, `include Mongoid::FeatureFlags`
|
22
|
+
|
23
|
+
class Person
|
24
|
+
include Mongoid::Document
|
25
|
+
|
26
|
+
has_many :possessions
|
27
|
+
belongs_to :family
|
28
|
+
|
29
|
+
include Mongoid::FeatureFlags
|
30
|
+
inherits_features_from :family
|
31
|
+
end
|
32
|
+
|
33
|
+
Then, you can do the following:
|
34
|
+
|
35
|
+
p = Person.first
|
36
|
+
p.add_feature(:play_guitar_like_slash)
|
37
|
+
|
38
|
+
p.feature?(:play_guitar_like_slash) { *works* }
|
39
|
+
p.feature?(:play_guitar_like_nikki_sixx) { *does not work* }
|
40
|
+
|
41
|
+
## Security ##
|
42
|
+
|
43
|
+
### Rails 3.2 ###
|
44
|
+
You are responsible for using `strong_parameters` at the controller level.
|
45
|
+
|
46
|
+
### < Rails 3.2 ###
|
47
|
+
You are responsible for adding `attr_accessible` or `attr_protected` to
|
48
|
+
your classes for security.
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
1. Fork it
|
53
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
54
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
55
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
56
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require "mongoid_feature_flags/version"
|
2
|
+
require "mongoid_feature_flags/errors"
|
3
|
+
|
4
|
+
module Mongoid
|
5
|
+
module FeatureFlags
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
begin
|
10
|
+
self.field :feature_flags, type: Hash
|
11
|
+
rescue
|
12
|
+
if $!.to_s == "undefined method `field' for WithoutMongoid:Class"
|
13
|
+
raise "Adding Mongoid::FeatureFlags requires Mongoid::Document to also be included on the class"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module ClassMethods
|
19
|
+
def inherits_features_from(*relative)
|
20
|
+
@feature_flag_parents ||= []
|
21
|
+
@feature_flag_parents += relative
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
module InstanceMethods
|
26
|
+
def add_feature(*flags)
|
27
|
+
self.feature_flags ||= {}
|
28
|
+
flags.each do |flag|
|
29
|
+
self.feature_flags[flag] = true
|
30
|
+
end
|
31
|
+
self.save
|
32
|
+
end
|
33
|
+
|
34
|
+
def features
|
35
|
+
inherited_feature_flags.merge(self.feature_flags).inject([]) { |new_hash, key| TrueClass === key[1] ? (new_hash + [key[0]]) : new_hash }
|
36
|
+
end
|
37
|
+
|
38
|
+
def feature?(*require_features, &block)
|
39
|
+
if (self.features & require_features).length > 0
|
40
|
+
block.present? ? block.yield : true
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def not_feature?(*unrequired_features, &block)
|
45
|
+
if (self.features & unrequired_features).length == 0
|
46
|
+
block.present? ? block.yield : true
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def feature_flag_validate_flag(*flag)
|
51
|
+
raise(InvalidFlagName, "Flags must be a symbol") if !flag.any? { |f| f.is_a?(Symbol) }
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
def inherited_feature_flags
|
56
|
+
self.class.instance_variable_get("@feature_flag_parents").inject({}) do |flag_hash, relative|
|
57
|
+
if self.respond_to?(relative) && self.send(relative).present? && self.send(relative).respond_to?(:feature_flags)
|
58
|
+
flag_hash.merge(self.send(relative).send(:feature_flags))
|
59
|
+
else
|
60
|
+
flag_hash
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/mongoid_feature_flags/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Chris Winslett"]
|
6
|
+
gem.email = ["chris@mongohq.com"]
|
7
|
+
gem.description = %q{Mongoid Feature Flags}
|
8
|
+
gem.summary = %q{Hopefully a nice little plug and play for }
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "mongoid_feature_flags"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Mongoid::FeatureFlags::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency "activesupport", "~> 3.2.0"
|
19
|
+
|
20
|
+
gem.add_development_dependency "rspec"
|
21
|
+
gem.add_development_dependency "guard-rspec"
|
22
|
+
gem.add_development_dependency "guard-bundler"
|
23
|
+
gem.add_development_dependency "mongoid"
|
24
|
+
gem.add_development_dependency "rb-fsevent", "~> 0.9.1"
|
25
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require_relative "../spec_helper"
|
2
|
+
|
3
|
+
describe Mongoid::FeatureFlags do
|
4
|
+
|
5
|
+
context "#direct" do
|
6
|
+
let :person do
|
7
|
+
p = Person.create!
|
8
|
+
p.add_feature(:can_see_cartoons)
|
9
|
+
p
|
10
|
+
end
|
11
|
+
|
12
|
+
it "#permitted" do
|
13
|
+
triggered = nil
|
14
|
+
person.feature?(:can_see_cartoons) do
|
15
|
+
triggered = "yep"
|
16
|
+
end
|
17
|
+
|
18
|
+
triggered.should eq("yep")
|
19
|
+
end
|
20
|
+
|
21
|
+
it "#unpermitted" do
|
22
|
+
triggered = nil
|
23
|
+
person.feature?(:can_see_cnn) do
|
24
|
+
triggered = "yep"
|
25
|
+
end
|
26
|
+
|
27
|
+
triggered.should eq(nil)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "#not_feature? permitted" do
|
31
|
+
triggered = nil
|
32
|
+
person.not_feature?(:can_see_cartoons) do
|
33
|
+
triggered = "nope"
|
34
|
+
end
|
35
|
+
|
36
|
+
triggered.should eq(nil)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "#featured? unpermitted" do
|
40
|
+
triggered = nil
|
41
|
+
person.not_feature?(:can_see_cnn) do
|
42
|
+
triggered = "nope"
|
43
|
+
end
|
44
|
+
|
45
|
+
triggered.should eq("nope")
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context "#inherited" do
|
50
|
+
let :family do
|
51
|
+
f = Family.create!
|
52
|
+
f.add_feature(:can_see_comedy_central)
|
53
|
+
f
|
54
|
+
end
|
55
|
+
|
56
|
+
let :person do
|
57
|
+
p = Person.create!(family: family)
|
58
|
+
p.add_feature(:can_see_cartoons)
|
59
|
+
p
|
60
|
+
end
|
61
|
+
|
62
|
+
it "#permitted" do
|
63
|
+
triggered = nil
|
64
|
+
person.feature?(:can_see_comedy_central) do
|
65
|
+
triggered = "yep"
|
66
|
+
end
|
67
|
+
|
68
|
+
triggered.should eq("yep")
|
69
|
+
end
|
70
|
+
|
71
|
+
it "#unpermitted" do
|
72
|
+
triggered = nil
|
73
|
+
person.feature?(:can_see_star_wars) do
|
74
|
+
triggered = "yep"
|
75
|
+
end
|
76
|
+
|
77
|
+
triggered.should eq(nil)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require "mongoid"
|
2
|
+
|
3
|
+
require_relative "../lib/mongoid_feature_flags"
|
4
|
+
|
5
|
+
require_relative "./models/family"
|
6
|
+
require_relative "./models/person"
|
7
|
+
require_relative "./models/possession"
|
8
|
+
|
9
|
+
Mongoid.database = Mongo::Connection.from_uri("mongodb://localhost:27017/test").db("test")
|
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mongoid_feature_flags
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Chris Winslett
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: &70105158078900 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70105158078900
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70105158078460 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70105158078460
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: guard-rspec
|
38
|
+
requirement: &70105158077940 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70105158077940
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: guard-bundler
|
49
|
+
requirement: &70105158077320 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70105158077320
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: mongoid
|
60
|
+
requirement: &70105158076580 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70105158076580
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rb-fsevent
|
71
|
+
requirement: &70105158076060 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ~>
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: 0.9.1
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *70105158076060
|
80
|
+
description: Mongoid Feature Flags
|
81
|
+
email:
|
82
|
+
- chris@mongohq.com
|
83
|
+
executables: []
|
84
|
+
extensions: []
|
85
|
+
extra_rdoc_files: []
|
86
|
+
files:
|
87
|
+
- .gitignore
|
88
|
+
- Gemfile
|
89
|
+
- Guardfile
|
90
|
+
- LICENSE
|
91
|
+
- README.md
|
92
|
+
- Rakefile
|
93
|
+
- lib/mongoid_feature_flags.rb
|
94
|
+
- lib/mongoid_feature_flags/errors.rb
|
95
|
+
- lib/mongoid_feature_flags/version.rb
|
96
|
+
- mongoid_feature_flags.gemspec
|
97
|
+
- spec/lib/feature_flag_spec.rb
|
98
|
+
- spec/models/family.rb
|
99
|
+
- spec/models/person.rb
|
100
|
+
- spec/models/possession.rb
|
101
|
+
- spec/models/without_mongoid.rb
|
102
|
+
- spec/spec_helper.rb
|
103
|
+
homepage: ''
|
104
|
+
licenses: []
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ! '>='
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
none: false
|
117
|
+
requirements:
|
118
|
+
- - ! '>='
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 1.8.10
|
124
|
+
signing_key:
|
125
|
+
specification_version: 3
|
126
|
+
summary: Hopefully a nice little plug and play for
|
127
|
+
test_files:
|
128
|
+
- spec/lib/feature_flag_spec.rb
|
129
|
+
- spec/models/family.rb
|
130
|
+
- spec/models/person.rb
|
131
|
+
- spec/models/possession.rb
|
132
|
+
- spec/models/without_mongoid.rb
|
133
|
+
- spec/spec_helper.rb
|