settis 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/settis.rb +2 -1
- data/lib/settis/container.rb +11 -17
- data/lib/settis/serializer.rb +47 -0
- data/{spec/settis/settings/integer_spec.rb → lib/settis/serializers/active_record.rb} +15 -12
- data/lib/settis/{settings → serializers}/boolean.rb +10 -8
- data/lib/settis/{settings.rb → serializers/integer.rb} +15 -4
- data/lib/settis/{settings/integer.rb → serializers/string.rb} +13 -4
- data/lib/settis/setting.rb +53 -0
- data/lib/settis/settings/scalar.rb +13 -47
- data/lib/settis/version.rb +1 -1
- data/spec/settis/container_spec.rb +14 -14
- data/spec/settis/serializer_spec.rb +40 -0
- data/spec/settis/serializers/active_record_spec.rb +48 -0
- data/spec/settis/{settings → serializers}/boolean_spec.rb +15 -15
- data/spec/settis/serializers/integer_spec.rb +43 -0
- data/spec/settis/serializers/string_spec.rb +43 -0
- data/spec/settis/setting_spec.rb +59 -0
- data/spec/settis/settings/scalar_spec.rb +15 -50
- data/spec/settis_spec.rb +2 -2
- data/spec/spec_helper.rb +4 -0
- metadata +27 -18
data/lib/settis.rb
CHANGED
data/lib/settis/container.rb
CHANGED
@@ -18,19 +18,19 @@ module Settis
|
|
18
18
|
module Container
|
19
19
|
# @private
|
20
20
|
def redis
|
21
|
-
@redis ||= Redis::Namespace.new(
|
21
|
+
@redis ||= Redis::Namespace.new(namespace, :redis => Settis.redis)
|
22
22
|
end
|
23
23
|
|
24
|
-
# @overload
|
25
|
-
# Sets the
|
24
|
+
# @overload namespace(value)
|
25
|
+
# Sets the namespace prefix to be added before all Redis key names.
|
26
26
|
# @param [String] value The prefix to add.
|
27
|
-
# @overload
|
28
|
-
# Gets the
|
29
|
-
def
|
27
|
+
# @overload namespace
|
28
|
+
# Gets the namespace prefix to be added before all Redis key names.
|
29
|
+
def namespace(value = nil)
|
30
30
|
if value.nil?
|
31
|
-
@
|
31
|
+
@namespace ||= 'settings'
|
32
32
|
else
|
33
|
-
@
|
33
|
+
@namespace = value
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
@@ -44,18 +44,12 @@ module Settis
|
|
44
44
|
@settings ||= {}
|
45
45
|
|
46
46
|
name = name.to_s
|
47
|
-
|
48
47
|
raise ArgumentError, "The setting '#{name}' already exists." if @settings.key?(name)
|
49
48
|
|
50
|
-
|
51
|
-
|
52
|
-
when :integer then Settings::Integer
|
53
|
-
when :boolean then Settings::Boolean
|
54
|
-
else
|
55
|
-
raise ArgumentError, "Unknown type '#{type}'."
|
56
|
-
end
|
49
|
+
serializer = Serializer.for(type)
|
50
|
+
raise ArgumentError, "Unknown setting type '#{type}'." if serializer.nil?
|
57
51
|
|
58
|
-
@settings[name] =
|
52
|
+
@settings[name] = ScalarSetting.new(self, name, serializer.new(type), options)
|
59
53
|
end
|
60
54
|
end
|
61
55
|
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Copyright 2011 Brennan Frydl (http://bfrydl.com/)
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
module Settis
|
18
|
+
class Serializer
|
19
|
+
class << self
|
20
|
+
def inherited(subclass)
|
21
|
+
@serializers ||= []
|
22
|
+
@serializers << subclass
|
23
|
+
end
|
24
|
+
|
25
|
+
def for(type)
|
26
|
+
@serializers ||= []
|
27
|
+
@serializers.detect {|s| s.serializes?(type) }
|
28
|
+
end
|
29
|
+
|
30
|
+
def serializes?(type) ; false ; end
|
31
|
+
end
|
32
|
+
|
33
|
+
def initialize(type)
|
34
|
+
@type = type
|
35
|
+
end
|
36
|
+
|
37
|
+
protected
|
38
|
+
|
39
|
+
attr_reader :type
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
require 'settis/serializers/string'
|
44
|
+
require 'settis/serializers/integer'
|
45
|
+
require 'settis/serializers/boolean'
|
46
|
+
|
47
|
+
require 'settis/serializers/active_record' if defined?(ActiveRecord)
|
@@ -14,21 +14,24 @@
|
|
14
14
|
# See the License for the specific language governing permissions and
|
15
15
|
# limitations under the License.
|
16
16
|
|
17
|
-
|
17
|
+
module Settis
|
18
|
+
class ActiveRecordSerializer < Serializer
|
19
|
+
def self.serializes?(type)
|
20
|
+
type.is_a?(Class) and type.ancestors.include?(ActiveRecord::Base)
|
21
|
+
end
|
18
22
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
+
def serialize(value)
|
24
|
+
case value
|
25
|
+
when Integer then value.to_s
|
26
|
+
when type then value.id.to_s
|
27
|
+
when nil then nil
|
28
|
+
else
|
29
|
+
raise ArgumentError, "Setting value must be an instance of #{type}, an ID number, or nil"
|
30
|
+
end
|
23
31
|
end
|
24
32
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
describe '#deserialize' do
|
29
|
-
it 'converts the value to an integer' do
|
30
|
-
@integer.send(:deserialize, '34').should == 34
|
31
|
-
@integer.send(:deserialize, 'not an int').should == 0
|
33
|
+
def deserialize(value)
|
34
|
+
type.find_by_id(value) unless value.nil?
|
32
35
|
end
|
33
36
|
end
|
34
37
|
end
|
@@ -15,15 +15,17 @@
|
|
15
15
|
# limitations under the License.
|
16
16
|
|
17
17
|
module Settis
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
18
|
+
class BooleanSerializer < Serializer
|
19
|
+
def self.serializes?(type)
|
20
|
+
type == :boolean
|
21
|
+
end
|
22
|
+
|
23
|
+
def serialize(value)
|
24
|
+
(value != false).to_s unless value.nil?
|
25
|
+
end
|
23
26
|
|
24
|
-
|
25
|
-
|
26
|
-
end
|
27
|
+
def deserialize(value)
|
28
|
+
value != 'false' unless value.nil?
|
27
29
|
end
|
28
30
|
end
|
29
31
|
end
|
@@ -15,9 +15,20 @@
|
|
15
15
|
# limitations under the License.
|
16
16
|
|
17
17
|
module Settis
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
class IntegerSerializer < Serializer
|
19
|
+
def self.serializes?(type)
|
20
|
+
case type
|
21
|
+
when :integer then true
|
22
|
+
when Class then type == Integer or type.ancestors.include?(Integer)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def serialize(value)
|
27
|
+
value.to_i.to_s unless value.nil? or !value.respond_to?(:to_i)
|
28
|
+
end
|
29
|
+
|
30
|
+
def deserialize(value)
|
31
|
+
value.to_i unless value.nil?
|
32
|
+
end
|
22
33
|
end
|
23
34
|
end
|
@@ -15,11 +15,20 @@
|
|
15
15
|
# limitations under the License.
|
16
16
|
|
17
17
|
module Settis
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
class StringSerializer < Serializer
|
19
|
+
def self.serializes?(type)
|
20
|
+
case type
|
21
|
+
when :string, 'string' then true
|
22
|
+
when Class then type == String or type.ancestors.include?(String)
|
22
23
|
end
|
23
24
|
end
|
25
|
+
|
26
|
+
def serialize(value)
|
27
|
+
value.to_s unless value.nil?
|
28
|
+
end
|
29
|
+
|
30
|
+
def deserialize(value)
|
31
|
+
value.to_s unless value.nil?
|
32
|
+
end
|
24
33
|
end
|
25
34
|
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Copyright 2011 Brennan Frydl (http://bfrydl.com/)
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
module Settis
|
18
|
+
class Setting
|
19
|
+
attr_reader :container, :name
|
20
|
+
protected :container
|
21
|
+
|
22
|
+
def initialize(container, name, options = {})
|
23
|
+
@container = container
|
24
|
+
@name = name
|
25
|
+
|
26
|
+
class << container ; self ; end.instance_exec(self) do |this|
|
27
|
+
define_method name do
|
28
|
+
this.get
|
29
|
+
end
|
30
|
+
|
31
|
+
define_method :"#{name}=" do |value|
|
32
|
+
this.set(value)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def get
|
38
|
+
@container.redis.get(name)
|
39
|
+
end
|
40
|
+
|
41
|
+
def set(value)
|
42
|
+
if value.nil?
|
43
|
+
@container.redis.del(name)
|
44
|
+
else
|
45
|
+
@container.redis.set(name, value)
|
46
|
+
end
|
47
|
+
|
48
|
+
value
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
require 'settis/settings/scalar'
|
@@ -15,56 +15,22 @@
|
|
15
15
|
# limitations under the License.
|
16
16
|
|
17
17
|
module Settis
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
class ScalarSetting < Setting
|
19
|
+
def initialize(container, name, serializer, options = {})
|
20
|
+
@serializer = serializer
|
21
|
+
@default = options.delete(:default)
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
@class = klass
|
25
|
-
@default = options[:default]
|
26
|
-
|
27
|
-
define_methods
|
28
|
-
end
|
29
|
-
|
30
|
-
def get
|
31
|
-
value = @class.redis.get(@name)
|
32
|
-
|
33
|
-
if value.nil?
|
34
|
-
@default
|
35
|
-
else
|
36
|
-
deserialize(value.to_s)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def set(value)
|
41
|
-
if value.nil?
|
42
|
-
@class.redis.del(@name)
|
43
|
-
else
|
44
|
-
@class.redis.set(@name, serialize(value))
|
45
|
-
end
|
46
|
-
|
47
|
-
value
|
48
|
-
end
|
49
|
-
|
50
|
-
protected
|
51
|
-
|
52
|
-
def serialize(value) ; value.to_s ; end
|
53
|
-
def deserialize(value) ; value ; end
|
54
|
-
|
55
|
-
def define_methods
|
56
|
-
setting = self
|
23
|
+
super(container, name, options)
|
24
|
+
end
|
57
25
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
26
|
+
def get
|
27
|
+
value = @serializer.deserialize(super)
|
28
|
+
value || @default
|
29
|
+
end
|
62
30
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
end
|
67
|
-
end
|
31
|
+
def set(value)
|
32
|
+
super(@serializer.serialize(value))
|
33
|
+
value
|
68
34
|
end
|
69
35
|
end
|
70
36
|
end
|
data/lib/settis/version.rb
CHANGED
@@ -18,40 +18,40 @@ require 'spec_helper'
|
|
18
18
|
|
19
19
|
describe Settis::Container do
|
20
20
|
before :each do
|
21
|
-
@
|
21
|
+
@container = Class.new do
|
22
22
|
extend Settis::Container
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
-
describe '#
|
27
|
-
it 'stores the
|
28
|
-
@
|
29
|
-
@
|
30
|
-
@
|
26
|
+
describe '#namespace' do
|
27
|
+
it 'stores the namespace prefix for Redis keys' do
|
28
|
+
@container.namespace.should == 'settings'
|
29
|
+
@container.namespace 'test'
|
30
|
+
@container.namespace.should == 'test'
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
34
|
describe '#redis' do
|
35
35
|
it 'returns a Redis::Namespace specific to the container' do
|
36
|
-
stub(@
|
36
|
+
stub(@container).namespace { 'configuration' }
|
37
37
|
|
38
|
-
@
|
39
|
-
@
|
38
|
+
@container.redis.should be_a(Redis::Namespace)
|
39
|
+
@container.redis.namespace.should == 'configuration'
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
43
|
describe '#setting' do
|
44
44
|
it 'defines a new setting' do
|
45
|
-
mock(Settis::
|
45
|
+
mock(Settis::ScalarSetting).new(@container, 'title', is_a(Settis::StringSerializer), :default => 'Hello World')
|
46
46
|
|
47
|
-
@
|
47
|
+
@container.setting :title, :string, :default => 'Hello World'
|
48
48
|
end
|
49
49
|
|
50
50
|
it 'does not allow duplicate names' do
|
51
|
-
stub(Settis::
|
52
|
-
@
|
51
|
+
stub(Settis::ScalarSetting).new.once
|
52
|
+
@container.setting :title, :string
|
53
53
|
|
54
|
-
expect { @
|
54
|
+
expect { @container.setting :title, :integer }.to raise_error(ArgumentError)
|
55
55
|
end
|
56
56
|
end
|
57
57
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Copyright 2011 Brennan Frydl (http://bfrydl.com/)
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require 'spec_helper'
|
18
|
+
|
19
|
+
describe Settis::Serializer do
|
20
|
+
before :all do
|
21
|
+
@serializer = Class.new(Settis::Serializer)
|
22
|
+
end
|
23
|
+
|
24
|
+
describe '#inherited' do
|
25
|
+
it 'adds the subclass to the list of serializers' do
|
26
|
+
Settis::Serializer.class_eval { @serializers }.should include @serializer
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#for' do
|
31
|
+
it 'returns the appropriate serializer' do
|
32
|
+
Settis::Serializer.for(:string).should == Settis::StringSerializer
|
33
|
+
Settis::Serializer.for(Fixnum).should == Settis::IntegerSerializer
|
34
|
+
|
35
|
+
Settis::Serializer.for(:testing).should be_nil
|
36
|
+
stub(@serializer).serializes?(:testing) { true }
|
37
|
+
Settis::Serializer.for(:testing).should == @serializer
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Copyright 2011 Brennan Frydl (http://bfrydl.com/)
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require 'spec_helper'
|
18
|
+
|
19
|
+
describe Settis::ActiveRecordSerializer do
|
20
|
+
before :all do
|
21
|
+
@model = Class.new(ActiveRecord::Base)
|
22
|
+
@serializer = Settis::ActiveRecordSerializer.new(@model)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'is registered' do
|
26
|
+
Settis::Serializer.for(@model).should == Settis::ActiveRecordSerializer
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '#serialize' do
|
30
|
+
it 'converts the value to its ID number' do
|
31
|
+
stub(value = @model.new).id { 4 }
|
32
|
+
|
33
|
+
@serializer.serialize(nil).should be_nil
|
34
|
+
@serializer.serialize(value).should == '4'
|
35
|
+
@serializer.serialize(9).should == '9'
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '#deserialize' do
|
40
|
+
it 'converts the value to an integer' do
|
41
|
+
stub(@model).find_by_id {|id| :model if id == '4' }
|
42
|
+
|
43
|
+
@serializer.deserialize(nil).should be_nil
|
44
|
+
@serializer.deserialize('4').should == :model
|
45
|
+
@serializer.deserialize('5').should be_nil
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -16,28 +16,28 @@
|
|
16
16
|
|
17
17
|
require 'spec_helper'
|
18
18
|
|
19
|
-
describe Settis::
|
20
|
-
before :
|
21
|
-
|
22
|
-
|
23
|
-
end
|
19
|
+
describe Settis::BooleanSerializer do
|
20
|
+
before :all do
|
21
|
+
@serializer = Settis::BooleanSerializer.new(:boolean)
|
22
|
+
end
|
24
23
|
|
25
|
-
|
24
|
+
it 'is registered' do
|
25
|
+
Settis::Serializer.for(:boolean).should == Settis::BooleanSerializer
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
describe '#serialize' do
|
29
|
-
it 'converts the value to
|
30
|
-
@
|
31
|
-
@
|
32
|
-
@
|
29
|
+
it 'converts the value to a string containing "true" or "false"' do
|
30
|
+
@serializer.serialize(nil).should be_nil
|
31
|
+
@serializer.serialize(true).should == 'true'
|
32
|
+
@serializer.serialize(false).should == 'false'
|
33
33
|
end
|
34
34
|
end
|
35
|
-
|
35
|
+
|
36
36
|
describe '#deserialize' do
|
37
37
|
it 'converts the value to true or false' do
|
38
|
-
@
|
39
|
-
@
|
40
|
-
@
|
38
|
+
@serializer.deserialize(nil).should be_nil
|
39
|
+
@serializer.deserialize('true').should be_true
|
40
|
+
@serializer.deserialize('false').should be_false
|
41
41
|
end
|
42
42
|
end
|
43
43
|
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Copyright 2011 Brennan Frydl (http://bfrydl.com/)
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require 'spec_helper'
|
18
|
+
|
19
|
+
describe Settis::IntegerSerializer do
|
20
|
+
before :all do
|
21
|
+
@serializer = Settis::IntegerSerializer.new(:integer)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'is registered' do
|
25
|
+
Settis::Serializer.for(:integer).should == Settis::IntegerSerializer
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '#serialize' do
|
29
|
+
it 'converts the value to a string containing an integer' do
|
30
|
+
@serializer.serialize(nil).should be_nil
|
31
|
+
@serializer.serialize('34').should == '34'
|
32
|
+
@serializer.serialize(23).should == '23'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#deserialize' do
|
37
|
+
it 'converts the value to an integer' do
|
38
|
+
@serializer.deserialize(nil).should be_nil
|
39
|
+
@serializer.deserialize('34').should == 34
|
40
|
+
@serializer.deserialize('NaN').should == 0
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Copyright 2011 Brennan Frydl (http://bfrydl.com/)
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require 'spec_helper'
|
18
|
+
|
19
|
+
describe Settis::StringSerializer do
|
20
|
+
before :all do
|
21
|
+
@serializer = Settis::StringSerializer.new(:string)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'is registered' do
|
25
|
+
Settis::Serializer.for(:string).should == Settis::StringSerializer
|
26
|
+
end
|
27
|
+
|
28
|
+
describe '#serialize' do
|
29
|
+
it 'converts the value to a string' do
|
30
|
+
@serializer.serialize(nil).should be_nil
|
31
|
+
@serializer.serialize(:hello).should == 'hello'
|
32
|
+
@serializer.serialize('world').should == 'world'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#deserialize' do
|
37
|
+
it 'converts the value to a string (does nothing)' do
|
38
|
+
@serializer.deserialize(nil).should be_nil
|
39
|
+
@serializer.deserialize('hello').should == 'hello'
|
40
|
+
@serializer.deserialize('world').should == 'world'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Copyright 2011 Brennan Frydl (http://bfrydl.com/)
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require 'spec_helper'
|
18
|
+
|
19
|
+
describe Settis::Setting do
|
20
|
+
before do
|
21
|
+
@container = Class.new
|
22
|
+
@setting = Settis::Setting.new(@container, 'test')
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#new' do
|
26
|
+
it 'adds accessor methods to the target class' do
|
27
|
+
@container.should respond_to(:test)
|
28
|
+
@container.should respond_to(:test=)
|
29
|
+
|
30
|
+
mock(@setting).get { :get }
|
31
|
+
mock(@setting).set(:value)
|
32
|
+
|
33
|
+
@container.test.should == :get
|
34
|
+
@container.test = :value
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe '#get' do
|
39
|
+
it 'retrieves the value from Redis' do
|
40
|
+
stub(@container).redis.stub!.get('test') { 'value' }
|
41
|
+
|
42
|
+
@setting.get.should == 'value'
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '#set' do
|
47
|
+
it 'stores the value in Redis' do
|
48
|
+
mock(@container).redis.mock!.set('test', 'value')
|
49
|
+
|
50
|
+
@setting.set('value').should == 'value'
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'deletes the key if set to nil' do
|
54
|
+
mock(@container).redis.mock!.del('test')
|
55
|
+
|
56
|
+
@setting.set(nil)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -16,69 +16,34 @@
|
|
16
16
|
|
17
17
|
require 'spec_helper'
|
18
18
|
|
19
|
-
describe Settis::
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
scalar = Settis::Settings::Scalar.new('test', @class)
|
25
|
-
|
26
|
-
@class.should respond_to(:test)
|
27
|
-
@class.should respond_to(:test=)
|
28
|
-
|
29
|
-
mock(scalar).get { :get }
|
30
|
-
mock(scalar).set(:value)
|
31
|
-
|
32
|
-
@class.test.should == :get
|
33
|
-
@class.test = :value
|
34
|
-
end
|
19
|
+
describe Settis::ScalarSetting do
|
20
|
+
before :all do
|
21
|
+
@container = Class.new
|
22
|
+
@serializer = Class.new(Settis::Serializer)
|
23
|
+
@setting = Settis::ScalarSetting.new(@container, 'test', @serializer, :default => 'default')
|
35
24
|
end
|
36
25
|
|
37
26
|
describe '#get' do
|
38
|
-
|
39
|
-
any_instance_of(Settis::
|
40
|
-
|
27
|
+
it 'deserializes the value from Setting#get' do
|
28
|
+
any_instance_of(Settis::Setting) do |s|
|
29
|
+
mock(s).get { 'raw' }
|
41
30
|
end
|
42
31
|
|
43
|
-
@
|
44
|
-
@scalar = Settis::Settings::Scalar.new('test', @class, :default => 'default')
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'retrieves and deserializes the value from Redis' do
|
48
|
-
stub(@class).redis.stub!.get('test') { 'value' }
|
49
|
-
stub(@scalar).deserialize('value') { 'deserialized' }
|
32
|
+
mock(@serializer).deserialize('raw') { 'deserialized' }
|
50
33
|
|
51
|
-
@
|
52
|
-
end
|
53
|
-
|
54
|
-
it 'returns the default value if the key does not exist' do
|
55
|
-
stub(@class).redis.stub!.get('test')
|
56
|
-
|
57
|
-
@scalar.get.should == 'default'
|
34
|
+
@setting.get.should == 'deserialized'
|
58
35
|
end
|
59
36
|
end
|
60
37
|
|
61
38
|
describe '#set' do
|
62
|
-
|
63
|
-
any_instance_of(Settis::
|
64
|
-
|
39
|
+
it 'serializes the value and passes it to Setting#set' do
|
40
|
+
any_instance_of(Settis::Setting) do |s|
|
41
|
+
mock(s).set('serialized')
|
65
42
|
end
|
66
43
|
|
67
|
-
@
|
68
|
-
@scalar = Settis::Settings::Scalar.new('test', @class)
|
69
|
-
end
|
70
|
-
|
71
|
-
it 'serializes and stores the value in Redis' do
|
72
|
-
stub(@scalar).serialize('value') { 'serialized' }
|
73
|
-
mock(@class).redis.mock!.set('test', 'serialized')
|
74
|
-
|
75
|
-
@scalar.set('value')
|
76
|
-
end
|
77
|
-
|
78
|
-
it 'deletes the key if set to nil' do
|
79
|
-
mock(@class).redis.mock!.del('test')
|
44
|
+
mock(@serializer).serialize('raw') { 'serialized' }
|
80
45
|
|
81
|
-
@
|
46
|
+
@setting.set('raw').should == 'raw'
|
82
47
|
end
|
83
48
|
end
|
84
49
|
end
|
data/spec/settis_spec.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: settis
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,12 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-08-
|
13
|
-
default_executable:
|
12
|
+
date: 2011-08-09 00:00:00.000000000Z
|
14
13
|
dependencies:
|
15
14
|
- !ruby/object:Gem::Dependency
|
16
15
|
name: redis-namespace
|
17
|
-
requirement: &
|
16
|
+
requirement: &8531320 !ruby/object:Gem::Requirement
|
18
17
|
none: false
|
19
18
|
requirements:
|
20
19
|
- - ! '>='
|
@@ -22,10 +21,10 @@ dependencies:
|
|
22
21
|
version: '0'
|
23
22
|
type: :runtime
|
24
23
|
prerelease: false
|
25
|
-
version_requirements: *
|
24
|
+
version_requirements: *8531320
|
26
25
|
- !ruby/object:Gem::Dependency
|
27
26
|
name: rspec
|
28
|
-
requirement: &
|
27
|
+
requirement: &8530880 !ruby/object:Gem::Requirement
|
29
28
|
none: false
|
30
29
|
requirements:
|
31
30
|
- - ! '>='
|
@@ -33,10 +32,10 @@ dependencies:
|
|
33
32
|
version: '0'
|
34
33
|
type: :development
|
35
34
|
prerelease: false
|
36
|
-
version_requirements: *
|
35
|
+
version_requirements: *8530880
|
37
36
|
- !ruby/object:Gem::Dependency
|
38
37
|
name: rr
|
39
|
-
requirement: &
|
38
|
+
requirement: &8530460 !ruby/object:Gem::Requirement
|
40
39
|
none: false
|
41
40
|
requirements:
|
42
41
|
- - ! '>='
|
@@ -44,7 +43,7 @@ dependencies:
|
|
44
43
|
version: '0'
|
45
44
|
type: :development
|
46
45
|
prerelease: false
|
47
|
-
version_requirements: *
|
46
|
+
version_requirements: *8530460
|
48
47
|
description: Settis provides simple settings storage for web applications using Redis.
|
49
48
|
It is intended as a replacement for single-row tables and other relational database
|
50
49
|
solutions to the same problem.
|
@@ -63,19 +62,25 @@ files:
|
|
63
62
|
- Rakefile
|
64
63
|
- lib/settis.rb
|
65
64
|
- lib/settis/container.rb
|
66
|
-
- lib/settis/
|
67
|
-
- lib/settis/
|
68
|
-
- lib/settis/
|
65
|
+
- lib/settis/serializer.rb
|
66
|
+
- lib/settis/serializers/active_record.rb
|
67
|
+
- lib/settis/serializers/boolean.rb
|
68
|
+
- lib/settis/serializers/integer.rb
|
69
|
+
- lib/settis/serializers/string.rb
|
70
|
+
- lib/settis/setting.rb
|
69
71
|
- lib/settis/settings/scalar.rb
|
70
72
|
- lib/settis/version.rb
|
71
73
|
- settis.gemspec
|
72
74
|
- spec/settis/container_spec.rb
|
73
|
-
- spec/settis/
|
74
|
-
- spec/settis/
|
75
|
+
- spec/settis/serializer_spec.rb
|
76
|
+
- spec/settis/serializers/active_record_spec.rb
|
77
|
+
- spec/settis/serializers/boolean_spec.rb
|
78
|
+
- spec/settis/serializers/integer_spec.rb
|
79
|
+
- spec/settis/serializers/string_spec.rb
|
80
|
+
- spec/settis/setting_spec.rb
|
75
81
|
- spec/settis/settings/scalar_spec.rb
|
76
82
|
- spec/settis_spec.rb
|
77
83
|
- spec/spec_helper.rb
|
78
|
-
has_rdoc: true
|
79
84
|
homepage: http://github.com/bfrydl/settis
|
80
85
|
licenses: []
|
81
86
|
post_install_message:
|
@@ -96,14 +101,18 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
96
101
|
version: '0'
|
97
102
|
requirements: []
|
98
103
|
rubyforge_project:
|
99
|
-
rubygems_version: 1.
|
104
|
+
rubygems_version: 1.8.5
|
100
105
|
signing_key:
|
101
106
|
specification_version: 3
|
102
107
|
summary: Redis-powered settings storage for web applications.
|
103
108
|
test_files:
|
104
109
|
- spec/settis/container_spec.rb
|
105
|
-
- spec/settis/
|
106
|
-
- spec/settis/
|
110
|
+
- spec/settis/serializer_spec.rb
|
111
|
+
- spec/settis/serializers/active_record_spec.rb
|
112
|
+
- spec/settis/serializers/boolean_spec.rb
|
113
|
+
- spec/settis/serializers/integer_spec.rb
|
114
|
+
- spec/settis/serializers/string_spec.rb
|
115
|
+
- spec/settis/setting_spec.rb
|
107
116
|
- spec/settis/settings/scalar_spec.rb
|
108
117
|
- spec/settis_spec.rb
|
109
118
|
- spec/spec_helper.rb
|