flaggle_rock 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ccaee874534642a48b8ffff3d5d31f1f63030b1d4f6004bfe852559e1b969040
4
+ data.tar.gz: 655265cebb31608b33ddaf3bd2f439f9f0bfd366f5ee24f031f6d548c9028481
5
+ SHA512:
6
+ metadata.gz: '098ffb87ca21ec30ba9f529f364e8f2ee852d408b318cfb789124c794a7746a802162b5aaa777463ac4c4e9ca0565cde7d2302271c8e1ec1ab48b56dbc2a1a77'
7
+ data.tar.gz: bd15f1ada45e12272e27add79ce010e5c23173998ff293d1ddc1dc9348055c0bc448b4c26507d1d1ccdb0d3d2382409b58bf2eb1bc5ce7ff5344f4b64c38304e
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2023 David Heinemeier Hansson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # Flaggle Rock
2
+
3
+ [View on RubyGems](https://rubygems.org/gems/flaggle_rock)
4
+
5
+ Flaggle Rock is a simple feature flagging gem for use with Ruby on Rails applications.
6
+
7
+ It is designed for easily hiding features from end users to help enable a fast-moving
8
+ CI workflow. It currently does not aim to support more advanced features such as A/B
9
+ testing.
10
+
11
+ ## Installation
12
+
13
+ Add Flaggle Rock to your Gemfile:
14
+
15
+ ```ruby
16
+ gem "flaggle_rock"
17
+ ```
18
+
19
+ And install
20
+
21
+ ```sh
22
+ bundle install
23
+ ```
24
+
25
+ Generate the migrations, and run them to add feature flags to your database:
26
+ ```sh
27
+ bundle exec rails generate flaggle_rock:install
28
+ bundle exec rails db:migrate
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ All flags are _off by default_. There is no need to explicitly create a new flag—turning
34
+ it on will do this.
35
+
36
+ To enable/disable a flag:
37
+ ```ruby
38
+ Rock.on(:flag_name)
39
+ Rock.off(:flag_name)
40
+ ```
41
+ To check whether a flag is on:
42
+ ```ruby
43
+ Rock.on?(:flag_name)
44
+ Rock.off?(:flag_name)
45
+ ```
46
+
47
+ To remove a flag which is no longer used:
48
+ ```ruby
49
+ Rock.delete(:flag_name)
50
+ ```
51
+
52
+ To remove all disabled flags:
53
+ ```ruby
54
+ Rock.delete_all_disabled
55
+ ```
56
+
57
+ ## Future
58
+
59
+ Goals with Flaggle Rock include the creation of a web UI for easily administering flags.
60
+
61
+ ## Compatibility
62
+
63
+ Flaggle Rock has been tested with Rails 7 and PostgreSQL 11, but should work with older versions of
64
+ Rails and other database engines.
65
+
66
+ ## License
67
+
68
+ Flaggle Rock is released under the [MIT License](MIT-LICENSE.md).
@@ -0,0 +1,49 @@
1
+ class Rock
2
+ def self.on(flag_name)
3
+ enable(flag_name, true)
4
+ end
5
+
6
+ def self.off(flag_name)
7
+ enable(flag_name, false)
8
+ end
9
+
10
+ def self.on?(flag_name)
11
+ result = sql <<-SQL
12
+ SELECT enabled FROM feature_flags
13
+ WHERE name = '#{flag_name}';
14
+ SQL
15
+ result&.first&.dig("enabled") || false
16
+ end
17
+
18
+ def self.off?(flag_name)
19
+ !on?(flag_name)
20
+ end
21
+
22
+ def self.delete(flag_name)
23
+ sql <<-SQL
24
+ DELETE FROM feature_flags
25
+ WHERE name = '#{flag_name}';
26
+ SQL
27
+ end
28
+
29
+ def self.delete_all_disabled
30
+ sql <<-SQL
31
+ DELETE FROM feature_flags
32
+ WHERE enabled = false;
33
+ SQL
34
+ end
35
+
36
+ private
37
+
38
+ def self.enable(flag_name, enabled)
39
+ sql <<-SQL
40
+ INSERT INTO feature_flags (name, enabled, created_at, updated_at)
41
+ VALUES ('#{flag_name}', #{enabled}, now(), now())
42
+ ON CONFLICT (name) DO UPDATE SET enabled = #{enabled};
43
+ SQL
44
+ end
45
+
46
+ def self.sql(query_string)
47
+ ActiveRecord::Base.connection.execute(query_string)
48
+ end
49
+ end
@@ -0,0 +1,21 @@
1
+ require "rails/generators"
2
+ require "rails/generators/migration"
3
+
4
+ module FlaggleRock
5
+ module Generators
6
+ class InstallGenerator < Rails::Generators::Base
7
+ include Rails::Generators::Migration
8
+ source_root File.expand_path("templates", __dir__)
9
+ desc "Generates a migration to create the feature flags table"
10
+
11
+ def self.next_migration_number(path)
12
+ next_migration_number = current_migration_number(path) + 1
13
+ ActiveRecord::Migration.next_migration_number(next_migration_number)
14
+ end
15
+
16
+ def copy_migrations
17
+ migration_template "create_feature_flags.rb", "db/migrate/create_feature_flags.rb"
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,10 @@
1
+ class CreateFeatureFlags < ActiveRecord::Migration[7.0]
2
+ def change
3
+ create_table :feature_flags do |t|
4
+ t.string :name, null: false, index: { unique: true }
5
+ t.boolean :enabled, null: false, default: false
6
+
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flaggle_rock
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - T S Vallender
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-01-26 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: t@tsvallender.co.uk
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - MIT-LICENSE
20
+ - README.md
21
+ - lib/flaggle_rock.rb
22
+ - lib/generators/flaggle_rock/install/install_generator.rb
23
+ - lib/generators/flaggle_rock/install/templates/create_feature_flags.rb
24
+ homepage: https://git.tsvallender.co.uk/tsv/flaggle_rock
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubygems_version: 3.5.5
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: A simple feature flag gem
47
+ test_files: []