arrest 0.0.49 → 0.0.50

Sign up to get free protection for your applications and to get access to all the features.
data/lib/arrest.rb CHANGED
@@ -20,6 +20,7 @@ require "arrest/helper/filter"
20
20
  require "arrest/helper/child_collection"
21
21
  require "arrest/helper/has_many_collection"
22
22
  require "arrest/helper/ordered_collection"
23
+ require "arrest/helper/has_many"
23
24
  require "arrest/exceptions"
24
25
  require "arrest/http_source"
25
26
  require "arrest/mem_source"
@@ -73,6 +73,7 @@ module Arrest
73
73
  include ActiveModel::Dirty
74
74
  extend ActiveModel::Translation
75
75
  include HasAttributes
76
+ include HasMany
76
77
  attribute :id, String
77
78
 
78
79
  attr_accessor :context
@@ -131,60 +132,6 @@ module Arrest
131
132
  end
132
133
  end
133
134
 
134
- def has_many(*args)
135
- method_name, options = args
136
- ids_field_name = (StringUtils.singular(method_name.to_s) + '_ids').to_sym
137
- method_name = method_name.to_sym
138
- clazz_name = StringUtils.singular(method_name.to_s)
139
- foreign_key = clazz_name + "_id"
140
- sub_resource = false
141
- read_only = false
142
- url_part = method_name.to_s
143
- if options
144
- clazz_name = options[:class_name].to_s unless options[:class_name] == nil
145
- foreign_key = "#{StringUtils.underscore(clazz_name)}_id"
146
- foreign_key = options[:foreign_key].to_s unless options[:foreign_key] == nil
147
- sub_resource = !!options[:sub_resource]
148
- read_only = options[:read_only]
149
- url_part = options[:url_part].to_s unless options[:url_part] == nil
150
- end
151
-
152
- hm_attr = create_has_many_attribute(sub_resource, # e.g. 'team_ids' attribute for 'has_many :teams'
153
- ids_field_name,
154
- method_name,
155
- clazz_name,
156
- url_part,
157
- foreign_key,
158
- read_only)
159
- add_attribute(hm_attr)
160
-
161
- send :define_method, method_name do # e.g. define 'teams' method for notation 'has_many :teams'
162
- @has_many_collections ||= {}
163
- @has_many_collections[method_name] ||= HasManyCollection.new(self, hm_attr)
164
- @has_many_collections[method_name]
165
- end
166
- end
167
-
168
- def create_has_many_attribute(sub_resource, ids_field_name, method_name,
169
- clazz_name, url_part, foreign_key, read_only)
170
- if sub_resource
171
- define_attribute_methods [ids_field_name]
172
- return HasManySubResourceAttribute.new(ids_field_name,
173
- method_name,
174
- clazz_name,
175
- url_part,
176
- foreign_key,
177
- read_only)
178
- else
179
- return HasManyAttribute.new(ids_field_name,
180
- method_name,
181
- clazz_name,
182
- url_part,
183
- foreign_key,
184
- read_only)
185
- end
186
- end
187
-
188
135
  def children(*args)
189
136
  method_name, options = args
190
137
  method_name = method_name.to_sym
@@ -199,7 +146,7 @@ module Arrest
199
146
 
200
147
  send :define_method, method_name do
201
148
  @child_collections ||= {}
202
- @child_collections[method_name] ||= ChildCollection.new(self, (StringUtils.classify (StringUtils.singular clazz_name)))
149
+ @child_collections[method_name] ||= ChildCollection.new(self, (StringUtils.classify(StringUtils.singular clazz_name)))
203
150
  @child_collections[method_name]
204
151
  end
205
152
  end
