proxima 0.2.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: '081e653fe66cc7ffbd3535a48fee5d3c4146ab81'
4
- data.tar.gz: b171427373e9544991aba53e5b934376a4f58850
3
+ metadata.gz: 5e4e449fa262703df9c749f876ff6b529d5c3bf1
4
+ data.tar.gz: '09ea541775f35a009a17b279c7a783cda0488745'
5
5
  SHA512:
6
- metadata.gz: 14aef58da59e38ef4b4f285cee5e68d4ceff0e0c472df0a523c8a9b8917f9dd21a0ada2391d204467ce21889d1d6fae1d889189f3c22aa754e9a242c3e12eb09
7
- data.tar.gz: 4414e6d9d63b35bf2da37564e05c33827541764395d99ad4dc61c036725f3ecd4ce8e9759a45729919da9af961457c5c50f1a024f9754f1788ee1f4012f33b3a
6
+ metadata.gz: 242abad6382332c49949e81d3748be602e011acd86c0073762d6c65c445c26ca9b1dffe54fe22bec8a2026f48c172c72ae167f1058f3e8da518ff4042723461b
7
+ data.tar.gz: b31124f05812c8baf9340da784f3fa348dad5d525e741a708f92a6790c1d8ddae7a5840c9234420cc1b4f2cbe8960f90b8f51a9babb1a36cc762f779ebc72d84
data/lib/proxima/model.rb CHANGED
@@ -60,9 +60,16 @@ module Proxima
60
60
  model
61
61
  end
62
62
 
63
- def self.find(query = {}, opts = {})
63
+ def self.find(query = {}, params = {}, opts = nil)
64
+
65
+ # NOTE: This is a compatibility fix for 0.2 to 0.3
66
+ if opts == nil
67
+ opts = params
68
+ params = query
69
+ end
70
+
64
71
  opts[:query] = self.convert_query_or_delta_to_json query
65
- @response = self.api.get self.find_path.call(query), opts
72
+ @response = self.api.get self.find_path.call(params), opts
66
73
 
67
74
  if @response.code != 200
68
75
  return []
@@ -72,29 +79,36 @@ module Proxima
72
79
  models.each { |model| model.new_record = false }
73
80
  end
74
81
 
75
- def self.find_one(query = {}, opts = {})
82
+ def self.find_one(query, params, opts = nil)
76
83
  query['$limit'] = 1
77
- self.find(query, opts)[0]
84
+ self.find(query, params, opts)[0]
78
85
  end
79
86
 
80
- def self.count(query = {}, opts = {})
87
+ def self.count(query = {}, params = {}, opts = nil)
88
+
89
+ # NOTE: This is a compatibility fix for 0.2 to 0.3
90
+ if opts == nil
91
+ opts = params
92
+ params = query
93
+ end
94
+
81
95
  query['$limit'] = 0
82
96
  opts[:query] = self.convert_query_or_delta_to_json query
83
- @response = self.api.get self.find_path.call(query), opts
97
+ @response = self.api.get self.find_path.call(params), opts
84
98
 
85
99
  return nil unless @response.code == 200
86
100
 
87
101
  @response.headers[:x_total_count] || 0
88
102
  end
89
103
 
90
- def self.find_by_id(id, query = {}, opts = {})
91
- if opts == nil && query
92
- opts = query
93
- query = {}
104
+ def self.find_by_id(id, params = {}, opts = nil)
105
+ if opts == nil
106
+ opts = params
107
+ params = {}
94
108
  end
95
109
 
96
- query[:id] = id
97
- @response = self.api.get self.find_by_id_path.call(query), opts
110
+ params[:id] = id
111
+ @response = self.api.get self.find_by_id_path.call(params), opts
98
112
 
99
113
  return nil unless @response.code == 200
100
114
 
@@ -132,7 +146,7 @@ module Proxima
132
146
  @response
133
147
  end
134
148
 
135
- def save(options = {})
149
+ def save(options = {}, params = {})
136
150
  return false unless self.valid?
137
151
 
138
152
  if self.new_record?
@@ -150,7 +164,7 @@ module Proxima
150
164
  return true if self.persisted?
151
165
 
