ncore 3.7.1 → 3.8.1

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
  SHA256:
3
- metadata.gz: 6c6562d9f0e6fca58cc5ad061d9461006dc509f92e960a55ed83bd127fd1d503
4
- data.tar.gz: f61859177171d285d7442dfdee1a66964b318bb14824b7e409da2e3e27b92e0d
3
+ metadata.gz: 0b92860b930948dd670a333583cd9e20ce4608929eb0c5f968234fe0cace69d3
4
+ data.tar.gz: 00e9f32713a037614ff36b322600b95338e23957ed165388c69269a4f9c129df
5
5
  SHA512:
6
- metadata.gz: 2b702ea0bc616e2aabbd685ac53579759e59176865e562f383aff61682f3cb6defc5aa177ec398a0a9b18229bd8f5a78d5fc5e390f61ce722998e5301bd7dfa1
7
- data.tar.gz: e2ab8234b5fc0358ba38f2460065dfe029ee92d59d575dbb7f35c9d5b8edb71aff8d15fb0278a50678401a71bea55705eb12bda756e4c99295430d45ae16cbee
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
@@ -1,4 +1,4 @@
1
- Copyright (c) 2014-2023 Notioneer, Inc.
1
+ Copyright (c) 2014-2024 Notioneer, Inc.
2
2
 
3
3
  MIT License
4
4
 
@@ -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
- return nil unless #{parent_key}
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}] ||= #{klass}.find(#{parent_key}, params)
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
@@ -8,7 +8,10 @@ module NCore
8
8
 
9
9
 
10
10
  module ClassMethods
11
- def attr(*attrs)
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
- def attr_datetime(*attrs)
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
- def attr_decimal(*attrs)
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
 
@@ -19,8 +19,6 @@ module NCore
19
19
  'singleton'
20
20
  end
21
21
 
22
- private
23
-
24
22
  def reload(find_params={})
25
23
  params = parse_request_params(find_params).reverse_merge credentials: api_creds
26
24
  parsed, @api_creds = request(:get, resource_path, params)
data/lib/ncore/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module NCore
2
- VERSION = '3.7.1'
2
+ VERSION = '3.8.1'
3
3
  end
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.7.1
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: 2023-12-27 00:00:00.000000000 Z
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.4.10
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