proxima 1.0.1 → 1.1.0
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.
- checksums.yaml +4 -4
- data/lib/proxima/attributes.rb +12 -0
- data/lib/proxima/model.rb +6 -8
- data/lib/proxima/rest.rb +28 -0
- data/lib/proxima/serialization.rb +20 -12
- data/lib/proxima/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2b6e9e03067a1f333f43b25db6b84209452f41f9
|
4
|
+
data.tar.gz: be46e25021188d6f145b24ad4f1de16a89164416
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 19fba539d2135ade5bb1fe07055dc120ce1a4fdda542cbef96c89bb976be6284b230f542acc1657b7c091fcef78b60f277886ad82884baca30fe0442b4c28b80
|
7
|
+
data.tar.gz: 3d3555bb65407aec5f2b6c5135583a692db2cda14bfed4caac0a0dfeeb2bd94aa0ff05255af8e30a790bbc6dfbc18940e75bc763403d446c68a18f80af66b2e7
|
data/lib/proxima/attributes.rb
CHANGED
data/lib/proxima/model.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'active_model'
|
2
2
|
require 'proxima/watch'
|
3
|
+
require 'proxima/rest'
|
3
4
|
require 'proxima/attributes'
|
4
5
|
require 'proxima/paths'
|
5
6
|
require 'proxima/serialization'
|
@@ -18,6 +19,7 @@ module Proxima
|
|
18
19
|
include ActiveModel::Serializers::JSON
|
19
20
|
include ActiveModel::Validations
|
20
21
|
|
22
|
+
include Proxima::Rest
|
21
23
|
include Proxima::Attributes
|
22
24
|
include Proxima::Paths
|
23
25
|
include Proxima::Serialization
|
@@ -66,8 +68,7 @@ module Proxima
|
|
66
68
|
|
67
69
|
return [] unless @response.code == 200
|
68
70
|
|
69
|
-
|
70
|
-
models.each { |model| model.new_record = false }
|
71
|
+
self.from_json @response.body
|
71
72
|
end
|
72
73
|
|
73
74
|
def self.count_and_find(query = {}, params = {}, opts = {})
|
@@ -98,10 +99,7 @@ module Proxima
|
|
98
99
|
|
99
100
|
return nil unless @response.code == 200
|
100
101
|
|
101
|
-
|
102
|
-
model.from_json @response.body
|
103
|
-
model.new_record = false
|
104
|
-
model
|
102
|
+
self.from_json @response.body, single_model_from_array: true
|
105
103
|
end
|
106
104
|
|
107
105
|
def initialize(record = {})
|
@@ -167,7 +165,7 @@ module Proxima
|
|
167
165
|
end
|
168
166
|
|
169
167
|
def destroy
|
170
|
-
|
168
|
+
raise "Cannot destroy a new record" if new_record?
|
171
169
|
|
172
170
|
@response = self.class.api.delete(self.class.delete_by_id_path.call(self.to_h))
|
173
171
|
|
@@ -176,7 +174,7 @@ module Proxima
|
|
176
174
|
end
|
177
175
|
|
178
176
|
def restore
|
179
|
-
|
177
|
+
raise "Cannot restore a new record" if new_record?
|
180
178
|
|
181
179
|
@response = self.class.api.post(self.class.restore_by_id_path.call(self.to_h))
|
182
180
|
|
data/lib/proxima/rest.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
module Proxima
|
4
|
+
|
5
|
+
module Rest
|
6
|
+
|
7
|
+
[:post, :get, :put, :delete].each do |http_method|
|
8
|
+
define_method :"#{http_method}" do |path, opts = {}, &block|
|
9
|
+
@response = self.class.api.public_send(:"#{http_method}", path, opts, &block)
|
10
|
+
@response
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module ClassMethods
|
15
|
+
|
16
|
+
[:post, :get, :put, :delete].each do |http_method|
|
17
|
+
define_method :"#{http_method}" do |path, opts = {}, &block|
|
18
|
+
@response = self.api.public_send(:"#{http_method}", path, opts, &block)
|
19
|
+
@response
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.included base
|
25
|
+
base.extend ClassMethods
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -3,9 +3,11 @@
|
|
3
3
|
module Proxima
|
4
4
|
module Serialization
|
5
5
|
|
6
|
-
def from_json(json,
|
6
|
+
def from_json(json, opts = {})
|
7
7
|
json = ActiveSupport::JSON.decode(json) if json.is_a?(String)
|
8
|
-
|
8
|
+
binding.pry
|
9
|
+
json = json.values.first if opts[:include_root] || self.include_root_in_json
|
10
|
+
json = json.first if opts[:single_model_from_array] && json.is_a?(Array)
|
9
11
|
hash = {}
|
10
12
|
|
11
13
|
self.class.attributes.each do |attribute, params|
|
@@ -33,16 +35,18 @@ module Proxima
|
|
33
35
|
end
|
34
36
|
|
35
37
|
self.attributes = hash
|
38
|
+
self.new_record = opts[:new_record] if opts[:new_record] != nil
|
36
39
|
self
|
37
40
|
end
|
38
41
|
|
39
|
-
def as_json(
|
40
|
-
hash = serializable_hash
|
42
|
+
def as_json(opts = {})
|
43
|
+
hash = self.serializable_hash opts
|
44
|
+
|
41
45
|
json = {}
|
42
46
|
self.class.attributes.each do |attribute, params|
|
43
47
|
next if (
|
44
|
-
!
|
45
|
-
!
|
48
|
+
!opts.key?(:include_clean) && !send("#{attribute}_changed?") ||
|
49
|
+
!opts.key?(:include_nil) && hash[attribute.to_s] == nil
|
46
50
|
)
|
47
51
|
|
48
52
|
json_path = params[:json_path]
|
@@ -57,7 +61,7 @@ module Proxima
|
|
57
61
|
end
|
58
62
|
end
|
59
63
|
|
60
|
-
if
|
64
|
+
if opts.key? :flatten
|
61
65
|
json[json_path] = value
|
62
66
|
next
|
63
67
|
end
|
@@ -72,8 +76,8 @@ module Proxima
|
|
72
76
|
json_ctx[last_json_path_chunk] = value
|
73
77
|
end
|
74
78
|
|
75
|
-
root = if
|
76
|
-
|
79
|
+
root = if opts.key? :root
|
80
|
+
opts[:root]
|
77
81
|
else
|
78
82
|
self.include_root_in_json
|
79
83
|
end
|
@@ -96,15 +100,19 @@ module Proxima
|
|
96
100
|
|
97
101
|
module ClassMethods
|
98
102
|
|
99
|
-
def from_json(json,
|
103
|
+
def from_json(json, opts = {})
|
100
104
|
json = ActiveSupport::JSON.decode(json) if json.is_a? String
|
101
|
-
json = json.values.first if include_root
|
105
|
+
json = json.values.first if opts[:include_root] || self.include_root_in_json
|
106
|
+
json = json.first if opts[:single_model_from_array] && json.is_a?(Array)
|
102
107
|
|
103
108
|
if json.is_a? Array
|
104
109
|
return json.map { |json| self.new.from_json json }
|
105
110
|
end
|
106
111
|
|
107
|
-
self.new.from_json json
|
112
|
+
model = self.new.from_json json
|
113
|
+
model.new_record = opts[:new_record] || false
|
114
|
+
|
115
|
+
model
|
108
116
|
end
|
109
117
|
|
110
118
|
def convert_query_or_delta_to_json(query)
|
data/lib/proxima/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: proxima
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert Hurst
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-05
|
11
|
+
date: 2017-06-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -105,6 +105,7 @@ files:
|
|
105
105
|
- lib/proxima/attributes.rb
|
106
106
|
- lib/proxima/model.rb
|
107
107
|
- lib/proxima/paths.rb
|
108
|
+
- lib/proxima/rest.rb
|
108
109
|
- lib/proxima/serialization.rb
|
109
110
|
- lib/proxima/validation.rb
|
110
111
|
- lib/proxima/version.rb
|