arrest 0.0.83.crud → 0.0.83

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, clazz, [:retrieve]))
91
+ add_attribute(Attribute.new(name, true, clazz))
92
92
  end
93
93
  end
94
94
 
@@ -1,22 +1,13 @@
1
1
  module Arrest
2
2
  class Attribute
3
- attr_accessor :name, :actions, :clazz, :json_name
4
-
5
- def initialize name, clazz, actions = nil
3
+ attr_accessor :name, :read_only, :clazz, :json_name
4
+ def initialize name, read_only, clazz
6
5
  @name = name.to_sym
7
- @actions = actions || [:create, :retrieve, :update, :delete]
6
+ @read_only = read_only
8
7
  @clazz = clazz
9
8
  @json_name = Source.json_key_converter.key_to_json(name).to_sym
10
9
  end
11
10
 
12
- def read_only?
13
- @actions == [:retrieve]
14
- end
15
-
16
- def mutable?
17
- @actions.include?(:create) || @actions.include?(:update)
18
- end
19
-
20
11
  def from_hash(parent, value)
21
12
  return if value == nil
22
13
 
@@ -17,11 +17,11 @@ module Arrest
17
17
  end
18
18
  end
19
19
 
20
- def create_and_add_attribute(field_name, polymorphic, actions, foreign_key, class_name)
20
+ def create_and_add_attribute(field_name, polymorphic, read_only, foreign_key, class_name)
21
21
  if polymorphic
22
- add_attribute(PolymorphicAttribute.new(field_name.to_sym, actions))
22
+ add_attribute(PolymorphicAttribute.new(field_name.to_sym, read_only))
23
23
  else
24
- add_attribute(BelongsToAttribute.new(field_name.to_sym, actions, String, foreign_key, class_name))
24
+ add_attribute(BelongsToAttribute.new(field_name.to_sym, read_only, String, foreign_key, class_name))
25
25
  end
26
26
  end
27
27
 
@@ -32,9 +32,8 @@ 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]
36
35
  if params
37
- actions = params[:actions] if params[:actions]
36
+ read_only = params[:read_only] == true
38
37
  polymorphic = !!params[:polymorphic]
39
38
  class_name = params[:class_name].to_s unless params[:class_name] == nil
40
39
  foreign_key = params[:foreign_key].to_s unless params[:foreign_key] == nil
@@ -42,7 +41,7 @@ module Arrest
42
41
 
43
42
  field_name = create_field_name(name, params, polymorphic)
44
43
 
45
- create_and_add_attribute(field_name, polymorphic, actions, foreign_key, class_name)
44
+ create_and_add_attribute(field_name, polymorphic, read_only, foreign_key, class_name)
46
45
 
47
46
  send :define_method, name do
48
47
  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, actions, field_class, foreign_key, target_class_name)
5
- super(name, field_class, actions)
4
+ def initialize(name, read_only, field_class, foreign_key, target_class_name)
5
+ super(name, read_only, field_class)
6
6
  @foreign_key = foreign_key
7
7
  @target_class_name = target_class_name
8
8
  end
@@ -61,9 +61,6 @@ 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
-
67
64
  self.send(field.name.to_s + '=', converted) unless converted == nil
68
65
  end
69
66
  end
@@ -96,16 +93,13 @@ module Arrest
96
93
  end
97
94
 
98
95
 
99
- def to_jhash(action)
100
- to_hash(false, true, action)
96
+ def to_jhash
97
+ to_hash(false, true)
101
98
  end
102
99
 
103
- def to_hash(show_all_fields = true, json_names = false, action = nil)
100
+ def to_hash(show_all_fields = true, json_names = false)
104
101
  result = {}
105
-
106
- self.class.all_fields.find_all{|a| show_all_fields ||
107
- !action ||
108
- a.actions.include?(action)}.each do |field|
102
+ self.class.all_fields.find_all{|a| show_all_fields || !a.read_only}.each do |field|
109
103
  if json_names
