kaka-rails-settings 1.0.4 → 1.0.5

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.
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ describe Settings do
4
+ before :each do
5
+ Settings.destroy_all
6
+ end
7
+
8
+ context "setting value" do
9
+ it "test setting" do
10
+ Settings.foo = "join"
11
+ Settings.foo.should eq("join")
12
+ Settings[:foo] = "Dom"
13
+ Settings.foo.should eq("Dom")
14
+ end
15
+
16
+ it "test destroy a key setting" do
17
+ Settings.foo = "abc"
18
+ Settings.foo.should eq("abc")
19
+ Settings.destroy(:foo)
20
+ Settings.foo.should be_nil
21
+ end
22
+
23
+ it "test destroy all setting " do
24
+ Settings.password = "join"
25
+ Settings.foo = "dom1"
26
+ Settings.foo1 = "dom2"
27
+ Settings.all.length.should eq(3)
28
+ Settings.destroy_all
29
+ Settings.all.length.should eq(0)
30
+ end
31
+ end
32
+
33
+ context "polymorphic setting value" do
34
+ before :each do
35
+ User.destroy_all
36
+ @user = User.create!(:name => "kk",:login=> 'kaka',:password => '123456',:email => "kaka@qq.com")
37
+ end
38
+
39
+ it "test setting" do
40
+ @user.settings.foo = "join"
41
+ @user.settings.foo.should eq("join")
42
+ @user.settings[:foo] = "kaka"
43
+ @user.settings[:foo].should eq("kaka")
44
+ end
45
+
46
+ it "test destroy a key setting" do
47
+ @user.settings.password = "fdsafdsa"
48
+ @user.settings.password.should eq("fdsafdsa")
49
+ @user.settings.destroy(:password)
50
+ @user.settings.password.should be_nil
51
+ end
52
+
53
+ it "test destroy all key" do
54
+ @user.settings.status = "yes"
55
+ @user.settings.password = "fdsafdsa"
56
+ @user.settings.foo = "kaka"
57
+ @user.settings.all.length.should eq(3)
58
+ @user.settings.destroy_all
59
+ @user.settings.all.length.should eq(0)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require File.expand_path("../lib/init",__FILE__)
3
+
4
+ ENV["RAILS_ENV"] ||= 'test'
5
+
6
+ RSpec.configure do |config|
7
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kaka-rails-settings
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -18,9 +18,8 @@ extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - lib/settings.rb
21
- - lib/settings/settings.rb
22
- - lib/settings/setting_functional.rb
23
- - lib/settings/model_relaction.rb
21
+ - spec/settings_spec.rb
22
+ - spec/spec_helper.rb
24
23
  homepage: https://github.com/huxinghai1988/rails-settings.git
25
24
  licenses: []
26
25
  post_install_message:
@@ -45,4 +44,6 @@ rubygems_version: 1.8.24
45
44
  signing_key:
46
45
  specification_version: 3
47
46
  summary: rails use settings support mysql,mongodb...
48
- test_files: []
47
+ test_files:
48
+ - spec/settings_spec.rb
49
+ - spec/spec_helper.rb
@@ -1,17 +0,0 @@
1
- module Common
2
- module Setting
3
- def self.included(base)
4
- base.extend(ClassMethods)
5
- end
6
-
7
- module ClassMethods
8
- def has_settings
9
- class_eval do
10
- def settings
11
- Settings.new(self)
12
- end
13
- end
14
- end
15
- end
16
- end
17
- end
@@ -1,68 +0,0 @@
1
- module SettingFunctional
2
- module Method
3
- attr_accessor :target
4
-
5
- def []=(key, value)
6
- add_key(key, value)
7
- end
8
-
9
- def [](key)
10
- get_key(key)
11
- end
12
-
13
- def destroy_all
14
- get_setting.first.destroy_all
15
- end
16
-
17
- def destroy(key)
18
- get_setting(key).first.destroy
19
- end
20
-
21
- def all
22
- keys = get_setting.first
23
- keys.map do | k |
24
- temp = {}
25
- temp[k.var] = k.value
26
- temp
27
- end
28
- end
29
-
30
- def method_missing(method_name, *args)
31
- rule_method(method_name, *args)
32
- end
33
-
34
- private
35
- def get_key(key)
36
- temp = get_setting(key).first
37
- temp.first.nil? ? nil : temp.first.value
38
- end
39
-
40
- def add_key(key, value)
41
- setting = get_setting(key)
42
- _setting = setting.first
43
- _setting.first.nil? ? Setting.create!(setting.last.merge!({value: value})) : _setting.first.update_attribute(:value, value)
44
- end
45
-
46
- def rule_method(method_name, *args)
47
- method = method_name.to_s
48
- if self.respond_to?(method_name)
49
- super(method_name, *args)
50
- else
51
- if method =~ /=$/
52
- add_key(method.gsub(/=$/,''), args.first)
53
- else
54
- get_key(method)
55
- end
56
- end
57
- end
58
-
59
- def get_setting(key = nil)
60
- options = key.nil? ? {} : {:var => key.to_s}
61
- options.merge!({:target_id => nil, :target_type => nil})
62
- options.merge!({:target_id => target.id, :target_type => target.class}) unless target.nil?
63
-
64
- temp = Setting.where(options)
65
- [temp, options]
66
- end
67
- end
68
- end
@@ -1,8 +0,0 @@
1
- class Settings
2
- include SettingFunctional::Method
3
- extend SettingFunctional::Method
4
-
5
- def initialize(target = nil)
6
- @target = target
7
- end
8
- end