rollout_postgres_store 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e93b50729f6b9f8c4ebfc7134c5c606ad0307739
4
+ data.tar.gz: e4ccf832f8bd8e394a940d9033f9c83f5cba6d4e
5
+ SHA512:
6
+ metadata.gz: 250b4f8ab02013f69637c26ef86040db980fb9aced6ca49dd7715e3a3622c4638839b57554886f4b6aa0e33ae474c35e5d56daf84c191544ba793f27f9955822
7
+ data.tar.gz: 2917a66e420f84ac3cd221047b81d4834927aedcf2b489356d031057e2c2af173171a721cce33ec00cef57d1a65c0fa089b93a1f756208a6cfcf41f7fd720216
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
@@ -0,0 +1,18 @@
1
+ language: ruby
2
+ cache: bundler
3
+
4
+ rvm:
5
+ - 2.0.0
6
+ - 2.1.0
7
+ - 2.1.2
8
+ - 1.9.3
9
+
10
+ before_install:
11
+ - createdb rollout_store_test
12
+ - gem install bundler
13
+
14
+ script: "bundle exec rake"
15
+
16
+ branches:
17
+ only:
18
+ - master
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rollout_postgres_store.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 pherefordATK
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.
@@ -0,0 +1,49 @@
1
+ # RolloutPostgresStore
2
+ [![Build Status](https://travis-ci.org/Americastestkitchen/rollout_postgres_store.svg)](https://travis-ci.org/Americastestkitchen/rollout_postgres_store)
3
+ [![Code Climate](https://codeclimate.com/github/Americastestkitchen/rollout_postgres_store/badges/gpa.svg)](https://codeclimate.com/github/Americastestkitchen/rollout_postgres_store)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rollout_postgres_store'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rollout_postgres_store
20
+
21
+ ## Dependencies
22
+
23
+ This gem relies on Postgres' Hstore extension and the Rollout gem. You will need
24
+ Gems:
25
+ - active_record >= 4.0.0
26
+ - rollout >= 2.0.0
27
+ - pg
28
+
29
+ Postgres >= 9.2
30
+
31
+ ## Usage
32
+
33
+ To use this gem, you will need to have an ActiveRecord model with a hstore
34
+ attribute. Once you have that, you should add the following inside of
35
+ `config/initializers/rollout.rb`:
36
+
37
+ ```
38
+ # FeatureFlag is the model that has your Hstore attribute.
39
+ # 'data' is the hstore attribute on your FeatureFlag model.
40
+ ROLLOUT = Rollout.new(RolloutPostgresStore.new(FeatureFlag, 'data'))
41
+ ```
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it ( https://github.com/[my-github-username]/rollout_postgres_store/fork )
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create a new Pull Request
@@ -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
@@ -0,0 +1,42 @@
1
+ class RolloutPostgresStore
2
+ def initialize(model, attribute)
3
+ @model = model
4
+ @attribute = attribute
5
+ end
6
+
7
+ def get(key)
8
+ if flag = @model.where("#{@attribute} ? '#{key}'").first
9
+ flag.send(@attribute)[key]
10
+ end
11
+ end
12
+
13
+ def set(key, value)
14
+ current = get(key)
15
+
16
+ if current.nil?
17
+ create_feature_flag(key, value)
18
+ else
19
+ update_flag(key, value)
20
+ end
21
+ end
22
+
23
+ def del(key)
24
+ if flag = @model.where("#{@attribute} ? '#{key}'").first
25
+ flag.delete
26
+ end
27
+ end
28
+
29
+ private
30
+ def create_feature_flag(key, value)
31
+ flag = @model.new
32
+ flag.send("#{@attribute}=", { key => value })
33
+ flag.save
34
+ end
35
+
36
+ def update_flag(key, value)
37
+ flag = @model.where("#{@attribute} ? '#{key}'").first
38
+ flag.send(@attribute)[key] = value
39
+ flag.send("#{@attribute}_will_change!")
40
+ flag.save
41
+ end
42
+ end
@@ -0,0 +1,3 @@
1
+ class RolloutPostgresStore
2
+ VERSION = '0.0.1'
3
+ end
@@ -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 'rollout_postgres_store/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rollout_postgres_store"
8
+ spec.version = RolloutPostgresStore::VERSION
9
+ spec.authors = ["phereford"]
10
+ spec.email = ["patrick.hereford@americastestkitchen.com"]
11
+ spec.summary = %q{Simple Postgres Hstore wrapper for Rollout gem.}
12
+ spec.description = %q{Simple Postgres Hstore wrapper for Rollout gem.}
13
+ spec.homepage = "http://github.com/Americastestkitchen/rollout_postgres_store"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activerecord", ">= 4.0.0"
22
+ spec.add_dependency "pg", ">= 0.15.1"
23
+ spec.add_dependency "rollout", ">= 2.0.0"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.7"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.2.0"
28
+ spec.add_development_dependency "database_cleaner", "~> 1.4.0"
29
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+ require 'rollout'
3
+ require 'rollout_postgres_store'
4
+
5
+ ROLLOUT = Rollout.new(RolloutPostgresStore.new(FeatureFlag, 'data'))
6
+
7
+ describe RolloutPostgresStore, '.new' do
8
+ it 'assigns the model instance variable' do
9
+ store = RolloutPostgresStore.new(FeatureFlag, 'data')
10
+ expect(store.instance_variable_get(:@model)).to be FeatureFlag
11
+ end
12
+
13
+ it 'assigns the attribute instance variable' do
14
+ store = RolloutPostgresStore.new(FeatureFlag, 'data')
15
+ expect(store.instance_variable_get(:@attribute)).to eql 'data'
16
+ end
17
+ end
18
+
19
+ describe RolloutPostgresStore, '#get' do
20
+ it 'returns nil for not having found a FeatureFlag with the given key' do
21
+ store = RolloutPostgresStore.new(FeatureFlag, 'data')
22
+ expect(store.get('test_key')).to be nil
23
+ end
24
+
25
+ it 'returns the value for a given key when a FeatureFlag is found' do
26
+ ROLLOUT.activate_percentage(:search, 20)
27
+
28
+ store = RolloutPostgresStore.new(FeatureFlag, 'data')
29
+ expect(store.get('feature:search')).to eql '20||'
30
+ end
31
+ end
32
+
33
+ describe RolloutPostgresStore, '#set' do
34
+ it 'receives the create_feature_flag message for not having found a flag' do
35
+ expect_any_instance_of(RolloutPostgresStore).to receive(:create_feature_flag).at_most(:once)
36
+
37
+ RolloutPostgresStore.new(FeatureFlag, 'data').set('search', '20||')
38
+ end
39
+
40
+ it 'receives the update_flag message for having found a flag' do
41
+ ROLLOUT.activate_percentage(:search, 20)
42
+
43
+ expect_any_instance_of(RolloutPostgresStore).to receive(:update_flag).at_most(:once)
44
+
45
+ RolloutPostgresStore.new(FeatureFlag, 'data').set('feature:search', '20||123')
46
+ end
47
+ end
48
+
49
+ describe RolloutPostgresStore, '#del' do
50
+ it 'returns the object deleted for finding the key to delete' do
51
+ ROLLOUT.activate_percentage(:search, 30)
52
+
53
+ # There is only one feature flag per the activation above
54
+ feature_flag = FeatureFlag.first
55
+ store = RolloutPostgresStore.new(FeatureFlag, 'data')
56
+ expect(store.del('feature:search')).to eq feature_flag
57
+ end
58
+
59
+ it 'returns nil for not being able to find the key to delete' do
60
+ expect(RolloutPostgresStore.new(FeatureFlag, 'data').del('feature:test')).to be nil
61
+ end
62
+ end
@@ -0,0 +1,25 @@
1
+ require 'active_record'
2
+ require 'database_cleaner'
3
+
4
+ begin
5
+ ActiveRecord::Base.establish_connection({
6
+ 'adapter' => 'postgresql',
7
+ 'host' => 'localhost'
8
+ }).connection.create_database('rollout_store_test')
9
+ rescue ActiveRecord::StatementInvalid
10
+ end
11
+
12
+ conn = ActiveRecord::Base.establish_connection({
13
+ 'database' => 'rollout_store_test',
14
+ 'adapter' => 'postgresql',
15
+ 'host' => 'localhost'
16
+ })
17
+
18
+ if !conn.connection.tables.include?('feature_flags')
19
+ load File.dirname(__FILE__) + '/support/schema.rb'
20
+ end
21
+
22
+ load File.dirname(__FILE__) + '/support/models.rb'
23
+
24
+ DatabaseCleaner.clean_with :truncation
25
+ DatabaseCleaner.strategy = :transaction
@@ -0,0 +1,2 @@
1
+ class FeatureFlag < ActiveRecord::Base
2
+ end
@@ -0,0 +1,9 @@
1
+ ActiveRecord::Schema.define do
2
+ self.verbose = false
3
+
4
+ execute "CREATE EXTENSION hstore"
5
+ create_table :feature_flags, :force => true do |t|
6
+ t.hstore :data
7
+ t.timestamps
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rollout_postgres_store
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - phereford
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-02-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: pg
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.15.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.15.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: rollout
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 2.0.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 2.0.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 3.2.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 3.2.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: database_cleaner
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 1.4.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 1.4.0
111
+ description: Simple Postgres Hstore wrapper for Rollout gem.
112
+ email:
113
+ - patrick.hereford@americastestkitchen.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".travis.yml"
120
+ - Gemfile
121
+ - LICENSE.txt
122
+ - README.md
123
+ - Rakefile
124
+ - lib/rollout_postgres_store.rb
125
+ - lib/rollout_postgres_store/version.rb
126
+ - rollout_postgres_store.gemspec
127
+ - spec/rollout_postgres_store_spec.rb
128
+ - spec/spec_helper.rb
129
+ - spec/support/models.rb
130
+ - spec/support/schema.rb
131
+ homepage: http://github.com/Americastestkitchen/rollout_postgres_store
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.2.2
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: Simple Postgres Hstore wrapper for Rollout gem.
155
+ test_files:
156
+ - spec/rollout_postgres_store_spec.rb
157
+ - spec/spec_helper.rb
158
+ - spec/support/models.rb
159
+ - spec/support/schema.rb