settings-goo 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/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Chris Kilmer
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,47 @@
1
+ = settings-goo
2
+
3
+ settings-goo is a simple ActiveRecord based model for storing global application settings for your rails application in your database. Settings was designed to be as simple as possible and to mimic a Hash. Data is stored in the database fields :key and :value. The :value field is serialized as json. The possible method_missing magic (ex: Settings.admin_email) was intentionally left off. Keep it simple! You can store any kind of data as your value, i.e. boolean, float, integer, array.
4
+
5
+ == Installation
6
+
7
+ Install the settings-goo gem
8
+ sudo gem install settings-goo
9
+
10
+ == Setup
11
+
12
+ Create the settings migration
13
+ script/generate settings_migration
14
+
15
+ Run the database migration
16
+ rake db:migrate
17
+
18
+ == Usage
19
+
20
+ Create some settings:
21
+ Settings[:admin_email] = 'admin@somewhere.com'
22
+ Settings[:role_enabled] = true
23
+ Settings[:per_page] = 1
24
+ Settings[:ca_tax] = 0.87
25
+
26
+ Retrieve some settings:
27
+ Settings[:admin_email] # returns 'admin@somewhere.com'
28
+ Settings[:role_enabled] # returns true
29
+ Settings[:per_page] # returns 1
30
+ Settings[:ca_tax] # return 0.87
31
+
32
+ Update a setting:
33
+ Settings[:admin_email] = 'admin@somewhere.com'
34
+ Settings[:admin_email] = 'another.admin@somewhere.com'
35
+ Settings[:admin_email] # returns 'another.admin@somewhere.com'
36
+
37
+ Remove a setting:
38
+ Settings.remove(:admin_email)
39
+ Settings[:admin_email] # returns nil
40
+
41
+ List all setting keys:
42
+ Settings.keys # returns [:admin_email, :ca_tax, :per_page, :role_enabled]
43
+
44
+ == TODO
45
+
46
+ Cache Settings.keys
47
+ Cache Create rails plugin gem
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ desc = "A simple ActiveRecord based model for storing global application settings for your rails application in your database."
8
+ gem.name = "settings-goo"
9
+ gem.summary = desc
10
+ gem.description = desc
11
+ gem.email = "chris.kilmer@dogwalkercentral.com"
12
+ gem.homepage = "http://github.com/chriskilmer/settings-goo"
13
+ gem.authors = ["Chris Kilmer"]
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "settings-goo #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,12 @@
1
+ class SettingsMigrationGenerator < Rails::Generator::NamedBase
2
+ def initialize(runtime_args, runtime_options = {})
3
+ runtime_args << 'create_settings' if runtime_args.empty?
4
+ super
5
+ end
6
+
7
+ def manifest
8
+ record do |m|
9
+ m.migration_template 'migration.rb', 'db/migrate'
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ class <%= class_name %> < ActiveRecord::Migration
2
+ @tablename = :settings
3
+
4
+ def self.up
5
+ create_table @tablename do |t|
6
+ t.string :key, :null => false
7
+ t.text :value
8
+ end
9
+ add_index @tablename, :key
10
+ end
11
+
12
+ def self.down
13
+ remove_index @tablename, :key
14
+ drop_table @tablename
15
+ end
16
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), %w(lib settings)))
data/lib/settings.rb ADDED
@@ -0,0 +1,56 @@
1
+ class Settings < ActiveRecord::Base
2
+ validates_uniqueness_of :key
3
+ after_save :clear_cache
4
+ after_destroy :clear_cache
5
+
6
+ def value
7
+ val = self[:value]
8
+ if val
9
+ ActiveSupport::JSON.decode val
10
+ else
11
+ nil
12
+ end
13
+ end
14
+
15
+ def value=(val)
16
+ self[:value] = val.to_json
17
+ end
18
+
19
+ def self.[](key)
20
+ if setting = find_cached(key)
21
+ setting.value
22
+ else
23
+ nil
24
+ end
25
+ end
26
+
27
+ def self.[]=(key, val)
28
+ key = key.to_s
29
+ setting = Settings.find_by_key(key) || Settings.new(:key => key)
30
+ setting.value = val
31
+ setting.save
32
+ end
33
+
34
+ def self.keys
35
+ self.find(:all, :select => 'key', :order => 'key ASC').map{|s| s.key.to_sym}
36
+ end
37
+
38
+ def self.remove(key)
39
+ key = key.to_s
40
+ setting = Settings.find_by_key(key)
41
+ if setting
42
+ setting.destroy
43
+ else
44
+ nil
45
+ end
46
+ end
47
+ private
48
+ def self.find_cached(key)
49
+ key = key.to_s
50
+ Rails.cache.fetch("settings/#{key}") { Settings.find_by_key(key) }
51
+ end
52
+
53
+ def clear_cache
54
+ Rails.cache.delete("settings/#{key}")
55
+ end
56
+ end
@@ -0,0 +1,52 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{settings-goo}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Chris Kilmer"]
12
+ s.date = %q{2010-02-11}
13
+ s.description = %q{A simple ActiveRecord based model for storing global application settings for your rails application in your database.}
14
+ s.email = %q{chris.kilmer@dogwalkercentral.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "generators/settings_migration/settings_migration_generator.rb",
27
+ "generators/settings_migration/templates/migration.rb",
28
+ "init.rb",
29
+ "lib/settings.rb",
30
+ "settings-goo.gemspec",
31
+ "test/test_settings.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/chriskilmer/settings-goo}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.5}
37
+ s.summary = %q{A simple ActiveRecord based model for storing global application settings for your rails application in your database.}
38
+ s.test_files = [
39
+ "test/test_settings.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
47
+ else
48
+ end
49
+ else
50
+ end
51
+ end
52
+
@@ -0,0 +1,150 @@
1
+ require File.dirname(__FILE__) + '/../../../../test/test_helper'
2
+
3
+ class SettingsTest < ActiveSupport::TestCase
4
+
5
+ def setup
6
+ @key_name = 'some_key'
7
+ Rails.cache.clear
8
+ end
9
+
10
+ ##############################
11
+ # Proper Serialization #
12
+ ##############################
13
+ test 'should serialize string correctly' do
14
+ val = 'some string'
15
+ setting = Settings.create(:key => @key_name, :value => val)
16
+ setting.reload
17
+ assert_equal val, setting.value
18
+ end
19
+
20
+ test 'should serialize boolen correctly' do
21
+ val = true
22
+ setting = Settings.create(:key => @key_name, :value => val)
23
+ setting.reload
24
+ assert_equal val, setting.value
25
+ end
26
+
27
+ test 'should serialize integer correctly' do
28
+ val = 1
29
+ setting = Settings.create(:key => @key_name, :value => val)
30
+ setting.reload
31
+ val = setting.value
32
+ assert_equal val, val
33
+ assert_equal 2, val*2
34
+ end
35
+
36
+ test 'should serialize float correctly' do
37
+ val = 0.1
38
+ setting = Settings.create(:key => @key_name, :value => val)
39
+ setting.reload
40
+ val = setting.value
41
+ assert_equal val, val
42
+ assert_equal 0.2, val*2
43
+ end
44
+
45
+ test 'should serialize nil correctly' do
46
+ setting = Settings.create(:key => @key_name)
47
+ setting.reload
48
+ assert_equal nil, setting.value
49
+ end
50
+
51
+ ##############################
52
+ # Hash Functionality #
53
+ ##############################
54
+ test 'should retrieve key' do
55
+ val = true
56
+ Settings.create(:key => @key_name, :value => val)
57
+
58
+ #key given as string
59
+ setting = Settings[@key_name]
60
+ assert_equal setting, val
61
+
62
+ #key given as symbol
63
+ setting = Settings[@key_name.to_sym]
64
+ assert_equal setting, val
65
+ end
66
+
67
+ test 'should set key' do
68
+ val = true
69
+ Settings[@key_name] = val
70
+ setting = Settings.find_by_key(@key_name)
71
+ assert_equal setting.value, val
72
+ end
73
+
74
+ test 'should return nil if key does not exist' do
75
+ assert_nil Settings[:some_unknown_key]
76
+ end
77
+
78
+ test 'should remove existing key' do
79
+ Settings[@key_name] = true
80
+ Settings.remove @key_name
81
+ assert_nil Settings[@key_name]
82
+ end
83
+
84
+ test 'should return nil when trying to remove non-existent key' do
85
+ assert_nil Settings.remove :non_existent_key
86
+ end
87
+
88
+ ##############################
89
+ # Duplicate Handling #
90
+ ##############################
91
+ test 'should not allow creation of duplicate keys' do
92
+ val = true
93
+ setting = Settings.create(:key => @key_name, :value => val)
94
+ assert !setting.new_record?
95
+
96
+ #Try to create duplicate key
97
+ setting = Settings.create(:key => @key_name, :value => val)
98
+ assert setting.new_record?
99
+ end
100
+
101
+ test 'should overwrite setting vs. creating a new one if existing key is specified' do
102
+ val = true
103
+ Settings[@key_name] = val
104
+ assert_no_difference('Settings.count') do
105
+ val = false
106
+ setting = (Settings[@key_name] = val)
107
+ assert_equal val, setting
108
+ end
109
+ end
110
+
111
+ ##############################
112
+ # Collections #
113
+ ##############################
114
+ test 'should return all keys' do
115
+ val = 'some val'
116
+ keys = [:a_key, :b_key]
117
+ keys.each {|k| Settings[k] = val}
118
+ assert_equal keys, Settings.keys
119
+ end
120
+
121
+ ##############################
122
+ # Cache #
123
+ ##############################
124
+ test 'should set cache' do
125
+ cache_key = "settings/#{@key_name}"
126
+ assert_nil Rails.cache.fetch(cache_key)
127
+ Settings[@key_name] = 'some val'
128
+ #Now fetch it so it will be cached
129
+ Settings[@key_name]
130
+ assert_not_nil Rails.cache.fetch(cache_key)
131
+ end
132
+
133
+ test 'should clear cache when updated' do
134
+ cache_key = "settings/#{@key_name}"
135
+ Settings[@key_name] = 'some val'
136
+ #Now fetch it so it will be cached
137
+ Settings[@key_name]
138
+ Settings[@key_name] = 'other val'
139
+ assert_nil Rails.cache.fetch(cache_key)
140
+ end
141
+
142
+ test 'should clear cache when destroyed' do
143
+ cache_key = "settings/#{@key_name}"
144
+ Settings[@key_name] = 'some val'
145
+ #Now fetch it so it will be cached
146
+ Settings[@key_name]
147
+ Settings.remove @key_name
148
+ assert_nil Rails.cache.fetch(cache_key)
149
+ end
150
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: settings-goo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris Kilmer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-11 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: A simple ActiveRecord based model for storing global application settings for your rails application in your database.
17
+ email: chris.kilmer@dogwalkercentral.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - generators/settings_migration/settings_migration_generator.rb
33
+ - generators/settings_migration/templates/migration.rb
34
+ - init.rb
35
+ - lib/settings.rb
36
+ - settings-goo.gemspec
37
+ - test/test_settings.rb
38
+ has_rdoc: true
39
+ homepage: http://github.com/chriskilmer/settings-goo
40
+ licenses: []
41
+
42
+ post_install_message:
43
+ rdoc_options:
44
+ - --charset=UTF-8
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.3.5
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: A simple ActiveRecord based model for storing global application settings for your rails application in your database.
66
+ test_files:
67
+ - test/test_settings.rb