arrest 0.0.82 → 0.0.83.crud

Sign up to get free protection for your applications and to get access to all the features.
@@ -88,7 +88,7 @@ module Arrest
88
88
  def read_only_attributes(args)
89
89
  args.each_pair do |name, clazz|
90
90
  self.send :attr_accessor,name
91
- add_attribute(Attribute.new(name, true, clazz))
91
+ add_attribute(Attribute.new(name, clazz, [:retrieve]))
92
92
  end
93
93
  end
94
94
 
@@ -1,13 +1,22 @@
1
1
  module Arrest
2
2
  class Attribute
3
- attr_accessor :name, :read_only, :clazz, :json_name
4
- def initialize name, read_only, clazz
3
+ attr_accessor :name, :actions, :clazz, :json_name
4
+
5
+ def initialize name, clazz, actions = nil
5
6
  @name = name.to_sym
6
- @read_only = read_only
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, read_only, foreign_key, class_name)
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, read_only))
22
+ add_attribute(PolymorphicAttribute.new(field_name.to_sym, actions))
23
23
  else
24
- add_attribute(BelongsToAttribute.new(field_name.to_sym, read_only, String, foreign_key, class_name))
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
- read_only = params[:read_only] == true
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, read_only, foreign_key, class_name)
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)
@@ -54,12 +55,12 @@ module Arrest
54
55
  begin
55
56
  if polymorphic
56
57
  clazz = self.class.json_type_to_class(val.type)
57
- clazz.find(self.context, val.id)
58
+ id = val.id
58
59
  else
59
- Arrest::Source.mod.const_get(class_name).find(self.context, val)
60
+ clazz = Arrest::Source.mod.const_get(class_name)
61
+ id = val
60
62
  end
61
- rescue Errors::DocumentNotFoundError => e
62
- raise Errors::DocumentNotFoundError, "Couldnt find a #{class_name} with id #{val}"
63
+ clazz.find(self.context, id)
63
64
  end
64
65
 
65
66
  end
@@ -1,8 +1,8 @@
1
1
  module Arrest
2
2
  class BelongsToAttribute < Attribute
3
3
  attr_accessor :foreign_key
4
- def initialize(name, read_only, field_class, foreign_key, target_class_name)
5
- super(name, read_only, field_class)
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
- self.class.all_fields.find_all{|a| show_all_fields || !a.read_only}.each do |field|
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
- if converted != nil
111
- result[json_name] = converted
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
- read_only = !!attribs[:read_only]
126
- add_attribute Attribute.new(name, read_only, clazz)
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
- read_only = !!options[:read_only]
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
- read_only = !!options[:read_only]
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
 
@@ -1,7 +1,7 @@
1
1
  module Arrest
2
2
  class NestedAttribute < Attribute
3
- def initialize name, read_only, clazz
4
- super name, read_only, clazz
3
+ def initialize name, clazz, options
4
+ super name, clazz, options
5
5
  end
6
6
 
7
7
  def from_hash(parent, value)
@@ -1,8 +1,8 @@
1
1
  module Arrest
2
2
 
3
3
  class NestedCollection < Attribute
4
- def initialize name, read_only, clazz
5
- super name, read_only, clazz
4
+ def initialize name, clazz, options
5
+ super name, clazz, options
6
6
  end
7
7
 
8
8
  def from_hash(parent, value)
@@ -4,11 +4,15 @@ module Arrest
4
4
 
5
5
  attribute :id, String
6
6
  attribute :type, String
7
+
8
+ def self.mk_json(value)
9
+ {:id => value.id, :type => value.type}.to_json
10
+ end
7
11
  end
8
12
 
9
13
  class PolymorphicAttribute < NestedAttribute
10
- def initialize name, read_only
11
- super name, read_only, Ref
14
+ def initialize name, actions
15
+ super name, Ref, actions
12
16
  end
13
17
 
14
18
  def from_hash(parent, value)
@@ -1,7 +1,16 @@
1
1
  module Arrest
2
2
  module Errors
3
- class DocumentNotFoundError < StandardError
4
3
 
4
+ class DocumentNotFoundError < StandardError
5
+ end
6
+
7
+ class SpecifiedDocumentNotFoundError < DocumentNotFoundError
8
+ attr_reader :id, :class_type
9
+
10
+ def initialize(id = nil, class_type = nil)
11
+ @id = id
12
+ @class_type = class_type
13
+ end
5
14
  end
6
15
 
7
16
  class PermissionDeniedError < StandardError
@@ -9,5 +18,6 @@ module Arrest
9
18
  super(err_obj)
10
19
  end
11
20
  end
21
+
12
22
  end
13
23
  end
@@ -132,6 +132,7 @@ module Arrest
132
132
  url = build_url(@base_url, params)
133
133
 
134
134
  response = resolved_class.by_url(@context, url)
135
+
135
136
  @total_count = response[:result_count]
136
137
 
137
138
  @collection = response[:collection]
@@ -43,20 +43,21 @@ module Arrest
43
43
 
44
44
  def find(context, id)
45
45
  context.cache.lookup(id) do
