arrest 0.0.59 → 0.0.60

Sign up to get free protection for your applications and to get access to all the features.
@@ -79,11 +79,14 @@ module Arrest
79
79
 
80
80
  attr_accessor :context
81
81
 
82
+ @@POLYMORPHIC_TYPE_MAP = {}
83
+
82
84
  class << self
83
85
  attr_reader :scopes
84
86
 
85
87
  def inherited(child)
86
88
  ScopedRoot::register_resource(child)
89
+ @@POLYMORPHIC_TYPE_MAP[ClassUtils.simple_name(child).to_sym] = child
87
90
  end
88
91
 
89
92
  def mk_proxy(context_provider)
@@ -184,6 +187,32 @@ module Arrest
184
187
  end
185
188
  all_filters
186
189
  end
190
+
191
+ def json_type_map
192
+ @@POLYMORPHIC_TYPE_MAP
193
+ end
194
+
195
+ def custom_json_type(new_key)
196
+ old_key = Arrest::ClassUtils.simple_name(self).to_sym
197
+ value = @@POLYMORPHIC_TYPE_MAP.delete(old_key) # remove default key for self
198
+ @@POLYMORPHIC_TYPE_MAP[new_key.to_sym] = value # add custom key for type
199
+ end
200
+
201
+ def json_type_to_class(type)
202
+ @@POLYMORPHIC_TYPE_MAP[type.to_sym]
203
+ end
204
+
205
+ def to_json_type
206
+ @@POLYMORPHIC_TYPE_MAP.invert[self]
207
+ end
208
+
209
+ def active_resource_classes
210
+ @@POLYMORPHIC_TYPE_MAP.values
211
+ end
212
+ end
213
+
214
+ def to_json_type
215
+ self.class.to_json_type
187
216
  end
188
217
 
189
218
  include BelongsTo
@@ -244,7 +273,7 @@ module Arrest
244
273
  # convenience method printing curl command
245
274
  def curl
246
275
  hs = ""
247
- Arrest::Source.header_decorator.headers.each_pair do |k,v|
276
+ self.context.header_decorator.headers.each_pair do |k,v|
248
277
  hs << " -H '#{k}:#{v}' "
249
278
  end
250
279
 
@@ -34,7 +34,7 @@ module Arrest
34
34
 
35
35
  if params
36
36
  read_only = params[:read_only] == true
37
- polymorphic = params[:polymorphic] unless params[:polymorphic] == nil
37
+ polymorphic = !!params[:polymorphic]
38
38
  class_name = params[:class_name].to_s unless params[:class_name] == nil
39
39
  foreign_key = params[:foreign_key].to_s unless params[:foreign_key] == nil
40
40
  end
@@ -51,7 +51,8 @@ module Arrest
51
51
 
52
52
  begin
53
53
  if polymorphic
54
- Arrest::Source.mod.const_get(polymorphic[val.type.to_sym]).find(self.context, val.id)
54
+ clazz = self.class.json_type_to_class(val.type)
55
+ clazz.find(self.context, val.id)
55
56
  else
56
57
  Arrest::Source.mod.const_get(class_name).find(self.context, val)
57
58
  end
@@ -1,3 +1,3 @@
1
1
  module Arrest
2
- VERSION = "0.0.59"
2
+ VERSION = "0.0.60"
3
3
  end
@@ -108,14 +108,16 @@ class CommentableB < Arrest::RootResource
108
108
  end
109
109
  class CommentableC < Arrest::RootResource
110
110
  end
111
+ class CommentableD < Arrest::RootResource
112
+ custom_json_type :ComD
113
+ end
111
114
  class Comment < Arrest::RootResource
112
- belongs_to :commentable, :polymorphic => { :coma => "CommentableA",
113
- :comb => "CommentableB" }
115
+ belongs_to :commentable, :polymorphic => true
114
116
  end
115
117
  class ExtendedComment < Comment
116
118
  belongs_to :other_commentable,
117
119
  :field_name => "special_commentable_ref",
118
- :polymorphic => { :comc => :CommentableC }
120
+ :polymorphic => true
119
121
  end
120
122
 
121
123
  class DeleteMeAll < Arrest::RootResource
