flipper-mongo 0.20.2 → 0.21.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
  SHA256:
3
- metadata.gz: a2437daf52f4312492d9fedcd89fc801654e9f92b9724a272dee43a395d6d0bd
4
- data.tar.gz: 6f781f5a864a17caac88cd714d9ec0488a611958e13aad66418340551f889a1e
3
+ metadata.gz: 656ce494cea1f36610fece84fe1557426d4843b430d4b845170632a4dd621615
4
+ data.tar.gz: 487e7251d6f36477ba2eb8696675ae2acbeddf21d1b3185f91887a8b27982164
5
5
  SHA512:
6
- metadata.gz: 630bcb2a27e50283d9cfe2bb73d29c9baa9f33951be8be339b5dcc3a98abb02e629d106fc4b4cd53dcbdcbcfd99eac975a658683ecabf4e68742f0de28c6a392
7
- data.tar.gz: c8dd4bec128d4b24be92d1227272721818b078e9364365a7b461dff8e83b60f98e854294773ccc78207e43217c69ec59afb195ca3ba083f13c8ffadd4dc8771d
6
+ metadata.gz: ca358dde4ef62bcb98feb576dc64244366aa82e9e5a57d40ebdeb66e707e8fb316480906c74f77766aef8e2e396b42047023d95a34c4995f591abe56bcb4d823
7
+ data.tar.gz: de6d0e904fbdc71fb00cac63d0474b9da3b2ee8124964020c0f6119ee6895c3ca286f2080af5c0098af20f09fdf374b227dcb5ead87691dfaad19c5bb18b312d
data/docs/mongo/README.md CHANGED
@@ -18,12 +18,21 @@ Or install it yourself with:
18
18
 
19
19
  ## Usage
20
20
 
