arrest 0.0.75 → 0.0.76

Sign up to get free protection for your applications and to get access to all the features.
data/lib/arrest.rb CHANGED
@@ -3,6 +3,16 @@ require 'logger'
3
3
  require "arrest/version"
4
4
  require 'arrest/helper/logger'
5
5
 
6
+ require "arrest/utils/class_utils.rb"
7
+ require "arrest/utils/string_utils.rb"
8
+
9
+ require "arrest/transport/source"
10
+ require "arrest/transport/http_source"
11
+ require "arrest/transport/mem_source"
12
+ require "arrest/transport/request_context"
13
+ require "arrest/transport/scoped_root"
14
+ require "arrest/transport/resource_proxy"
15
+
6
16
  require "arrest/attributes/belongs_to"
7
17
  require "arrest/attributes/has_attributes"
8
18
  require "arrest/attributes/attribute"
@@ -11,10 +21,7 @@ require "arrest/attributes/nested_collection"
11
21
  require "arrest/attributes/belongs_to_attribute"
12
22
  require "arrest/attributes/polymorphic_attribute"
13
23
  require "arrest/attributes/converter"
14
- require "arrest/class_utils.rb"
15
- require "arrest/string_utils.rb"
16
24
  require "arrest/handler"
17
- require "arrest/source"
18
25
  require "arrest/helper/filter"
19
26
  require "arrest/helper/child_collection"
20
27
  require "arrest/helper/ordered_collection"
@@ -23,9 +30,6 @@ require "arrest/helper/ids_collection"
23
30
  require "arrest/helper/has_many"
24
31
  require "arrest/helper/has_view"
25
32
  require "arrest/exceptions"
26
- require "arrest/http_source"
27
- require "arrest/mem_source"
28
- require "arrest/source"
29
33
  require "arrest/abstract_resource"
30
34
  require "arrest/nested_resource"
31
35
  require "arrest/root_resource"
@@ -1,5 +1,4 @@
1
1
  require 'json'
2
- require 'arrest/string_utils'
3
2
  require 'time'
4
3
  require 'active_model'
5
4
 
@@ -7,65 +6,6 @@ Scope = Struct.new(:name, :options, :block)
7
6
 
8
7
  module Arrest
9
8
 
10
- class RequestContext
11
- attr_accessor :header_decorator
12
- end
13
-
14
- class ScopedRoot
15
- attr_accessor :context
16
-
17
- def initialize(context = Arrest::RequestContext.new())
18
- @context = context
19
- end
20
-
21
- def self.register_resource(clazz)
22
- @classes ||= []
23
- @classes << clazz
24
- send :define_method, ClassUtils.simple_name(clazz) do ||
25
- proxy = clazz.mk_proxy(self)
26
- proxy
27
- end
28
- end
29
-
30
- def self.registered_classes
31
- @classes ||= []
32
- @classes
33
- end
34
-
35
- def delete_all
36
- self.class.registered_classes.each do |clazz|
37
- begin
38
- clazz.delete_all(@context)
39
- rescue
40
- puts "couldnt delete #{clazz.name}s"
41
- end
42
- end
43
- end
44
-
45
- def get_context
46
- @context
47
- end
48
- end
49
-
50
- class ResourceProxy
51
- def initialize(resource, context_provider)
52
- @resource = resource
53
- @context_provider = context_provider
54
- end
55
-
56
- def method_missing(*args, &block)
57
- params = [@context_provider.get_context]
58
- params += args.drop(1)
59
- @resource.send(args.first, *params)
60
- end
61
-
62
- def load(*args)
63
- ext = [@context_provider.get_context] + args
64
- @resource.load(*ext)
65
- end
66
-
67
- end
68
-
69
9
  class AbstractResource
70
10
  extend ActiveModel::Naming
71
11
  include ActiveModel::Validations
@@ -223,7 +163,9 @@ module Arrest
223
163
  def save
224
164
  if Source.skip_validations || self.valid?
225
165
  req_type = new_record? ? :post : :put
226
- !!AbstractResource::source.send(req_type, @context, self)
166
+ success = !!AbstractResource::source.send(req_type, @context, self)
167
+ context.cache.update(self.id, self) if success
168
+ success
227
169
  else
228
170
  false
229
171
  end
@@ -233,12 +175,14 @@ module Arrest
233
175
  raise self.errors.inspect unless self.save
234
176
  end
235
177
 
