redis-settings 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -3,7 +3,6 @@ PATH
3
3
  specs:
4
4
  redis-settings (0.1.0)
5
5
  redis
6
- yajl-ruby
7
6
 
8
7
  GEM
9
8
  remote: http://rubygems.org/
@@ -48,7 +47,6 @@ GEM
48
47
  archive-tar-minitar (>= 0.5.2)
49
48
  sqlite3-ruby (1.3.2)
50
49
  tzinfo (0.3.23)
51
- yajl-ruby (0.7.8)
52
50
 
53
51
  PLATFORMS
54
52
  ruby
@@ -60,4 +58,3 @@ DEPENDENCIES
60
58
  rspec (>= 2.0.0)
61
59
  ruby-debug19
62
60
  sqlite3-ruby
63
- yajl-ruby
data/README.rdoc CHANGED
@@ -8,6 +8,8 @@ Store application and user settings on Redis. Comes with ActiveRecord support.
8
8
 
9
9
  Redis::Settings store all data in JSON format. The default parser is yajl-ruby[https://rubygems.org/gems/yajl-ruby], with fallback to json[https://rubygems.org/gems/json]. Make sure you have one of those installed.
10
10
 
11
+ If you're running Ruby 1.9, you're ready to go!
12
+
11
13
  == Usage
12
14
 
13
15
  require "redis/settings"
@@ -47,6 +49,14 @@ Redis::Settings store all data in JSON format. The default parser is yajl-ruby[h
47
49
 
48
50
  <b>NOTE:</b> When record is destroyed, all settings are erased.
49
51
 
52
+ === Rails 3
53
+
54
+ Rails 3 set its own namespace like <tt>settings/#{Rails.env}</tt>.
55
+
56
+ You can override the root namespace as
57
+
58
+ Redis::Settings.root_namespace = "custom"
59
+
50
60
  = Mantainer
51
61
 
52
62
  * Nando Vieira (http://nandovieira.com.br)
@@ -9,17 +9,20 @@ end
9
9
  class Redis
10
10
  class Settings
11
11
  require "redis/settings/active_record" if defined?(ActiveRecord)
12
+ require "redis/settings/railtie" if defined?(Rails) && Rails.version >= "3.0.0"
12
13
 
13
14
  class NewRecordError < StandardError
14
- def message
15
- "You can't access settings on new records"
16
- end
15
+ def message; "You can't access settings on new records"; end
17
16
  end
18
17
 
19
18
  class << self
20
19
  attr_accessor :connection
20
+ attr_accessor :root_namespace
21
21
  end
22
22
 
23
+ # Set the root namespace to "settings" by default.
24
+ self.root_namespace = "settings"
25
+
23
26
  # Return Redis::Settings.
24
27
  #
25
28
  # Redis::Settings.configure do |config|
@@ -39,14 +42,14 @@ class Redis
39
42
  @namespace = namespace
40
43
  end
41
44
 
42
- # Returned namespace with <tt>settings</tt> as its root.
45
+ # Return instance's namespace concatenated with Redis::Settings.root_namespace.
43
46
  #
44
47
  # s = Redis::Settings.new("app")
45
48
  # s.namespace
46
49
  # #=> "settings/app"
47
50
  #
48
51
  def namespace
49
- "settings/#{@namespace}"
52
+ "#{self.class.root_namespace}/#{@namespace}"
50
53
  end
51
54
 
52
55
  # Retrieve setting by its name. When nil, return the default value.
@@ -0,0 +1,9 @@
1
+ class Redis
2
+ class Settings
3
+ class Railtie < Rails::Railtie
4
+ initializer :redis_settings do
5
+ Settings.root_namespace = "settings/#{Rails.env}"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -3,7 +3,7 @@ class Redis
3
3
  module Version
4
4
  MAJOR = 0
5
5
  MINOR = 1
6
- PATCH = 0
6
+ PATCH = 1
7
7
  STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
8
8
  end
9
9
  end
@@ -3,6 +3,7 @@ require "spec_helper"
3
3
  describe Redis::Settings::ActiveRecord do
4
4
  let(:user) { User.create }
5
5
  let(:admin) { Admin::User.create }
6
+ let(:root) { Redis::Settings.root_namespace }
6
7
 
7
8
  before do
8
9
  user.settings.clear
@@ -20,8 +21,8 @@ describe Redis::Settings::ActiveRecord do
20
21
  end
21
22
 
22
23
  it "should set namespace accordingly" do
23
- user.settings.namespace.should == "settings/user/#{user.id}"
24
- admin.settings.namespace.should == "settings/admin/user/#{admin.id}"
24
+ user.settings.namespace.should == "#{root}/user/#{user.id}"
25
+ admin.settings.namespace.should == "#{root}/admin/user/#{admin.id}"
25
26
  end
26
27
 
27
28
  it "should define setting" do
@@ -35,6 +36,6 @@ describe Redis::Settings::ActiveRecord do
35
36
  it "should remove all settings when destroy a record" do
36
37
  user.settings[:role] = "customer"
37
38
  user.destroy
38
- Redis::Settings.connection.hgetall("settings/user/#{user.id}").should be_empty
39
+ Redis::Settings.connection.hgetall("#{root}/user/#{user.id}").should be_empty
39
40
  end
40
41
  end
@@ -8,7 +8,7 @@ describe Redis::Settings do
8
8
  subject.clear
9
9
  end
10
10
 
11
- describe ".all" do
11
+ describe "#all" do
12
12
  it "should return all settings" do
13
13
  subject[:items] = 10
14
14
  subject[:enabled] = true
@@ -23,13 +23,27 @@ describe Redis::Settings do
23
23
  end
24
24
  end
25
25
 
26
- describe ".namespace" do
26
+ describe ".root_namespace" do
27
+ before { Redis::Settings.root_namespace = "settings/development" }
28
+ after { Redis::Settings.root_namespace = "settings" }
29
+
30
+ it "should use custom namespace" do
31
+ subject.namespace.should == "settings/development/app"
32
+ end
33
+
34
+ it "should set value using custom namespace" do
35
+ subject[:items] = 10
36
+ JSON.parse(redis.hget("settings/development/app", :items)).should == {"data" => 10}
37
+ end
38
+ end
39
+
40
+ describe "#namespace" do
27
41
  it "should include settings as namespace root" do
28
42
  subject.namespace.should == "settings/app"
29
43
  end
30
44
  end
31
45
 
32
- describe ".set" do
46
+ describe "#set" do
33
47
  it "should set value" do
34
48
  subject.set(:items, 5)
35
49
  JSON.parse(redis.hget(subject.namespace, :items)).should == {"data" => 5}
@@ -47,7 +61,7 @@ describe Redis::Settings do
47
61
  end
48
62
  end
49
63
 
50
- describe ".get" do
64
+ describe "#get" do
51
65
  it "should get value" do
52
66
  subject.set(:items, 5)
53
67
  subject.get(:items).should == 5
@@ -64,7 +78,7 @@ describe Redis::Settings do
64
78
  end
65
79
  end
66
80
 
67
- describe ".clear" do
81
+ describe "#clear" do
68
82
  it "should remove all settings" do
69
83
  subject[:items] = 5
70
84
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Nando Vieira
@@ -102,6 +102,7 @@ files:
102
102
  - lib/redis-settings.rb
103
103
  - lib/redis/settings.rb
104
104
  - lib/redis/settings/active_record.rb
105
+ - lib/redis/settings/railtie.rb
105
106
  - lib/redis/settings/version.rb
106
107
  - redis-settings.gemspec
107
108
  - spec/redis/settings/active_record_spec.rb