21
+ In most cases, all you need to do is require the adapter. You must set the `MONGO_URL` or `FLIPPER_MONGO_URL` environment vairable to specify which Mongo database to connect to.
22
+
21
23
  ```ruby
22
- require 'flipper/adapters/mongo'
23
- collection = Mongo::Client.new(["127.0.0.1:27017"], database: 'testing')['flipper']
24
- adapter = Flipper::Adapters::Mongo.new(collection)
25
- flipper = Flipper.new(adapter)
26
- # profit...
24
+ require 'flipper-mongo`
25
+ ```
26
+
27
+ **If you need to customize the adapter**, you can add this to an initializer:
28
+
29
+ ```ruby
30
+ Flipper.configure do |config|
31
+ config.adapter do
32
+ collection = Mongo::Client.new(ENV["MONGO_URL"])["flipper"]
33
+ Flipper::Adapters::Mongo.new(collection)
34
+ end
35
+ end
27
36
  ```
28
37
 
29
38
  ## Internals
@@ -1,27 +1,20 @@
1
- require 'pathname'
1
+ require 'bundler/setup'
2
2
  require 'logger'
3
3
 
4
- root_path = Pathname(__FILE__).dirname.join('..').expand_path
5
- lib_path = root_path.join('lib')
6
- $:.unshift(lib_path)
7
-
4
+ ENV["FLIPPER_MONGO_URL"] ||= "mongodb://127.0.0.1:#{ENV["MONGODB_PORT"] || 27017}"
8
5
  require 'flipper/adapters/mongo'
9
- Mongo::Logger.logger.level = Logger::INFO
10
- collection = Mongo::Client.new(["127.0.0.1:#{ENV["MONGODB_PORT"] || 27017}"], :database => 'testing')['flipper']
11
- adapter = Flipper::Adapters::Mongo.new(collection)
12
- flipper = Flipper.new(adapter)
13
6
 
14
- flipper[:stats].enable
7
+ Flipper[:stats].enable
15
8
 
16
- if flipper[:stats].enabled?
9
+ if Flipper[:stats].enabled?
17
10
  puts "Enabled!"
18
11
  else
19
12
  puts "Disabled!"
20
13
  end
21
14
 
22
- flipper[:stats].disable
15
+ Flipper[:stats].disable
23
16
 
24
- if flipper[:stats].enabled?
17
+ if Flipper[:stats].enabled?
25
18
  puts "Enabled!"
26
19
  else
27
20
  puts "Disabled!"
@@ -1,16 +1,9 @@
1
+ require 'bundler/setup'
1
2
  require 'pp'
2
- require 'pathname'
3
3
  require 'logger'
4
4
 
5
- root_path = Pathname(__FILE__).dirname.join('..').expand_path
6
- lib_path = root_path.join('lib')
7
- $:.unshift(lib_path)
8
-
5
+ ENV["FLIPPER_MONGO_URL"] ||= "mongodb://127.0.0.1:#{ENV["MONGODB_PORT"] || 27017}"
9
6
  require 'flipper/adapters/mongo'
10
- Mongo::Logger.logger.level = Logger::INFO
11
- collection = Mongo::Client.new(["127.0.0.1:#{ENV["MONGODB_PORT"] || 27017}"], :database => 'testing')['flipper']
12
- adapter = Flipper::Adapters::Mongo.new(collection)
13
- flipper = Flipper.new(adapter)
14
7
 
15
8
  # Register a few groups.
16
9
  Flipper.register(:admins) { |thing| thing.admin? }
@@ -19,19 +12,19 @@ Flipper.register(:early_access) { |thing| thing.early_access? }
19
12
  # Create a user class that has flipper_id instance method.
20
13
  User = Struct.new(:flipper_id)
21
14
 
22
- flipper[:stats].enable
23
- flipper[:stats].enable_group :admins
24
- flipper[:stats].enable_group :early_access
25
- flipper[:stats].enable_actor User.new('25')
26
- flipper[:stats].enable_actor User.new('90')
27
- flipper[:stats].enable_actor User.new('180')
28
- flipper[:stats].enable_percentage_of_time 15
29
- flipper[:stats].enable_percentage_of_actors 45
15
+ Flipper[:stats].enable
16
+ Flipper[:stats].enable_group :admins
17
+ Flipper[:stats].enable_group :early_access
18
+ Flipper[:stats].enable_actor User.new('25')
19
+ Flipper[:stats].enable_actor User.new('90')
20
+ Flipper[:stats].enable_actor User.new('180')
21
+ Flipper[:stats].enable_percentage_of_time 15
22
+ Flipper[:stats].enable_percentage_of_actors 45
30
23
 
31
- flipper[:search].enable
24
+ Flipper[:search].enable
32
25
 
33
26
  puts 'all docs in collection'
34
- pp collection.find.to_a
27
+ pp Flipper.adapter.adapter.collection.find.to_a
35
28
  # all docs in collection
36
29
  # [{"_id"=>"stats",
37
30
  # "actors"=>["25", "90", "180"],
@@ -44,7 +37,7 @@ pp collection.find.to_a
44
37
  puts
45
38
 
46
39
  puts 'flipper get of feature'
47
- pp adapter.get(flipper[:stats])
40
+ pp Flipper.adapter.get(Flipper[:stats])
48
41
  # flipper get of feature
49
42
  # {:boolean=>"true",
50
43
  # :groups=>#<Set: {"admins", "early_access"}>,
@@ -13,6 +13,9 @@ module Flipper
13
13
  # Public: The name of the adapter.
14
14
  attr_reader :name
15
15
 
16
+ # Public: The name of the collection storing the feature data.
17
+ attr_reader :collection
18
+
16
19
  def initialize(collection)
17
20
  @collection = collection
18
21
  @name = :mongo
@@ -173,3 +176,16 @@ module Flipper
173
176
  end
174
177
  end
175
178
  end
179
+
180
+ Flipper.configure do |config|
181
+ config.adapter do
182
+ url = ENV["FLIPPER_MONGO_URL"] || ENV["MONGO_URL"]
183
+ collection = ENV["FLIPPER_MONGO_COLLECTION"] || "flipper"
184
+
185
+ unless url
186
+ raise ArgumentError, "The MONGO_URL environment variable must be set. For example: mongodb://127.0.0.1:27017/flipper"
187
+ end
188
+
189
+ Flipper::Adapters::Mongo.new(Mongo::Client.new(url)[collection])
190
+ end
191
+ end
@@ -1,3 +1,3 @@
1
1
  module Flipper
2
- VERSION = '0.20.2'.freeze
2
+ VERSION = '0.21.0'.freeze
3
3
  end
@@ -11,17 +11,30 @@ RSpec.describe Flipper::Adapters::Mongo do
11
11
  let(:port) { ENV['MONGODB_PORT'] || 27017 }
12
12
 
13
13
  let(:client) do
14
- Mongo::Client.new(["#{host}:#{port}"], server_selection_timeout: 1, database: 'testing')
14
+ logger = Logger.new('/dev/null')
15
+ Mongo::Client.new(["#{host}:#{port}"], server_selection_timeout: 0.01, database: 'testing', logger: logger)
15
16
  end
16
17
  let(:collection) { client['testing'] }
17
18
 
18
19
  before do
19
20
  begin
20
21
  collection.drop
22
+ rescue Mongo::Error::NoServerAvailable
23
+ ENV['CI'] ? raise : skip('Mongo not available')
21
24
  rescue Mongo::Error::OperationFailure
22
25
  end
23
26
  collection.create
24
27
  end
25
28
 
26
29
  it_should_behave_like 'a flipper adapter'
30
+
31
+ it 'configures itself on load' do
32
+ Flipper.configuration = nil
33
+ Flipper.instance = nil
34
+
35
+ load 'flipper/adapters/mongo.rb'
36
+
37
+ ENV["MONGO_URL"] ||= "mongodb://127.0.0.1:27017/testing"
38
+ expect(Flipper.adapter.adapter).to be_a(Flipper::Adapters::Mongo)
39
+ end
27
40
  end
@@ -9,13 +9,15 @@ class MongoTest < MiniTest::Test
9
9
  port = '27017'
10
10
  logger = Logger.new('/dev/null')
11
11
  client = Mongo::Client.new(["#{host}:#{port}"],
12
- server_selection_timeout: 1,
12
+ server_selection_timeout: 0.01,
13
13
  database: 'testing',
14
14
  logger: logger)
15
15
  collection = client['testing']
16
16
  begin
17
17
  collection.drop
18
18
  collection.create
19
+ rescue Mongo::Error::NoServerAvailable
20
+ ENV['CI'] ? raise : skip('Mongo not available')
19
21
  rescue Mongo::Error::OperationFailure
20
22
  end
21
23
  @adapter = Flipper::Adapters::Mongo.new(collection)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flipper-mongo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.20.2
4
+ version: 0.21.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Nunemaker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-08 00:00:00.000000000 Z
11
+ date: 2021-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: flipper
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.20.2
19
+ version: 0.21.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.20.2
26
+ version: 0.21.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: mongo
29
29
  requirement: !ruby/object:Gem::Requirement