178
+ def clone
179
+ self.class.new(self.context, self.to_hash)
180
+ end
181
+
236
182
  def reload
237
183
  @child_collections = {}
238
184
  @views = {}
239
185
  @belongs_tos = {}
240
- @ids_collections = {}
241
- @has_many_collections = {}
242
186
  hash = internal_reload
243
187
  self.attributes= hash
244
188
  end
@@ -7,7 +7,7 @@ module Arrest
7
7
  @target_class_name = target_class_name
8
8
  end
9
9
  def target_class
10
- Arrest::Source.mod.const_get(@target_class_name)
10
+ @target_class ||= Arrest::Source.mod.const_get(@target_class_name)
11
11
  end
12
12
  end
13
- end
13
+ end
@@ -1,4 +1,3 @@
1
- require "arrest/source"
2
1
  require 'active_model'
3
2
 
4
3
  module Arrest
@@ -191,4 +190,4 @@ module Arrest
191
190
  end
192
191
 
193
192
  end
194
- end
193
+ end
@@ -1,4 +1,3 @@
1
- require 'arrest/string_utils'
2
1
 
3
2
  module Arrest
4
3
 
@@ -42,21 +42,22 @@ module Arrest
42
42
  end
43
43
 
44
44
  def find(context, id)
45
- if id == nil || "" == id
46
- Arrest::logger.info "DocumentNotFoundError: no id given"
47
- raise Errors::DocumentNotFoundError.new
48
- end
45
+ context.cache.lookup(id) do
46
+ if id == nil || "" == id
47
+ Arrest::logger.info "DocumentNotFoundError: no id given"
48
+ raise Errors::DocumentNotFoundError.new
49
+ end
50
+ full_resource_path = "#{self.resource_path}/#{id}"
51
+ r = source().get(context, full_resource_path)
52
+ body = body_root(r)
53
+ if body == nil || body.empty?
54
+ Arrest::logger.info "DocumentNotFoundError for #{full_resource_path}"
55
+ raise Errors::DocumentNotFoundError.new
56
+ end
49
57
 
50
- full_resource_path = "#{self.resource_path}/#{id}"
51
- r = source().get(context, full_resource_path)
52
- body = body_root(r)
53
- if body == nil || body.empty?
54
- Arrest::logger.info "DocumentNotFoundError for #{full_resource_path}"
55
- raise Errors::DocumentNotFoundError.new
58
+ resource = self.build(context, body.merge({:id => id}))
59
+ resource
56
60
  end
57
-
58
- resource = self.build(context, body.merge({:id => id}))
59
- resource
60
61
  end
61
62
 
62
63
  def filter name, &aproc
@@ -0,0 +1,38 @@
1
+ module Arrest
2
+
3
+ class RequestContext
4
+ attr_accessor :header_decorator
5
+
6
+ ##
7
+ # override with actual cache if desired
8
+ def cache
9
+ @cache ||= DummyCache.new
10
+ end
11
+
12
+ end
13
+
14
+ class IdentityCache
15
+ def initialize()
16
+ @map = {}
17
+ end
18
+
19
+ def lookup(id, &blk)
20
+ hit = @map[id]
21
+ unless hit
22
+ hit = @map[id] = yield
23
+ end
24
+ hit.clone
25
+ end
26
+
27
+ def update(id, object)
28
+ @map[id] = object
29
+ end
30
+ end
31
+
32
+ class DummyCache
33
+ def lookup(id, &blk)
34
+ yield
35
+ end
36
+ end
37
+ end
38
+
@@ -0,0 +1,24 @@
1
+ module Arrest
2
+ ##
3
+ # Forwards context as first parameter to every method call
4
+ # of the proxied class
5
+ class ResourceProxy
6
+ def initialize(resource, context_provider)
7
+ @resource = resource
8
+ @context_provider = context_provider
9
+ end
10
+
11
+ def method_missing(*args, &block)
12
+ params = [@context_provider.get_context]
13
+ params += args.drop(1)
14
+ @resource.send(args.first, *params)
15
+ end
16
+
17
+ def load(*args)
18
+ ext = [@context_provider.get_context] + args
19
+ @resource.load(*ext)
20
+ end
21
+
22
+ end
23
+
24
+ end
@@ -0,0 +1,38 @@
1
+ module Arrest
2
+ class ScopedRoot
3
+ attr_accessor :context
4
+
5
+ def initialize(context = Arrest::RequestContext.new())
6
+ @context = context
7
+ end
8
+
9
+ def self.register_resource(clazz)
10
+ @classes ||= {}
11
+ @classes[clazz] = true
12
+ class_name = ClassUtils.simple_name(clazz)
13
+ send :define_method, class_name do ||
14
+ clazz.mk_proxy(self)
15
+ end
16
+ end
17
+
18
+ def self.registered_classes
19
+ @classes ||= {}
20
+ @classes.keys
21
+ end
22
+
23
+ def delete_all
24
+ self.class.registered_classes.each do |clazz|
25
+ begin
26
+ clazz.delete_all(@context)
27
+ rescue
28
+ puts "couldnt delete #{clazz.name}s"
29
+ end
30
+ end
31
+ end
32
+
33
+ def get_context
34
+ @context
35
+ end
36
+ end
37
+
38
+ end
File without changes
File without changes
@@ -1,3 +1,3 @@
1
1
  module Arrest
