arrest 0.0.62 → 0.0.63
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/arrest.rb +0 -1
- data/lib/arrest/helper/has_many.rb +4 -5
- data/lib/arrest/helper/ordered_collection.rb +51 -42
- data/lib/arrest/version.rb +1 -1
- metadata +21 -22
- data/lib/arrest/helper/has_many_collection.rb +0 -163
data/lib/arrest.rb
CHANGED
@@ -19,7 +19,6 @@ require "arrest/handler"
|
|
19
19
|
require "arrest/source"
|
20
20
|
require "arrest/helper/filter"
|
21
21
|
require "arrest/helper/child_collection"
|
22
|
-
require "arrest/helper/has_many_collection"
|
23
22
|
require "arrest/helper/ordered_collection"
|
24
23
|
require "arrest/helper/has_many"
|
25
24
|
require "arrest/helper/has_view"
|
@@ -15,14 +15,14 @@ module Arrest
|
|
15
15
|
foreign_key = clazz_name + "_id"
|
16
16
|
sub_resource = false
|
17
17
|
read_only = false
|
18
|
-
url_part = method_name.to_s
|
18
|
+
url_part = "/" + method_name.to_s
|
19
19
|
if options
|
20
20
|
clazz_name = options[:class_name].to_s unless options[:class_name] == nil
|
21
21
|
foreign_key = "#{StringUtils.underscore(clazz_name)}_id"
|
22
22
|
foreign_key = options[:foreign_key].to_s unless options[:foreign_key] == nil
|
23
23
|
sub_resource = !!options[:sub_resource]
|
24
24
|
read_only = options[:read_only]
|
25
|
-
url_part = options[:url_part].to_s unless options[:url_part] == nil
|
25
|
+
url_part = "/" + options[:url_part].to_s unless options[:url_part] == nil
|
26
26
|
end
|
27
27
|
|
28
28
|
hm_attr = create_has_many_attribute(sub_resource, # e.g. 'team_ids' attribute for 'has_many :teams'
|
@@ -33,9 +33,8 @@ module Arrest
|
|
33
33
|
foreign_key,
|
34
34
|
read_only)
|
35
35
|
add_attribute(hm_attr)
|
36
|
-
|
37
|
-
|
38
|
-
HasManyCollection.new(self, hm_attr)
|
36
|
+
send :define_method, method_name do |filter = {}|# e.g. define 'teams' method for notation 'has_many :teams'
|
37
|
+
OrderedCollection.new(self.context, clazz_name, self.resource_location + url_part.to_s, filter)
|
39
38
|
end
|
40
39
|
end
|
41
40
|
|
@@ -1,43 +1,39 @@
|
|
1
1
|
module Arrest
|
2
2
|
class OrderedCollection #< BasicObject
|
3
3
|
|
4
|
-
def initialize(context,class_or_class_name, base_url, filter = {})
|
4
|
+
def initialize(context, class_or_class_name, base_url, filter = {})
|
5
|
+
reset_params()
|
5
6
|
@filter = filter
|
6
7
|
@context = context
|
7
8
|
if class_or_class_name.is_a?(String) || class_or_class_name.is_a?(Symbol)
|
8
|
-
@clazz_name = (StringUtils.classify(
|
9
|
+
@clazz_name = (StringUtils.classify(class_or_class_name.to_s))
|
9
10
|
else
|
10
11
|
@clazz = class_or_class_name
|
11
12
|
end
|
12
13
|
@base_url = base_url
|
13
|
-
|
14
|
-
@page = 1
|
15
|
-
@page_size = nil
|
16
|
-
@per_page = 5
|
14
|
+
define_filters
|
17
15
|
end
|
18
16
|
|
19
17
|
def method_missing(*args, &block)
|
20
18
|
collection.send(*args, &block)
|
21
19
|
end
|
22
20
|
|
21
|
+
def inspect
|
22
|
+
collection.inspect
|
23
|
+
end
|
24
|
+
|
23
25
|
def limit(count)
|
24
|
-
|
25
|
-
@collection = nil
|
26
|
-
end
|
27
|
-
@page_size = count
|
26
|
+
page(count)
|
28
27
|
self
|
29
28
|
end
|
30
29
|
|
31
30
|
def offset(count)
|
32
|
-
if @
|
33
|
-
new_page = count / @
|
31
|
+
if @page_hash[:pageSize]
|
32
|
+
new_page = count / @page_hash[:pageSize]
|
34
33
|
else
|
35
34
|
new_page = 1
|
36
35
|
end
|
37
|
-
|
38
|
-
@collection = nil
|
39
|
-
end
|
40
|
-
@page = new_page
|
36
|
+
page(new_page)
|
41
37
|
self
|
42
38
|
end
|
43
39
|
|
@@ -50,11 +46,11 @@ module Arrest
|
|
50
46
|
# TODO: move to external module
|
51
47
|
|
52
48
|
def limit_value #:nodoc:
|
53
|
-
@
|
49
|
+
@page_hash[:pageSize] || 0
|
54
50
|
end
|
55
51
|
|
56
52
|
def offset_value #:nodoc:
|
57
|
-
((@
|
53
|
+
((@page_hash[:pageSize] || 0) * (@page - 1)) || 0
|
58
54
|
end
|
59
55
|
|
60
56
|
def current_page
|
@@ -62,8 +58,8 @@ module Arrest
|
|
62
58
|
end
|
63
59
|
|
64
60
|
def num_pages
|
65
|
-
if @
|
66
|
-
(total_count.to_f / @
|
61
|
+
if @page_hash[:pageSize]
|
62
|
+
(total_count.to_f / (@page_hash[:pageSize])).ceil
|
67
63
|
else
|
68
64
|
1
|
69
65
|
end
|
@@ -82,21 +78,19 @@ module Arrest
|
|
82
78
|
end
|
83
79
|
|
84
80
|
def per(num)
|
85
|
-
@
|
81
|
+
@page_size = num.to_i
|
82
|
+
@page_hash.merge!({:pageSize => @page_size, :page => @page})
|
86
83
|
self
|
87
84
|
end
|
88
85
|
|
89
86
|
def page(num)
|
90
87
|
num ||= 1
|
91
|
-
if @page_size != @per_page || @page != num
|
92
|
-
@collection = nil
|
93
|
-
end
|
94
|
-
@page_size = @per_page
|
95
88
|
@page = num.to_i
|
89
|
+
@page_hash.merge!({:pageSize => @page_size, :page => @page})
|
96
90
|
self
|
97
91
|
end
|
98
92
|
|
99
|
-
def
|
93
|
+
def order_by(field, order = :asc)
|
100
94
|
@sort_hash = {:sort => field.to_sym, :order => order.to_sym}
|
101
95
|
self
|
102
96
|
end
|
@@ -104,22 +98,26 @@ module Arrest
|
|
104
98
|
private
|
105
99
|
|
106
100
|
def collection
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
101
|
+
params = {}
|
102
|
+
params.merge!(@page_hash)
|
103
|
+
params.merge!(@sort_hash)
|
104
|
+
|
105
|
+
params.merge!(@filter) # override with params that got passed in
|
106
|
+
url = build_url(@base_url, params)
|
107
|
+
|
108
|
+
response = resolved_class.by_url(@context, url)
|
109
|
+
@total_count = response[:result_count]
|
110
|
+
|
111
|
+
reset_params()
|
112
|
+
|
113
|
+
response[:collection]
|
114
|
+
end
|
115
|
+
|
116
|
+
def reset_params
|
117
|
+
@page = 1
|
118
|
+
@page_size = 5
|
119
|
+
@page_hash = {}
|
120
|
+
@sort_hash = {}
|
123
121
|
end
|
124
122
|
|
125
123
|
def build_url(base_url, params_hash)
|
@@ -133,6 +131,17 @@ module Arrest
|
|
133
131
|
@clazz ||= Source.mod.const_get(@clazz_name)
|
134
132
|
end
|
135
133
|
|
134
|
+
def define_filters
|
135
|
+
resolved_class.all_filters.each do |filter|
|
136
|
+
self.instance_eval <<-"end_eval"
|
137
|
+
def #{filter.name} *args
|
138
|
+
real_args = [collection] + args
|
139
|
+
#{resolved_class.name}.FILTER_#{filter.name}(real_args)
|
140
|
+
end
|
141
|
+
end_eval
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
136
145
|
end
|
137
146
|
end
|
138
147
|
|
data/lib/arrest/version.rb
CHANGED
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.
|
4
|
+
version: 0.0.63
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-06-12 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: json
|
16
|
-
requirement: &
|
16
|
+
requirement: &20728140 !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: *
|
24
|
+
version_requirements: *20728140
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: faraday
|
27
|
-
requirement: &
|
27
|
+
requirement: &20727640 !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: *
|
35
|
+
version_requirements: *20727640
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: activemodel
|
38
|
-
requirement: &
|
38
|
+
requirement: &20727140 !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: *
|
46
|
+
version_requirements: *20727140
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: bundler
|
49
|
-
requirement: &
|
49
|
+
requirement: &20726680 !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: *
|
57
|
+
version_requirements: *20726680
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: rake
|
60
|
-
requirement: &
|
60
|
+
requirement: &20726300 !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: *
|
68
|
+
version_requirements: *20726300
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rdoc
|
71
|
-
requirement: &
|
71
|
+
requirement: &20725840 !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: *
|
79
|
+
version_requirements: *20725840
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: rspec
|
82
|
-
requirement: &
|
82
|
+
requirement: &20725340 !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: *
|
90
|
+
version_requirements: *20725340
|
91
91
|
- !ruby/object:Gem::Dependency
|
92
92
|
name: rr
|
93
|
-
requirement: &
|
93
|
+
requirement: &20724920 !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: *
|
101
|
+
version_requirements: *20724920
|
102
102
|
- !ruby/object:Gem::Dependency
|
103
103
|
name: simplecov
|
104
|
-
requirement: &
|
104
|
+
requirement: &20724460 !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: *
|
112
|
+
version_requirements: *20724460
|
113
113
|
- !ruby/object:Gem::Dependency
|
114
114
|
name: rack
|
115
|
-
requirement: &
|
115
|
+
requirement: &20724040 !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: *
|
123
|
+
version_requirements: *20724040
|
124
124
|
description: Consume a rest API in a AR like fashion
|
125
125
|
email:
|
126
126
|
- axel.tetzlaff@fortytools.com
|
@@ -152,7 +152,6 @@ files:
|
|
152
152
|
- lib/arrest/helper/child_collection.rb
|
153
153
|
- lib/arrest/helper/filter.rb
|
154
154
|
- lib/arrest/helper/has_many.rb
|
155
|
-
- lib/arrest/helper/has_many_collection.rb
|
156
155
|
- lib/arrest/helper/has_view.rb
|
157
156
|
- lib/arrest/helper/logger.rb
|
158
157
|
- lib/arrest/helper/ordered_collection.rb
|
@@ -1,163 +0,0 @@
|
|
1
|
-
module Arrest
|
2
|
-
class HasManyCollection #< BasicObject
|
3
|
-
|
4
|
-
def initialize(abstract_resource, has_many_attribute)
|
5
|
-
@parent = abstract_resource
|
6
|
-
@clazz_name = (StringUtils.classify(has_many_attribute.clazz_name.to_s))
|
7
|
-
@url_part = has_many_attribute.url_part
|
8
|
-
@children = nil
|
9
|
-
@foreign_key_name = (StringUtils.underscore(@parent.class.name).gsub(/^.*\//, '') + '_id').to_sym
|
10
|
-
define_filters
|
11
|
-
@attribute = has_many_attribute
|
12
|
-
@page = 1
|
13
|
-
@page_size = nil
|
14
|
-
@per_page = 5
|
15
|
-
@sort_hash = {}
|
16
|
-
end
|
17
|
-
|
18
|
-
def build attributes = {}
|
19
|
-
extended_attrs = attributes.merge({@foreign_key_name => @parent.id})
|
20
|
-
resolved_class.new(@parent.context, extended_attrs)
|
21
|
-
end
|
22
|
-
|
23
|
-
def method_missing(*args, &block)
|
24
|
-
children.send(*args, &block)
|
25
|
-
end
|
26
|
-
|
27
|
-
def inspect
|
28
|
-
children.inspect
|
29
|
-
end
|
30
|
-
|
31
|
-
def limit(count)
|
32
|
-
if count != @page_size
|
33
|
-
@children = nil
|
34
|
-
end
|
35
|
-
@page_size = count
|
36
|
-
self
|
37
|
-
end
|
38
|
-
|
39
|
-
def offset(count)
|
40
|
-
if @page_size
|
41
|
-
new_page = count / @page_size
|
42
|
-
else
|
43
|
-
new_page = 1
|
44
|
-
end
|
45
|
-
if new_page != @page
|
46
|
-
@children = nil
|
47
|
-
end
|
48
|
-
@page = new_page
|
49
|
-
self
|
50
|
-
end
|
51
|
-
|
52
|
-
def total_count
|
53
|
-
children() # make sure request was made before
|
54
|
-
@total_count
|
55
|
-
end
|
56
|
-
|
57
|
-
# == for kaminari
|
58
|
-
# TODO: move to external module
|
59
|
-
|
60
|
-
def limit_value #:nodoc:
|
61
|
-
@page_size || 0
|
62
|
-
end
|
63
|
-
|
64
|
-
def offset_value #:nodoc:
|
65
|
-
((@page_size || 0) * (@page - 1)) || 0
|
66
|
-
end
|
67
|
-
|
68
|
-
def current_page
|
69
|
-
@page
|
70
|
-
end
|
71
|
-
|
72
|
-
def num_pages
|
73
|
-
if @page_size
|
74
|
-
(total_count.to_f / @page_size).ceil
|
75
|
-
else
|
76
|
-
1
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
def first_page?
|
81
|
-
current_page == 1
|
82
|
-
end
|
83
|
-
|
84
|
-
def last_page?
|
85
|
-
current_page >= num_pages
|
86
|
-
end
|
87
|
-
|
88
|
-
def current_page_count #:nodoc:
|
89
|
-
@page
|
90
|
-
end
|
91
|
-
|
92
|
-
def per(num)
|
93
|
-
@per_page = num.to_i
|
94
|
-
self
|
95
|
-
end
|
96
|
-
|
97
|
-
def page(num)
|
98
|
-
num ||= 1
|
99
|
-
if @page_size != @per_page || @page != num
|
100
|
-
@children = nil
|
101
|
-
end
|
102
|
-
@page_size = @per_page
|
103
|
-
@page = num.to_i
|
104
|
-
self
|
105
|
-
end
|
106
|
-
|
107
|
-
def sort_by(field, order = :asc)
|
108
|
-
@sort_hash = {:sort => field.to_sym, :order => order.to_sym}
|
109
|
-
self
|
110
|
-
end
|
111
|
-
|
112
|
-
private
|
113
|
-
|
114
|
-
def children
|
115
|
-
if @children == nil
|
116
|
-
params = {}
|
117
|
-
if @page_size
|
118
|
-
params[:pageSize] = @page_size
|
119
|
-
params[:page] = @page
|
120
|
-
end
|
121
|
-
if @sort_hash
|
122
|
-
params.merge!(@sort_hash)
|
123
|
-
end
|
124
|
-
|
125
|
-
base_url = @parent.resource_location + '/' + @url_part.to_s
|
126
|
-
url = build_url(base_url, params)
|
127
|
-
|
128
|
-
response = resolved_class.by_url(@parent.context, url)
|
129
|
-
@total_count = response[:result_count]
|
130
|
-
@children = response[:collection]
|
131
|
-
end
|
132
|
-
@children
|
133
|
-
end
|
134
|
-
|
135
|
-
def build_url(base_url, params_hash)
|
136
|
-
return base_url if params_hash.empty?
|
137
|
-
query_str = (base_url.include?('?') ? '&' : '?')
|
138
|
-
query_str += params_hash.map{|k,v| "#{k}=#{v}"}.join('&')
|
139
|
-
base_url + query_str
|
140
|
-
end
|
141
|
-
|
142
|
-
def resolved_class
|
143
|
-
if @clazz == nil
|
144
|
-
@clazz = Source.mod.const_get(@clazz_name)
|
145
|
-
end
|
146
|
-
@clazz
|
147
|
-
end
|
148
|
-
|
149
|
-
|
150
|
-
def define_filters
|
151
|
-
resolved_class.all_filters.each do |filter|
|
152
|
-
self.instance_eval <<-"end_eval"
|
153
|
-
def #{filter.name} *args
|
154
|
-
real_args = [children] + args
|
155
|
-
#{resolved_class.name}.FILTER_#{filter.name}(real_args)
|
156
|
-
end
|
157
|
-
end_eval
|
158
|
-
end
|
159
|
-
end
|
160
|
-
|
161
|
-
end
|
162
|
-
end
|
163
|
-
|