ledermann-rails-settings 1.0.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml CHANGED
@@ -1,9 +1,9 @@
1
+ language: ruby
1
2
  rvm:
2
3
  - 1.8.7
3
- - 1.9.2
4
4
  - 1.9.3
5
- - rbx-2.0
6
5
  gemfile:
7
6
  - ci/Gemfile.rails-2.3.x
8
- - ci/Gemfile.rails-3.0.x
7
+ # - ci/Gemfile.rails-3.0.x
9
8
  - ci/Gemfile.rails-3.1.x
9
+ - ci/Gemfile.rails-3.2.x
data/Changelog.md ADDED
@@ -0,0 +1,13 @@
1
+ Version 1.1.0 (2012-06-01)
2
+
3
+ - Added caching (thanks to Matthew McEachen)
4
+
5
+ Version 1.0.1 (2011-11-05)
6
+
7
+ - Gemspec: Fixed missing dependency
8
+
9
+ Version 1.0.0 (2011-11-05)
10
+
11
+ - Conversion from Plugin to Gem
12
+ - Destroying false values (thanks to @Pavling)
13
+ - Testing with Travis
data/MIT-LICENSE CHANGED
@@ -1,5 +1,5 @@
1
1
  Copyright (c) 2006 Alex Wayne
2
- Some additional features added 2009-2011 by Georg Ledermann
2
+ Some additional features added 2009-2012 by Georg Ledermann
3
3
 
4
4
  Permission is hereby granted, free of charge, to any person obtaining
5
5
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -7,9 +7,9 @@ Settings is a gem/plugin that makes managing a table of key/value pairs easy. Th
7
7
 
8
8
  ## Requirements
9
9
 
