settis 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,7 +17,8 @@
17
17
  require 'redis/namespace'
18
18
 
19
19
  require 'settis/container'
20
- require 'settis/settings'
20
+ require 'settis/serializer'
21
+ require 'settis/setting'
21
22
  require 'settis/version'
22
23
 
23
24
  module Settis
@@ -18,19 +18,19 @@ module Settis
18
18
  module Container
19
19
  # @private
20
20
  def redis
21
- @redis ||= Redis::Namespace.new(root, :redis => Settis.redis)
21
+ @redis ||= Redis::Namespace.new(namespace, :redis => Settis.redis)
22
22
  end
23
23
 
24
- # @overload root(value)
25
- # Sets the root namespace prefix to be added before all Redis key names.
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 root
28
- # Gets the root namespace prefix to be added before all Redis key names.
29
- def root(value = nil)
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
- @root ||= 'settings'
31
+ @namespace ||= 'settings'
32
32
  else
33
- @root = value
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
- klass = case type
51
- when :string then Settings::Scalar
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] = klass.new(name, self, options)
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
- require 'spec_helper'
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
- describe Settis::Settings::Integer do
20
- before :each do
21
- any_instance_of(Settis::Settings::Integer) do |integer|
22
- stub(integer).define_methods
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
- @integer = Settis::Settings::Integer.new('test', Object)
26
- end
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
- module Settings
19
- class Boolean < Scalar
20
- def serialize(value)
21
- value ? '1' : '0'
22
- end
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
- def deserialize(value)
25
- value != '0'
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
- module Settings
19
- autoload :Scalar, 'settis/settings/scalar'
20
- autoload :Integer, 'settis/settings/integer'
21
- autoload :Boolean, 'settis/settings/boolean'
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
- module Settings
19
- class Integer < Scalar
20
- def deserialize(value)
21
- value.to_i
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
- module Settings
19
- class Scalar
20
- attr_reader :name
18
+ class ScalarSetting < Setting
19
+ def initialize(container, name, serializer, options = {})
20
+ @serializer = serializer
21
+ @default = options.delete(:default)
21
22
 
22
- def initialize(name, klass, options = {})
23
- @name = name
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
- class << @class ; self ; end.instance_eval do
59
- define_method setting.name do
60
- setting.get
61
- end
26
+ def get
27
+ value = @serializer.deserialize(super)
28
+ value || @default
29
+ end
62
30
 
63
- define_method :"#{setting.name}=" do |value|
64
- setting.set(value)
65
- end
66
- end
67
- end
31
+ def set(value)
32
+ super(@serializer.serialize(value))
33
+ value
68
34
  end
69
35
  end
70
36
  end
@@ -15,5 +15,5 @@
15
15
  # limitations under the License.
16
16
 
17
17
  module Settis
18
- VERSION = '0.1.0'
18
+ VERSION = '0.2.0'
19
19
  end
@@ -18,40 +18,40 @@ require 'spec_helper'
18
18
 
19
19
  describe Settis::Container do
20
20
  before :each do
21
- @class = Class.new do
21
+ @container = Class.new do
22
22
  extend Settis::Container
23
23
  end
24
24
  end
25
25
 
26
- describe '#root' do
27
- it 'stores the root namespace prefix for Redis keys' do
28
- @class.root.should == 'settings'
29
- @class.root 'test'
30
- @class.root.should == 'test'
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(@class).root { 'configuration' }
36
+ stub(@container).namespace { 'configuration' }
37
37
 
38
- @class.redis.should be_a(Redis::Namespace)
39
- @class.redis.namespace.should == 'configuration'
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::Settings::Scalar).new('title', @class, :default => 'Hello World')
45
+ mock(Settis::ScalarSetting).new(@container, 'title', is_a(Settis::StringSerializer), :default => 'Hello World')
46
46
 
47
- @class.setting :title, :string, :default => 'Hello World'
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::Settings::Scalar).new.once
52
- @class.setting :title, :string
51
+ stub(Settis::ScalarSetting).new.once
52
+ @container.setting :title, :string
53
53
 
54
- expect { @class.setting :title, :integer }.to raise_error(ArgumentError)
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::Settings::Boolean do
20
- before :each do
21
- any_instance_of(Settis::Settings::Boolean) do |boolean|
22
- stub(boolean).define_methods
23
- end
19
+ describe Settis::BooleanSerializer do
20
+ before :all do
21
+ @serializer = Settis::BooleanSerializer.new(:boolean)
22
+ end
24
23
 
25
- @boolean = Settis::Settings::Boolean.new('test', Object)
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 either 1 or 0' do
30
- @boolean.send(:serialize, false).should == '0'
31
- @boolean.send(:serialize, true).should == '1'
32
- @boolean.send(:serialize, Object.new).should == '1'
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
- @boolean.send(:deserialize, '0').should be_false
39
- @boolean.send(:deserialize, '1').should be_true
40
- @boolean.send(:deserialize, 'HI').should be_true
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::Settings::Scalar do
20
- describe '#new' do
21
- before { @class = Class.new }
22
-
23
- it 'adds accessor methods to the target class' do
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
- before do
39
- any_instance_of(Settis::Settings::Scalar) do |scalar|
40
- stub(scalar).define_methods
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
- @class = Object.new
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
- @scalar.get.should == 'deserialized'
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
- before do
63
- any_instance_of(Settis::Settings::Scalar) do |scalar|
64
- stub(scalar).define_methods
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
- @class = Object.new
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
- @scalar.set(nil)
46
+ @setting.set('raw').should == 'raw'
82
47
  end
83
48
  end
84
49
  end
@@ -17,8 +17,8 @@
17
17
  require 'spec_helper'
18
18
 
19
19
  describe Settis do
20
- context '#redis' do
21
- after do
20
+ describe '#redis' do
21
+ before do
22
22
  Settis.class_eval { @redis = nil }
23
23
  end
24
24
 
@@ -14,6 +14,10 @@
14
14
  # See the License for the specific language governing permissions and
15
15
  # limitations under the License.
16
16
 
17
+ module ActiveRecord
18
+ class Base ; end
19
+ end
20
+
17
21
  require 'settis'
18
22
 
19
23
  RSpec.configure do |config|
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.1.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-02 00:00:00.000000000 -04:00
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: &18645380 !ruby/object:Gem::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: *18645380
24
+ version_requirements: *8531320
26
25
  - !ruby/object:Gem::Dependency
27
26
  name: rspec
28
- requirement: &18644940 !ruby/object:Gem::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: *18644940
35
+ version_requirements: *8530880
37
36
  - !ruby/object:Gem::Dependency
38
37
  name: rr
39
- requirement: &18644520 !ruby/object:Gem::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: *18644520
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/settings.rb
67
- - lib/settis/settings/boolean.rb
68
- - lib/settis/settings/integer.rb
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/settings/boolean_spec.rb
74
- - spec/settis/settings/integer_spec.rb
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.6.2
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/settings/boolean_spec.rb
106
- - spec/settis/settings/integer_spec.rb
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