arrest 0.0.83.1 → 0.0.84
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/arrest/abstract_resource.rb +1 -1
- data/lib/arrest/attributes/attribute.rb +12 -3
- data/lib/arrest/attributes/belongs_to.rb +6 -5
- data/lib/arrest/attributes/belongs_to_attribute.rb +2 -2
- data/lib/arrest/attributes/has_attributes.rb +20 -14
- data/lib/arrest/attributes/nested_attribute.rb +2 -2
- data/lib/arrest/attributes/nested_collection.rb +2 -2
- data/lib/arrest/attributes/polymorphic_attribute.rb +3 -3
- data/lib/arrest/helper/has_many.rb +8 -2
- data/lib/arrest/helper/ordered_collection.rb +1 -0
- data/lib/arrest/root_resource.rb +1 -1
- data/lib/arrest/transport/http_source.rb +8 -16
- data/lib/arrest/version.rb +1 -1
- metadata +22 -22
|
@@ -1,13 +1,22 @@
|
|
|
1
1
|
module Arrest
|
|
2
2
|
class Attribute
|
|
3
|
-
attr_accessor :name, :
|
|
4
|
-
|
|
3
|
+
attr_accessor :name, :actions, :clazz, :json_name
|
|
4
|
+
|
|
5
|
+
def initialize name, clazz, actions = nil
|
|
5
6
|
@name = name.to_sym
|
|
6
|
-
@
|
|
7
|
+
@actions = actions || [:create, :retrieve, :update, :delete]
|
|
7
8
|
@clazz = clazz
|
|
8
9
|
@json_name = Source.json_key_converter.key_to_json(name).to_sym
|
|
9
10
|
end
|
|
10
11
|
|
|
12
|
+
def read_only?
|
|
13
|
+
@actions == [:retrieve]
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def mutable?
|
|
17
|
+
@actions.include?(:create) || @actions.include?(:update)
|
|
18
|
+
end
|
|
19
|
+
|
|
11
20
|
def from_hash(parent, value)
|
|
12
21
|
return if value == nil
|
|
13
22
|
|
|
@@ -17,11 +17,11 @@ module Arrest
|
|
|
17
17
|
end
|
|
18
18
|
end
|
|
19
19
|
|
|
20
|
-
def create_and_add_attribute(field_name, polymorphic,
|
|
20
|
+
def create_and_add_attribute(field_name, polymorphic, actions, foreign_key, class_name)
|
|
21
21
|
if polymorphic
|
|
22
|
-
add_attribute(PolymorphicAttribute.new(field_name.to_sym,
|
|
22
|
+
add_attribute(PolymorphicAttribute.new(field_name.to_sym, actions))
|
|
23
23
|
else
|
|
24
|
-
add_attribute(BelongsToAttribute.new(field_name.to_sym,
|
|
24
|
+
add_attribute(BelongsToAttribute.new(field_name.to_sym, actions, String, foreign_key, class_name))
|
|
25
25
|
end
|
|
26
26
|
end
|
|
27
27
|
|
|
@@ -32,8 +32,9 @@ module Arrest
|
|
|
32
32
|
foreign_key = "#{StringUtils.underscore(ClassUtils.simple_name(self))}_id"
|
|
33
33
|
params = args[1] unless args.length < 2
|
|
34
34
|
|
|
35
|
+
actions = [:create, :retrieve, :update, :delete]
|
|
35
36
|
if params
|
|
36
|
-
|
|
37
|
+
actions = params[:actions] if params[:actions]
|
|
37
38
|
polymorphic = !!params[:polymorphic]
|
|
38
39
|
class_name = params[:class_name].to_s unless params[:class_name] == nil
|
|
39
40
|
foreign_key = params[:foreign_key].to_s unless params[:foreign_key] == nil
|
|
@@ -41,7 +42,7 @@ module Arrest
|
|
|
41
42
|
|
|
42
43
|
field_name = create_field_name(name, params, polymorphic)
|
|
43
44
|
|
|
44
|
-
create_and_add_attribute(field_name, polymorphic,
|
|
45
|
+
create_and_add_attribute(field_name, polymorphic, actions, foreign_key, class_name)
|
|
45
46
|
|
|
46
47
|
send :define_method, name do
|
|
47
48
|
val = self.send(field_name)
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
module Arrest
|
|
2
2
|
class BelongsToAttribute < Attribute
|
|
3
3
|
attr_accessor :foreign_key
|
|
4
|
-
def initialize(name,
|
|
5
|
-
super(name,
|
|
4
|
+
def initialize(name, actions, field_class, foreign_key, target_class_name)
|
|
5
|
+
super(name, field_class, actions)
|
|
6
6
|
@foreign_key = foreign_key
|
|
7
7
|
@target_class_name = target_class_name
|
|
8
8
|
end
|
|
@@ -61,6 +61,9 @@ module Arrest
|
|
|
61
61
|
end
|
|
62
62
|
value = as[key]
|
|
63
63
|
converted = field.from_hash(self, value)
|
|
64
|
+
|
|
65
|
+
@attribute_values[field.name.to_sym] = converted
|
|
66
|
+
|
|
64
67
|
self.send(field.name.to_s + '=', converted) unless converted == nil
|
|
65
68
|
end
|
|
66
69
|
end
|
|
@@ -93,13 +96,16 @@ module Arrest
|
|
|
93
96
|
end
|
|
94
97
|
|
|
95
98
|
|
|
96
|
-
def to_jhash
|
|
97
|
-
to_hash(false, true)
|
|
99
|
+
def to_jhash(action)
|
|
100
|
+
to_hash(false, true, action)
|
|
98
101
|
end
|
|
99
102
|
|
|
100
|
-
def to_hash(show_all_fields = true, json_names = false)
|
|
103
|
+
def to_hash(show_all_fields = true, json_names = false, action = nil)
|
|
101
104
|
result = {}
|
|
102
|
-
|
|
105
|
+
|
|
106
|
+
self.class.all_fields.find_all{|a| show_all_fields ||
|
|
107
|
+
!action ||
|
|
108
|
+
a.actions.include?(action)}.each do |field|
|
|
103
109
|
if json_names
|
|
104
110
|
json_name = field.json_name
|
|
105
111
|
else
|
|
@@ -107,9 +113,8 @@ module Arrest
|
|
|
107
113
|
end
|
|
108
114
|
val = self.send(field.name)
|
|
109
115
|
converted = field.to_hash val
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
end
|
|
116
|
+
|
|
117
|
+
result[json_name] = converted
|
|
113
118
|
end
|
|
114
119
|
result
|
|
115
120
|
end
|
|
@@ -122,8 +127,12 @@ module Arrest
|
|
|
122
127
|
end
|
|
123
128
|
|
|
124
129
|
def attribute(name, clazz, attribs = {})
|
|
125
|
-
|
|
126
|
-
|
|
130
|
+
if !!attribs[:read_only] && !attribs[:actions]
|
|
131
|
+
actions = [:retrieve]
|
|
132
|
+
else
|
|
133
|
+
actions = attribs[:actions]
|
|
134
|
+
end
|
|
135
|
+
add_attribute Attribute.new(name, clazz, actions)
|
|
127
136
|
end
|
|
128
137
|
|
|
129
138
|
def attributes(args)
|
|
@@ -140,7 +149,6 @@ module Arrest
|
|
|
140
149
|
Arrest::debug "setter #{self.class.name} #{attribute.name} = #{converted_v}"
|
|
141
150
|
self.attribute_values[attribute.name] = converted_v
|
|
142
151
|
end
|
|
143
|
-
|
|
144
152
|
# define getter for attribute value
|
|
145
153
|
send :define_method, "#{attribute.name}" do
|
|
146
154
|
Arrest::debug "getter #{self.class.name} #{attribute.name}"
|
|
@@ -156,13 +164,11 @@ module Arrest
|
|
|
156
164
|
end
|
|
157
165
|
|
|
158
166
|
def nested name, clazz, options = {}
|
|
159
|
-
|
|
160
|
-
add_attribute NestedAttribute.new(name, read_only, clazz)
|
|
167
|
+
add_attribute NestedAttribute.new(name, clazz, options[:actions])
|
|
161
168
|
end
|
|
162
169
|
|
|
163
170
|
def nested_array name, clazz, options = {}
|
|
164
|
-
|
|
165
|
-
add_attribute Arrest::NestedCollection.new(name, read_only, clazz)
|
|
171
|
+
add_attribute Arrest::NestedCollection.new(name, clazz, options[:actions])
|
|
166
172
|
end
|
|
167
173
|
end
|
|
168
174
|
|
|
@@ -4,15 +4,15 @@ module Arrest
|
|
|
4
4
|
|
|
5
5
|
attribute :id, String
|
|
6
6
|
attribute :type, String
|
|
7
|
-
|
|
7
|
+
|
|
8
8
|
def self.mk_json(value)
|
|
9
9
|
{:id => value.id, :type => value.type}.to_json
|
|
10
10
|
end
|
|
11
11
|
end
|
|
12
12
|
|
|
13
13
|
class PolymorphicAttribute < NestedAttribute
|
|
14
|
-
def initialize name,
|
|
15
|
-
super name,
|
|
14
|
+
def initialize name, actions
|
|
15
|
+
super name, Ref, actions
|
|
16
16
|
end
|
|
17
17
|
|
|
18
18
|
def from_hash(parent, value)
|
|
@@ -17,17 +17,23 @@ module Arrest
|
|
|
17
17
|
ids_method_url_part = "/" + ids_method_name.to_s # /my_team_ids
|
|
18
18
|
method_url_part = "/" + method_name.to_s # /my_teams
|
|
19
19
|
|
|
20
|
+
read_only = false
|
|
20
21
|
if options
|
|
21
22
|
clazz_name = options[:class_name].to_s unless options[:class_name] == nil # :Team
|
|
22
23
|
foreign_key = "#{StringUtils.underscore(clazz_name)}_id" # team_id
|
|
23
24
|
foreign_key = options[:foreign_key].to_s unless options[:foreign_key] == nil # team_id
|
|
24
25
|
ids_method_url_part = "/" + options[:url_part].to_s unless options[:url_part] == nil # /my_url_part_ids
|
|
25
26
|
method_url_part = "/" + options[:url_part].to_s unless options[:url_part] == nil # /my_url_part
|
|
27
|
+
read_only = !!options[:read_only] unless options[:read_only] == nil
|
|
26
28
|
end
|
|
27
29
|
|
|
28
30
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
+
if read_only
|
|
32
|
+
send(:define_method, ids_method_name) do |filter = {}|
|
|
33
|
+
IdsCollection.new(self, self.resource_location + ids_method_url_part)
|
|
34
|
+
end
|
|
35
|
+
else
|
|
36
|
+
self.attribute ids_method_name.to_sym, Array
|
|
31
37
|
end
|
|
32
38
|
|
|
33
39
|
send(:define_method, method_name) do |filter = {}|
|
data/lib/arrest/root_resource.rb
CHANGED
|
@@ -84,8 +84,7 @@ module Arrest
|
|
|
84
84
|
|
|
85
85
|
def put(context, rest_resource)
|
|
86
86
|
raise "To change an object it must have an id" unless rest_resource.respond_to?(:id) && rest_resource.id != nil
|
|
87
|
-
hash = rest_resource.to_jhash
|
|
88
|
-
insert_nulls!(rest_resource,hash)
|
|
87
|
+
hash = rest_resource.to_jhash(:update)
|
|
89
88
|
hash.delete(:id)
|
|
90
89
|
hash.delete("id")
|
|
91
90
|
body = JSON.generate(hash)
|
|
@@ -96,7 +95,9 @@ module Arrest
|
|
|
96
95
|
def internal_put(rest_resource, location, body)
|
|
97
96
|
profiler_status_str = ""
|
|
98
97
|
::ActiveSupport::Notifications.instrument("http.sgdb",
|
|
99
|
-
|
|
98
|
+
:method => :delete,
|
|
99
|
+
:url => rest_resource.resource_location,
|
|
100
|
+
:status => profiler_status_str) do
|
|
100
101
|
headers = nil
|
|
101
102
|
response = self.connection().put do |req|
|
|
102
103
|
req.url(location)
|
|
@@ -114,27 +115,18 @@ module Arrest
|
|
|
114
115
|
end
|
|
115
116
|
end
|
|
116
117
|
|
|
117
|
-
def insert_nulls!(rest_resource, hash)
|
|
118
|
-
rest_resource.class.all_fields.each do |field|
|
|
119
|
-
changed = (!rest_resource.respond_to?("#{field.name}_changed?" || rest_resource.send("#{field}_changed?")))
|
|
120
|
-
if !field.read_only && hash[field.json_name] == nil && changed
|
|
121
|
-
hash[field.json_name] = Null.singleton
|
|
122
|
-
end
|
|
123
|
-
end
|
|
124
|
-
end
|
|
125
|
-
|
|
126
118
|
def post(context, rest_resource)
|
|
127
119
|
profiler_status_str = ""
|
|
128
120
|
::ActiveSupport::Notifications.instrument("http.sgdb",
|
|
129
|
-
|
|
121
|
+
:method => :post,
|
|
122
|
+
:url => rest_resource.resource_path,
|
|
123
|
+
:status => profiler_status_str) do
|
|
130
124
|
raise "new object must have setter for id" unless rest_resource.respond_to?(:id=)
|
|
131
125
|
raise "new object must not have id" if rest_resource.respond_to?(:id) && rest_resource.id != nil
|
|
132
|
-
hash = rest_resource.to_jhash
|
|
126
|
+
hash = rest_resource.to_jhash(:create)
|
|
133
127
|
hash.delete(:id)
|
|
134
128
|
hash.delete('id')
|
|
135
129
|
|
|
136
|
-
insert_nulls!(rest_resource,hash)
|
|
137
|
-
|
|
138
130
|
body = JSON.generate(hash)
|
|
139
131
|
headers = nil
|
|
140
132
|
response = self.connection().post do |req|
|
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.84
|
|
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-09-
|
|
12
|
+
date: 2012-09-04 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: json
|
|
16
|
-
requirement: &
|
|
16
|
+
requirement: &23482040 !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: *23482040
|
|
25
25
|
- !ruby/object:Gem::Dependency
|
|
26
26
|
name: faraday
|
|
27
|
-
requirement: &
|
|
27
|
+
requirement: &23480980 !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: *23480980
|
|
36
36
|
- !ruby/object:Gem::Dependency
|
|
37
37
|
name: activemodel
|
|
38
|
-
requirement: &
|
|
38
|
+
requirement: &23480480 !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: *23480480
|
|
47
47
|
- !ruby/object:Gem::Dependency
|
|
48
48
|
name: bundler
|
|
49
|
-
requirement: &
|
|
49
|
+
requirement: &23479940 !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: *23479940
|
|
58
58
|
- !ruby/object:Gem::Dependency
|
|
59
59
|
name: rake
|
|
60
|
-
requirement: &
|
|
60
|
+
requirement: &23479360 !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: *23479360
|
|
69
69
|
- !ruby/object:Gem::Dependency
|
|
70
70
|
name: rdoc
|
|
71
|
-
requirement: &
|
|
71
|
+
requirement: &23468200 !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: *23468200
|
|
80
80
|
- !ruby/object:Gem::Dependency
|
|
81
81
|
name: rspec
|
|
82
|
-
requirement: &
|
|
82
|
+
requirement: &23467700 !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: *23467700
|
|
91
91
|
- !ruby/object:Gem::Dependency
|
|
92
92
|
name: rr
|
|
93
|
-
requirement: &
|
|
93
|
+
requirement: &23467280 !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: *23467280
|
|
102
102
|
- !ruby/object:Gem::Dependency
|
|
103
103
|
name: simplecov
|
|
104
|
-
requirement: &
|
|
104
|
+
requirement: &23466820 !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: *23466820
|
|
113
113
|
- !ruby/object:Gem::Dependency
|
|
114
114
|
name: rack
|
|
115
|
-
requirement: &
|
|
115
|
+
requirement: &23466400 !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: *23466400
|
|
124
124
|
description: Consume a rest API in a AR like fashion
|
|
125
125
|
email:
|
|
126
126
|
- axel.tetzlaff@fortytools.com
|