ncore 3.7.1 → 3.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -0
- data/LICENSE +1 -1
- data/lib/ncore/associations.rb +88 -2
- data/lib/ncore/attributes.rb +29 -3
- data/lib/ncore/methods/find_single.rb +0 -2
- data/lib/ncore/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0b92860b930948dd670a333583cd9e20ce4608929eb0c5f968234fe0cace69d3
|
4
|
+
data.tar.gz: 00e9f32713a037614ff36b322600b95338e23957ed165388c69269a4f9c129df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fdaddd93c446f6c57d76d490f742d64683e1218f1c9628b2fb6de984d1fc586911746f9f7ffb126e057ffc5b3d57d896bbea3545d1e99276499743b1b884d95b
|
7
|
+
data.tar.gz: d5af6a87561366c3fc1e92823dd64e281016b8c3b0128edd17998b7ae48879eb590f0b9ca56a6b342baa98784cac7b3aeea8a4418631ae12b0b0f0607ae24eee
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,15 @@
|
|
1
|
+
#### 3.8.1
|
2
|
+
|
3
|
+
- Add option to disable some_attr?() definition
|
4
|
+
|
5
|
+
#### 3.8.0
|
6
|
+
|
7
|
+
- Add has_one association helper
|
8
|
+
- Make belongs_to return existing data even when association_key is blank
|
9
|
+
- Make reload() public for singleton resources
|
10
|
+
- Optimize calling some_attr?() by defining such methods directly.
|
11
|
+
Previously these calls went through method_missing.
|
12
|
+
|
1
13
|
#### 3.7.1
|
2
14
|
|
3
15
|
- Don't send both mixed and lowercase headers for accept, user-agent
|
data/LICENSE
CHANGED
data/lib/ncore/associations.rb
CHANGED
@@ -1,6 +1,89 @@
|
|
1
1
|
module NCore
|
2
2
|
module Associations
|
3
3
|
|
4
|
+
# assoc_name - singular association name
|
5
|
+
# :association_key - key used by the association to reference the parent
|
6
|
+
# defaults to `attrib_name+'_id'`
|
7
|
+
# :class_name - Module::Class of the child association, as a string
|
8
|
+
def has_one(assoc_name, association_key: nil, class_name: nil)
|
9
|
+
assoc_name = assoc_name.to_s
|
10
|
+
parent_key = association_key&.to_s || "#{attrib_name}_id"
|
11
|
+
klass = class_name || "#{module_name}::#{assoc_name.camelize}"
|
12
|
+
|
13
|
+
# def item({})
|
14
|
+
class_eval <<-A1, __FILE__, __LINE__+1
|
15
|
+
def #{assoc_name}(params={})
|
16
|
+
return nil unless id
|
17
|
+
reload = params.delete :reload
|
18
|
+
cacheable = params.except(:credentials, :request).empty?
|
19
|
+
params = parse_request_params(params).reverse_merge credentials: api_creds
|
20
|
+
params[:#{parent_key}] = id
|
21
|
+
if cacheable
|
22
|
+
# only cache unfiltered, default api call
|
23
|
+
@attribs[:#{assoc_name}] = (!reload && @attribs[:#{assoc_name}]) || #{klass}.first(params)
|
24
|
+
else
|
25
|
+
#{klass}.first(params)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
A1
|
29
|
+
|
30
|
+
# def create_item({})
|
31
|
+
# will always return the object; check .errors? or .valid? to see how it went
|
32
|
+
class_eval <<-C1, __FILE__, __LINE__+1
|
33
|
+
def create_#{assoc_name}(params={})
|
34
|
+
raise UnsavedObjectError unless id
|
35
|
+
params = parse_request_params(params).reverse_merge credentials: api_creds
|
36
|
+
params[:#{parent_key}] = id
|
37
|
+
#{klass}.create(params)
|
38
|
+
end
|
39
|
+
C1
|
40
|
+
|
41
|
+
# def create_item!({})
|
42
|
+
class_eval <<-C2, __FILE__, __LINE__+1
|
43
|
+
def create_#{assoc_name}!(params={})
|
44
|
+
raise UnsavedObjectError unless id
|
45
|
+
params = parse_request_params(params).reverse_merge credentials: api_creds
|
46
|
+
params[:#{parent_key}] = id
|
47
|
+
#{klass}.create!(params)
|
48
|
+
end
|
49
|
+
C2
|
50
|
+
|
51
|
+
# def update_item({})
|
52
|
+
# will always return the object; check .errors? or .valid? to see how it went
|
53
|
+
class_eval <<-U1, __FILE__, __LINE__+1
|
54
|
+
def update_#{assoc_name}(params={})
|
55
|
+
raise UnsavedObjectError unless id
|
56
|
+
#{assoc_name}.update(params)
|
57
|
+
end
|
58
|
+
U1
|
59
|
+
|
60
|
+
# def update_item!({})
|
61
|
+
class_eval <<-U2, __FILE__, __LINE__+1
|
62
|
+
def update_#{assoc_name}!(params={})
|
63
|
+
raise UnsavedObjectError unless id
|
64
|
+
#{assoc_name}.update!(params)
|
65
|
+
end
|
66
|
+
U2
|
67
|
+
|
68
|
+
# def delete_item({})
|
69
|
+
# will always return the object; check .errors? or .valid? to see how it went
|
70
|
+
class_eval <<-D1, __FILE__, __LINE__+1
|
71
|
+
def delete_#{assoc_name}(params={})
|
72
|
+
raise UnsavedObjectError unless id
|
73
|
+
#{assoc_name}.delete(params)
|
74
|
+
end
|
75
|
+
D1
|
76
|
+
|
77
|
+
# def delete_item!({})
|
78
|
+
class_eval <<-D2, __FILE__, __LINE__+1
|
79
|
+
def delete_#{assoc_name}!(params={})
|
80
|
+
raise UnsavedObjectError unless id
|
81
|
+
#{assoc_name}.delete!(params)
|
82
|
+
end
|
83
|
+
D2
|
84
|
+
end
|
85
|
+
|
86
|
+
|
4
87
|
# assoc_name - plural association name
|
5
88
|
# :association_key - key used by the association to reference the parent
|
6
89
|
# defaults to `attrib_name+'_id'`
|
@@ -111,6 +194,7 @@ module NCore
|
|
111
194
|
D2
|
112
195
|
end
|
113
196
|
|
197
|
+
|
114
198
|
# assoc_name - singular association name
|
115
199
|
# :association_key - key on this resource used to reference the parent association
|
116
200
|
# defaults to `assoc_name+'_id'`
|
@@ -125,12 +209,14 @@ module NCore
|
|
125
209
|
class_eval <<-P1, __FILE__, __LINE__+1
|
126
210
|
attr :#{parent_key}
|
127
211
|
def #{assoc_name}(params={})
|
128
|
-
|
212
|
+
reload = params.delete :reload
|
129
213
|
params = parse_request_params(params).reverse_merge credentials: api_creds
|
130
214
|
if params.except(:credentials, :request).empty?
|
131
215
|
# only cache unfiltered, default api call
|
132
|
-
@attribs[:#{assoc_name}]
|
216
|
+
@attribs[:#{assoc_name}] = nil if reload
|
217
|
+
@attribs[:#{assoc_name}] ||= #{parent_key} && #{klass}.find(#{parent_key}, params)
|
133
218
|
else
|
219
|
+
return nil unless #{parent_key}
|
134
220
|
#{klass}.find(#{parent_key}, params)
|
135
221
|
end
|
136
222
|
end
|
data/lib/ncore/attributes.rb
CHANGED
@@ -8,7 +8,10 @@ module NCore
|
|
8
8
|
|
9
9
|
|
10
10
|
module ClassMethods
|
11
|
-
|
11
|
+
# attr(:name, ...)
|
12
|
+
# adds: obj.name => raw json type
|
13
|
+
# obj.name? => bool
|
14
|
+
def attr(*attrs, predicate: true)
|
12
15
|
attrs.each do |attr|
|
13
16
|
check_existing_method(attr)
|
14
17
|
class_eval <<-AR, __FILE__, __LINE__+1
|
@@ -16,10 +19,14 @@ module NCore
|
|
16
19
|
self[:#{attr}]
|
17
20
|
end
|
18
21
|
AR
|
22
|
+
attr_boolean :"#{attr}?" if predicate
|
19
23
|
end
|
20
24
|
end
|
21
25
|
|
22
|
-
|
26
|
+
# attr_datetime(:updated_at, ...)
|
27
|
+
# adds: obj.updated_at => Time, or raw json type if not parseable
|
28
|
+
# obj.updated_at? => bool
|
29
|
+
def attr_datetime(*attrs, predicate: true)
|
23
30
|
attrs.each do |attr|
|
24
31
|
check_existing_method(attr)
|
25
32
|
class_eval <<-AD, __FILE__, __LINE__+1
|
@@ -36,10 +43,14 @@ module NCore
|
|
36
43
|
self[:#{attr}]
|
37
44
|
end
|
38
45
|
AD
|
46
|
+
attr_boolean :"#{attr}?" if predicate
|
39
47
|
end
|
40
48
|
end
|
41
49
|
|
42
|
-
|
50
|
+
# attr_decimal(:amount, ...)
|
51
|
+
# adds: obj.amount => BigMoney if String, else raw json type
|
52
|
+
# obj.amount? => bool
|
53
|
+
def attr_decimal(*attrs, predicate: true)
|
43
54
|
attrs.each do |attr|
|
44
55
|
check_existing_method(attr)
|
45
56
|
class_eval <<-AD, __FILE__, __LINE__+1
|
@@ -52,6 +63,21 @@ module NCore
|
|
52
63
|
end
|
53
64
|
end
|
54
65
|
AD
|
66
|
+
attr_boolean :"#{attr}?" if predicate
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# attr_boolean(:active, :active?, ...)
|
71
|
+
# adds: obj.active
|
72
|
+
# adds: obj.active? - in attrs hash, this looks for the key :active, not :active?
|
73
|
+
def attr_boolean(*attrs)
|
74
|
+
attrs.each do |attr|
|
75
|
+
check_existing_method(attr)
|
76
|
+
class_eval <<-AB, __FILE__, __LINE__+1
|
77
|
+
def #{attr}
|
78
|
+
!! self[:#{attr.to_s.sub(/\?$/,'')}]
|
79
|
+
end
|
80
|
+
AB
|
55
81
|
end
|
56
82
|
end
|
57
83
|
|
data/lib/ncore/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ncore
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Notioneer Team
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-02-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -142,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
142
142
|
- !ruby/object:Gem::Version
|
143
143
|
version: '0'
|
144
144
|
requirements: []
|
145
|
-
rubygems_version: 3.
|
145
|
+
rubygems_version: 3.5.3
|
146
146
|
signing_key:
|
147
147
|
specification_version: 4
|
148
148
|
summary: NCore - Gem for building REST API clients
|