Skip to content

Commit

Permalink
Squelch block parameter warning during tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristján Pétursson authored and mmangino committed Jun 28, 2010
1 parent 65c9b70 commit b174b1e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 43 deletions.
39 changes: 20 additions & 19 deletions lib/mogli/model.rb
Expand Up @@ -3,7 +3,7 @@ class Model < Hashie::Dash
def client=(val) def client=(val)
@client=val @client=val
end end

def client def client
@client || Mogli::Client.new @client || Mogli::Client.new
end end
Expand All @@ -12,20 +12,20 @@ def initialize(hash={},client=nil)
self.client=client self.client=client
super(hash||{}) super(hash||{})
end end

def post_params def post_params
post_params = {} post_params = {}
self.class.creation_keys.each do |key| self.class.creation_keys.each do |key|
post_params[key] = self[key] post_params[key] = self[key]
end end
post_params post_params
end end

def destroy def destroy
client.delete(id) client.delete(id)
freeze freeze
end end

def self.included(other) def self.included(other)
other.extend(ClassMethods) other.extend(ClassMethods)
end end
Expand All @@ -34,48 +34,49 @@ def method_missing(method, *args)
method_as_s = method.to_s method_as_s = method.to_s
if method_as_s.to_s[-1].chr == "=" if method_as_s.to_s[-1].chr == "="
warn_about_invalid_property(method_as_s.chop) warn_about_invalid_property(method_as_s.chop)
else else
super super
end end
end end
def warn_about_invalid_property(property) def warn_about_invalid_property(property)
puts "Warning: property #{property} doesn't exist for class #{self.class.name}" puts "Warning: property #{property} doesn't exist for class #{self.class.name}"
end end

def self.define_properties(*args) def self.define_properties(*args)
args.each do |arg| args.each do |arg|
property arg property arg
end end
end end

def self.creation_properties(*args) def self.creation_properties(*args)
@creation_properties = args @creation_properties = args
end end

def self.creation_keys def self.creation_keys
@creation_properties || [] @creation_properties || []
end end

def self.hash_populating_accessor(method_name,*klass) def self.hash_populating_accessor(method_name,*klass)
define_method "#{method_name}=" do |hash| define_method "#{method_name}=" do |hash|
instance_variable_set("@#{method_name}",client.map_data(hash,klass)) instance_variable_set("@#{method_name}",client.map_data(hash,klass))
end end
define_method "#{method_name}" do define_method "#{method_name}" do
instance_variable_get "@#{method_name}" instance_variable_get "@#{method_name}"
end end

add_creation_method(method_name,klass) add_creation_method(method_name,klass)

end end

def self.add_creation_method(name,klass) def self.add_creation_method(name,klass)
define_method "#{name}_create" do |arg| define_method "#{name}_create" do |*args|
arg = args.first
params = arg.nil? ? {} : arg.post_params params = arg.nil? ? {} : arg.post_params
klass_to_send = arg.nil? ? nil : klass klass_to_send = arg.nil? ? nil : klass
client.post("#{id}/#{name}", klass_to_send, params) client.post("#{id}/#{name}", klass_to_send, params)
end end
end end

def self.has_association(name,klass) def self.has_association(name,klass)
define_method name do |*fields| define_method name do |*fields|
body_args = fields.empty? ? {} : {:fields => fields} body_args = fields.empty? ? {} : {:fields => fields}
Expand All @@ -85,23 +86,23 @@ def self.has_association(name,klass)
end end
return ret return ret
end end

add_creation_method(name,klass) add_creation_method(name,klass)
end end

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

def self.recognize?(data) def self.recognize?(data)
true true
end end

def self.find(id,client=nil, *fields) def self.find(id,client=nil, *fields)
body_args = fields.empty? ? {} : {:fields => fields} body_args = fields.empty? ? {} : {:fields => fields}
(client||Mogli::Client.new).get_and_map(id,self, body_args) (client||Mogli::Client.new).get_and_map(id,self, body_args)
end end
end end
end end
48 changes: 24 additions & 24 deletions spec/model_spec.rb
Expand Up @@ -3,31 +3,31 @@ class TestModel < Mogli::Model
define_properties :id, :other_property define_properties :id, :other_property
creation_properties :other_property creation_properties :other_property
has_association :comments, "Comment" has_association :comments, "Comment"