2
- VERSION = "0.0.75"
2
+ VERSION = "0.0.76"
3
3
  end
@@ -95,25 +95,6 @@ class NestedResourcesTest < Test::Unit::TestCase
95
95
 
96
96
  end
97
97
 
98
- def test_custom_belongs_to
99
-
100
- new_zoo = @scope.Zoo.new({:name => "Foo"})
101
- new_zoo.save
102
-
103
- c = @scope.CustomNamedBelongsTo.new({:name => 'Bar', :schinken => new_zoo.id, :batzen => new_zoo.id})
104
-
105
- c.save
106
- assert_not_nil c.id, "Persisted object should have id"
107
- assert_equal "Foo", c.zoo_thing.name
108
- assert_equal "Foo", c.zoo.name
109
-
110
-
111
- assert_not_nil c.id, "Persisted zoo should have id"
112
- c_reloaded = @scope.CustomNamedBelongsTo.all.first
113
- assert_equal "Foo", c_reloaded.zoo_thing.name
114
- assert_equal "Foo", c_reloaded.zoo.name
115
-
116
- end
117
98
 
118
99
  def assert_equal_hashes expected, actual
119
100
  assert_equal_hashes_ expected, actual, ''
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arrest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.75
4
+ version: 0.0.76
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-25 00:00:00.000000000Z
12
+ date: 2012-07-26 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &24274620 !ruby/object:Gem::Requirement
16
+ requirement: &2158672560 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *24274620
24
+ version_requirements: *2158672560
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: faraday
27
- requirement: &24274120 !ruby/object:Gem::Requirement
27
+ requirement: &2158671820 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.7.5
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *24274120
35
+ version_requirements: *2158671820
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activemodel
38
- requirement: &24273620 !ruby/object:Gem::Requirement
38
+ requirement: &2158671080 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '3'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *24273620
46
+ version_requirements: *2158671080
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: bundler
49
- requirement: &24273160 !ruby/object:Gem::Requirement
49
+ requirement: &2158670480 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.0.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *24273160
57
+ version_requirements: *2158670480
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rake
60
- requirement: &24272780 !ruby/object:Gem::Requirement
60
+ requirement: &2158669820 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *24272780
68
+ version_requirements: *2158669820
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rdoc
71
- requirement: &24272320 !ruby/object:Gem::Requirement
71
+ requirement: &2158669040 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *24272320
79
+ version_requirements: *2158669040
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rspec
82
- requirement: &24271820 !ruby/object:Gem::Requirement
82
+ requirement: &2158661900 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ~>
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '2'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *24271820
90
+ version_requirements: *2158661900
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: rr
93
- requirement: &24271400 !ruby/object:Gem::Requirement
93
+ requirement: &2158661380 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :development
100
100
  prerelease: false
101
- version_requirements: *24271400
101
+ version_requirements: *2158661380
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: simplecov
104
- requirement: &24270940 !ruby/object:Gem::Requirement
104
+ requirement: &2158660540 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,10 +109,10 @@ dependencies:
109
109
  version: '0'
110
110
  type: :development
111
111
  prerelease: false
112
- version_requirements: *24270940
112
+ version_requirements: *2158660540
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: rack
115
- requirement: &24270520 !ruby/object:Gem::Requirement
115
+ requirement: &2158659220 !ruby/object:Gem::Requirement
116
116
  none: false
117
117
  requirements:
118
118
  - - ! '>='