152
166
  options[:flatten] = true if options[:flatten] == nil
153
- path = self.class.update_by_id_path.call self.to_h
167
+ path = self.class.update_by_id_path.call params.merge(self.to_h)
154
168
  payload = { json: self.as_json(options) }
155
169
  @response = self.class.api.put path, payload
156
170
 
@@ -20,8 +20,13 @@ module Proxima
20
20
 
21
21
  next unless value
22
22
 
23
- if params[:klass] && params[:klass].respond_to?(:from_json)
24
- value = params[:klass].from_json(value)
23
+ if params[:klass]
24
+ begin
25
+ value = Proxima.type_from_json params[:klass], value
26
+ rescue Exception => e
27
+ raise "Cannot convert value \"#{value}\" for attribute \"#{attribute}\" to type" +
28
+ " #{params[:klass].name}: #{e.message}"
29
+ end
25
30
  end
26
31
 
27
32
  hash[attribute] = value
@@ -42,7 +47,15 @@ module Proxima
42
47
 
43
48
  json_path = params[:json_path]
44
49
  value = hash[attribute.to_s]
45
- value = value.as_json if value.respond_to? :as_json
50
+
51
+ if params[:klass]
52
+ begin
53
+ value = Proxima.type_to_json params[:klass], value
54
+ rescue Exception => e
55
+ raise "Cannot convert value \"#{value}\" for attribute \"#{attribute}\" from type" +
56
+ " #{params[:klass].name}: #{e.message}"
57
+ end
58
+ end
46
59
 
47
60
  if options.key? :flatten
48
61
  json[json_path] = value
@@ -1,5 +1,5 @@
1
1
 
2
2
 
3
3
  module Proxima
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/proxima.rb CHANGED
@@ -4,4 +4,70 @@ require "proxima/model"
4
4
 
5
5
 
6
6
  module Proxima
7
+
8
+ @types = [
9
+ {
10
+ klass: String,
11
+ from_json: -> v { v.to_s },
12
+ to_json: -> v { v.to_s }
13
+ }, {
14
+ klass: Integer,
15
+ from_json: -> v { v.to_i },
16
+ to_json: -> v { v.to_i }
17
+ }, {
18
+ klass: Float,
19
+ from_json: -> v { v.to_f },
20
+ to_json: -> v { v.to_f }
21
+ }, {
22
+ klass: Rational,
23
+ from_json: -> v { v.to_r },
24
+ to_json: -> v { v.to_r }
25
+ }, {
26
+ klass: Complex,
27
+ from_json: -> v { v.to_c },
28
+ to_json: -> v { v.to_c }
29
+ }, {
30
+ klass: TrueClass,
31
+ from_json: -> v { v.to_s == 'true' },
32
+ to_json: -> v { v.to_s == 'true' }
33
+ }, {
34
+ klass: Array,
35
+ from_json: -> v { v.to_a },
36
+ to_json: -> v { v.to_a }
37
+ }, {
38
+ klass: Hash,
39
+ from_json: -> v { v.to_h },
40
+ to_json: -> v { v.to_h }
41
+ }, {
42
+ klass: DateTime,
43
+ from_json: -> v { DateTime.iso8601 v },
44
+ to_json: -> v { v.iso8601 3 }
45
+ }
46
+ ]
47
+
48
+ def self.add_type(klass, from = nil, to = nil)
49
+ @types.push({
50
+ klass: klass,
51
+ from: from,
52
+ to: to
53
+ })
54
+ end
55
+
56
+ def self.remove_type(klass)
57
+ @types.delete_if({
58
+ klass: klass,
59
+ from: from,
60
+ to: to
61
+ })
62
+ end
63
+
64
+ def self.type_from_json(klass, value)
65
+ type = @types.find { |t| t[:klass] == klass }
66
+ type.from_json(value) if type else value
67
+ end
68
+
69
+ def self.type_to_json(klass, value)
70
+ type = @types.find { |t| t[:klass] == klass }
71
+ type.to_json(value) if type else value
72
+ end
7
73
  end
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: 0.2.0
4
+ version: 0.3.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-09 00:00:00.000000000 Z
11
+ date: 2017-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler