motion-resource 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,7 +1,7 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  gem "bubble-wrap", "1.3.0.osx"
4
- gem "motion-support"
4
+ gem "motion-support", ">= 0.2.4"
5
5
  gem "webstub", :git => 'git://github.com/mattgreen/webstub.git'
6
6
 
7
7
  # Specify your gem's dependencies in motion-support.gemspec
@@ -0,0 +1,17 @@
1
+ module MotionResource
2
+ class Base
3
+ include MotionSupport::Callbacks
4
+
5
+ define_callbacks :create, :save, :update, :destroy
6
+
7
+ [:create, :save, :update, :destroy].each do |callback|
8
+ define_singleton_method "before_#{callback}" do |*filters, &blk|
9
+ set_callback(callback, :before, *filters, &blk)
10
+ end
11
+
12
+ define_singleton_method "after_#{callback}" do |*filters, &blk|
13
+ set_callback(callback, :after, *filters, &blk)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -1,26 +1,40 @@
1
1
  module MotionResource
2
2
  class Base
3
3
  def save(&block)
4
- @new_record ? create(&block) : update(&block)
4
+ run_callbacks :save do
5
+ @new_record ? create(&block) : update(&block)
6
+ end
5
7
  end
6
8
 
7
9
  def update(&block)
8
- self.class.put(member_url, :payload => { self.class.name.underscore => attributes }) do |response, json|
9
- self.class.request_block_call(block, json.blank? ? self : self.class.instantiate(json), response) if block
10
+ run_callbacks :update do
11
+ self.class.put(member_url, :payload => { self.class.name.underscore => attributes }) do |response, json|
12
+ self.class.request_block_call(block, json.blank? ? self : self.class.instantiate(json), response) if block
13
+ end
10
14
  end
11
15
  end
12
16
 
13
17
  def create(&block)
14
18
  # weird heisenbug: Specs crash without that line :(
15
19
  dummy = self
16
- self.class.post(collection_url, :payload => { self.class.name.underscore => attributes }) do |response, json|
17
- self.class.request_block_call(block, json.blank? ? self : self.class.instantiate(json), response) if block
20
+ run_callbacks :create do
21
+ self.class.post(collection_url, :payload => { self.class.name.underscore => attributes }) do |response, json|
22
+ self.class.request_block_call(block, json.blank? ? self : self.class.instantiate(json), response) if block
23
+ end
24
+ end
25
+ end
26
+
27
+ def self.create(attributes = {}, &block)
28
+ new(attributes).tap do |model|
29
+ model.create(&block)
18
30
  end
19
31
  end
20
32
 
21
33
  def destroy(&block)
22
- self.class.delete(member_url) do |response, json|
23
- self.class.request_block_call(block, json.blank? ? nil : self.class.instantiate(json), response) if block
34
+ run_callbacks :destroy do
35
+ self.class.delete(member_url) do |response, json|
36
+ self.class.request_block_call(block, json.blank? ? nil : self.class.instantiate(json), response) if block
37
+ end
24
38
  end
25
39
  end
26
40
 
@@ -48,10 +48,11 @@ module MotionResource
48
48
  if self.default_url_options
49
49
  options.merge!(self.default_url_options)
50
50
  end
51
- logger.log "#{method.upcase} #{url}"
52
51
 
53
52
  url.insert_extension!(self.extension)
54
53
 
54
+ logger.log "#{method.upcase} #{url}"
55
+
55
56
  BubbleWrap::HTTP.send(method, url, options) do |response|
56
57
  if response.ok?
57
58
  body = response.body.to_str.strip rescue nil