@@ -0,0 +1,66 @@
1
+ module Arrest
2
+ module HasMany
3
+ include Arrest::HasAttributes
4
+
5
+ def self.included(base)
6
+ base.extend(ClassMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+ def has_many(*args)
11
+ method_name, options = args
12
+ ids_field_name = (StringUtils.singular(method_name.to_s) + '_ids').to_sym
13
+ method_name = method_name.to_sym
14
+ clazz_name = StringUtils.singular(method_name.to_s)
15
+ foreign_key = clazz_name + "_id"
16
+ sub_resource = false
17
+ read_only = false
18
+ url_part = method_name.to_s
19
+ if options
20
+ clazz_name = options[:class_name].to_s unless options[:class_name] == nil
21
+ foreign_key = "#{StringUtils.underscore(clazz_name)}_id"
22
+ foreign_key = options[:foreign_key].to_s unless options[:foreign_key] == nil
23
+ sub_resource = !!options[:sub_resource]
24
+ read_only = options[:read_only]
25
+ url_part = options[:url_part].to_s unless options[:url_part] == nil
26
+ end
27
+
28
+ hm_attr = create_has_many_attribute(sub_resource, # e.g. 'team_ids' attribute for 'has_many :teams'
29
+ ids_field_name,
30
+ method_name,
31
+ clazz_name,
32
+ url_part,
33
+ foreign_key,
34
+ read_only)
35
+ add_attribute(hm_attr)
36
+
37
+ send :define_method, method_name do # e.g. define 'teams' method for notation 'has_many :teams'
38
+ @has_many_collections ||= {}
39
+ @has_many_collections[method_name] ||= HasManyCollection.new(self, hm_attr)
40
+ @has_many_collections[method_name]
41
+ end
42
+ end
43
+
44
+ def create_has_many_attribute(sub_resource, ids_field_name, method_name,
45
+ clazz_name, url_part, foreign_key, read_only)
46
+ if sub_resource
47
+ define_attribute_methods [ids_field_name]
48
+ return HasManySubResourceAttribute.new(ids_field_name,
49
+ method_name,
50
+ clazz_name,
51
+ url_part,
52
+ foreign_key,
53
+ read_only)
54
+ else
55
+ return HasManyAttribute.new(ids_field_name,
56
+ method_name,
57
+ clazz_name,
58
+ url_part,
59
+ foreign_key,
60
+ read_only)
61
+ end
62
+ end
63
+ end
64
+
65
+ end
66
+ end
@@ -1,8 +1,8 @@
1
1
  module Arrest
2
2
  class HasManyCollection #< BasicObject
3
3
 
4
- def initialize(parent, has_many_attribute)
5
- @parent = parent
4
+ def initialize(abstract_resource, has_many_attribute)
5
+ @parent = abstract_resource
6
6
  @clazz_name = (StringUtils.classify(has_many_attribute.clazz_name.to_s))
7
7
  @url_part = has_many_attribute.url_part
8
8
  @children = nil
@@ -41,7 +41,7 @@ module Arrest
41
41
  else
42
42
  new_page = 1
43
43
  end
44
- if new_page != @page
44
+ if new_page != @page
45
45
  @children = nil
46
46
  end
47
47
  @page = new_page
@@ -1,3 +1,3 @@
1
1
  module Arrest
2
- VERSION = "0.0.49"
2
+ VERSION = "0.0.50"
3
3
  end
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.49
4
+ version: 0.0.50
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-05-11 00:00:00.000000000Z
12
+ date: 2012-05-15 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &2164460740 !ruby/object:Gem::Requirement
16
+ requirement: &8844920 !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: *2164460740
24
+ version_requirements: *8844920
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: faraday
27
- requirement: &2164460240 !ruby/object:Gem::Requirement
27
+ requirement: &8839500 !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: *2164460240
35
+ version_requirements: *8839500
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activemodel
38
- requirement: &2164459740 !ruby/object:Gem::Requirement
38
+ requirement: &8889900 !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: *2164459740
46
+ version_requirements: *8889900
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: bundler
49
- requirement: &2164459280 !ruby/object:Gem::Requirement
49
+ requirement: &10680020 !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: *2164459280
57
+ version_requirements: *10680020
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rake
60
- requirement: &2164458900 !ruby/object:Gem::Requirement
60
+ requirement: &10695600 !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: *2164458900
68
+ version_requirements: *10695600
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rdoc
71
- requirement: &2164458440 !ruby/object:Gem::Requirement
71
+ requirement: &10700420 !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: *2164458440
79
+ version_requirements: *10700420
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rspec
82
- requirement: &2164457940 !ruby/object:Gem::Requirement
82
+ requirement: &10707620 !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: *2164457940
90
+ version_requirements: *10707620
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: rr
93
- requirement: &2164457520 !ruby/object:Gem::Requirement
93
+ requirement: &11478380 !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: *2164457520
101
+ version_requirements: *11478380
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: simplecov
104
- requirement: &2164457060 !ruby/object:Gem::Requirement
104
+ requirement: &12979520 !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: *2164457060
112
+ version_requirements: *12979520
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: rack
115
- requirement: &2158326960 !ruby/object:Gem::Requirement
115
+ requirement: &13647440 !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: *2158326960
123
+ version_requirements: *13647440
124
124
  description: Consume a rest API in a AR like fashion
125
125
  email:
126
126
  - axel.tetzlaff@fortytools.com
@@ -150,6 +150,7 @@ files:
150
150
  - lib/arrest/handler.rb
151
151
  - lib/arrest/helper/child_collection.rb
152
152
  - lib/arrest/helper/filter.rb
153
+ - lib/arrest/helper/has_many.rb
153
154
  - lib/arrest/helper/has_many_collection.rb
154
155
  - lib/arrest/helper/logger.rb
155
156
  - lib/arrest/helper/ordered_collection.rb
@@ -195,13 +196,4 @@ rubygems_version: 1.8.10
195
196
  signing_key:
196
197
  specification_version: 3
197
198
  summary: Another ruby rest client
198
- test_files:
199
- - spec/arrest_spec.rb
200
- - spec/spec_helper.rb
201
- - spec/support/models/user.rb
202
- - test/context.rb
203
- - test/has_attributed.rb
204
- - test/models.rb
205
- - test/nested_resource.rb
206
- - test/unit.rb
207
- - test/validations.rb
199
+ test_files: []