arrest 0.0.15 → 0.0.16

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,6 +5,9 @@ require 'arrest/helper/logger'
5
5
 
6
6
  require "arrest/attributes/belongs_to"
7
7
  require "arrest/attributes/has_attributes"
8
+ require "arrest/attributes/attribute"
9
+ require "arrest/attributes/nested_attribute"
10
+ require "arrest/attributes/polymorphic_attribute"
8
11
  require "arrest/attributes/converter"
9
12
  require "arrest/handler"
10
13
  require "arrest/source"
@@ -15,8 +18,8 @@ require "arrest/exceptions"
15
18
  require "arrest/http_source"
16
19
  require "arrest/mem_source"
17
20
  require "arrest/source"
18
- require "arrest/nested_resource"
19
21
  require "arrest/abstract_resource"
22
+ require "arrest/nested_resource"
20
23
  require "arrest/root_resource"
21
24
  require "arrest/rest_child"
22
25
 
@@ -116,7 +116,6 @@ module Arrest
116
116
  add_attribute Attribute.new(name, true, clazz)
117
117
  end
118
118
  end
119
-
120
119
  end
121
120
 
122
121
  include BelongsTo
@@ -0,0 +1,31 @@
1
+ module Arrest
2
+ class Attribute
3
+ attr_accessor :name, :read_only, :clazz, :json_name
4
+ def initialize name, read_only, clazz
5
+ @name = name.to_sym
6
+ @read_only = read_only
7
+ @clazz = clazz
8
+ @json_name = Source.json_key_converter.key_to_json(name).to_sym
9
+ end
10
+
11
+ def from_hash value
12
+ return if value == nil
13
+ converter = CONVERTER[@clazz]
14
+ if converter == nil
15
+ puts "No converter for: #{@clazz.name}"
16
+ converter = IdentConv
17
+ end
18
+ converter.convert value
19
+ end
20
+
21
+ def to_hash value
22
+ return nil unless value != nil
23
+ converter = CONVERTER[@clazz]
24
+ if converter == nil
25
+ puts "No converter for: #{@clazz.name}"
26
+ converter = IdentConv
27
+ end
28
+ converter.mk_json value
29
+ end
30
+ end
31
+ end
@@ -7,28 +7,55 @@ module Arrest
7
7
  end
8
8
 
9
9
  module BelongsToMethods
10
+ def create_field_name(name, params, polymorphic)
11
+ if (params && params[:field_name])
12
+ params[:field_name]
13
+ elsif polymorphic
14
+ "#{name}_ref"
15
+ else
16
+ "#{name}_id"
17
+ end
18
+ end
19
+
20
+ def create_and_add_attribute(field_name, polymorphic, read_only)
21
+ if polymorphic
22
+ add_attribute(PolymorphicAttribute.new(field_name.to_sym, read_only))
23
+ else
24
+ add_attribute(Attribute.new(field_name.to_sym, read_only, String))
25
+ end
26
+ end
27
+
10
28
  def belongs_to(*args)
11
29
  arg = args[0]
12
30
  name = arg.to_s.downcase
13
31
  class_name = StringUtils.classify name
14
32
  params = args[1] unless args.length < 2
15
- field_name = "#{name}_id"
16
33
  if params
17
- field_name = params[:field_name] unless params[:field_name] == nil
18
- class_name = params[:class_name].to_s unless params[:class_name] == nil
19
34
  read_only = params[:read_only] == true
35
+ polymorphic = params[:polymorphic] unless params[:polymorphic] == nil
36
+ class_name = params[:class_name].to_s unless params[:class_name] == nil
20
37
  end
21
- add_attribute(Attribute.new(field_name.to_sym, read_only, String))
38
+
39
+ field_name = create_field_name(name, params, polymorphic)
40
+
41
+ create_and_add_attribute(field_name, polymorphic, read_only)
42
+
22
43
  send :define_method, name do
23
44
  val = self.send(field_name)
24
45
  if val == nil || val == ""
25
46
  return nil
26
- end
47
+ end
48
+
27
49
  begin
28
- Arrest::Source.mod.const_get(class_name).find(val)
50
+ if polymorphic
51
+ Arrest::Source.mod.const_get(polymorphic[val.type.to_sym]).find(val.id)
52
+ else
53
+ Arrest::Source.mod.const_get(class_name).find(val)
54
+ end
29
55
  rescue Errors::DocumentNotFoundError => e
30
56
  raise Errors::DocumentNotFoundError, "Couldnt find a #{class_name} with id #{val}"
31
57
  end
58
+
32
59
  end
33
60
  end
34
61
  end
@@ -4,52 +4,6 @@ end
4
4
 
5
5
  module Arrest
6
6
 
7
- class Attribute
8
- attr_accessor :name, :read_only, :clazz, :json_name
9
- def initialize name, read_only, clazz
10
- @name = name.to_sym
11
- @read_only = read_only
12
- @clazz = clazz
13
- @json_name = Source.json_key_converter.key_to_json(name).to_sym
14
- end
15
-
16
- def from_hash value
17
- return if value == nil
18
- converter = CONVERTER[@clazz]
19
- if converter == nil
20
- puts "No converter for: #{@clazz.name}"
21
- converter = IdentConv
22
- end
23
- converter.convert value
24
- end
25
-
26
- def to_hash value
27
- return nil unless value != nil
28
- converter = CONVERTER[@clazz]
29
- if converter == nil
30
- puts "No converter for: #{@clazz.name}"
31
- converter = IdentConv
32
- end
33
- converter.mk_json value
34
- end
35
- end
36
-
37
- class NestedAttribute < Attribute
38
- def initialize name, read_only, clazz
39
- super name, read_only, clazz
40
- end
41
-
42
- def from_hash value
43
- return nil unless value != nil
44
- @clazz.new value
45
- end
46
-
47
- def to_hash val
48
- return nil unless val!= nil
49
- val.to_hash
50
- end
51
- end
52
-
53
7
  class NestedCollection < Attribute
54
8
  def initialize name, read_only, clazz
55
9
  super name, read_only, clazz
@@ -122,6 +76,7 @@ module Arrest
122
76
  target Array
123
77
  end
124
78
 
79
+
125
80
  class TimeConv < Converter
126
81
  target Time
127
82
 
@@ -0,0 +1,17 @@
1
+ module Arrest
2
+ class NestedAttribute < Attribute
3
+ def initialize name, read_only, clazz
4
+ super name, read_only, clazz
5
+ end
6
+
7
+ def from_hash value
8
+ return nil unless value != nil
9
+ @clazz.new value
10
+ end
11
+
12
+ def to_hash val
13
+ return nil unless val!= nil
14
+ val.to_hash
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ module Arrest
2
+ class Ref
3
+ include HasAttributes
4
+
5
+ attribute :id, String
6
+ attribute :type, String
7
+ end
8
+
9
+ class PolymorphicAttribute < NestedAttribute
10
+ def initialize name, read_only
11
+ super name, read_only, Ref
12
+ end
13
+ end
14
+ end
@@ -1,3 +1,5 @@
1
+ require 'arrest/string_utils'
2
+
1
3
  module Arrest
2
4
 
3
5
  RequestLog = Struct.new(:method, :url, :body)
@@ -44,6 +44,19 @@ module Arrest
44
44
  response.body
45
45
  end
46
46
 
47
+
48
+ def delete_all resource_path
49
+ response = self.connection().delete do |req|
50
+ req.url(resource_path)
51
+ add_headers(req.headers)
52
+ end
53
+ rql = RequestLog.new(:delete, "#{resource_path}", nil)
54
+ rsl = ResponseLog.new(response.env[:status], response.body)
55
+ Arrest::Source.call_logger.log(rql, rsl)
56
+
57
+ response.env[:status] == 200
58
+ end
59
+
47
60
  def delete rest_resource
48
61
  raise "To delete an object it must have an id" unless rest_resource.respond_to?(:id) && rest_resource.id != nil
49
62
  response = self.connection().delete do |req|
@@ -88,6 +88,16 @@ module Arrest
88
88
  wrap val.to_jhash.to_json, 1
89
89
  end
90
90
 
91
+ def delete_all resource_path
92
+ id_list = Array.new(@@collections[resource_path] || [])
93
+ id_list.each do |base_id|
94
+ @@collections.each_pair do |k,v|
95
+ v.reject!{ |id| id == base_id }
96
+ end
97
+ @@all_objects[base_id].delete
98
+ end
99
+ end
100
+
91
101
  def collection_json values
92
102
  single_jsons = values.map do |v|
93
103
  v.to_jhash.to_json
@@ -112,6 +112,11 @@ module Arrest
112
112
  end
113
113
  n
114
114
  end
115
+
116
+
117
+ def delete_all
118
+ source().delete_all(self.resource_path)
119
+ end
115
120
  end
116
121
 
117
122
  def resource_path
@@ -1,3 +1,3 @@
1
1
  module Arrest
2
- VERSION = "0.0.15"
2
+ VERSION = "0.0.16"
3
3
  end
@@ -101,3 +101,22 @@ class ChildFilter < Arrest::RestChild
101
101
 
102
102
  filter(:child_nnn) {|s| bfield == s}
103
103
  end
104
+
105
+ class CommentableA < Arrest::RootResource
106
+ end
107
+ class CommentableB < Arrest::RootResource
108
+ end
109
+ class CommentableC < Arrest::RootResource
110
+ end
111
+ class Comment < Arrest::RootResource
112
+ belongs_to :commentable, :polymorphic => { :coma => "CommentableA",
113
+ :comb => "CommentableB" }
114
+ end
115
+ class ExtendedComment < Comment
116
+ belongs_to :other_commentable,
117
+ :field_name => "special_commentable_ref",
118
+ :polymorphic => { :comc => :CommentableC }
119
+ end
120
+
121
+ class DeleteMeAll < Arrest::RootResource
122
+ end
@@ -397,7 +397,59 @@ class FirstTest < Test::Unit::TestCase
397
397
  expected = now.strftime "%FT%T%z"
398
398
  t = TimeClass.new(:time => now)
