Skip to content

Commit

Permalink
Merge branch 'master' of github.com:landcentral/eloqua
Browse files Browse the repository at this point in the history
  • Loading branch information
lightsofapollo committed Jun 8, 2011
2 parents 7a2f1db + 2410560 commit 4ca0751
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 5 deletions.
25 changes: 24 additions & 1 deletion lib/eloqua/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ class Entity < RemoteObject

self.remote_group = :entity

# Returns an :id indexed list of memberships for contact
#
# # Example output
# {'1' => {:id => '1', :name => 'Contact Group Name', :type => 'ContactGroup}}
#
# @return [Hash] Integer => Hash
def list_memberships
self.class.list_memberships(id)
end
Expand All @@ -20,8 +26,25 @@ def remove_membership(asset)

class << self

# Returns an :id indexed list of memberships for given contact id
#
# # Example output
# {'1' => {:id => '1', :name => 'Contact Group Name', :type => 'ContactGroup}}
#
# @param [String, Integer] contact id
# @return [Hash] Integer => Hash
def list_memberships(id)
api.list_memberships(remote_type, id)
memberships = api.list_memberships(remote_type, id)

if(memberships && !memberships.empty?)
memberships.inject({}) do |map, membership|
map[membership[:id]] = membership
map
end
else
memberships || {}
end

end

def where(conditions = nil, fields = [], limit = 200, page = 1)
Expand Down
6 changes: 4 additions & 2 deletions lib/eloqua/remote_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,10 @@ def find(id)
[:save, :update, :create].each do |callback_type|
[:before, :after].each do |callback_state|
module_eval(<<-RUBY, __FILE__, (__LINE__ - 2))
def #{callback_state}_#{callback_type}(&block)
set_callback(:#{callback_type}, :#{callback_state}, &block)
def #{callback_state}_#{callback_type}(*args, &block)
args.unshift(:#{callback_state})
args.unshift(:#{callback_type})
set_callback(*args, &block)
end
RUBY
end
Expand Down
41 changes: 39 additions & 2 deletions spec/lib/eloqua/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,56 @@ def self.name
end

context "#list_memberships" do

it 'should delegate call to class level with current id' do
flexmock(subject).should_receive(:list_memberships).with(1).once
object.list_memberships
end

end

context "#self.list_memberships" do

let(:mock_memberships) do
list = []

3.times do |n|
list << {:id => n, :type => 'ContactGroup', :name => "ContactGroup#{n}"}
end

list
end


it 'should delegate call to api' do
flexmock(subject.api).should_receive(:list_memberships).with(subject.remote_type, 1).once
subject.list_memberships(1)
end

it "should index memberships by id" do
flexmock(subject.api).should_receive(:list_memberships).with(subject.remote_type, 1).once.\
and_return(mock_memberships)

memberships = subject.list_memberships(1)
memberships.should be_an(Hash)
memberships.each do |key, membership|
key.should == membership[:id]
end
end

context "when low level api returns nil" do

before do
flexmock(subject.api).should_receive(:list_memberships).\
with(subject.remote_type, 1).\
once.and_return(nil)
end

it "should return an empty hash" do
subject.list_memberships(1).should == {}
end
end

end

context "#self.remote_group" do
Expand Down
9 changes: 9 additions & 0 deletions spec/lib/eloqua/remote_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@
klass.send(method, &block)
end


it "should be able to set a symbol as the final argument for a method" do
callback_method = :object_id

klass = flexmock(subject)
klass.should_receive(:set_callback).with(callback, state, callback_method).once
klass.send(method, callback_method)
end

end

end
Expand Down

0 comments on commit 4ca0751

Please sign in to comment.