110
104
  json_name = field.json_name
111
105
  else
@@ -113,8 +107,9 @@ module Arrest
113
107
  end
114
108
  val = self.send(field.name)
115
109
  converted = field.to_hash val
116
-
117
- result[json_name] = converted
110
+ if converted != nil
111
+ result[json_name] = converted
112
+ end
118
113
  end
119
114
  result
120
115
  end
@@ -127,12 +122,8 @@ module Arrest
127
122
  end
128
123
 
129
124
  def attribute(name, clazz, attribs = {})
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)
125
+ read_only = !!attribs[:read_only]
126
+ add_attribute Attribute.new(name, read_only, clazz)
136
127
  end
137
128
 
138
129
  def attributes(args)
@@ -149,6 +140,7 @@ module Arrest
149
140
  Arrest::debug "setter #{self.class.name} #{attribute.name} = #{converted_v}"
150
141
  self.attribute_values[attribute.name] = converted_v
151
142
  end
143
+
152
144
  # define getter for attribute value
153
145
  send :define_method, "#{attribute.name}" do
154
146
  Arrest::debug "getter #{self.class.name} #{attribute.name}"
@@ -164,11 +156,13 @@ module Arrest
164
156
  end
165
157
 
166
158
  def nested name, clazz, options = {}
167
- add_attribute NestedAttribute.new(name, clazz, options[:actions])
159
+ read_only = !!options[:read_only]
160
+ add_attribute NestedAttribute.new(name, read_only, clazz)
168
161
  end
169
162
 
170
163
  def nested_array name, clazz, options = {}
171
- add_attribute Arrest::NestedCollection.new(name, clazz, options[:actions])
164
+ read_only = !!options[:read_only]
165
+ add_attribute Arrest::NestedCollection.new(name, read_only, clazz)
172
166
  end
173
167
  end
174
168
 
@@ -1,7 +1,7 @@
1
1
  module Arrest
2
2
  class NestedAttribute < Attribute
3
- def initialize name, clazz, options
4
- super name, clazz, options
3
+ def initialize name, read_only, clazz
4
+ super name, read_only, clazz
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, clazz, options
5
- super name, clazz, options
4
+ def initialize name, read_only, clazz
5
+ super name, read_only, clazz
6
6
  end
7
7
 
8
8
  def from_hash(parent, value)
@@ -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, actions
15
- super name, Ref, actions
14
+ def initialize name, read_only
15
+ super name, read_only, Ref
16
16
  end
17
17
 
18
18
  def from_hash(parent, value)
@@ -132,7 +132,6 @@ module Arrest
132
132
  url = build_url(@base_url, params)
133
133
 
134
134
  response = resolved_class.by_url(@context, url)
135
-
136
135
  @total_count = response[:result_count]
137
136
 
138
137
  @collection = response[:collection]
@@ -130,7 +130,7 @@ module Arrest
130
130
  protected
131
131
  def internal_reload
132
132
  context.cache.remove(self.id)
133
- self.class.find(self.context, self.id).to_hash(false, false, :retrieve)
133
+ self.class.find(self.context, self.id).to_hash
134
134
  end
135
135
  end
136
136
  end
@@ -84,7 +84,8 @@ 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(:update)
87
+ hash = rest_resource.to_jhash
88
+ insert_nulls!(rest_resource,hash)
88
89
  hash.delete(:id)
89
90
  hash.delete("id")
90
91
  body = JSON.generate(hash)
@@ -95,9 +96,7 @@ module Arrest
95
96
  def internal_put(rest_resource, location, body)
96
97
  profiler_status_str = ""
97
98
  ::ActiveSupport::Notifications.instrument("http.sgdb",
98
- :method => :delete,
99
- :url => rest_resource.resource_location,
100
- :status => profiler_status_str) do
99
+ :method => :delete, :url => rest_resource.resource_location, :status => profiler_status_str) do
101
100
  headers = nil