399
399
  assert_equal expected, t.to_jhash[:time], "This is the expected default format"
400
-
400
+ end
401
+
402
+ def test_polymorphic_belongs_to
403
+ coma = CommentableA.new()
404
+ coma.save
405
+ comb = CommentableB.new()
406
+ comb.save
407
+
408
+ c = Comment.new(:commentable_ref => { :id => coma.id, :type => "coma"})
409
+ result = c.commentable
410
+ assert_equal coma.id, c.commentable_ref.id
411
+ assert_equal result.class, CommentableA
412
+
413
+ c2 = Comment.new(:commentable_ref => { :id => comb.id, :type => "comb"})
414
+ result2 = c2.commentable
415
+ assert_equal comb.id, c2.commentable_ref.id
416
+ assert_equal result2.class, CommentableB
417
+ end
418
+
419
+ def test_polymorphic_belongs_to_extended
420
+ coma = CommentableA.new()
421
+ coma.save
422
+ comc = CommentableC.new()
423
+ comc.save
424
+
425
+ c = ExtendedComment.new({ :special_commentable_ref => { :id => comc.id, :type => "comc"},
426
+ :commentable_ref => { :id => coma.id, :type => "coma" }})
427
+ assert_equal c.commentable.class, CommentableA
428
+ assert_equal c.other_commentable.class, CommentableC
429
+
430
+ c.save
431
+ c_reloaded = ExtendedComment.find(c.id)
432
+ assert_equal comc.id, c_reloaded.special_commentable_ref.id
433
+ assert_equal CommentableC, c_reloaded.other_commentable.class
434
+ assert_equal CommentableA, c_reloaded.commentable.class
435
+ end
436
+
437
+ def test_delete_all_root_resources
438
+ d1 = DeleteMeAll.new()
439
+ d1.save
440
+ d2 = DeleteMeAll.new()
441
+ d2.save
442
+
443
+ d1_rel = DeleteMeAll.find(d1.id)
444
+ assert_not_nil d1_rel
445
+ d2_rel = DeleteMeAll.find(d2.id)
446
+ assert_not_nil d2_rel
447
+ all = DeleteMeAll.all
448
+ assert_equal 2, all.length
449
+
450
+ DeleteMeAll.delete_all
451
+ all = DeleteMeAll.all
452
+ assert_equal [], all
401
453
  end
402
454
  end
403
455
 
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.15
4
+ version: 0.0.16
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-01-12 00:00:00.000000000Z
12
+ date: 2012-01-18 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &13130900 !ruby/object:Gem::Requirement
16
+ requirement: &18586060 !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: *13130900
24
+ version_requirements: *18586060
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: faraday
27
- requirement: &13129960 !ruby/object:Gem::Requirement
27
+ requirement: &18585500 !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: *13129960
35
+ version_requirements: *18585500
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activemodel
38
- requirement: &13129460 !ruby/object:Gem::Requirement
38
+ requirement: &18585000 !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: *13129460
46
+ version_requirements: *18585000
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: bundler
49
- requirement: &13128960 !ruby/object:Gem::Requirement
49
+ requirement: &18584540 !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: *13128960
57
+ version_requirements: *18584540
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rake
60
- requirement: &13128580 !ruby/object:Gem::Requirement
60
+ requirement: &18584160 !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: *13128580
68
+ version_requirements: *18584160
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rdoc
71
- requirement: &13128080 !ruby/object:Gem::Requirement
71
+ requirement: &18583700 !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: *13128080
79
+ version_requirements: *18583700
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rspec
82
- requirement: &13127520 !ruby/object:Gem::Requirement
82
+ requirement: &18583040 !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: *13127520
90
+ version_requirements: *18583040
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: rr
93
- requirement: &13113200 !ruby/object:Gem::Requirement
93
+ requirement: &18582580 !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: *13113200
101
+ version_requirements: *18582580
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: simplecov
104
- requirement: &13112540 !ruby/object:Gem::Requirement
104
+ requirement: &18581960 !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: *13112540
112
+ version_requirements: *18581960
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: rack
115
- requirement: &13111800 !ruby/object:Gem::Requirement
115
+ requirement: &18581440 !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: *13111800
123
+ version_requirements: *18581440
124
124
  description: Consume a rest API in a AR like fashion
125
125
  email:
126
126
  - axel.tetzlaff@fortytools.com
@@ -136,9 +136,12 @@ files:
136
136
  - arrest.gemspec
137
137
  - lib/arrest.rb
138
138
  - lib/arrest/abstract_resource.rb
139
+ - lib/arrest/attributes/attribute.rb
139
140
  - lib/arrest/attributes/belongs_to.rb
140
141
  - lib/arrest/attributes/converter.rb
141
142
  - lib/arrest/attributes/has_attributes.rb
143
+ - lib/arrest/attributes/nested_attribute.rb
144
+ - lib/arrest/attributes/polymorphic_attribute.rb
142
145
  - lib/arrest/exceptions.rb
143
146
  - lib/arrest/handler.rb
144
147
  - lib/arrest/helper/child_collection.rb