persistent_settings 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module PersistentSettings
2
- VERSION = "1.1.1"
2
+ VERSION = "1.2.0"
3
3
  end
data/lib/settings.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  class Settings < ActiveRecord::Base
2
2
  include ::PersistentSettings
3
+ @mutex = Mutex.new
3
4
 
4
5
  serialize :value
5
6
 
@@ -17,12 +18,19 @@ class Settings < ActiveRecord::Base
17
18
 
18
19
  (class << self; self; end).instance_eval do
19
20
  define_method method_name do |value|
20
- persist(getter, value)
21
- instance_variable_set(:"@#{getter}", value)
21
+ @mutex.synchronize do
22
+ persist(getter, value)
23
+ write_to_cache getter, value
24
+ end
22
25
  end
23
26
 
24
27
  define_method getter do
25
- instance_variable_get(:"@#{getter}")
28
+ value = read_from_cache getter
29
+ unless value
30
+ value = read_from_persistance getter
31
+ write_to_cache getter, value
32
+ end
33
+ value
26
34
  end
27
35
  end
28
36
  end
@@ -46,10 +54,26 @@ class Settings < ActiveRecord::Base
46
54
  end
47
55
  end
48
56
 
57
+ def self.read_from_persistance(key)
58
+ Settings.find_by_var(key).value
59
+ end
60
+
49
61
  def self.load_from_persistance
50
62
  load_from_persistance! if ready?
51
63
  end
52
64
 
65
+ def self.cache_key_for(key)
66
+ "settings/#{key}"
67
+ end
68
+
69
+ def self.write_to_cache(key, value)
70
+ ::Rails.cache.write(cache_key_for(key), value)
71
+ end
72
+
73
+ def self.read_from_cache(key)
74
+ ::Rails.cache.fetch(cache_key_for(key))
75
+ end
76
+
53
77
  def self.ready?
54
78
  connected? && table_exists?
55
79
  end
@@ -16,13 +16,50 @@ describe Settings do
16
16
  end
17
17
 
18
18
  describe :define_setter_and_getter do
19
- it "creates the setter and getter methods" do
19
+ before do
20
20
  Settings.define_setter_and_getter(:method_name=)
21
+ end
22
+
23
+ it "creates the setter and getter methods" do
21
24
  Settings.should respond_to(:method_name=)
22
25
  Settings.should respond_to(:method_name)
23
- Settings.should_receive(:persist)
24
- Settings.method_name = 'value'
25
- Settings.method_name.should == 'value'
26
+ end
27
+
28
+ context :getter_method do
29
+ context "value is persisted on cache" do
30
+ before do
31
+ Settings.should_receive(:read_from_cache).with('method_name').
32
+ and_return('value from cache')
33
+ end
34
+
35
+ it "reads and returns value from cache" do
36
+ Settings.method_name.should == 'value from cache'
37
+ end
38
+ end
39
+
40
+ context "value is not persisted on cache" do
41
+ before do
42
+ Settings.should_receive(:read_from_cache).with('method_name')
43
+ end
44
+
45
+ it "reads and returns value from persistance, then saves to cache" do
46
+ Settings.should_receive(:read_from_persistance).with('method_name').
47
+ and_return 'value from persistance'
48
+
49
+ Settings.should_receive(:write_to_cache).
50
+ with('method_name', 'value from persistance')
51
+
52
+ Settings.method_name.should == 'value from persistance'
53
+ end
54
+ end
55
+ end
56
+
57
+ context :setter_method do
58
+ it "persists and writes to cache the key/value" do
59
+ Settings.should_receive(:persist).with('method_name', 'value')
60
+ Settings.should_receive(:write_to_cache).with('method_name', 'value')
61
+ Settings.method_name = 'value'
62
+ end
26
63
  end
27
64
  end
28
65
 
@@ -48,6 +85,44 @@ describe Settings do
48
85
  end
49
86
  end
50
87
 
88
+ describe :read_from_persistance do
89
+ it "reads the value from persistance" do
90
+ object = Settings.new :var => 'foo', :value => 'bar'
91
+ Settings.should_receive(:find_by_var).with('foo').and_return(object)
92
+
93
+ Settings.read_from_persistance('foo').should eq 'bar'
94
+ end
95
+ end
96
+
97
+ describe :cache_key_for do
98
+ it "returns the name for the rails cache key" do
99
+ Settings.cache_key_for('foo').should eq('settings/foo')
100
+ end
101
+ end
102
+
103
+ context "cache" do
104
+ let(:cache) { mock }
105
+
106
+ before do
107
+ Rails.should_receive(:cache).and_return cache
108
+ Settings.stub(:cache_key_for).with('foo').and_return 'settings/foo'
109
+ end
110
+
111
+ describe :write_to_cache do
112
+ it "writes the key, value to Rails cache" do
113
+ cache.should_receive(:write).with('settings/foo', 'bar')
114
+ Settings.write_to_cache('foo', 'bar')
115
+ end
116
+ end
117
+
118
+ describe :read_from_cache do
119
+ it "reads the value from Rails cache" do
120
+ cache.should_receive(:fetch).with('settings/foo')
121
+ Settings.read_from_cache('foo')
122
+ end
123
+ end
124
+ end
125
+
51
126
  describe :method_missing do
52
127
  context "A method ending with =" do
53
128
  it "calls define_setter_and_getter" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: persistent_settings
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -147,7 +147,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
147
147
  version: '0'
148
148
  segments:
149
149
  - 0
150
- hash: 2798642706016126182
150
+ hash: 4009893083530099684
151
151
  required_rubygems_version: !ruby/object:Gem::Requirement
152
152
  none: false
153
153
  requirements:
@@ -156,7 +156,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
156
156
  version: '0'
157
157
  segments:
158
158
  - 0
159
- hash: 2798642706016126182
159
+ hash: 4009893083530099684
160
160
  requirements: []
161
161
  rubyforge_project: persistent_settings
162
162
  rubygems_version: 1.8.23