@@ -426,15 +426,23 @@ class FirstTest < Test::Unit::TestCase
426
426
  comb = @scope.CommentableB.new()
427
427
  comb.save
428
428
 
429
- c = @scope.Comment.new(:commentable_ref => { :id => coma.id, :type => "coma"})
429
+ c = @scope.Comment.new(:commentable_ref => { :id => coma.id, :type => coma.to_json_type.to_s})
430
430
  result = c.commentable
431
431
  assert_equal coma.id, c.commentable_ref.id
432
432
  assert_equal result.class, CommentableA
433
433
 
434
- c2 = @scope.Comment.new(:commentable_ref => { :id => comb.id, :type => "comb"})
434
+ c2 = @scope.Comment.new(:commentable_ref => { :id => comb.id, :type => comb.to_json_type.to_s})
435
435
  result2 = c2.commentable
436
436
  assert_equal comb.id, c2.commentable_ref.id
437
437
  assert_equal result2.class, CommentableB
438
+
439
+ comd = @scope.CommentableD.new()
440
+ comd.save
441
+
442
+ c3 = @scope.Comment.new(:commentable_ref => { :id => comd.id, :type => comd.to_json_type.to_s})
443
+ result = c3.commentable
444
+ assert_equal comd.id, c3.commentable_ref.id
445
+ assert_equal result.class, CommentableD
438
446
  end
439
447
 
440
448
  def test_polymorphic_belongs_to_extended
@@ -443,8 +451,8 @@ class FirstTest < Test::Unit::TestCase
443
451
  comc = @scope.CommentableC.new()
444
452
  comc.save
445
453
 
446
- c = @scope.ExtendedComment.new({ :special_commentable_ref => { :id => comc.id, :type => "comc"},
447
- :commentable_ref => { :id => coma.id, :type => "coma" }})
454
+ c = @scope.ExtendedComment.new({ :special_commentable_ref => { :id => comc.id, :type => comc.to_json_type.to_s},
455
+ :commentable_ref => { :id => coma.id, :type => coma.to_json_type.to_s }})
448
456
  assert_equal c.commentable.class, CommentableA
449
457
  assert_equal c.other_commentable.class, CommentableC
450
458
 
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.59
4
+ version: 0.0.60
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-06-05 00:00:00.000000000Z
12
+ date: 2012-06-07 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &10269940 !ruby/object:Gem::Requirement
16
+ requirement: &20022840 !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: *10269940
24
+ version_requirements: *20022840
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: faraday
27
- requirement: &10254540 !ruby/object:Gem::Requirement
27
+ requirement: &20022340 !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: *10254540
35
+ version_requirements: *20022340
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activemodel
38
- requirement: &10249920 !ruby/object:Gem::Requirement
38
+ requirement: &20021840 !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: *10249920
46
+ version_requirements: *20021840
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: bundler
49
- requirement: &10234280 !ruby/object:Gem::Requirement
49
+ requirement: &20021380 !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: *10234280
57
+ version_requirements: *20021380
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rake
60
- requirement: &10231280 !ruby/object:Gem::Requirement
60
+ requirement: &20021000 !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: *10231280
68
+ version_requirements: *20021000
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rdoc
71
- requirement: &10225860 !ruby/object:Gem::Requirement
71
+ requirement: &20020540 !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: *10225860
79
+ version_requirements: *20020540
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rspec
82
- requirement: &10222700 !ruby/object:Gem::Requirement
82
+ requirement: &20020040 !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: *10222700
90
+ version_requirements: *20020040
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: rr
93
- requirement: &10211000 !ruby/object:Gem::Requirement
93
+ requirement: &20019620 !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: *10211000
101
+ version_requirements: *20019620
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: simplecov
104
- requirement: &10210280 !ruby/object:Gem::Requirement
104
+ requirement: &20019160 !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: *10210280
112
+ version_requirements: *20019160
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: rack
115
- requirement: &10209440 !ruby/object:Gem::Requirement
115
+ requirement: &20018740 !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: *10209440
123
+ version_requirements: *20018740
124
124
  description: Consume a rest API in a AR like fashion
125
125
  email:
126
126
  - axel.tetzlaff@fortytools.com