hash_populating_accessor :from, "User" hash_populating_accessor :from, "User"
hash_populating_accessor :activities, "Activity" hash_populating_accessor :activities, "Activity"
end end



describe Mogli::Model do describe Mogli::Model do

let :model do let :model do
model = TestModel.new(:id=>1) model = TestModel.new(:id=>1)
model.client = mock_client model.client = mock_client
model model
end end

let :mock_client do let :mock_client do
Mogli::Client.new Mogli::Client.new
end end


it "allow setting the client" do it "allow setting the client" do
user = Mogli::Model.new user = Mogli::Model.new
user.client = "client" user.client = "client"
user.client.should == "client" user.client.should == "client"
end end

it "has a define proeprties method" do it "has a define proeprties method" do
model.respond_to?(:id).should be_true model.respond_to?(:id).should be_true
model.respond_to?(:other_property).should be_true model.respond_to?(:other_property).should be_true
Expand All @@ -43,9 +43,9 @@ class TestModel < Mogli::Model
model.comments model.comments
model.comments model.comments
end end

describe "creation" do describe "creation" do

it "has a create method on associations" do it "has a create method on associations" do
mock_client.stub!(:post) mock_client.stub!(:post)
model.comments_create(Mogli::Activity.new) model.comments_create(Mogli::Activity.new)
Expand All @@ -55,62 +55,62 @@ class TestModel < Mogli::Model
mock_client.should_receive(:post).with("1/comments","Comment",:message=>"my message") mock_client.should_receive(:post).with("1/comments","Comment",:message=>"my message")
model.comments_create(Mogli::Comment.new(:message=>"my message")) model.comments_create(Mogli::Comment.new(:message=>"my message"))
end end

end end

it "populates from as the user from a hash" do it "populates from as the user from a hash" do
model.from = {"id" => "12451752", "name" => "Mike Mangino"} model.from = {"id" => "12451752", "name" => "Mike Mangino"}
model.from.should be_an_instance_of(Mogli::User) model.from.should be_an_instance_of(Mogli::User)
model.from.id.should == "12451752" model.from.id.should == "12451752"
model.from.name.should == "Mike Mangino" model.from.name.should == "Mike Mangino"
end end

it "populates activities from a hash array" do it "populates activities from a hash array" do
model.activities = {"data"=>[{"id"=>1,"message"=>"message1"},{"id"=>2}]} model.activities = {"data"=>[{"id"=>1,"message"=>"message1"},{"id"=>2}]}
model.activities.size.should == 2 model.activities.size.should == 2
model.activities.each {|c| c.should be_an_instance_of(Mogli::Activity)} model.activities.each {|c| c.should be_an_instance_of(Mogli::Activity)}
model.activities.first.id.should == 1 model.activities.first.id.should == 1
model.activities.first.client.should == mock_client model.activities.first.client.should == mock_client
end end

it "allows deleting the post" do it "allows deleting the post" do
mock_client.should_receive(:delete).with(1) mock_client.should_receive(:delete).with(1)
model.destroy model.destroy
end end

it "freezes deleted objects" do it "freezes deleted objects" do
mock_client.should_receive(:delete).with(1) mock_client.should_receive(:delete).with(1)
model.destroy model.destroy
model.should be_frozen model.should be_frozen
end end

it "knows which attributes are posted" do it "knows which attributes are posted" do
TestModel.new(:id=>1,:other_property=>2).post_params.should == {:other_property=>2} TestModel.new(:id=>1,:other_property=>2).post_params.should == {:other_property=>2}
end end

it "will allow updating status with no object" do it "will allow updating status with no object" do
mock_client.should_receive(:post).once.with("1/comments",nil,{}).and_return([]) mock_client.should_receive(:post).once.with("1/comments",nil,{}).and_return([])
model.comments_create model.comments_create
end end

it "emits 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.should_receive(:warn_about_invalid_property).with("doesnt_exist")
model.doesnt_exist=1 model.doesnt_exist=1
end end

describe "Fetching" do describe "Fetching" do

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

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

end end

0 comments on commit b174b1e

Please sign in to comment.