From c3c68ac2738c9d25717236eb96cea0c6b5d10e65 Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Thu, 15 May 2008 20:11:50 +0000 Subject: [PATCH] ActionItem394: adding type support for acts_as_having_settings --- lib/acts_as_having_settings.rb | 9 ++++++++- test/unit/acts_as_having_settings_test.rb | 13 ++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/acts_as_having_settings.rb b/lib/acts_as_having_settings.rb index 7646b48..181001b 100644 --- a/lib/acts_as_having_settings.rb +++ b/lib/acts_as_having_settings.rb @@ -22,6 +22,7 @@ module ActsAsHavingSettings options = names.last.is_a?(Hash) ? names.pop : {} default = options[:default] ? options[:default].inspect : "val" + data_type = options[:type] || :string names.each do |setting| class_eval <<-CODE @@ -30,12 +31,18 @@ module ActsAsHavingSettings val.nil? ? #{default} : val end def #{setting}=(value) - send(self.class.settings_field)[:#{setting}] = value + send(self.class.settings_field)[:#{setting}] = self.class.acts_as_having_settings_type_cast(value, #{data_type.inspect}) end CODE end end + def acts_as_having_settings_type_cast(value, type) + # FIXME creating a new instance at every call, will the garbage collector + # be able to cope with it? + ActiveRecord::ConnectionAdapters::Column.new(:dummy, nil, type.to_s).type_cast(value) + end + end end diff --git a/test/unit/acts_as_having_settings_test.rb b/test/unit/acts_as_having_settings_test.rb index ae6605a..8572ec2 100644 --- a/test/unit/acts_as_having_settings_test.rb +++ b/test/unit/acts_as_having_settings_test.rb @@ -3,6 +3,9 @@ require File.dirname(__FILE__) + '/../test_helper' class ActsAsHavingSettingsTest < Test::Unit::TestCase # using Block class as a sample user of the module + class TestClass < Block + settings_items :flag, :type => :boolean + end should 'store settings in a hash' do block = Block.new @@ -49,11 +52,15 @@ class ActsAsHavingSettingsTest < Test::Unit::TestCase end should 'be able to set boolean attributes to false with a default of true' do - klass = Class.new(Block) - klass.settings_items :flag, :default => true - obj = klass.new + obj = TestClass.new obj.flag = false assert_equal false, obj.flag end + should 'be able to specify type of atrributes (boolean)' do + obj = TestClass.new + obj.flag = 'true' + assert_equal true, obj.flag + end + end -- libgit2 0.21.2