@@ -1,3 +1,3 @@
1
1
  module MotionResource
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -0,0 +1,70 @@
1
+ module CallbackSpec
2
+ class Model < MotionResource::Base
3
+ self.member_url = 'models/:id'
4
+ self.collection_url = 'models'
5
+
6
+ before_create { |m| m.history << "before_create" }
7
+ after_create { |m| m.history << "after_create" }
8
+ before_save { |m| m.history << "before_save" }
9
+ after_save { |m| m.history << "after_save" }
10
+ before_update { |m| m.history << "before_update" }
11
+ after_update { |m| m.history << "after_update" }
12
+ before_destroy { |m| m.history << "before_destroy" }
13
+ after_destroy { |m| m.history << "after_destroy" }
14
+
15
+ def history
16
+ @history ||= []
17
+ end
18
+ end
19
+ end
20
+
21
+ describe "base" do
22
+ extend WebStub::SpecHelpers
23
+ extend MotionResource::SpecHelpers
24
+
25
+ describe "callbacks" do
26
+ it "should run create callbacks" do
27
+ stub_request(:post, "http://example.com/models.json").to_return(json: {})
28
+ @model = CallbackSpec::Model.create { |obj| resume }
29
+ wait_max 1.0 do
30
+ @model.history.should == ['before_create', 'after_create']
31
+ end
32
+ end
33
+
34
+ it "should run create and save callbacks on first save" do
35
+ stub_request(:post, "http://example.com/models.json").to_return(json: {})
36
+ @model = CallbackSpec::Model.new
37
+ @model.save { |obj| resume }
38
+ wait_max 1.0 do
39
+ @model.history.should == ['before_save', 'before_create', 'after_create', 'after_save']
40
+ end
41
+ end
42
+
43
+ it "should run update and save callbacks on subsequent save" do
44
+ stub_request(:put, "http://example.com/models/10.json").to_return(json: {})
45
+ @model = CallbackSpec::Model.instantiate(:id => 10)
46
+ @model.save { |obj| resume }
47
+ wait_max 1.0 do
48
+ @model.history.should == ['before_save', 'before_update', 'after_update', 'after_save']
49
+ end
50
+ end
51
+
52
+ it "should run update callbacks" do
53
+ stub_request(:put, "http://example.com/models/10.json").to_return(json: {})
54
+ @model = CallbackSpec::Model.instantiate(:id => 10)
55
+ @model.update { |obj| resume }
56
+ wait_max 1.0 do
57
+ @model.history.should == ['before_update', 'after_update']
58
+ end
59
+ end
60
+
61
+ it "should run destroy callbacks" do
62
+ stub_request(:delete, "http://example.com/models/10.json").to_return(json: {})
63
+ @model = CallbackSpec::Model.instantiate(:id => 10)
64
+ @model.destroy { |obj| resume }
65
+ wait_max 1.0 do
66
+ @model.history.should == ['before_destroy', 'after_destroy']
67
+ end
68
+ end
69
+ end
70
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motion-resource
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-11 00:00:00.000000000 Z
12
+ date: 2013-05-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bubble-wrap
@@ -84,6 +84,7 @@ files:
84
84
  - lib/motion-resource/associations.rb
85
85
  - lib/motion-resource/attributes.rb
86
86
  - lib/motion-resource/base.rb
87
+ - lib/motion-resource/callbacks.rb
87
88
  - lib/motion-resource/crud.rb
88
89
  - lib/motion-resource/find.rb
89
90
  - lib/motion-resource/logger.rb
@@ -100,6 +101,7 @@ files:
100
101
  - spec/motion-resource/associations/scope_spec.rb
101
102
  - spec/motion-resource/attributes_spec.rb
102
103
  - spec/motion-resource/base_spec.rb
104
+ - spec/motion-resource/callbacks_spec.rb
103
105
  - spec/motion-resource/crud_spec.rb
104
106
  - spec/motion-resource/find_spec.rb
105
107
  - spec/motion-resource/requests_spec.rb
@@ -119,7 +121,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
119
121
  version: '0'
120
122
  segments:
121
123
  - 0
122
- hash: -124098762633124978
124
+ hash: 4144364702470032416
123
125
  required_rubygems_version: !ruby/object:Gem::Requirement
124
126
  none: false
125
127
  requirements:
@@ -128,7 +130,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
128
130
  version: '0'
129
131
  segments:
130
132
  - 0
131
- hash: -124098762633124978
133
+ hash: 4144364702470032416
132
134
  requirements: []
133
135
  rubyforge_project:
134
136
  rubygems_version: 1.8.25
@@ -143,6 +145,7 @@ test_files:
143
145
  - spec/motion-resource/associations/scope_spec.rb
144
146
  - spec/motion-resource/attributes_spec.rb
145
147
  - spec/motion-resource/base_spec.rb
148
+ - spec/motion-resource/callbacks_spec.rb
146
149
  - spec/motion-resource/crud_spec.rb
147
150
  - spec/motion-resource/find_spec.rb
148
151
  - spec/motion-resource/requests_spec.rb