Skip to content

Commit

Permalink
add basic reader, writer and present? methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kronn committed May 28, 2011
1 parent 3e6fcf6 commit 43304da
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/date_checkbox/has_date_checkbox.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions test/has_date_checkbox_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.