46
- if id == nil || "" == id
47
- Arrest::logger.info "DocumentNotFoundError: no id given"
48
- raise Errors::DocumentNotFoundError.new
49
- end
50
- full_resource_path = "#{self.resource_path}/#{id}"
51
- r = source().get(context, full_resource_path)
52
- body = body_root(r)
53
- if body == nil || body.empty?
54
- Arrest::logger.info "DocumentNotFoundError for #{full_resource_path}"
55
- raise Errors::DocumentNotFoundError.new
56
- end
46
+ begin
47
+ raise "Document Id must not be blank" if id == nil || "" == id
48
+
49
+ full_resource_path = "#{self.resource_path}/#{id}"
50
+ r = source().get(context, full_resource_path)
51
+ body = body_root(r)
57
52
 
58
- resource = self.build(context, body.merge({:id => id}))
59
- resource
53
+ raise "Response body must not be empty for #{full_resource_path}" if body == nil || body.empty?
54
+
55
+ resource = self.build(context, body.merge({:id => id}))
56
+ resource
57
+ rescue Exception => e
58
+ Arrest::logger.info e.message if e.message
59
+ raise Errors::SpecifiedDocumentNotFoundError.new(id, self.class)
60
+ end
60
61
  end
61
62
  end
62
63
 
@@ -129,7 +130,7 @@ module Arrest
129
130
  protected
130
131
  def internal_reload
131
132
  context.cache.remove(self.id)
132
- self.class.find(self.context, self.id).to_hash
133
+ self.class.find(self.context, self.id).to_hash(false, false, :retrieve)
133
134
  end
134
135
  end
135
136
  end
@@ -7,14 +7,14 @@ module Arrest
7
7
  r = source().get(context, "#{self.resource_path}")
8
8
  body = body_root(r)
9
9
  if body == nil || body.empty?
10
- Arrest::logger.info "DocumentNotFoundError for #{self.resource_path}"
11
- raise Errors::DocumentNotFoundError.new
10
+ Arrest::logger.info "SpecifiedDocumentNotFoundError for #{self.resource_path}"
11
+ raise Errors::SpecifiedDocumentNotFoundError.new(nil, self.class)
12
12
  end
13
13
  self.build(context, body)
14
14
  end
15
15
 
16
16
  def find(context, id)
17
- raise "A find is not possible for a SingleResource"
17
+ raise "A find is not possible for a SingleResource"
18
18
  end
19
19
  end
20
20
 
@@ -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
- :method => :delete, :url => rest_resource.resource_location, :status => profiler_status_str) do
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
- :method => :post, :url => rest_resource.resource_path, :status => profiler_status_str) do
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|
@@ -1,3 +1,3 @@
1
1
  module Arrest
2
- VERSION = "0.0.82"
2
+ VERSION = "0.0.83.crud"
3
3
  end
metadata CHANGED
@@ -1,19 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arrest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.82
5
- prerelease:
4
+ version: 0.0.83.crud
5
+ prerelease: 7
6
6
  platform: ruby
7
7
  authors:
8
8
  - Axel Tetzlaff
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-09 00:00:00.000000000 Z
12
+ date: 2012-09-03 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &9877060 !ruby/object:Gem::Requirement
16
+ requirement: &26670600 !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: *9877060
24
+ version_requirements: *26670600
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: faraday
27
- requirement: &9892260 !ruby/object:Gem::Requirement
27
+ requirement: &26670100 !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: *9892260
35
+ version_requirements: *26670100
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activemodel
38
- requirement: &9890880 !ruby/object:Gem::Requirement
38
+ requirement: &26669600 !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: *9890880
46
+ version_requirements: *26669600
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: bundler
49
- requirement: &9887760 !ruby/object:Gem::Requirement
49
+ requirement: &26669140 !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: *9887760
57
+ version_requirements: *26669140
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rake
60
- requirement: &9885500 !ruby/object:Gem::Requirement
60
+ requirement: &26668720 !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: *9885500
68
+ version_requirements: *26668720
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rdoc
71
- requirement: &9900900 !ruby/object:Gem::Requirement
71
+ requirement: &26668240 !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: *9900900
79
+ version_requirements: *26668240
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rspec
82
- requirement: &9899160 !ruby/object:Gem::Requirement
82
+ requirement: &26667540 !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: *9899160
90
+ version_requirements: *26667540
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: rr
93
- requirement: &9897980 !ruby/object:Gem::Requirement
93
+ requirement: &26667000 !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: *9897980
101
+ version_requirements: *26667000
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: simplecov
104
- requirement: &9896720 !ruby/object:Gem::Requirement
104
+ requirement: &26666380 !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: *9896720
112
+ version_requirements: *26666380
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: rack
115
- requirement: &9895740 !ruby/object:Gem::Requirement
115
+ requirement: &26665680 !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: *9895740
123
+ version_requirements: *26665680
124
124
  description: Consume a rest API in a AR like fashion
125
125
  email:
126
126
  - axel.tetzlaff@fortytools.com
@@ -185,21 +185,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
185
185
  - - ! '>='
186
186
  - !ruby/object:Gem::Version
187
187
  version: '0'
188
- segments:
189
- - 0
190
- hash: 1138449475091621378
191
188
  required_rubygems_version: !ruby/object:Gem::Requirement
192
189
  none: false
193
190
  requirements:
194
- - - ! '>='
191
+ - - ! '>'
195
192
  - !ruby/object:Gem::Version
196
- version: '0'
197
- segments:
198
- - 0
199
- hash: 1138449475091621378
193
+ version: 1.3.1
200
194
  requirements: []
201
195
  rubyforge_project: arrest
202
- rubygems_version: 1.8.11
196
+ rubygems_version: 1.8.10
203
197
  signing_key:
204
198
  specification_version: 3
205
199
  summary: Another ruby rest client