simple_cached_settings 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +53 -0
- data/Rakefile +2 -0
- data/lib/generators/simple_cached_settings/simple_cached_settings_generator.rb +29 -0
- data/lib/generators/simple_cached_settings/templates/migration.rb +9 -0
- data/lib/generators/simple_cached_settings/templates/model.rb +2 -0
- data/lib/simple_cached_settings.rb +3 -0
- data/lib/simple_cached_settings/setting_serializer.rb +21 -0
- data/lib/simple_cached_settings/settings.rb +39 -0
- data/lib/simple_cached_settings/version.rb +3 -0
- data/settings.gemspec +27 -0
- data/spec/simple_settings/settings_spec.rb +52 -0
- data/spec/spec_helper.rb +57 -0
- metadata +160 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c2533551bbe3c9613ec7d70cbfcdf24ef00adfd5
|
4
|
+
data.tar.gz: b4b3a84e74d923a520970ad747b21f86f581d850
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8de5cfecd0aebd326d15e91bd1c7335481af464172f4ce6a1e3fa86b5bef3ff76a71e6d143236e4c7105f634a488f37ff746a0de261c7a7641c73ed2f30f414e
|
7
|
+
data.tar.gz: 85fb1bf5a47475be69a25c087347b97615df8460bbd23996a493f4bfa74e04d98c5ce2112d3bd476021c5bb8ef98dab482deff2290629735525187aa3c9a3730
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Le6ow5k1
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# Simple Cached Settings
|
2
|
+
|
3
|
+
Gem for storing global configuration settings in an ActiveRecord::Model.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'simple_cached_settings'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install simple_cached_settings
|
18
|
+
|
19
|
+
## Setup
|
20
|
+
|
21
|
+
Generate settings model and migration:
|
22
|
+
|
23
|
+
$ rails g simple_cached_settings <settings_name>
|
24
|
+
|
25
|
+
Apply migration:
|
26
|
+
|
27
|
+
$ rake db:migrate
|
28
|
+
|
29
|
+
Now you have the model that looks something like this:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
class MySettings < SimpleCachedSettings::Settings
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
## Usage
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
MySettings.write(:foo, 'bar')
|
40
|
+
|
41
|
+
MySettings.read(:foo)
|
42
|
+
=> 'bar'
|
43
|
+
```
|
44
|
+
|
45
|
+
It's possible to store settings values of four types: String, Integer, Float and Boolean
|
46
|
+
|
47
|
+
## Contributing
|
48
|
+
|
49
|
+
1. Fork it ( https://github.com/[my-github-username]/settings/fork )
|
50
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
51
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
52
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
53
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'rails/generators/migration'
|
2
|
+
|
3
|
+
class SimpleCachedSettingsGenerator < Rails::Generators::NamedBase
|
4
|
+
include Rails::Generators::Migration
|
5
|
+
|
6
|
+
argument :name, type: :string, default: 'my_settings'
|
7
|
+
|
8
|
+
source_root File.expand_path('../templates', __FILE__)
|
9
|
+
|
10
|
+
@@migrations = false
|
11
|
+
|
12
|
+
def self.next_migration_number(dirname)
|
13
|
+
if ActiveRecord::Base.timestamped_migrations
|
14
|
+
if @@migrations
|
15
|
+
(current_migration_number(dirname) + 1)
|
16
|
+
else
|
17
|
+
@@migrations = true
|
18
|
+
Time.now.utc.strftime('%Y%m%d%H%M%S')
|
19
|
+
end
|
20
|
+
else
|
21
|
+
'%.3d' % (current_migration_number(dirname) + 1)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def settings
|
26
|
+
template 'model.rb', File.join('app/models', class_path, "#{file_name}.rb"), force: true
|
27
|
+
migration_template 'migration.rb', 'db/migrate/create_settings.rb'
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module SimpleCachedSettings
|
2
|
+
class SettingSerializer
|
3
|
+
ALLOWED_VALUE_TYPES = [Fixnum, String, Float, TrueClass, FalseClass]
|
4
|
+
|
5
|
+
class SettingTypeNotAllowed < TypeError; end
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def load(value)
|
9
|
+
YAML.load(value) if value
|
10
|
+
end
|
11
|
+
|
12
|
+
def dump(value)
|
13
|
+
if ALLOWED_VALUE_TYPES.any? { |t| value.is_a?(t) }
|
14
|
+
YAML.dump(value)
|
15
|
+
else
|
16
|
+
raise SettingTypeNotAllowed, "Setting values of type #{value.class} not allowed"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module SimpleCachedSettings
|
2
|
+
class Settings < ActiveRecord::Base
|
3
|
+
self.table_name = 'settings'
|
4
|
+
|
5
|
+
validates :key, uniqueness: true
|
6
|
+
|
7
|
+
serialize :value, SettingSerializer
|
8
|
+
|
9
|
+
class << self
|
10
|
+
def write(key, value)
|
11
|
+
setting = find_or_initialize_by(key: key)
|
12
|
+
setting.value = value
|
13
|
+
setting.save
|
14
|
+
end
|
15
|
+
|
16
|
+
def read(key)
|
17
|
+
value = Rails.cache.read(__cache_key(key))
|
18
|
+
return value if value
|
19
|
+
|
20
|
+
setting = find_by(key: key)
|
21
|
+
setting.value if setting
|
22
|
+
end
|
23
|
+
|
24
|
+
def __cache_key(setting_key)
|
25
|
+
"simple_settings:#{setting_key}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
after_commit :update_cache, on: [:create, :update]
|
30
|
+
def update_cache
|
31
|
+
Rails.cache.write(self.class.__cache_key(key), value)
|
32
|
+
end
|
33
|
+
|
34
|
+
after_commit :expire_cache, on: [:destroy]
|
35
|
+
def expire_cache
|
36
|
+
Rails.cache.delete(self.class.__cache_key(key))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/settings.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'simple_cached_settings/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'simple_cached_settings'
|
8
|
+
spec.version = SimpleCachedSettings::VERSION
|
9
|
+
spec.authors = ['Le6ow5k1']
|
10
|
+
spec.email = ['Le6oww5k1@gmail.com']
|
11
|
+
spec.summary = %q{Gem for storing global configuration settings in an ActiveRecord::Model}
|
12
|
+
spec.homepage = 'https://github.com/Le6ow5k1/simple_cached_settings'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
|
20
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
21
|
+
spec.add_development_dependency 'rake'
|
22
|
+
spec.add_development_dependency 'rspec-rails'
|
23
|
+
spec.add_development_dependency 'factory_girl'
|
24
|
+
spec.add_development_dependency 'sqlite3'
|
25
|
+
spec.add_development_dependency 'database_cleaner'
|
26
|
+
spec.add_development_dependency 'rails', '>= 4.0.0'
|
27
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MySettings do
|
4
|
+
describe '.write' do
|
5
|
+
context 'when row with this key does not exist' do
|
6
|
+
it 'creates new instance of Settings model' do
|
7
|
+
expect { described_class.write(:god_mode, true) }.to change { described_class.count }.by(1)
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'saves instance with right key and value fields' do
|
11
|
+
described_class.write(:god_mode, true)
|
12
|
+
setting = described_class.find_by(key: :god_mode)
|
13
|
+
|
14
|
+
expect(setting.key).to eq('god_mode')
|
15
|
+
expect(setting.value).to be true
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'when row with this key exist' do
|
20
|
+
before do
|
21
|
+
@setting = described_class.create(key: :password, value: 'qwerty')
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'does not create new instance of Settings model' do
|
25
|
+
expect { described_class.write(@setting.key, '123') }.to_not change { described_class.count }
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'updates value of existing Settings model' do
|
29
|
+
described_class.write(@setting.key, '1')
|
30
|
+
expect(@setting.reload.value).to eq('1')
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe '.read' do
|
36
|
+
context 'when there is a setting with such key' do
|
37
|
+
before do
|
38
|
+
@setting = described_class.create(key: :number, value: 10)
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'returns value of that setting' do
|
42
|
+
expect(described_class.read(@setting.key)).to eq(@setting.value)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context 'when there is no setting with such key' do
|
47
|
+
it 'returns nil' do
|
48
|
+
expect(described_class.read(:non_existing_key)).to be nil
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'active_record'
|
3
|
+
require 'active_support'
|
4
|
+
require 'sqlite3'
|
5
|
+
require 'database_cleaner'
|
6
|
+
require 'rails/railtie'
|
7
|
+
|
8
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
9
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'rails'))
|
10
|
+
require 'simple_settings'
|
11
|
+
|
12
|
+
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
|
13
|
+
ActiveRecord::Schema.verbose = false
|
14
|
+
ActiveRecord::Schema.define(version: 1) do
|
15
|
+
create_table :settings do |t|
|
16
|
+
t.string :key, null: false
|
17
|
+
t.text :value
|
18
|
+
t.timestamps null: false
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
module Rails
|
23
|
+
def self.cache
|
24
|
+
@cache ||= ActiveSupport::Cache::MemoryStore.new
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class ::MySettings < SimpleSettings::Settings
|
29
|
+
self.table_name = 'settings'
|
30
|
+
end
|
31
|
+
|
32
|
+
RSpec.configure do |config|
|
33
|
+
config.warnings = false
|
34
|
+
|
35
|
+
config.expect_with :rspec do |expectations|
|
36
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
37
|
+
end
|
38
|
+
|
39
|
+
config.mock_with :rspec do |mocks|
|
40
|
+
mocks.verify_partial_doubles = true
|
41
|
+
end
|
42
|
+
|
43
|
+
config.before(:suite) do
|
44
|
+
DatabaseCleaner.strategy = :transaction
|
45
|
+
DatabaseCleaner.clean_with(:truncation)
|
46
|
+
end
|
47
|
+
|
48
|
+
config.around(:each) do |example|
|
49
|
+
DatabaseCleaner.cleaning do
|
50
|
+
example.run
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
config.before(:all) do
|
55
|
+
Rails.cache.clear
|
56
|
+
end
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,160 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_cached_settings
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Le6ow5k1
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec-rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: factory_girl
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sqlite3
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: database_cleaner
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rails
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 4.0.0
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 4.0.0
|
111
|
+
description:
|
112
|
+
email:
|
113
|
+
- Le6oww5k1@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- ".rspec"
|
120
|
+
- Gemfile
|
121
|
+
- LICENSE.txt
|
122
|
+
- README.md
|
123
|
+
- Rakefile
|
124
|
+
- lib/generators/simple_cached_settings/simple_cached_settings_generator.rb
|
125
|
+
- lib/generators/simple_cached_settings/templates/migration.rb
|
126
|
+
- lib/generators/simple_cached_settings/templates/model.rb
|
127
|
+
- lib/simple_cached_settings.rb
|
128
|
+
- lib/simple_cached_settings/setting_serializer.rb
|
129
|
+
- lib/simple_cached_settings/settings.rb
|
130
|
+
- lib/simple_cached_settings/version.rb
|
131
|
+
- settings.gemspec
|
132
|
+
- spec/simple_settings/settings_spec.rb
|
133
|
+
- spec/spec_helper.rb
|
134
|
+
homepage: https://github.com/Le6ow5k1/simple_cached_settings
|
135
|
+
licenses:
|
136
|
+
- MIT
|
137
|
+
metadata: {}
|
138
|
+
post_install_message:
|
139
|
+
rdoc_options: []
|
140
|
+
require_paths:
|
141
|
+
- lib
|
142
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
143
|
+
requirements:
|
144
|
+
- - ">="
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
version: '0'
|
147
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
148
|
+
requirements:
|
149
|
+
- - ">="
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
version: '0'
|
152
|
+
requirements: []
|
153
|
+
rubyforge_project:
|
154
|
+
rubygems_version: 2.2.2
|
155
|
+
signing_key:
|
156
|
+
specification_version: 4
|
157
|
+
summary: Gem for storing global configuration settings in an ActiveRecord::Model
|
158
|
+
test_files:
|
159
|
+
- spec/simple_settings/settings_spec.rb
|
160
|
+
- spec/spec_helper.rb
|