10
- ActiveRecord 2.3.x, 3.0.x or 3.1.x
10
+ Rails 2.3.x, 3.1.x or 3.2.x (due to an [issue with Rails caching](https://github.com/rails/rails/pull/2010) it does not work properly with Rails 3.0.x)
11
11
 
12
- Tested with Ruby 1.8.7, 1.9.2, 1.9.3 and RBX2.0
12
+ Tested with Ruby 1.8.7 and 1.9.3
13
13
 
14
14
 
15
15
  ## Installation
@@ -132,4 +132,9 @@ I you want to find users having or not having some settings, there are named sco
132
132
  User.without_settings('color')
133
133
  # returns a scope of users having no 'color' setting (means user.settings.color == nil)
134
134
 
135
+ For better performance, you can enable caching, e.g.:
136
+
137
+ Settings.cache = ActiveSupport::Cache::MemoryStore.new
138
+ Settings.cache_options = { :expires_in => 5.minutes }
139
+
135
140
  That's all there is to it! Enjoy!
@@ -1,5 +1,5 @@
1
1
  source :rubygems
2
2
 
3
- gem 'activerecord', '~> 3.0.10'
3
+ gem 'activerecord', '~> 3.0.12'
4
4
  gem 'sqlite3'
5
5
  gem 'rake'
@@ -1,5 +1,5 @@
1
1
  source :rubygems
2
2
 
3
- gem 'activerecord', '~> 3.1.1'
3
+ gem 'activerecord', '~> 3.1.4'
4
4
  gem 'sqlite3'
5
5
  gem 'rake'
@@ -0,0 +1,5 @@
1
+ source :rubygems
2
+
3
+ gem 'activerecord', '~> 3.2.2'
4
+ gem 'sqlite3'
5
+ gem 'rake'
@@ -1,4 +1,5 @@
1
- require 'rails-settings/version'
2
- require 'rails-settings/active_record'
3
- require 'rails-settings/settings'
1
+ require 'rails-settings/version'
2
+ require 'rails-settings/null_store'
3
+ require 'rails-settings/active_record'
4
+ require 'rails-settings/settings'
4
5
  require 'rails-settings/scoped_settings'
@@ -0,0 +1,48 @@
1
+ unless defined?(ActiveSupport::Cache::NullStore)
2
+ # Copied from Rails 3
3
+ # https://github.com/rails/rails/blob/master/activesupport/lib/active_support/cache/null_store.rb
4
+ module ActiveSupport
5
+ module Cache
6
+ # A cache store implementation which doesn't actually store anything. Useful in
7
+ # development and test environments where you don't want caching turned on but
8
+ # need to go through the caching interface.
9
+ #
10
+ # This cache does implement the local cache strategy, so values will actually
11
+ # be cached inside blocks that utilize this strategy. See
12
+ # ActiveSupport::Cache::Strategy::LocalCache for more details.
13
+ class NullStore < Store
14
+ def initialize(options = nil)
15
+ super()
16
+ extend Strategy::LocalCache
17
+ end
18
+
19
+ def clear(options = nil)
20
+ end
21
+
22
+ def cleanup(options = nil)
23
+ end
24
+
25
+ def increment(name, amount = 1, options = nil)
26
+ end
27
+
28
+ def decrement(name, amount = 1, options = nil)
29
+ end
30
+
31
+ def delete_matched(matcher, options = nil)
32
+ end
33
+
34
+ protected
35
+ def read_entry(key, options) # :nodoc:
36
+ end
37
+
38
+ def write_entry(key, entry, options) # :nodoc:
39
+ true
40
+ end
41
+
42
+ def delete_entry(key, options) # :nodoc:
43
+ false
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -2,11 +2,23 @@ class Settings < ActiveRecord::Base
2
2
  class SettingNotFound < RuntimeError; end
3
3
 
4
4
  cattr_accessor :defaults
5
- @@defaults = {}.with_indifferent_access
6
-
5
+ self.defaults = {}.with_indifferent_access
6
+
7
+ # cache must follow the contract of ActiveSupport::Cache. Defaults to no-op.
8
+ cattr_accessor :cache
9
+ self.cache = ActiveSupport::Cache::NullStore.new
10
+
11
+ # options passed to cache.fetch() and cache.write(). example: {:expires_in => 5.minutes}
12
+ cattr_accessor :cache_options
13
+ self.cache_options = {}
14
+
15
+ def self.cache_key(var_name)
16
+ [target_id, target_type, var_name].compact.join("::")
17
+ end
18
+
7
19
  # Support old plugin
8
20
  if defined?(SettingsDefaults::DEFAULTS)
9
- @@defaults = SettingsDefaults::DEFAULTS.with_indifferent_access
21
+ self.defaults = SettingsDefaults::DEFAULTS.with_indifferent_access
10
22
  end
11
23
 
12
24
  #get or set a variable with the variable as the called method
@@ -35,12 +47,18 @@ class Settings < ActiveRecord::Base
35
47
  var_name = var_name.to_s
36
48
  begin
37
49
  target(var_name).destroy
50
+ cache.delete(cache_key(var_name))
38
51
  true
39
52
  rescue NoMethodError
40
53
  raise SettingNotFound, "Setting variable \"#{var_name}\" not found"
41
54
  end
42
55
  end
43
56
 
57
+ def self.delete_all(conditions = nil)
58
+ cache.clear
59
+ super
60
+ end
61
+
44
62
  #retrieve all settings as a hash (optionally starting with a given namespace)
45
63
  def self.all(starting_with=nil)
46
64
  options = starting_with ? { :conditions => "var LIKE '#{starting_with}%'"} : {}
@@ -55,10 +73,12 @@ class Settings < ActiveRecord::Base
55
73
 
56
74
  #get a setting value by [] notation
57
75
  def self.[](var_name)
58
- if var = target(var_name)
59
- var.value
60
- else
61
- @@defaults[var_name.to_s]
76
+ cache.fetch(cache_key(var_name), cache_options) do
77
+ if (var = target(var_name)).present?
78
+ var.value
79
+ else
80
+ defaults[var_name.to_s]
81
+ end
62
82
  end
63
83
  end
64
84
 
@@ -67,7 +87,7 @@ class Settings < ActiveRecord::Base
67
87
  record = target_scoped.find_or_initialize_by_var(var_name.to_s)
68
88
  record.value = value
69
89
  record.save!
70
-
90
+ cache.write(cache_key(var_name), value, cache_options)
71
91
  value
72
92
  end
73
93
 
@@ -113,4 +133,4 @@ class Settings < ActiveRecord::Base
113
133
  def self.target_type
114
134
  nil
115
135
  end
116
- end
136
+ end
@@ -1,3 +1,3 @@
1
1
  module RailsSettings
2
- VERSION = '1.0.1'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -22,4 +22,6 @@ Gem::Specification.new do |s|
22
22
  # s.add_development_dependency "rspec"
23
23
  # s.add_runtime_dependency "rest-client"
24
24
  s.add_dependency 'activerecord', '>= 2.3'
25
+ s.add_development_dependency 'rake'
26
+ s.add_development_dependency 'sqlite3'
25
27
  end
@@ -9,6 +9,7 @@ class SettingsTest < Test::Unit::TestCase
9
9
  end
10
10
 
11
11
  def teardown
12
+ User.delete_all
12
13
  Settings.delete_all
13
14
  end
14
15
 
@@ -23,6 +24,11 @@ class SettingsTest < Test::Unit::TestCase
23
24
  assert_not_nil Settings.target(:foo)
24
25
  end
25
26
 
27
+ def tests_defaults_true
28
+ Settings.defaults[:foo] = true
29
+ assert_equal true, Settings.foo
30
+ end
31
+
26
32
  def tests_defaults_false
27
33
  Settings.defaults[:foo] = false
28
34
  assert_equal false, Settings.foo
@@ -183,4 +189,25 @@ class SettingsTest < Test::Unit::TestCase
183
189
  assert_setting value, key
184
190
  end
185
191
  end
186
- end
192
+ end
193
+
194
+ class CachedSettingsTest < SettingsTest
195
+ def setup
196
+ Settings.cache = ActiveSupport::Cache::MemoryStore.new
197
+ Settings.cache_options = { :expires_in => 5.minutes }
198
+ super
199
+ end
200
+
201
+ def test_caching_is_in_place
202
+ Settings.progress = "boing"
203
+ Settings.connection.execute("delete from settings")
204
+ assert_equal "boing", Settings.progress
205
+ Settings.cache.clear
206
+ assert_nil Settings.progress
207
+ end
208
+
209
+ def teardown
210
+ Settings.cache.clear
211
+ super
212
+ end
213
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ledermann-rails-settings
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 0
9
8
  - 1
10
- version: 1.0.1
9
+ - 0
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Georg Ledermann
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-05 00:00:00 +01:00
19
- default_executable:
18
+ date: 2012-06-01 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: activerecord
@@ -33,6 +32,34 @@ dependencies:
33
32
  version: "2.3"
34
33
  type: :runtime
35
34
  version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rake
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: sqlite3
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :development
62
+ version_requirements: *id003
36
63
  description: Ruby Gem that makes managing a table of key/value pairs easy. Think of it like a Hash stored in you database, that uses simple ActiveRecord like methods for manipulation.
37
64
  email:
38
65
  - mail@georg-ledermann.de
@@ -45,6 +72,7 @@ extra_rdoc_files: []
45
72
  files:
46
73
  - .gitignore
47
74
  - .travis.yml
75
+ - Changelog.md
48
76
  - Gemfile
49
77
  - MIT-LICENSE
50
78
  - README.md
@@ -52,16 +80,17 @@ files:
52
80
  - ci/Gemfile.rails-2.3.x
53
81
  - ci/Gemfile.rails-3.0.x
54
82
  - ci/Gemfile.rails-3.1.x
83
+ - ci/Gemfile.rails-3.2.x
55
84
  - init.rb
56
85
  - lib/rails-settings.rb
57
86
  - lib/rails-settings/active_record.rb
87
+ - lib/rails-settings/null_store.rb
58
88
  - lib/rails-settings/scoped_settings.rb
59
89
  - lib/rails-settings/settings.rb
60
90
  - lib/rails-settings/version.rb
61
91
  - rails-settings.gemspec
62
92
  - test/settings_test.rb
63
93
  - test/test_helper.rb
64
- has_rdoc: true
65
94
  homepage: https://github.com/ledermann/rails-settings
66
95
  licenses: []
67
96
 
@@ -91,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
120
  requirements: []
92
121
 
93
122
  rubyforge_project: ledermann-rails-settings
94
- rubygems_version: 1.6.2
123
+ rubygems_version: 1.8.24
95
124
  signing_key:
96
125
  specification_version: 3
97
126
  summary: Settings management for ActiveRecord objects