nobrainer 0.35.0 → 0.36.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
  SHA256:
3
- metadata.gz: c6d7ce930d1fba4a7c3e3f6368c7a9650c0a298ae1bd46517097284591ae1a22
4
- data.tar.gz: 6d9cc4350f77a229f779acc0d88f4893f146b0fd0028f9a214c29da9e4f5e70a
3
+ metadata.gz: 61c15de4e6f0d6ede87a2db309ff25a79d36d61befdfcd280815925d0f66bb68
4
+ data.tar.gz: f1516356fe4f53577a744dfacca6a8143d98844aee540e8c0290950113bedd88
5
5
  SHA512:
6
- metadata.gz: fcb9ef68e4ce874a89761465e0b9861182feb526665cfd08a19be7019a8a0e5f716e6fdd3d940648a42fe1fe1f2e1bba277291778a3752e06c21e67bb94163d3
7
- data.tar.gz: fa9b10e21bfd4a987821f365e840c95b6314f5bbaf32efeebe0810f9d8157fe6b0f4f9607ab1931d278f2d22beb24442b0ed2f1115e360a7f82aa8ad3c5a111b
6
+ metadata.gz: 3fabd4ff40209300d3630cee18bd27115d77f6c157b6d9f910ac93896ef11487f1cf1cb116fb7a98ef356e3c78033412c05ddf06d60ed473b753a26a9aab4ab2
7
+ data.tar.gz: 9d3299f45f30001d6856ea015b15c19a9f5b7ec5642337c3416da160468deb5f00b5714f54eb3837b0af716c1a61fc817963280830bffa6b09bf15775624590f
data/CHANGELOG.md CHANGED
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
  ## [Unreleased]
8
8
 
9
9
 
10
+ ## [0.36.0] - 2021-08-08
11
+ ### Added
12
+ - Array and TypedArray types for validation and serialization
13
+
10
14
  ## [0.35.0] - 2021-08-08
11
15
  ### Added
12
16
  - Dockerfile, docker-compose and Earthfile
@@ -100,7 +104,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
100
104
  - Locks: bug fix: allow small timeouts in lock()
101
105
  - Fix reentrant lock counter on steals
102
106
 
103
- [Unreleased]: https://github.com/nobrainerorm/nobrainer/compare/v0.35.0...HEAD
107
+ [Unreleased]: https://github.com/nobrainerorm/nobrainer/compare/v0.36.0...HEAD
108
+ [0.36.0]: https://github.com/nobrainerorm/nobrainer/compare/v0.35.0...v0.36.0
104
109
  [0.35.0]: https://github.com/nobrainerorm/nobrainer/compare/v0.34.1...v0.35.0
105
110
  [0.34.1]: https://github.com/nobrainerorm/nobrainer/compare/v0.34.0...v0.34.1
106
111
  [0.34.0]: https://github.com/nobrainerorm/nobrainer/compare/v0.33.0...v0.34.0
@@ -33,8 +33,12 @@ module NoBrainer::Document::Association::EagerLoader
33
33
  def eager_load_association(docs, association_name, criteria=nil)
34
34
  docs = docs.compact
35
35
  return [] if docs.empty?
36
- meta = docs.first.root_class.association_metadata
37
- association = meta[association_name.to_sym] || meta[association_name.to_s.singularize.to_sym]
36
+
37
+ meta = docs.first.class.association_metadata
38
+ root_meta = docs.first.root_class.association_metadata
39
+ association = meta[association_name.to_sym] || root_meta[association_name.to_sym] ||
40
+ meta[association_name.to_s.singularize.to_sym] || root_meta[association_name.to_s.singularize.to_sym]
41
+
38
42
  raise "Unknown association #{association_name}" unless association
39
43
  association.eager_load(docs, criteria)
40
44
  end
