activejsonmodel 0.1.4 → 0.1.5
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6358d312dd78f6dec607bfcc0417e3bb716c8029d19502701d2c96704107d0f7
|
4
|
+
data.tar.gz: 0da1d126eead2748545c9455ac94a4b79c94e4278526ef9f78332104c63f121a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3e9d4ec02edfcdfc3ac5eea8ed235094e37d18be43e4980d1fc789219856c73fe442116c53362f5e36e91ca1a79f3a5845f7da85e6a0822dc0271e7a2b6c7e45
|
7
|
+
data.tar.gz: 78b47a73bc4f50e8309655b01dbac8851377630941e1e85fba9c30e02e4f5a7c0c0a326c1bfd5e6fc3f0c0f04437a23e8b948a1295e235d609abfa91d0b2a40f
|
@@ -18,25 +18,29 @@ if Gem.find_files("symmetric-encryption").any? &&
|
|
18
18
|
# end
|
19
19
|
# end
|
20
20
|
#
|
21
|
-
# class
|
21
|
+
# class User < ActiveRecord::Base
|
22
22
|
# attribute :credentials, Credentials.encrypted_attribute_type
|
23
23
|
# end
|
24
|
+
#
|
25
|
+
# Alternatively, the type can be registered ahead of time:
|
26
|
+
#
|
27
|
+
# # config/initializers/types.rb
|
28
|
+
# ActiveRecord::Type.register(:credentials_encrypted_type, Credentials.encrypted_attribute_type)
|
29
|
+
#
|
30
|
+
# Then the custom type can be used as:
|
31
|
+
#
|
32
|
+
# class User < ActiveRecord::Base
|
33
|
+
# attribute :credentials, :credentials_encrypted_type
|
34
|
+
# end
|
35
|
+
#
|
24
36
|
class ActiveRecordEncryptedType < ::ActiveJsonModel::ActiveRecordType
|
25
37
|
def type
|
26
38
|
:string
|
27
39
|
end
|
28
40
|
|
29
|
-
def cast(value)
|
30
|
-
if value.is_a?(@clazz)
|
31
|
-
value
|
32
|
-
elsif value.is_a?(::Array)
|
33
|
-
@clazz.load(value)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
41
|
def deserialize(value)
|
38
42
|
if String === value
|
39
|
-
|
43
|
+
decoded = SymmetricEncryption.decrypt(value, type: :json) rescue nil
|
40
44
|
@clazz.load(decoded)
|
41
45
|
else
|
42
46
|
super
|
@@ -46,9 +50,17 @@ if Gem.find_files("symmetric-encryption").any? &&
|
|
46
50
|
def serialize(value)
|
47
51
|
case value
|
48
52
|
when @clazz
|
49
|
-
|
50
|
-
|
51
|
-
|
53
|
+
SymmetricEncryption.encrypt(
|
54
|
+
@clazz.dump(value),
|
55
|
+
random_iv: true,
|
56
|
+
type: :json
|
57
|
+
)
|
58
|
+
when ::Hash, ::HashWithIndifferentAccess, ::Array
|
59
|
+
SymmetricEncryption.encrypt(
|
60
|
+
value,
|
61
|
+
random_iv: true,
|
62
|
+
type: :json
|
63
|
+
)
|
52
64
|
else
|
53
65
|
super
|
54
66
|
end
|
@@ -15,10 +15,21 @@ if Gem.find_files("active_record").any?
|
|
15
15
|
# end
|
16
16
|
# end
|
17
17
|
#
|
18
|
-
# class
|
18
|
+
# class User < ActiveRecord::Base
|
19
19
|
# attribute :credentials, Credentials.attribute_type
|
20
20
|
# end
|
21
21
|
#
|
22
|
+
# Alternatively, the type can be registered ahead of time:
|
23
|
+
#
|
24
|
+
# # config/initializers/types.rb
|
25
|
+
# ActiveRecord::Type.register(:credentials_type, Credentials.attribute_type)
|
26
|
+
#
|
27
|
+
# Then the custom type can be used as:
|
28
|
+
#
|
29
|
+
# class User < ActiveRecord::Base
|
30
|
+
# attribute :credentials, :credentials_type
|
31
|
+
# end
|
32
|
+
#
|
22
33
|
# This is based on:
|
23
34
|
# https://jetrockets.pro/blog/rails-5-attributes-api-value-objects-and-jsonb
|
24
35
|
class ActiveRecordType < ::ActiveRecord::Type::Value
|
@@ -39,11 +50,7 @@ if Gem.find_files("active_record").any?
|
|
39
50
|
end
|
40
51
|
|
41
52
|
def cast(value)
|
42
|
-
|
43
|
-
value
|
44
|
-
elsif value.is_a?(::Array)
|
45
|
-
@clazz.load(value)
|
46
|
-
end
|
53
|
+
@clazz.active_json_model_cast(value)
|
47
54
|
end
|
48
55
|
|
49
56
|
def deserialize(value)
|
@@ -59,7 +66,7 @@ if Gem.find_files("active_record").any?
|
|
59
66
|
case value
|
60
67
|
when @clazz
|
61
68
|
::ActiveSupport::JSON.encode(@clazz.dump(value))
|
62
|
-
when
|
69
|
+
when ::Hash, ::HashWithIndifferentAccess, ::Array
|
63
70
|
::ActiveSupport::JSON.encode(value)
|
64
71
|
else
|
65
72
|
super
|
@@ -617,6 +617,26 @@ module ActiveJsonModel
|
|
617
617
|
end
|
618
618
|
end
|
619
619
|
|
620
|
+
# Convert a value that might already be an instance of this class from underlying data.
|
621
|
+
# Used to delegate potential loading from ActiveRecord attributes
|
622
|
+
#
|
623
|
+
# @param vals either an instance of this model or an array-like object
|
624
|
+
def active_json_model_cast(vals)
|
625
|
+
if vals.is_a?(self)
|
626
|
+
vals
|
627
|
+
elsif vals.is_a?(::Array)
|
628
|
+
if vals.length == 0
|
629
|
+
self.new(values: vals)
|
630
|
+
elsif vals[0].respond_to?(:dump_to_json)
|
631
|
+
self.new(values: vals)
|
632
|
+
else
|
633
|
+
self.load(vals)
|
634
|
+
end
|
635
|
+
elsif vals.nil?
|
636
|
+
self.new(values: [])
|
637
|
+
end
|
638
|
+
end
|
639
|
+
|
620
640
|
# Define a polymorphic factory to choose the concrete class for the list model. Note that because the array_data passed
|
621
641
|
# to the block is an array of models, you must account for what the behavior is if there are no elements.
|
622
642
|
#
|
@@ -559,6 +559,18 @@ module ActiveJsonModel
|
|
559
559
|
end
|
560
560
|
end
|
561
561
|
|
562
|
+
# Convert a value that might already be an instance of this class from underlying data.
|
563
|
+
# Used to delegate potential loading from ActiveRecord attributes
|
564
|
+
#
|
565
|
+
# @param val either an instance of this model or a Hash like object
|
566
|
+
def active_json_model_cast(val)
|
567
|
+
if val.is_a?(self)
|
568
|
+
val
|
569
|
+
elsif val.is_a?(::Hash) || val.is_a?(::HashWithIndifferentAccess)
|
570
|
+
self.load(val)
|
571
|
+
end
|
572
|
+
end
|
573
|
+
|
562
574
|
# Register a new after load callback which is invoked after the instance is loaded from JSON
|
563
575
|
#
|
564
576
|
# @param method_name [Symbol, String] the name of the method to be invoked
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activejsonmodel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Morlok
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-09-
|
11
|
+
date: 2022-09-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|