redis-settings 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in redis-options.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,63 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ redis-settings (0.1.0)
5
+ redis
6
+ yajl-ruby
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ activemodel (3.0.1)
12
+ activesupport (= 3.0.1)
13
+ builder (~> 2.1.2)
14
+ i18n (~> 0.4.1)
15
+ activerecord (3.0.1)
16
+ activemodel (= 3.0.1)
17
+ activesupport (= 3.0.1)
18
+ arel (~> 1.0.0)
19
+ tzinfo (~> 0.3.23)
20
+ activesupport (3.0.1)
21
+ archive-tar-minitar (0.5.2)
22
+ arel (1.0.1)
23
+ activesupport (~> 3.0.0)
24
+ builder (2.1.2)
25
+ columnize (0.3.2)
26
+ diff-lcs (1.1.2)
27
+ i18n (0.4.2)
28
+ linecache19 (0.5.11)
29
+ ruby_core_source (>= 0.1.4)
30
+ redis (2.1.1)
31
+ rspec (2.1.0)
32
+ rspec-core (~> 2.1.0)
33
+ rspec-expectations (~> 2.1.0)
34
+ rspec-mocks (~> 2.1.0)
35
+ rspec-core (2.1.0)
36
+ rspec-expectations (2.1.0)
37
+ diff-lcs (~> 1.1.2)
38
+ rspec-mocks (2.1.0)
39
+ ruby-debug-base19 (0.11.24)
40
+ columnize (>= 0.3.1)
41
+ linecache19 (>= 0.5.11)
42
+ ruby_core_source (>= 0.1.4)
43
+ ruby-debug19 (0.11.6)
44
+ columnize (>= 0.3.1)
45
+ linecache19 (>= 0.5.11)
46
+ ruby-debug-base19 (>= 0.11.19)
47
+ ruby_core_source (0.1.4)
48
+ archive-tar-minitar (>= 0.5.2)
49
+ sqlite3-ruby (1.3.2)
50
+ tzinfo (0.3.23)
51
+ yajl-ruby (0.7.8)
52
+
53
+ PLATFORMS
54
+ ruby
55
+
56
+ DEPENDENCIES
57
+ activerecord
58
+ redis
59
+ redis-settings!
60
+ rspec (>= 2.0.0)
61
+ ruby-debug19
62
+ sqlite3-ruby
63
+ yajl-ruby
data/README.rdoc ADDED
@@ -0,0 +1,75 @@
1
+ = Redis::Settings
2
+
3
+ Store application and user settings on Redis. Comes with ActiveRecord support.
4
+
5
+ == Installation
6
+
7
+ gem install redis-settings
8
+
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
+
11
+ == Usage
12
+
13
+ require "redis/settings"
14
+
15
+ # Reuse an existing Redis connection.
16
+ Redis::Settings.configure do |config|
17
+ config.connection = Redis.new(:host => "localhost", :port => 6379)
18
+ end
19
+
20
+ # Create settings namespaced to app
21
+ settings = Redis::Settings.new("app")
22
+ settings.namespace
23
+ #=> "settings/app"
24
+
25
+ # Set values
26
+ settings[:items_per_page] = 10
27
+ settings.set(:items_per_page, 10)
28
+
29
+ # Get values
30
+ settings[:items_per_page]
31
+ settings.get(:items_per_page)
32
+ settings.get(:items_per_page, 20) #=> return 20 when items_per_page is not defined
33
+
34
+ # Remove all settings on this namespace
35
+ settings.clear
36
+
37
+ # Retrieve a hash with defined settings
38
+ settings.all
39
+
40
+ === ActiveRecord
41
+
42
+ class User < ActiveRecord::Base
43
+ end
44
+
45
+ user = User.first
46
+ user.settings[:role] = "admin"
47
+
48
+ <b>NOTE:</b> When record is destroyed, all settings are erased.
49
+
50
+ = Mantainer
51
+
52
+ * Nando Vieira (http://nandovieira.com.br)
53
+
54
+ = License
55
+
56
+ (The MIT License)
57
+
58
+ Permission is hereby granted, free of charge, to any person obtaining
59
+ a copy of this software and associated documentation files (the
60
+ 'Software'), to deal in the Software without restriction, including
61
+ without limitation the rights to use, copy, modify, merge, publish,
62
+ distribute, sublicense, and/or sell copies of the Software, and to
63
+ permit persons to whom the Software is furnished to do so, subject to
64
+ the following conditions:
65
+
66
+ The above copyright notice and this permission notice shall be
67
+ included in all copies or substantial portions of the Software.
68
+
69
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
70
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
71
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
72
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
73
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
74
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
75
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new
@@ -0,0 +1,25 @@
1
+ class Redis
2
+ class Settings
3
+ module ActiveRecord
4
+ def self.included(base)
5
+ base.class_eval do
6
+ include InstanceMethods
7
+ after_destroy :clear_settings
8
+ end
9
+ end
10
+
11
+ module InstanceMethods
12
+ def settings
13
+ raise Redis::Settings::NewRecordError if new_record?
14
+ @settings ||= Settings.new("#{self.class.name.underscore}/#{id}")
15
+ end
16
+
17
+ def clear_settings
18
+ settings.clear
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ ActiveRecord::Base.send :include, Redis::Settings::ActiveRecord
@@ -0,0 +1,10 @@
1
+ class Redis
2
+ class Settings
3
+ module Version
4
+ MAJOR = 0
5
+ MINOR = 1
6
+ PATCH = 0
7
+ STRING = "#{MAJOR}.#{MINOR}.#{PATCH}"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,100 @@
1
+ require "redis"
2
+
3
+ begin
4
+ require "yajl/json_gem"
5
+ rescue LoadError
6
+ require "json"
7
+ end
8
+
9
+ class Redis
10
+ class Settings
11
+ require "redis/settings/active_record" if defined?(ActiveRecord)
12
+
13
+ class NewRecordError < StandardError
14
+ def message
15
+ "You can't access settings on new records"
16
+ end
17
+ end
18
+
19
+ class << self
20
+ attr_accessor :connection
21
+ end
22
+
23
+ # Return Redis::Settings.
24
+ #
25
+ # Redis::Settings.configure do |config|
26
+ # config.connection = Redis.new(:host => "localhost", :port => 6379)
27
+ # end
28
+ #
29
+ def self.configure(&block)
30
+ yield self
31
+ end
32
+
33
+ # Initialize a new object that will be wrapped into the specified
34
+ # namespace.
35
+ #
36
+ # s = Redis::Settings.new("app")
37
+ #
38
+ def initialize(namespace)
39
+ @namespace = namespace
40
+ end
41
+
42
+ # Returned namespace with <tt>settings</tt> as its root.
43
+ #
44
+ # s = Redis::Settings.new("app")
45
+ # s.namespace
46
+ # #=> "settings/app"
47
+ #
48
+ def namespace
49
+ "settings/#{@namespace}"
50
+ end
51
+
52
+ # Retrieve setting by its name. When nil, return the default value.
53
+ #
54
+ # s = Redis::Settings.new("app")
55
+ # s.get(:items_per_page)
56
+ # s.get(:items_per_page, 10)
57
+ #
58
+ def get(name, default = nil)
59
+ value = redis.hget(namespace, name)
60
+
61
+ if value
62
+ payload = JSON.parse(value)
63
+ value = payload["data"]
64
+ end
65
+
66
+ value || default
67
+ end
68
+
69
+ # Define a value for the specified setting.
70
+ #
71
+ # s = Redis::Settings.new("app")
72
+ # s.set(:items_per_page, 10)
73
+ #
74
+ def set(name, value)
75
+ if value.nil?
76
+ redis.hdel(namespace, name)
77
+ else
78
+ redis.hset(namespace, name, {:data => value}.to_json)
79
+ end
80
+ end
81
+
82
+ # Remove all settings from the current namespace
83
+ def clear
84
+ redis.del(namespace)
85
+ end
86
+
87
+ # Return a hash with all settings
88
+ def all
89
+ Hash[redis.hgetall(namespace).collect {|k, v| [k.to_sym, JSON.parse(v)["data"]]}]
90
+ end
91
+
92
+ alias_method :[]=, :set
93
+ alias_method :[], :get
94
+
95
+ private
96
+ def redis # :nodoc:
97
+ self.class.connection
98
+ end
99
+ end
100
+ end
@@ -0,0 +1 @@
1
+ require "redis/settings"
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "redis/settings/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "redis-settings"
7
+ s.version = Redis::Settings::Version::STRING
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Nando Vieira"]
10
+ s.email = ["fnando.vieira@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %[Store application and user settings on Redis. Comes with ActiveRecord support.]
13
+ s.description = s.summary
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency "redis"
21
+ s.add_development_dependency "rspec", ">= 2.0.0"
22
+ s.add_development_dependency "ruby-debug19"
23
+ s.add_development_dependency "activerecord"
24
+ s.add_development_dependency "sqlite3-ruby"
25
+ end
@@ -0,0 +1,40 @@
1
+ require "spec_helper"
2
+
3
+ describe Redis::Settings::ActiveRecord do
4
+ let(:user) { User.create }
5
+ let(:admin) { Admin::User.create }
6
+
7
+ before do
8
+ user.settings.clear
9
+ admin.settings.clear
10
+ end
11
+
12
+ it "should inject settings method" do
13
+ User.new.should respond_to(:settings)
14
+ end
15
+
16
+ it "should raise when trying to access settings from a new record" do
17
+ expect {
18
+ User.new.settings
19
+ }.to raise_error(Redis::Settings::NewRecordError)
20
+ end
21
+
22
+ 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}"
25
+ end
26
+
27
+ it "should define setting" do
28
+ admin.settings[:role] = "admin"
29
+ user.settings[:role] = "support"
30
+
31
+ user.settings[:role].should == "support"
32
+ admin.settings[:role].should == "admin"
33
+ end
34
+
35
+ it "should remove all settings when destroy a record" do
36
+ user.settings[:role] = "customer"
37
+ user.destroy
38
+ Redis::Settings.connection.hgetall("settings/user/#{user.id}").should be_empty
39
+ end
40
+ end
@@ -0,0 +1,76 @@
1
+ require "spec_helper"
2
+
3
+ describe Redis::Settings do
4
+ subject { Redis::Settings.new("app") }
5
+ let(:redis) { Redis::Settings.connection }
6
+
7
+ before do
8
+ subject.clear
9
+ end
10
+
11
+ describe ".all" do
12
+ it "should return all settings" do
13
+ subject[:items] = 10
14
+ subject[:enabled] = true
15
+
16
+ subject.all.should == {:items => 10, :enabled => true}
17
+ end
18
+ end
19
+
20
+ describe ".configure" do
21
+ it "should yield module" do
22
+ Redis::Settings.configure {|config| config.should be(Redis::Settings)}
23
+ end
24
+ end
25
+
26
+ describe ".namespace" do
27
+ it "should include settings as namespace root" do
28
+ subject.namespace.should == "settings/app"
29
+ end
30
+ end
31
+
32
+ describe ".set" do
33
+ it "should set value" do
34
+ subject.set(:items, 5)
35
+ JSON.parse(redis.hget(subject.namespace, :items)).should == {"data" => 5}
36
+ end
37
+
38
+ it "should have shortcut" do
39
+ subject[:items] = 10
40
+ JSON.parse(redis.hget(subject.namespace, :items)).should == {"data" => 10}
41
+ end
42
+
43
+ it "should remove setting when assigning nil" do
44
+ subject[:items] = 20
45
+ subject[:items] = nil
46
+ redis.hget(subject.namespace, :items).should be_nil
47
+ end
48
+ end
49
+
50
+ describe ".get" do
51
+ it "should get value" do
52
+ subject.set(:items, 5)
53
+ subject.get(:items).should == 5
54
+ end
55
+
56
+ it "should have alias" do
57
+ subject[:items] = 10
58
+ subject[:items].should == 10
59
+ end
60
+
61
+ it "should return default value" do
62
+ subject[:items] = nil
63
+ subject.get(:items, 15).should == 15
64
+ end
65
+ end
66
+
67
+ describe ".clear" do
68
+ it "should remove all settings" do
69
+ subject[:items] = 5
70
+
71
+ redis.hgetall(subject.namespace).should_not be_empty
72
+ subject.clear
73
+ redis.hgetall(subject.namespace).should be_empty
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,13 @@
1
+ require "active_record"
2
+ require "redis/settings"
3
+
4
+ Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|file| require file}
5
+
6
+ ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":memory:"
7
+ Redis::Settings.connection = Redis.new(:host => "localhost", :port => 6379)
8
+
9
+ ActiveRecord::Schema.define(:version => 0) do
10
+ create_table :users do |t|
11
+ t.string :login
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ class User < ActiveRecord::Base
2
+ end
3
+
4
+ module Admin
5
+ class User < ActiveRecord::Base
6
+ set_table_name "users"
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis-settings
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Nando Vieira
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-11-18 00:00:00 -02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: redis
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: rspec
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 2
43
+ - 0
44
+ - 0
45
+ version: 2.0.0
46
+ type: :development
47
+ version_requirements: *id002
48
+ - !ruby/object:Gem::Dependency
49
+ name: ruby-debug19
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ type: :development
60
+ version_requirements: *id003
61
+ - !ruby/object:Gem::Dependency
62
+ name: activerecord
63
+ prerelease: false
64
+ requirement: &id004 !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ type: :development
73
+ version_requirements: *id004
74
+ - !ruby/object:Gem::Dependency
75
+ name: sqlite3-ruby
76
+ prerelease: false
77
+ requirement: &id005 !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ type: :development
86
+ version_requirements: *id005
87
+ description: Store application and user settings on Redis. Comes with ActiveRecord support.
88
+ email:
89
+ - fnando.vieira@gmail.com
90
+ executables: []
91
+
92
+ extensions: []
93
+
94
+ extra_rdoc_files: []
95
+
96
+ files:
97
+ - .gitignore
98
+ - Gemfile
99
+ - Gemfile.lock
100
+ - README.rdoc
101
+ - Rakefile
102
+ - lib/redis-settings.rb
103
+ - lib/redis/settings.rb
104
+ - lib/redis/settings/active_record.rb
105
+ - lib/redis/settings/version.rb
106
+ - redis-settings.gemspec
107
+ - spec/redis/settings/active_record_spec.rb
108
+ - spec/redis/settings_spec.rb
109
+ - spec/spec_helper.rb
110
+ - spec/support/user.rb
111
+ has_rdoc: true
112
+ homepage: ""
113
+ licenses: []
114
+
115
+ post_install_message:
116
+ rdoc_options: []
117
+
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ segments:
126
+ - 0
127
+ version: "0"
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ segments:
134
+ - 0
135
+ version: "0"
136
+ requirements: []
137
+
138
+ rubyforge_project:
139
+ rubygems_version: 1.3.7
140
+ signing_key:
141
+ specification_version: 3
142
+ summary: Store application and user settings on Redis. Comes with ActiveRecord support.
143
+ test_files:
144
+ - spec/redis/settings/active_record_spec.rb
145
+ - spec/redis/settings_spec.rb
146
+ - spec/spec_helper.rb
147
+ - spec/support/user.rb