Skip to content

Commit

Permalink
Add fetching support for models
Browse files Browse the repository at this point in the history
  • Loading branch information
mmangino committed May 6, 2010
1 parent 849f63c commit 5bae255
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/mogli/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ def self.has_association(name,klass)
add_creation_method(name,klass)
end

def fetch
raise ArgumentError.new("You cannot fetch models without a populated id attribute") if id.nil?
other = self.class.find(id,client)
merge!(other)
end

def self.recognize?(data)
true
end
Expand Down
15 changes: 14 additions & 1 deletion spec/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,24 @@ class TestModel < Mogli::Model
model.comments_create
end

it "should emit warnings when properties that don't exist are written to" do
it "emits warnings when properties that don't exist are written to" do
model.should_receive(:warn_about_invalid_property).with("doesnt_exist")
model.doesnt_exist=1
end

describe "Fetching" do

it "fetches data for a model with an id " do
Mogli::Client.should_receive(:get).with("http://graph.facebook.com/1", :query=>{}).and_return({:id=>1,:other_property=>2})
model.fetch
model.other_property.should == 2
end

it "raises an exception when there is no id" do
lambda do
TestModel.new.fetch
end.should raise_error(ArgumentError)
end
end

end

0 comments on commit 5bae255

Please sign in to comment.