setler 0.0.12 → 0.0.13

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MDlmODVjNzM5ZWZiMzY3Njk4YjJkMWM4YTZiMTQyMDEyYTRkYzU3MA==
5
+ data.tar.gz: !binary |-
6
+ MWM0MTJjNWE2YmYyZmE4MWM1NGUyYWZkNzllNTM3YzgxMTQ4NzdiNg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ NDVjNDRlNjU0MDZjZWJiODdkNzNiY2YyNGQ2MDBhMTBmZjE3NzJiYTcxMjc0
10
+ NmFkNzRhZTZhOTllZTU2MTk5MDZkZDBmOTBiZjNhMTdiNDQ0NjQ5MGQ0ZDg4
11
+ NTcwOWY2ZWFiMGM5YjU4MGM0MDg1MzhjMGVhZjQ3YWVhZTcxMGM=
12
+ data.tar.gz: !binary |-
13
+ MzdlNDgwMGZlNzYxMGQ3ODQ5Y2Q2Y2JkMDNiNWIwNmYyY2FhMjYzMmE4MWYw
14
+ OTY0MDE3MDVhNjVmYWM3Mjc1OWEwOGUzNDVjYmJmZWViOTY3NjYzYjczMmEw
15
+ M2NkNDZhNDQ4ZmViOGNiMzJjZTc2MmE3MjIwOTY1ZDM1NmM2Mzk=
@@ -0,0 +1 @@
1
+ setler
@@ -0,0 +1 @@
1
+ ruby-1.9.3-p547
@@ -0,0 +1,8 @@
1
+ rvm:
2
+ - 1.9.3
3
+ - 2.1.2
4
+ cache: bundler
5
+ script:
6
+ - bundle exec rake test
7
+ notifications:
8
+ email: false
@@ -2,20 +2,24 @@
2
2
 
3
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
4
 
