setler 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,3 +1,5 @@
1
+ coverage/
2
+ coverage.data
1
3
  *.gem
2
4
  .bundle
3
5
  Gemfile.lock
data/README.rdoc CHANGED
@@ -43,7 +43,7 @@ Set defaults in an initializer with something like:
43
43
  Featureflags.defaults[:bacon_dispenser_enabled] = false
44
44
  Settings.defaults[:allowed_meats] = ['itsnotbacon']
45
45
 
46
- COING SOON! Add them to any ActiveRecord object:
46
+ Add them to any ActiveRecord object:
47
47
 
48
48
  class User < ActiveRecord::Base
49
49
  has_setler as: :settings
@@ -56,6 +56,6 @@ Then you get:
56
56
  user.settings.favorite_meat # :bacon
57
57
  user.settings.all # { "favorite_meat" => :bacon }
58
58
 
59
- And look em up:
59
+ TODO: And look em up:
60
60
 
61
61
  User.with_settings_for('favorite_meat') # => scope of users with the favorite_meat setting
data/Rakefile CHANGED
@@ -7,3 +7,19 @@ Rake::TestTask.new(:test) do |test|
7
7
  end
8
8
 
9
9
  task default: [:test]
10
+
11
+ namespace :cover_me do
12
+ desc "Generates and opens code coverage report."
13
+ task :report do
14
+ require 'cover_me'
15
+ CoverMe.complete!
16
+ end
17
+ end
18
+
19
+ task :test do
20
+ Rake::Task['cover_me:report'].invoke
21
+ end
22
+
23
+ task :spec do
24
+ Rake::Task['cover_me:report'].invoke
25
+ end
@@ -0,0 +1,22 @@
1
+ module Setler
2
+ module ActiveRecord
3
+
4
+ def has_setler(scopename = 'settings')
5
+ define_method scopename do
6
+ Setler::ScopedSettings.for_thing(self)
7
+ end
8
+
9
+ define_method "#{scopename}=" do |hash|
10
+ hash.each do |key,value|
11
+ Setler::Settings.find_or_create_by_thing_id_and_thing_type_and_var(
12
+ self.id,
13
+ self.class,
14
+ key,
15
+ value: value
16
+ )
17
+ end
18
+ end
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,4 @@
1
+ module Setler
2
+ class SettingNotFound < StandardError
3
+ end
4
+ end
@@ -0,0 +1,13 @@
1
+ module Setler
2
+ class ScopedSettings < Settings
3
+ def self.for_thing(object)
4
+ @object = object
5
+ self
6
+ end
7
+
8
+ def self.thing_scoped
9
+ Setler::Settings.where(thing_type: @object.class.base_class.to_s, thing_id: @object.id)
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,38 @@
1
+ module Setler
2
+ class Settings < ActiveRecord::Base
3
+ serialize :value
4
+ self.abstract_class = true
5
+
6
+ cattr_accessor :defaults
7
+ @@defaults = {}.with_indifferent_access
8
+
9
+ # Get and Set variables when the calling method is the variable name
10
+ def self.method_missing(method, *args, &block)
11
+ if respond_to?(method)
12
+ super(method, *args, &block)
13
+ else
14
+ method_name = method.to_s
15
+ if method_name.ends_with?("=")
16
+ self.find_or_create_by_var(method_name[0..-2]).update_attribute(:value, args.first)
17
+ else
18
+ self.find_by_var(method_name).try(:value) || @@defaults[method_name]
19
+ end
20
+ end
21
+ end
22
+
23
+ def self.destroy(var_name)
24
+ var_name = var_name.to_s
25
+ if setting = self.find_by_var(var_name)
26
+ setting.destroy
27
+ true
28
+ else
29
+ raise SettingNotFound, "Setting variable \"#{var_name}\" not found"
30
+ end
31
+ end
32
+
33
+ def self.all
34
+ Hash[super.collect{ |s| [s.var, s.value] }].merge(@@defaults)
35
+ end
36
+ end
37
+
38
+ end
@@ -1,3 +1,3 @@
1
1
  module Setler
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/setler.rb CHANGED
@@ -1,39 +1,9 @@
1
- require File.join(File.dirname(__FILE__), 'setler', 'version')
2
- require 'pp'
1
+ require 'active_record'
3
2
 
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
3
+ require_relative 'setler/version'
4
+ require_relative 'setler/exceptions'
5
+ require_relative 'setler/settings'
6
+ require_relative 'setler/scoped_settings'
7
+ require_relative 'setler/active_record'
8
+
9
+ ::ActiveRecord::Base.extend Setler::ActiveRecord
data/setler.gemspec CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
7
7
  s.version = Setler::VERSION
8
8
  s.authors = ["Chris Kelly"]
9
9
  s.email = ["ckdake@ckdake.com"]
10
- s.homepage = ""
10
+ s.homepage = "https://github.com/ckdake/setler"
11
11
  s.summary = %q{Settler lets you use the 'Feature Flags' pettern or add settings to models.}
12
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
13
 
@@ -18,7 +18,10 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
+ s.add_dependency('activerecord', '>=3.0.0')
22
+ s.add_dependency('rails', '>=3.0.0')
23
+
21
24
  s.add_development_dependency('sqlite3')
22
- s.add_development_dependency('activerecord')
23
25
  s.add_development_dependency('rake')
26
+ s.add_development_dependency('cover_me')
24
27
  end
@@ -63,4 +63,36 @@ class Setler::SettingsTest < Test::Unit::TestCase
63
63
  Setler::Settings.destroy :test
