Permalink
Browse files

add basic reader, writer and present? methods

  • Loading branch information...
1 parent 3e6fcf6 commit 43304da7823888f34e3a875b6aad42773d0ae996 @kronn committed May 28, 2011
Showing with 31 additions and 0 deletions.
  1. +11 −0 lib/date_checkbox/has_date_checkbox.rb
  2. +20 −0 test/has_date_checkbox_test.rb
@@ -6,6 +6,17 @@ def self.included(base)
module ClassMethods
def has_date_checkbox(db_field)
+ method_name = db_field.to_s.sub(/_at$/, '')
+ class_eval do
+ define_method("#{method_name}") do
+ end
+
+ define_method("#{method_name}?") do
+ end
+
+ define_method("#{method_name}=") do |value|
+ end
+ end
end
end
end
@@ -3,18 +3,38 @@
class HasDateCheckboxTest < ActiveSupport::TestCase
def teardown
@klass = nil
+ @instance = nil
end
test "a class method for one field is added" do
assert_respond_to klass, :has_date_checkbox
assert_equal 1, klass.method(:has_date_checkbox).arity
end
+ test "has_date_checkbox defines a custom reader for the given attribute" do
+ assert_respond_to instance, :published
+ assert_respond_to instance, :published?
+ end
+
+ test "has_date_checkbox defines a custom writer for the given attribute" do
+ assert_respond_to instance, :'published='
+ end
protected
def klass
@klass ||= Class.new do
include DateCheckbox::HasDateCheckbox
end
end
+
+ def instance
+ @instance ||= begin
+ inst = PublishedAtPost.new
+ inst.class.send(:include, DateCheckbox::HasDateCheckbox)
+ inst.class.class_eval do
+ has_date_checkbox :published_at
+ end
+ inst
+ end
+ end
end

0 comments on commit 43304da

Please sign in to comment.