5
+ {<img src="https://travis-ci.org/ckdake/setler.svg?branch=master" alt="Build Status" />}[https://travis-ci.org/ckdake/setler]
6
+
5
7
  == Setup
6
8
 
7
9
  Install the gem by adding this to your Gemfile:
8
10
 
9
11
  gem "setler"
10
-
12
+
11
13
  Generate the model:
12
14
 
13
15
  rails g setler <model_name>
14
-
16
+
15
17
  Run the migration:
16
18
 
17
19
  rake db:migrate
18
-
20
+
21
+ If you are using the `protected_attributes` gem you must add `attr_protected` to the top of you setler model.
22
+
19
23
  == Usage
20
24
 
21
25
  Create/Update settings:
@@ -23,22 +27,22 @@ Create/Update settings:
23
27
  # Method calls and []= are synonymous
24
28
  Featureflags.bacon_dispenser_enabled = true
25
29
  Settings[:allowed_meats] = ['bacon', 'crunchy bacon']
26
-
30
+
27
31
  Read settings:
28
32
 
29
33
  Featureflags.bacon_dispenser_enabled # true
30
34
  Settings[:allowed_meats].include?('bacon') # true
31
-
35
+
32
36
  Destroy them:
33
37
 
34
38
  Featureflags.destroy :bacon_dispenser_enabled
35
39
  Settings.destroy :allowed_meats
36
-
40
+
37
41
  List all settings:
38
42
 
39
43
  Featureflags.all_settings
40
44
  Settings.all_settings
41
-
45
+
42
46
  Set defaults in an initializer with something like:
43
47
 
44
48
  Featureflags.defaults[:bacon_dispenser_enabled] = false
@@ -46,20 +50,20 @@ Set defaults in an initializer with something like:
46
50
 
47
51
  To revert to the default after changing a setting, destroy it.
48
52
  Note: Updating the setting to nil or false no longer makes it the default setting (> 0.0.6), but changes the setting to nil or false.
49
-
53
+
50
54
  Add them to any ActiveRecord object:
51
55
 
52
56
  class User < ActiveRecord::Base
53
- has_setler as: :settings
57
+ has_setler :settings
54
58
  end
55
-
59
+
56
60
  Then you get:
57
-
61
+
58
62
  user = User.first
59
63
  user.settings.favorite_meat = :bacon
60
64
  user.settings.favorite_meat # :bacon
61
65
  user.settings.all # { "favorite_meat" => :bacon }
62
-
66
+
63
67
  TODO: And look em up:
64
68
 
65
69
  User.with_settings_for('favorite_meat') # => scope of users with the favorite_meat setting
@@ -71,7 +75,7 @@ Getting started is pretty straightforward:
71
75
  1. Check out the code: `git clone git://github.com/ckdake/setler.git`
72
76
  2. Bundle install: `bundle install`
73
77
  3. Run the tests and make sure they all pass and code coverage is still 100%: `rake test`
74
-
78
+
75
79
  If you'd like to contribute code, make your changes and submit a pull request that includes appropriate tests
76
80
 
77
81
  For building the gem: `rake build` and to release a gem to github and Rubygems: `rake release`
data/Rakefile CHANGED
@@ -1,3 +1,4 @@
1
+ require 'rake'
1
2
  require 'rake/testtask'
2
3
  require 'bundler/gem_tasks'
3
4
 
@@ -8,19 +9,3 @@ Rake::TestTask.new(:test) do |test|
8
9
  end
9
10
 
10
11
  task default: [:test]
11
-
12
- namespace :cover_me do
13
- desc "Generates and opens code coverage report."
14
- task :report do
15
- require 'cover_me'
16
- CoverMe.complete!
17
- end
18
- end
19
-
20
- task :test do
21
- Rake::Task['cover_me:report'].invoke
22
- end
23
-
24
- task :spec do
25
- Rake::Task['cover_me:report'].invoke
26
- end
@@ -2,6 +2,7 @@ module Setler
2
2
  class ScopedSettings < Settings
3
3
  def self.for_thing(object, scopename)
4
4
  self.table_name = scopename
5
+ self.defaults = settings_constantize(scopename).defaults
5
6
  @object = object
6
7
  self
7
8
  end
@@ -10,5 +11,11 @@ module Setler
10
11
  self.base_class.where(thing_type: @object.class.base_class.to_s, thing_id: @object.id)
11
12
  end
12
13
 
14
+ # do not use rails default to singularize because setler examples
15
+ # user plural class names
16
+ def self.settings_constantize(scopename)
17
+ Object.const_get(scopename.to_s.camelize)
18
+ end
19
+
13
20
  end
14
21
  end
@@ -5,8 +5,13 @@ module Setler
5
5
  serialize :value
6
6
  self.abstract_class = true
7
7
 
8
- cattr_accessor :defaults
9
- @@defaults = {}.with_indifferent_access
8
+ def self.defaults
9
+ @defaults ||= {}.with_indifferent_access
10
+ end
11
+
12
+ def self.defaults=(defaults)
13
+ @defaults = defaults.with_indifferent_access
14
+ end
10
15
 
11
16
  if Rails::VERSION::MAJOR == 3
12
17
  attr_accessible :var, :value
@@ -35,7 +40,7 @@ module Setler
35
40
 
36
41
  def self.[](var)
37
42
  the_setting = thing_scoped.find_by_var(var.to_s)
38
- the_setting.present? ? the_setting.value : @@defaults[var]
43
+ the_setting.present? ? the_setting.value : defaults[var]
39
44
  end
40
45
 
41
46
  def self.[]=(var, value)
@@ -43,10 +48,10 @@ module Setler
43
48
  # thing_scoped.find_or_create_by_var(method_name[0..-2]) should work but doesnt for some reason
44
49
  # When @object is present, thing_scoped sets the where scope for the polymorphic association
45
50
  # but the find_or_create_by wasn't using the thing_type and thing_id
46
- thing_scoped.find_or_create_by_var_and_thing_type_and_thing_id(
47
- var.to_s,
48
- @object.try(:class).try(:base_class).try(:to_s),
49
- @object.try(:id)
51
+ thing_scoped.find_or_create_by(
52
+ var: var.to_s,
53
+ thing_type: @object.try(:class).try(:base_class).try(:to_s),
54
+ thing_id: @object.try(:id)
50
55
  ).update_attributes({ :value => value })
51
56
  end
52
57
 
@@ -61,7 +66,7 @@ module Setler
61
66
  end
62
67
 
63
68
  def self.all_settings
64
- @@defaults.merge(Hash[thing_scoped.all.collect{ |s| [s.var, s.value] }])
69
+ defaults.merge(Hash[thing_scoped.all.collect{ |s| [s.var, s.value] }])
65
70
  end
66
71
 
67
72
  def self.thing_scoped
@@ -1,3 +1,3 @@
1
1
  module Setler
2
- VERSION = "0.0.12"
2
+ VERSION = "0.0.13"
3
3
  end
@@ -23,5 +23,5 @@ Gem::Specification.new do |s|
23
23
 
24
24
  s.add_development_dependency('sqlite3')
25
25
  s.add_development_dependency('rake')
26
- s.add_development_dependency('cover_me')
26
+ s.add_development_dependency('minitest')
27
27
  end
@@ -1,6 +1,6 @@
1
1
  require 'test_helper'
2
2
 
3
- class ::SettingsTest < Test::Unit::TestCase
3
+ class ::SettingsTest < Minitest::Test
4
4
  setup_db
5
5
 
6
6
  def setup
@@ -10,7 +10,10 @@ class ::SettingsTest < Test::Unit::TestCase
10
10
 
11
11
  def teardown
12
12
  ::Settings.delete_all
13
+ ::Settings.defaults = {}.with_indifferent_access
14
+
13
15
  ::Preferences.delete_all
16
+ ::Preferences.defaults = {}.with_indifferent_access
14
17
  end
15
18
 
16
19
  def test_defaults
@@ -91,7 +94,7 @@ class ::SettingsTest < Test::Unit::TestCase
91
94
  end
92
95
 
93
96
  def test_destroy
94
- assert_not_nil ::Settings.test
97
+ refute_nil ::Settings.test
95
98
  ::Settings.destroy :test
96
99
  assert_nil ::Settings.test
97
100
  end
@@ -155,7 +158,7 @@ class ::SettingsTest < Test::Unit::TestCase
155
158
  # end
156
159
 
157
160
  def test_destroy_when_setting_does_not_exist
158
- assert_raise Setler::SettingNotFound do
161
+ assert_raises Setler::SettingNotFound do
159
162
  ::Settings.destroy :not_a_setting
160
163
  end
161
164
  end
@@ -164,11 +167,17 @@ class ::SettingsTest < Test::Unit::TestCase
164
167
  ::Preferences.create var: 'test', value: 'preferences foo'
165
168
  ::Preferences.create var: 'test2', value: 'preferences bar'
166
169
 
167
- assert_not_equal ::Settings.defaults, ::Preferences.defaults
170
+ refute_match ::Settings.all_settings, ::Preferences.all_settings
168
171
 
169
172
  assert_equal 'foo', ::Settings[:test]
170
173
  assert_equal 'bar', ::Settings[:test2]
171
174
  assert_equal 'preferences foo', ::Preferences[:test]
172
175
  assert_equal 'preferences bar', ::Preferences[:test2]
173
176
  end
177
+
178
+ def test_defaults_are_independent
179
+ ::Settings.defaults[:foo] = false
180
+
181
+ refute_equal ::Settings.defaults, ::Preferences.defaults
182
+ end
174
183
  end
@@ -1,4 +1,3 @@
1
- require 'cover_me'
2
1
  require 'rubygems'
3
2
 
4
3
  require 'bundler'
@@ -10,8 +9,9 @@ rescue Bundler::BundlerError => e
10
9
  exit e.status_code
11
10
  end
12
11
 
12
+ require 'rails'
13
13
  require 'active_record'
14
- require 'test/unit'
14
+ require 'minitest/autorun'
15
15
 
16
16
  require_relative '../lib/setler'
17
17
 
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: setler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
5
- prerelease:
4
+ version: 0.0.13
6
5
  platform: ruby
7
6
  authors:
8
7
  - Chris Kelly
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-09-04 00:00:00.000000000 Z
11
+ date: 2014-06-26 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activerecord
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ! '>='
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ! '>='
28
25
  - !ruby/object:Gem::Version
@@ -30,7 +27,6 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rails
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ! '>='
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ! '>='
44
39
  - !ruby/object:Gem::Version
@@ -46,7 +41,6 @@ dependencies:
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: sqlite3
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
45
  - - ! '>='
52
46
  - !ruby/object:Gem::Version
@@ -54,7 +48,6 @@ dependencies:
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
52
  - - ! '>='
60
53
  - !ruby/object:Gem::Version
@@ -62,7 +55,6 @@ dependencies:
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: rake
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
59
  - - ! '>='
68
60
  - !ruby/object:Gem::Version
@@ -70,15 +62,13 @@ dependencies:
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
66
  - - ! '>='
76
67
  - !ruby/object:Gem::Version
77
68
  version: '0'
78
69
  - !ruby/object:Gem::Dependency
79
- name: cover_me
70
+ name: minitest
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
73
  - - ! '>='
84
74
  - !ruby/object:Gem::Version
@@ -86,7 +76,6 @@ dependencies:
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
80
  - - ! '>='
92
81
  - !ruby/object:Gem::Version
@@ -103,7 +92,9 @@ extensions: []
103
92
  extra_rdoc_files: []
104
93
  files:
105
94
  - .gitignore
106
- - .rvmrc
95
+ - .ruby-gemset
96
+ - .ruby-version
97
+ - .travis.yml
107
98
  - Gemfile
108
99
  - MIT-LICENSE
109
100
  - README.rdoc
@@ -123,33 +114,26 @@ files:
123
114
  - test/test_helper.rb
124
115
  homepage: https://github.com/ckdake/setler
125
116
  licenses: []
117
+ metadata: {}
126
118
  post_install_message:
127
119
  rdoc_options: []
128
120
  require_paths:
129
121
  - lib
130
122
  required_ruby_version: !ruby/object:Gem::Requirement
131
- none: false
132
123
  requirements:
133
124
  - - ! '>='
134
125
  - !ruby/object:Gem::Version
135
126
  version: '0'
136
- segments:
137
- - 0
138
- hash: 153021876405387793
139
127
  required_rubygems_version: !ruby/object:Gem::Requirement
140
- none: false
141
128
  requirements:
142
129
  - - ! '>='
143
130
  - !ruby/object:Gem::Version
144
131
  version: '0'
145
- segments:
146
- - 0
147
- hash: 153021876405387793
148
132
  requirements: []
149
133
  rubyforge_project: setler
150
- rubygems_version: 1.8.25
134
+ rubygems_version: 2.2.2
151
135
  signing_key:
152
- specification_version: 3
136
+ specification_version: 4
153
137
  summary: Settler lets you use the 'Feature Flags' pettern or add settings to models.
154
138
  test_files:
155
139
  - test/settings_test.rb
data/.rvmrc DELETED
@@ -1 +0,0 @@
1
- rvm --create 1.9.2@setler