Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added boolean methods for keys that return true or false based on whe…
…ther they have value present.
  • Loading branch information
jnunemaker committed Jul 27, 2009
1 parent e37fd46 commit 54acbeb
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
2 changes: 2 additions & 0 deletions History
Expand Up @@ -7,6 +7,8 @@
* Fixed bug where conditions that disallowed using $in, $all and $any with an array
* Bumped version of validatable so :if validation option supports symbol/string to proc.
* Document#create with no attributes now creates a document as long as it is valid
* Now defining accessor methods when key is declared rather than using method missing and all that jazz
* Attributes now have boolean methods that return true or false based on whether they have value present

0.2.0 7/7/2009
* 2 major additions (observers, associations), several minor additions, and a few bug fixes
Expand Down
8 changes: 6 additions & 2 deletions lib/mongomapper/embedded_document.rb
Expand Up @@ -52,12 +52,16 @@ def create_accessors_for(key)
read_attribute(key.name)
end

define_method("#{key.name}_before_typecast") do
read_attribute_before_typecast(key.name)
end

define_method("#{key.name}=") do |value|
write_attribute(key.name, value)
end

define_method("#{key.name}_before_typecast") do
read_attribute_before_typecast(key.name)
define_method("#{key.name}?") do
read_attribute(key.name).present?
end
end

Expand Down
42 changes: 40 additions & 2 deletions test/unit/test_embedded_document.rb
Expand Up @@ -82,6 +82,30 @@ class EmbeddedDocumentTest < Test::Unit::TestCase
@document.key(:foo, Integer)
@document.keys['foo'].type.should == Integer
end

should "create reader method" do
@document.new.should_not respond_to(:foo)
@document.key(:foo, String)
@document.new.should respond_to(:foo)
end

should "create reader before typecast method" do
@document.new.should_not respond_to(:foo_before_typecast)
@document.key(:foo, String)
@document.new.should respond_to(:foo_before_typecast)
end

should "create writer method" do
@document.new.should_not respond_to(:foo=)
@document.key(:foo, String)
@document.new.should respond_to(:foo=)
end

should "create boolean method" do
@document.new.should_not respond_to(:foo?)
@document.key(:foo, String)
@document.new.should respond_to(:foo?)
end
end

context "keys" do
Expand Down Expand Up @@ -174,7 +198,7 @@ def passwd
end
end

context "requesting keys" do
context "attributes" do
should "default to empty hash" do
doc = @document.new
doc.attributes.should == {}
Expand All @@ -186,7 +210,7 @@ def passwd
end
end

context "key shorcuts" do
context "key shorcut access" do
should "be able to read key with []" do
doc = @document.new(:name => 'string')
doc[:name].should == 'string'
Expand Down Expand Up @@ -290,6 +314,20 @@ def name_and_age=(new_value)
end
end # writing an attribute

context "checking if an attributes value is present" do
should "work for defined keys" do
doc = @document.new
doc.name?.should be_false
doc.name = 'John'
doc.name?.should be_true
end

should "raise no method error for undefined keys" do
doc = @document.new
lambda { doc.fart? }.should raise_error(NoMethodError)
end
end

context "equality" do
should "be true if all keys and values are equal" do
doc1 = @document.new(:name => 'John', :age => 27)
Expand Down

0 comments on commit 54acbeb

Please sign in to comment.