@@ -120,7 +120,7 @@ dependencies:
120
120
  version: '0'
121
121
  type: :development
122
122
  prerelease: false
123
- version_requirements: *24270520
123
+ version_requirements: *2158659220
124
124
  description: Consume a rest API in a AR like fashion
125
125
  email:
126
126
  - axel.tetzlaff@fortytools.com
@@ -144,7 +144,6 @@ files:
144
144
  - lib/arrest/attributes/nested_attribute.rb
145
145
  - lib/arrest/attributes/nested_collection.rb
146
146
  - lib/arrest/attributes/polymorphic_attribute.rb
147
- - lib/arrest/class_utils.rb
148
147
  - lib/arrest/exceptions.rb
149
148
  - lib/arrest/handler.rb
150
149
  - lib/arrest/helper/child_collection.rb
@@ -155,19 +154,22 @@ files:
155
154
  - lib/arrest/helper/ids_collection.rb
156
155
  - lib/arrest/helper/logger.rb
157
156
  - lib/arrest/helper/ordered_collection.rb
158
- - lib/arrest/http_source.rb
159
- - lib/arrest/mem_source.rb
160
157
  - lib/arrest/nested_resource.rb
161
158
  - lib/arrest/rest_child.rb
162
159
  - lib/arrest/root_resource.rb
163
160
  - lib/arrest/single_resource.rb
164
- - lib/arrest/source.rb
165
- - lib/arrest/string_utils.rb
161
+ - lib/arrest/transport/http_source.rb
162
+ - lib/arrest/transport/mem_source.rb
163
+ - lib/arrest/transport/request_context.rb
164
+ - lib/arrest/transport/resource_proxy.rb
165
+ - lib/arrest/transport/scoped_root.rb
166
+ - lib/arrest/transport/source.rb
167
+ - lib/arrest/utils/class_utils.rb
168
+ - lib/arrest/utils/string_utils.rb
166
169
  - lib/arrest/version.rb
167
170
  - spec/arrest_spec.rb
168
171
  - spec/spec_helper.rb
169
172
  - spec/support/models/user.rb
170
- - test/context.rb
171
173
  - test/has_attributed.rb
172
174
  - test/models.rb
173
175
  - test/nested_resource.rb
@@ -185,16 +187,30 @@ required_ruby_version: !ruby/object:Gem::Requirement
185
187
  - - ! '>='
186
188
  - !ruby/object:Gem::Version
187
189
  version: '0'
190
+ segments:
191
+ - 0
192
+ hash: 2183449960608707573
188
193
  required_rubygems_version: !ruby/object:Gem::Requirement
189
194
  none: false
190
195
  requirements:
191
196
  - - ! '>='
192
197
  - !ruby/object:Gem::Version
193
198
  version: '0'
199
+ segments:
200
+ - 0
201
+ hash: 2183449960608707573
194
202
  requirements: []
195
203
  rubyforge_project: arrest
196
204
  rubygems_version: 1.8.10
197
205
  signing_key:
198
206
  specification_version: 3
199
207
  summary: Another ruby rest client
200
- test_files: []
208
+ test_files:
209
+ - spec/arrest_spec.rb
210
+ - spec/spec_helper.rb
211
+ - spec/support/models/user.rb
212
+ - test/has_attributed.rb
213
+ - test/models.rb
214
+ - test/nested_resource.rb
215
+ - test/unit.rb
216
+ - test/validations.rb
data/test/context.rb DELETED
@@ -1,35 +0,0 @@
1
- require 'test/unit'
2
- require 'arrest'
3
- class ContextTest < Test::Unit::TestCase
4
-
5
- def setup
6
- Arrest::Source.source = nil
7
- end
8
-
9
- class Facility < Arrest::RootResource
10
- end
11
-
12
-
13
- class HeaderDeco
14
- def self.headers
15
- puts "MAAAAAHH"
16
- {}
17
- end
18
- end
19
-
20
- def test_context
21
- context = Arrest::RequestContext.new()
22
- context.header_decorator = HeaderDeco
23
- scope = Arrest::ScopedRoot.new(context)
24
- assert_not_nil scope.Facility
25
- assert_not_nil scope.Facility.all
26
-
27
- f0 = scope.Facility.new(:name => 'Foo')
28
- assert f0.save
29
-
30
- f1 = scope.Facility.all.first
31
- f2 = scope.Facility.find(f1.id)
32
-
33
- end
34
- end
35
-