setler 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.2@setler
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in setler.gemspec
4
+ gemspec
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Chris Kelly <ckdake@ckdake.com>
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.
@@ -0,0 +1,61 @@
1
+ = Setler
2
+
3
+ Setler is a Gem that lets one easily implement the "Feature Flags" pattern, or add settings to individual models. This is a cleanroom implementation of what the 'rails-settings' gem does. It's been forked all over the place, and my favorite version of it doesn't have any tests and doesn't work with settings associated with models.
4
+
5
+ == Setup
6
+
7
+ Install the gem by adding this to your Gemfile:
8
+
9
+ gem "setler", git: "git://github.com/ckdake/setler.git"
10
+
11
+ Generate the model:
12
+
13
+ rails g setler <model_name>
14
+
15
+ Run the migration:
16
+
17
+ rake db:migrate
18
+
19
+ == Usage
20
+
21
+ Create/Update settings:
22
+
23
+ Featureflags.bacon_dispenser_enabled = true
24
+ Settings.allowed_meats = ['bacon', 'crunchy bacon']
25
+
26
+ Read settings:
27
+
28
+ Featureflags.bacon_dispenser_enabled # true
29
+ Settings.allowed_meats.include?('bacon') # true
30
+
31
+ Destroy them:
32
+
33
+ Featureflags.destroy :bacon_dispenser_enabled
34
+ Settings.destroy :allowed_meats
35
+
36
+ List all:
37
+
38
+ Featureflags.all
39
+ Settings.all
40
+
41
+ Set defaults in an initializer with something like:
42
+
43
+ Featureflags.defaults[:bacon_dispenser_enabled] = false
44
+ Settings.defaults[:allowed_meats] = ['itsnotbacon']
45
+
46
+ COING SOON! Add them to any ActiveRecord object:
47
+
48
+ class User < ActiveRecord::Base
49
+ has_setler as: :settings
50
+ end
51
+
52
+ Then you get:
53
+
54
+ user = User.first
55
+ user.settings.favorite_meat = :bacon
56
+ user.settings.favorite_meat # :bacon
57
+ user.settings.all # { "favorite_meat" => :bacon }
58
+
59
+ And look em up:
60
+
61
+ User.with_settings_for('favorite_meat') # => scope of users with the favorite_meat setting
@@ -0,0 +1,9 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new(:test) do |test|
4
+ test.libs << 'lib' << 'test'
5
+ test.pattern = 'test/**/*_test.rb'
6
+ test.verbose = true
7
+ end
8
+
9
+ task default: [:test]
@@ -0,0 +1,29 @@
1
+ require 'rails/generators/migration'
2
+
3
+ class SetlerGenerator < Rails::Generators::NamedBase
4
+ include Rails::Generators::Migration
5
+
6
+ argument :name, type: :string, default: "settings"
7
+
8
+ source_root File.expand_path('../templates', __FILE__)
9
+
10
+ @@migrations = false
11
+
12
+ def self.next_migration_number(dirname)
13
+ if ActiveRecord::Base.timestamped_migrations
14
+ if @@migrations
15
+ (current_migration_number(dirname) + 1)
16
+ else
17
+ @@migrations = true
18
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
19
+ end
20
+ else
21
+ "%.3d" % (current_migration_number(dirname) + 1)
22
+ end
23
+ end
24
+
25
+ def generate_model
26
+ template "model.rb", File.join("app/models",class_path,"#{file_name}.rb"), force: true
27
+ migration_template "migration.rb", "db/migrate/setler_create_#{table_name}.rb"
28
+ end
29
+ end
@@ -0,0 +1,17 @@
1
+ class SetlerCreate<%= table_name.camelize %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table(:<%= table_name %>) do |t|
4
+ t.string :var, null: false
5
+ t.text :value, null: true
6
+ t.integer :thing_id, null: true
7
+ t.string :thing_type, limit: 30, null: true
8
+ t.timestamps
9
+ end
10
+
11
+ add_index :<%= table_name %>, [ :thing_type, :thing_id, :var ], unique: true
12
+ end
13
+
14
+ def self.down
15
+ drop_table :<%= table_name %>
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ class <%= class_name %> < Setler::Settings
2
+
3
+ end
@@ -0,0 +1,39 @@
1
+ require File.join(File.dirname(__FILE__), 'setler', 'version')
2
+ require 'pp'
3
+
4
+ module Setler
5
+ class Settings < ActiveRecord::Base
6
+ serialize :value
7
+ self.abstract_class = true
8
+
9
+ cattr_accessor :defaults
10
+ @@defaults = {}.with_indifferent_access
11
+
12
+ # Get and Set variables when the calling method is the variable name
13
+ def self.method_missing(method, *args)
14
+ method_name = method.to_s
15
+ super(method, *args)
16
+
17
+ rescue NoMethodError
18
+ if method_name =~ /=$/
19
+ self.find_or_create_by_var(method_name.gsub('=', '')).update_attribute(:value, args.first)
20
+ else
21
+ self.find_by_var(method_name).try(:value) || @@defaults[method_name]
22
+ end
23
+ end
24
+
25
+ def self.destroy(var_name)
26
+ var_name = var_name.to_s
27
+ if setting = self.find_by_var(var_name)
28
+ setting.destroy
29
+ true
30
+ else
31
+ raise SettingNotFound, "Setting variable \"#{var_name}\" not found"
32
+ end
33
+ end
34
+
35
+ def self.all
36
+ Hash[super.collect{ |s| [s.var, s.value] }].merge(@@defaults)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module Setler
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "setler/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "setler"
7
+ s.version = Setler::VERSION
8
+ s.authors = ["Chris Kelly"]
9
+ s.email = ["ckdake@ckdake.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Settler lets you use the 'Feature Flags' pettern or add settings to models.}
12
+ s.description = %q{Setler is a Gem that lets one easily implement the "Feature Flags" pattern, or add settings to individual models. This is a cleanroom implementation of what the 'rails-settings' gem does. It's been forked all over the place, and my favorite version of it doesn't have any tests and doesn't work with settings associated with models.}
13
+
14
+ s.rubyforge_project = "setler"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency('sqlite3')
22
+ s.add_development_dependency('activerecord')
23
+ s.add_development_dependency('rake')
24
+ end
@@ -0,0 +1,66 @@
1
+ require 'test_helper'
2
+
3
+ class Setler::SettingsTest < Test::Unit::TestCase
4
+ setup_db
5
+
6
+ def setup
7
+ Setler::Settings.create(:var => 'test', :value => 'foo')
8
+ Setler::Settings.create(:var => 'test2', :value => 'bar')
9
+ end
10
+
11
+ def teardown
12
+ Setler::Settings.delete_all
13
+ end
14
+
15
+ def test_defaults
16
+ Setler::Settings.defaults[:foo] = 'default foo'
17
+
18
+ assert_equal 'default foo', Setler::Settings.foo
19
+
20
+ Setler::Settings.foo = 'bar'
21
+ assert_equal 'bar', Setler::Settings.foo
22
+ end
23
+
24
+ def tests_defaults_false
25
+ Setler::Settings.defaults[:foo] = false
26
+ assert_equal false, Setler::Settings.foo
27
+ end
28
+
29
+ def test_get
30
+ assert_equal 'foo', Setler::Settings.test
31
+ assert_equal 'bar', Setler::Settings.test2
32
+ end
33
+
34
+ def test_update
35
+ Setler::Settings.test = '321'
36
+ assert_equal '321', Setler::Settings.test
37
+ end
38
+
39
+ def test_create
40
+ Setler::Settings.onetwothree = '123'
41
+ assert_equal '123', Setler::Settings.onetwothree
42
+ end
43
+
44
+ def test_complex_serialization
45
+ complex = [1, '2', {:three => true}]
46
+ Setler::Settings.complex = complex
47
+ assert_equal complex, Setler::Settings.complex
48
+ end
49
+
50
+ def test_serialization_of_float
51
+ Setler::Settings.float = 0.01
52
+ Setler::Settings.reload
53
+ assert_equal 0.01, Setler::Settings.float
54
+ assert_equal 0.02, Setler::Settings.float * 2
55
+ end
56
+
57
+ def test_all
58
+ assert_equal({ "test2" => "bar", "test" => "foo" }, Setler::Settings.all)
59
+ end
60
+
61
+ def test_destroy
62
+ assert_not_nil Setler::Settings.test
63
+ Setler::Settings.destroy :test
64
+ assert_nil Setler::Settings.test
65
+ end
66
+ end
@@ -0,0 +1,38 @@
1
+ require 'rubygems'
2
+
3
+ require 'bundler'
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts "Run `bundle install` to install missing gems"
9
+ exit e.status_code
10
+ end
11
+
12
+ require 'active_record'
13
+ require 'test/unit'
14
+
15
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'setler.rb')
16
+
17
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
18
+
19
+ # class User < ActiveRecord::Base
20
+ # has_settings
21
+ # end
22
+
23
+ def setup_db
24
+ ActiveRecord::Schema.define(:version => 1) do
25
+ create_table :settings do |t|
26
+ t.string :var, :null => false
27
+ t.text :value, :null => true
28
+ t.integer :target_id, :null => true
29
+ t.string :target_type, :limit => 30, :null => true
30
+ t.timestamps
31
+ end
32
+ add_index :settings, [ :target_type, :target_id, :var ], :unique => true
33
+
34
+ # create_table :users do |t|
35
+ # t.string :name
36
+ # end
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: setler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chris Kelly
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-05 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sqlite3
16
+ requirement: &70360670678960 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70360670678960
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ requirement: &70360670678540 !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: *70360670678540
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &70360670678120 !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: *70360670678120
47
+ description: Setler is a Gem that lets one easily implement the "Feature Flags" pattern,
48
+ or add settings to individual models. This is a cleanroom implementation of what
49
+ the 'rails-settings' gem does. It's been forked all over the place, and my favorite
50
+ version of it doesn't have any tests and doesn't work with settings associated with
51
+ models.
52
+ email:
53
+ - ckdake@ckdake.com
54
+ executables: []
55
+ extensions: []
56
+ extra_rdoc_files: []
57
+ files:
58
+ - .gitignore
59
+ - .rvmrc
60
+ - Gemfile
61
+ - MIT-LICENSE
62
+ - README.rdoc
63
+ - Rakefile
64
+ - lib/generators/setler/setler_generator.rb
65
+ - lib/generators/setler/templates/migration.rb
66
+ - lib/generators/setler/templates/model.rb
67
+ - lib/setler.rb
68
+ - lib/setler/version.rb
69
+ - setler.gemspec
70
+ - test/settings_test.rb
71
+ - test/test_helper.rb
72
+ homepage: ''
73
+ licenses: []
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project: setler
92
+ rubygems_version: 1.8.6
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Settler lets you use the 'Feature Flags' pettern or add settings to models.
96
+ test_files:
97
+ - test/settings_test.rb
98
+ - test/test_helper.rb