102
101
  response = self.connection().put do |req|
103
102
  req.url(location)
@@ -115,18 +114,27 @@ module Arrest
115
114
  end
116
115
  end
117
116
 
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
+
118
126
  def post(context, rest_resource)
119
127
  profiler_status_str = ""
120
128
  ::ActiveSupport::Notifications.instrument("http.sgdb",
121
- :method => :post,
122
- :url => rest_resource.resource_path,
123
- :status => profiler_status_str) do
129
+ :method => :post, :url => rest_resource.resource_path, :status => profiler_status_str) do
124
130
  raise "new object must have setter for id" unless rest_resource.respond_to?(:id=)
125
131
  raise "new object must not have id" if rest_resource.respond_to?(:id) && rest_resource.id != nil
126
- hash = rest_resource.to_jhash(:create)
132
+ hash = rest_resource.to_jhash
127
133
  hash.delete(:id)
128
134
  hash.delete('id')
129
135
 
136
+ insert_nulls!(rest_resource,hash)
137
+
130
138
  body = JSON.generate(hash)
131
139
  headers = nil
132
140
  response = self.connection().post do |req|
@@ -1,3 +1,3 @@
1
1
  module Arrest
2
- VERSION = "0.0.83.crud"
2
+ VERSION = "0.0.83"
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.83.crud
5
- prerelease: 7
4
+ version: 0.0.83
5
+ prerelease:
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-09-03 00:00:00.000000000Z
12
+ date: 2012-08-10 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &26670600 !ruby/object:Gem::Requirement
16
+ requirement: &23245620 !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: *26670600
24
+ version_requirements: *23245620
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: faraday
27
- requirement: &26670100 !ruby/object:Gem::Requirement
27
+ requirement: &23244860 !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: *26670100
35
+ version_requirements: *23244860
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: activemodel
38
- requirement: &26669600 !ruby/object:Gem::Requirement
38
+ requirement: &23244120 !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: *26669600
46
+ version_requirements: *23244120
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: bundler
49
- requirement: &26669140 !ruby/object:Gem::Requirement
49
+ requirement: &23243420 !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: *26669140
57
+ version_requirements: *23243420
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rake
60
- requirement: &26668720 !ruby/object:Gem::Requirement
60
+ requirement: &23242840 !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: *26668720
68
+ version_requirements: *23242840
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rdoc
71
- requirement: &26668240 !ruby/object:Gem::Requirement
71
+ requirement: &23239740 !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: *26668240
79
+ version_requirements: *23239740
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rspec
82
- requirement: &26667540 !ruby/object:Gem::Requirement
82
+ requirement: &23239240 !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: *26667540
90
+ version_requirements: *23239240
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: rr
93
- requirement: &26667000 !ruby/object:Gem::Requirement
93
+ requirement: &23238820 !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: *26667000
101
+ version_requirements: *23238820
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: simplecov
104
- requirement: &26666380 !ruby/object:Gem::Requirement
104
+ requirement: &23238360 !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: *26666380
112
+ version_requirements: *23238360
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: rack
115
- requirement: &26665680 !ruby/object:Gem::Requirement
115
+ requirement: &23237940 !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: *26665680
123
+ version_requirements: *23237940
124
124
  description: Consume a rest API in a AR like fashion
125
125
  email:
126
126
  - axel.tetzlaff@fortytools.com
@@ -188,9 +188,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
188
188
  required_rubygems_version: !ruby/object:Gem::Requirement
189
189
  none: false
190
190
  requirements:
191
- - - ! '>'
191
+ - - ! '>='
192
192
  - !ruby/object:Gem::Version
193
- version: 1.3.1
193
+ version: '0'
194
194
  requirements: []
195
195
  rubyforge_project: arrest
196
196
  rubygems_version: 1.8.10