From fd983d96da8ef7f90dedbe1ee09ba8d75e9df22a Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Thu, 24 Jan 2008 17:10:57 +0000 Subject: [PATCH] ActionItem152: blocks can declare settings items --- app/models/block.rb | 11 +++++++++++ test/unit/block_test.rb | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 0 deletions(-) diff --git a/app/models/block.rb b/app/models/block.rb index 05bb50d..ab750bb 100644 --- a/app/models/block.rb +++ b/app/models/block.rb @@ -11,6 +11,17 @@ class Block < ActiveRecord::Base self[:settings] ||= Hash.new end + def self.settings_item(name) + class_eval <<-CODE + def #{name} + settings[:#{name}] + end + def #{name}=(value) + settings[:#{name}] = value + end + CODE + end + def self.description _('A dummy block.') end diff --git a/test/unit/block_test.rb b/test/unit/block_test.rb index 7f67654..b727258 100644 --- a/test/unit/block_test.rb +++ b/test/unit/block_test.rb @@ -32,4 +32,23 @@ class BlockTest < Test::Unit::TestCase assert_nil Block.new.owner end + should 'be able to declare settings items' do + block_class = Class.new(Block) + + block = block_class.new + assert !block.respond_to?(:limit) + assert !block.respond_to?(:limit=) + + block_class.settings_item :limit + + assert_respond_to block, :limit + assert_respond_to block, :limit= + + assert_nil block.limit + block.limit = 10 + assert_equal 10, block.limit + + assert_equal({ :limit => 10}, block.settings) + end + end -- libgit2 0.21.2