persistent_settings 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ rvm:
2
+ - 1.9.2
3
+ - 1.9.3
4
+ - rbx
5
+ - 1.8.7
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in persistent_settings.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # Persistent Settings
2
+
3
+ A simple key value store for ActiveRecord based applications.
4
+
5
+ Best used to store global app settings.
6
+
7
+ ## Installation
8
+
9
+ Add it to your Gemfile
10
+
11
+ gem 'persistent_settings'
12
+
13
+ Install
14
+
15
+ bundle install
16
+
17
+ ## Usage
18
+
19
+ The gem adds a new class called Settings. It will automatically create new
20
+ keys as you assign a value to them.
21
+
22
+ Settings.a_key = 'value'
23
+
24
+ Reload your app:
25
+
26
+ Settings.a_key # => 'value'
27
+
28
+ It accepts all kinds of objects as the value
29
+
30
+
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ require "cucumber/rake/task"
4
+
5
+ RSpec::Core::RakeTask.new
6
+ Cucumber::Rake::Task.new
7
+
8
+ task :default => [ :spec, :cucumber ]
@@ -0,0 +1,6 @@
1
+ Feature: Settings class
2
+
3
+ Scenario: Assign a new global setting
4
+ Given I assign "'a value'" to "a_key"
5
+ Then Settings should respond to "a_key"
6
+ Then "a_key" should hold "a value"
@@ -0,0 +1,11 @@
1
+ Given /^I assign "([^"]*)" to "([^"]*)"$/ do |value, key|
2
+ eval "Settings.#{key} = #{value}"
3
+ end
4
+
5
+ Then /^Settings should respond to "([^"]*)"$/ do |key|
6
+ Settings.should respond_to(key)
7
+ end
8
+
9
+ Then /^"([^"]*)" should hold "([^"]*)"$/ do |key, value|
10
+ Settings.send(key).should eq(value)
11
+ end
@@ -0,0 +1,9 @@
1
+ require 'persistent_settings'
2
+
3
+ ::ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
4
+ ::ActiveRecord::Schema.define(:version => 1) do
5
+ create_table :settings do |t|
6
+ t.column :var, :string
7
+ t.column :value, :string
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ require "active_record"
2
+ require "settings"
3
+ require "settings/version"
@@ -0,0 +1,3 @@
1
+ class Settings < ActiveRecord::Base
2
+ VERSION = "1.0.0"
3
+ end
data/lib/settings.rb ADDED
@@ -0,0 +1,48 @@
1
+ class Settings < ActiveRecord::Base
2
+ serialize :value
3
+
4
+ def self.method_missing(method_name, *args)
5
+ if assignation?(method_name)
6
+ self.define_setter_and_getter(method_name)
7
+ self.send(method_name, args.first)
8
+ else
9
+ super
10
+ end
11
+ end
12
+
13
+ def self.define_setter_and_getter(method_name)
14
+ getter = method_name.to_s.chop
15
+
16
+ (class << self; self; end).instance_eval do
17
+ define_method method_name do |value|
18
+ persist(getter, value)
19
+ instance_variable_set(:"@#{getter}", value)
20
+ end
21
+
22
+ define_method getter do
23
+ instance_variable_get(:"@#{getter}")
24
+ end
25
+ end
26
+ end
27
+
28
+ def self.assignation?(method_name)
29
+ method_name.to_s.match(/=$/)
30
+ end
31
+
32
+ def self.persist(getter, value)
33
+ setting = Settings.where(:var => getter).last
34
+ if setting
35
+ setting.update_attribute(:value, value)
36
+ else
37
+ Settings.create(:var => getter, :value => value)
38
+ end
39
+ end
40
+
41
+ def self.load_from_persistance
42
+ self.all.each do |setting|
43
+ self.send("#{setting.var}=", setting.value)
44
+ end
45
+ end
46
+
47
+ load_from_persistance if connected? && table_exists?
48
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "persistent_settings"
6
+ s.version = "1.0.0"
7
+ s.authors = ["David Padilla"]
8
+ s.email = ["david@crowdint.com"]
9
+ s.homepage = ""
10
+ s.summary = %q{Simple global Settings class for ActiveRecord based applications}
11
+ s.description = %q{Simple global Settings class for ActiveRecord based applications}
12
+
13
+ s.rubyforge_project = "persistent_settings"
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 'activerecord', '~> 3'
21
+ s.add_development_dependency "cucumber" , "~> 1.1.4"
22
+ s.add_development_dependency "rake"
23
+ s.add_development_dependency "rspec" , "~> 2.7.0"
24
+ s.add_development_dependency "sqlite3" , "~> 1.3"
25
+ end
@@ -0,0 +1,84 @@
1
+ require 'spec_helper'
2
+
3
+ describe Settings do
4
+ before do
5
+ end
6
+ describe :assignation? do
7
+ context "the method name ends with =" do
8
+ it "returns true" do
9
+ Settings.assignation?("method=").should be_true
10
+ end
11
+ end
12
+
13
+ context "the method name does not end with =" do
14
+ it "returns false" do
15
+ Settings.assignation?("method").should be_false
16
+ end
17
+ end
18
+ end
19
+
20
+ describe :define_setter_and_getter do
21
+ it "creates the setter and getter methods" do
22
+ Settings.define_setter_and_getter(:method_name=)
23
+ Settings.should respond_to(:method_name=)
24
+ Settings.should respond_to(:method_name)
25
+ Settings.should_receive(:persist)
26
+ Settings.method_name = 'value'
27
+ Settings.method_name.should == 'value'
28
+ end
29
+ end
30
+
31
+ describe :persist do
32
+ context "a value already persisted" do
33
+ before :each do
34
+ @setting = mock(Settings)
35
+ Settings.stub_chain(:where, :last).and_return(@setting)
36
+ end
37
+
38
+ it "updates the setting" do
39
+ @setting.should_receive(:update_attribute).with(:value, :new_value)
40
+ Settings.persist('some_method', :new_value)
41
+ end
42
+ end
43
+
44
+ context "a value that is not persisted" do
45
+ it "creates the setting" do
46
+ Settings.stub_chain(:where, :last).and_return(nil)
47
+ Settings.should_receive(:create).with(:var => 'new_method', :value => 'value')
48
+ Settings.persist('new_method', 'value')
49
+ end
50
+ end
51
+ end
52
+
53
+ describe :method_missing do
54
+ context "A method ending with =" do
55
+ it "calls define_setter_and_getter" do
56
+ Settings.should_receive(:define_setter_and_getter).with(:new_method=)
57
+ Settings.should_receive(:new_method=).with('value')
58
+ Settings.method_missing(:new_method=, 'value')
59
+ end
60
+ end
61
+
62
+ context "method that does not end with =" do
63
+ context "already persisted" do
64
+ before :each do
65
+ @setting = mock(Settings, :var => 'persisted_method', :value => 'some value')
66
+ Settings.stub(:all).and_return([@setting])
67
+ end
68
+
69
+ it "pulls the value from the database and creates its setter and getter" do
70
+ Settings.should_receive(:persisted_method=).with('some value')
71
+ Settings.load_from_persistance
72
+ end
73
+ end
74
+
75
+ context "not persisited" do
76
+ it "blows up" do
77
+ lambda {
78
+ Settings.just_a_method
79
+ }.should raise_error(NoMethodError)
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,5 @@
1
+ require 'active_record'
2
+ require 'persistent_settings'
3
+
4
+ ::ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
5
+
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: persistent_settings
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Padilla
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: &70264754810580 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70264754810580
25
+ - !ruby/object:Gem::Dependency
26
+ name: cucumber
27
+ requirement: &70264754810100 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 1.1.4
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70264754810100
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &70264754809720 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70264754809720
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &70264754809180 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 2.7.0
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70264754809180
58
+ - !ruby/object:Gem::Dependency
59
+ name: sqlite3
60
+ requirement: &70264754808680 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: '1.3'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70264754808680
69
+ description: Simple global Settings class for ActiveRecord based applications
70
+ email:
71
+ - david@crowdint.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .travis.yml
78
+ - Gemfile
79
+ - README.md
80
+ - Rakefile
81
+ - features/settings.feature
82
+ - features/step_definitions/settings_steps.rb
83
+ - features/support/env.rb
84
+ - lib/persistent_settings.rb
85
+ - lib/settings.rb
86
+ - lib/settings/version.rb
87
+ - persistent_settings.gemspec
88
+ - spec/lib/settings_spec.rb
89
+ - spec/spec_helper.rb
90
+ homepage: ''
91
+ licenses: []
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project: persistent_settings
110
+ rubygems_version: 1.8.10
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: Simple global Settings class for ActiveRecord based applications
114
+ test_files:
115
+ - features/settings.feature
116
+ - features/step_definitions/settings_steps.rb
117
+ - features/support/env.rb
118
+ - spec/lib/settings_spec.rb
119
+ - spec/spec_helper.rb