@@ -0,0 +1,90 @@
1
+ require 'active_support/core_ext/array/wrap'
2
+
3
+ module NoBrainer
4
+ class Array < ::Array
5
+ # delegate cast to each array element
6
+ def self.nobrainer_cast_user_to_model(values)
7
+ ::Array.wrap(values).map do |value|
8
+ if value.class.respond_to?(:nobrainer_cast_user_to_model)
9
+ value.class.nobrainer_cast_user_to_model(value)
10
+ else
11
+ value
12
+ end
13
+ end
14
+ end
15
+
16
+ # delegate cast to each array element
17
+ def self.nobrainer_cast_model_to_db(values)
18
+ ::Array.wrap(values).map do |value|
19
+ if value.class.respond_to?(:nobrainer_cast_model_to_db)
20
+ value.class.nobrainer_cast_model_to_db(value)
21
+ else
22
+ value
23
+ end
24
+ end
25
+ end
26
+
27
+ # delegate cast to each array element
28
+ def self.nobrainer_cast_db_to_model(values)
29
+ ::Array.wrap(values).map do |value|
30
+ if value.class.respond_to?(:nobrainer_cast_db_to_model)
31
+ value.class.nobrainer_cast_db_to_model(method, value)
32
+ else
33
+ value
34
+ end
35
+ end
36
+ end
37
+
38
+ # convenience method to create a TypedArray
39
+ def self.of(object_type = nil, **options)
40
+ NoBrainer::TypedArray.of(object_type, **options)
41
+ end
42
+ end
43
+
44
+ class TypedArray < Array
45
+ def self.of(object_type, allow_nil: false)
46
+ NoBrainer::Document::Types.load_type_extensions(object_type)
47
+ ::Class.new(TypedArray) do
48
+ define_singleton_method(:object_type) { object_type }
49
+ define_singleton_method(:allow_nil?) { allow_nil }
50
+ end
51
+ end
52
+
53
+ def self.name
54
+ str = String.new "Array"
55
+ str += "(#{object_type.name})" if respond_to?(:object_type)
56
+ str
57
+ end
58
+
59
+ # delegate cast methods to object_type cast methods, if defined
60
+ def self.nobrainer_cast_user_to_model(values)
61
+ cast_type = object_type.respond_to?(:nobrainer_cast_user_to_model) && object_type
62
+ values = ::Array.wrap(values).map do |value|
63
+ value = cast_type.nobrainer_cast_user_to_model(value) if cast_type
64
+ unless (value.nil? && allow_nil?) || value.is_a?(object_type)
65
+ raise NoBrainer::Error::InvalidType, type: object_type.name, value: value
66
+ end
67
+ value
68
+ end
69
+ new(values)
70
+ end
71
+
72
+ def self.nobrainer_cast_model_to_db(values)
73
+ values = ::Array.wrap(values)
74
+ if object_type.respond_to?(:nobrainer_cast_model_to_db)
75
+ values.map { |value| object_type.nobrainer_cast_model_to_db(value) }
76
+ else
77
+ values
78
+ end
79
+ end
80
+
81
+ def self.nobrainer_cast_db_to_model(values)
82
+ values = ::Array.wrap(values)
83
+ if object_type.respond_to?(:nobrainer_cast_db_to_model)
84
+ values.map { |value| object_type.nobrainer_cast_db_to_model(value) }
85
+ else
86
+ values
87
+ end
88
+ end
89
+ end
90
+ end
@@ -62,6 +62,11 @@ module NoBrainer::Document::Types
62
62
  end
63
63
 
64
64
  def field(attr, options={})
65
+ if (type = options[:type]).is_a?(::Array)
66
+ raise ArgumentError, "Expected Array type to have single element, got #{types.inspect}" unless type.length == 1
67
+ options[:type] = NoBrainer::TypedArray.of(type.first)
68
+ end
69
+
65
70
  super
66
71
 
67
72
  type = options[:type]
@@ -108,7 +113,7 @@ module NoBrainer::Document::Types
108
113
  end
109
114
  end
110
115
 
111
- %w(binary boolean text geo enum).each do |type|
116
+ %w(array binary boolean text geo enum).each do |type|
112
117
  require File.join(File.dirname(__FILE__), 'types', type)
113
118
  const_set(type.camelize, NoBrainer.const_get(type.camelize))
114
119
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nobrainer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.35.0
4
+ version: 0.36.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Viennot
@@ -154,6 +154,7 @@ files:
154
154
  - lib/no_brainer/document/table_config/synchronizer.rb
155
155
  - lib/no_brainer/document/timestamps.rb
156
156
  - lib/no_brainer/document/types.rb
157
+ - lib/no_brainer/document/types/array.rb
157
158
  - lib/no_brainer/document/types/binary.rb
158
159
  - lib/no_brainer/document/types/boolean.rb
159
160
  - lib/no_brainer/document/types/date.rb