64
64
  assert_nil Setler::Settings.test
65
65
  end
66
- end
66
+
67
+ def test_user_has_setler
68
+ user = User.create name: 'user 1'
69
+ assert_nil user.preferences.likes_bacon
70
+ user.preferences.likes_bacon = true
71
+ assert user.preferences.likes_bacon
72
+ user.preferences.destroy :likes_bacon
73
+ assert_nil user.preferences.likes_bacon
74
+ end
75
+
76
+ def test_user_settings_all
77
+ user = User.create name: 'user 1'
78
+ assert_equal user.preferences.all, Setler::Settings.all
79
+ user.preferences.likes_bacon = true
80
+ user.preferences.really_likes_bacon = true
81
+ assert user.preferences.all['likes_bacon']
82
+ assert user.preferences.all['really_likes_bacon']
83
+ end
84
+
85
+ # def test_user_has_settings_for
86
+ # user1 = User.create name: 'awesome user'
87
+ # user2 = User.create name: 'bad user'
88
+ # user1.preferences.likes_bacon = true
89
+ # assert_equal 1, User.with_settings_for('likes_bacon').count
90
+ # assert_equal user1, User.with_settings_for('likes_bacon').first
91
+ # end
92
+
93
+ def test_destroy_when_setting_does_not_exist
94
+ assert_raise Setler::SettingNotFound do
95
+ Setler::Settings.destroy :not_a_setting
96
+ end
97
+ end
98
+ end
data/test/test_helper.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'cover_me'
1
2
  require 'rubygems'
2
3
 
3
4
  require 'bundler'
@@ -12,13 +13,13 @@ end
12
13
  require 'active_record'
13
14
  require 'test/unit'
14
15
 
15
- require File.join(File.dirname(__FILE__), '..', 'lib', 'setler.rb')
16
+ require_relative '../lib/setler'
16
17
 
17
18
  ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
18
19
 
19
- # class User < ActiveRecord::Base
20
- # has_settings
21
- # end
20
+ class User < ActiveRecord::Base
21
+ has_setler :preferences
22
+ end
22
23
 
23
24
  def setup_db
24
25
  ActiveRecord::Schema.define(:version => 1) do
@@ -31,8 +32,8 @@ def setup_db
31
32
  end
32
33
  add_index :settings, [ :target_type, :target_id, :var ], :unique => true
33
34
 
34
- # create_table :users do |t|
35
- # t.string :name
36
- # end
35
+ create_table :users do |t|
36
+ t.string :name
37
+ end
37
38
  end
38
- end
39
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: setler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,33 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-05 00:00:00.000000000Z
12
+ date: 2011-08-08 00:00:00.000000000Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: &70243758967160 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70243758967160
25
+ - !ruby/object:Gem::Dependency
26
+ name: rails
27
+ requirement: &70243758966660 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 3.0.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70243758966660
14
36
  - !ruby/object:Gem::Dependency
15
37
  name: sqlite3
16
- requirement: &70360670678960 !ruby/object:Gem::Requirement
38
+ requirement: &70243758966280 !ruby/object:Gem::Requirement
17
39
  none: false
18
40
  requirements:
19
41
  - - ! '>='
@@ -21,10 +43,10 @@ dependencies:
21
43
  version: '0'
22
44
  type: :development
23
45
  prerelease: false
24
- version_requirements: *70360670678960
46
+ version_requirements: *70243758966280
25
47
  - !ruby/object:Gem::Dependency
26
- name: activerecord
27
- requirement: &70360670678540 !ruby/object:Gem::Requirement
48
+ name: rake
49
+ requirement: &70243758965820 !ruby/object:Gem::Requirement
28
50
  none: false
29
51
  requirements:
30
52
  - - ! '>='
@@ -32,10 +54,10 @@ dependencies:
32
54
  version: '0'
33
55
  type: :development
34
56
  prerelease: false
35
- version_requirements: *70360670678540
57
+ version_requirements: *70243758965820
36
58
  - !ruby/object:Gem::Dependency
37
- name: rake
38
- requirement: &70360670678120 !ruby/object:Gem::Requirement
59
+ name: cover_me
60
+ requirement: &70243758965400 !ruby/object:Gem::Requirement
39
61
  none: false
40
62
  requirements:
41
63
  - - ! '>='
@@ -43,7 +65,7 @@ dependencies:
43
65
  version: '0'
44
66
  type: :development
45
67
  prerelease: false
46
- version_requirements: *70360670678120
68
+ version_requirements: *70243758965400
47
69
  description: Setler is a Gem that lets one easily implement the "Feature Flags" pattern,
48
70
  or add settings to individual models. This is a cleanroom implementation of what
49
71
  the 'rails-settings' gem does. It's been forked all over the place, and my favorite
@@ -65,11 +87,15 @@ files:
65
87
  - lib/generators/setler/templates/migration.rb
66
88
  - lib/generators/setler/templates/model.rb
67
89
  - lib/setler.rb
90
+ - lib/setler/active_record.rb
91
+ - lib/setler/exceptions.rb
92
+ - lib/setler/scoped_settings.rb
93
+ - lib/setler/settings.rb
68
94
  - lib/setler/version.rb
69
95
  - setler.gemspec
70
96
  - test/settings_test.rb
71
97
  - test/test_helper.rb
72
- homepage: ''
98
+ homepage: https://github.com/ckdake/setler
73
99
  licenses: []
74
100
  post_install_message:
75
101
  rdoc_options: []