Skip to content

Commit

Permalink
Change save to create/update. Added model to payload.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Jan 31, 2013
1 parent 5568ec0 commit 6482c88
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
15 changes: 7 additions & 8 deletions lib/toy/persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,14 @@ def persisted?
def save(options={})
default_payload = {
:id => persisted_id,
:model => self.class,
}

Toy.instrumenter.instrument('save.toystore', default_payload) { |payload|
if new_record?
payload[:new_record] = true
create
else
payload[:new_record] = false
update
end
new_record = new_record?
action = new_record ? 'create' : 'update'

Toy.instrumenter.instrument("#{action}.toystore", default_payload) { |payload|
new_record ? create : update
}
end

Expand All @@ -99,6 +97,7 @@ def update_attributes(attrs)
def destroy
default_payload = {
:id => persisted_id,
:model => self.class,
}

Toy.instrumenter.instrument('destroy.toystore', default_payload) { |payload|
Expand Down
13 changes: 9 additions & 4 deletions lib/toy/querying.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ module Querying

module ClassMethods
def read(id, options = nil)
Toy.instrumenter.instrument('read.toystore') { |payload|
payload[:id] = id
payload[:options] = options
payload[:hit] = false # hit if found, miss if not found
default_payload = {
:id => id,
:options => options,
:model => self,
:hit => false, # default to not found
}

Toy.instrumenter.instrument('read.toystore', default_payload) { |payload|
if (attrs = adapter.read(id, options))
payload[:hit] = true
load(id, attrs)
Expand All @@ -30,6 +33,7 @@ def read_multiple(ids, options = nil)
default_payload = {
:ids => ids,
:options => options,
:model => self,
:hits => 0,
:misses => 0,
}
Expand All @@ -56,6 +60,7 @@ def key?(id, options = nil)
default_payload = {
:id => id,
:options => options,
:model => self,
}

Toy.instrumenter.instrument('key.toystore', default_payload) { |payload|
Expand Down
9 changes: 5 additions & 4 deletions spec/toy/persistence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,10 @@
@doc.save

event = instrumenter.events.last
event.name.should eq('save.toystore')
event.name.should eq('create.toystore')
event.payload.should eq({
:id => @doc.persisted_id,
:new_record => true,
:model => User,
})
end

Expand Down Expand Up @@ -328,10 +328,10 @@ def persist
@doc.save

event = instrumenter.events.last
event.name.should eq('save.toystore')
event.name.should eq('update.toystore')
event.payload.should eq({
:id => @doc.persisted_id,
:new_record => false,
:model => User,
})
end

Expand Down Expand Up @@ -403,6 +403,7 @@ def persist
event.name.should eq('destroy.toystore')
event.payload.should eq({
:id => @doc.id,
:model => User,
})
end
end
Expand Down
5 changes: 5 additions & 0 deletions spec/toy/querying_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
event.payload.should eq({
:id => @user.id,
:options => {some: 'thing'},
:model => User,
:hit => true,
})
end
Expand All @@ -50,6 +51,7 @@
event.payload.should eq({
:id => @id,
:options => {some: 'thing'},
:model => User,
:hit => false,
})
end
Expand Down Expand Up @@ -116,6 +118,7 @@
event.payload.should eq({
:ids => ids,
:options => {some: 'thing'},
:model => User,
:hits => 2,
:misses => 1,
})
Expand Down Expand Up @@ -148,6 +151,7 @@
event.payload.should eq({
:id => @user.id,
:options => {some: 'thing'},
:model => User,
:hit => true,
})
end
Expand All @@ -172,6 +176,7 @@
event.payload.should eq({
:id => @id,
:options => {some: 'thing'},
:model => User,
:hit => false,
})
end
Expand Down

0 comments on commit 6482c88

Please sign in to comment.