nrser 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1c1aebae949432774359be83991d38a1f2401011
4
- data.tar.gz: 4c30490c2fd5fdab98b488e20a1fbc3a1b7b6273
3
+ metadata.gz: f720f1c58fc47b32ab949e9e898360ff3cf89d6b
4
+ data.tar.gz: 0d0080e605b5cec3cc3b3fa9361ac64fc6dd4a66
5
5
  SHA512:
6
- metadata.gz: 4d57b3ce6c67e7bc25834d9109c290a86c64a56798c47ed23b3f01ca2ce3011ea90263cf3785fa6f76a3db00d14c2c9d04595e7fa1f35006f87cbaffebad823c
7
- data.tar.gz: fbd33e894a99dadbe17b8dbc7569607871e829c161518e20f0f6eb7bcea537856a1a3a9d85edcd7b7a004be01ec1928b098bfac4f332913332cd059b329c7904
6
+ metadata.gz: 1d3d8af368c349faad2c129ba472ec3dc4f0107feded5bcb8421b45826ab00536703b385953229091642ef13d5876618485fd6f85c2219d27f25420f84ea7e6b
7
+ data.tar.gz: affdad306f4d35a39fabeab3a184d738dd06d0a335865466907b4de0b97b1318afcef177258fffdd51590e99f99ed75e22652ac60ea7b0d388756f095abac68b
@@ -137,9 +137,8 @@ module NRSER::Props::InstanceMethods
137
137
  def to_data only_own: false,
138
138
  only_primary: false,
139
139
  add_class: true,
140
- class_key: '__class__',
140
+ class_key: NRSER::Props::DEFAULT_CLASS_KEY,
141
141
  compact: true
142
- # class_key: NRSER::Props::DEFAULT_CLASS_KEY
143
142
 
144
143
  hash = self.class.props(only_own: only_own, only_primary: only_primary).
145
144
  map { |name, prop|
data/lib/nrser/props.rb CHANGED
@@ -1,9 +1,87 @@
1
1
  require_relative './props/class_methods'
2
2
  require_relative './props/instance_methods'
3
3
 
4
+
5
+ require 'nrser/refinements/types'
6
+ using NRSER::Types
7
+
8
+
4
9
  module NRSER::Props
10
+ DEFAULT_CLASS_KEY = '__class__';
11
+
5
12
  def self.included base
6
13
  base.include NRSER::Props::InstanceMethods
7
14
  base.extend NRSER::Props::ClassMethods
8
15
  end
16
+
17
+ # Instantiate a class from a data hash. The hash must contain the
18
+ # `__class__` key and the target class must be loaded already.
19
+ #
20
+ # **WARNING**
21
+ #
22
+ # I'm sure this is all-sorts of unsafe. Please don't ever think this is
23
+ # reasonable to use on untrusted data.
24
+ #
25
+ # @param [Hash<String, Object>] data
26
+ # Data hash to load from.
27
+ #
28
+ # @param
29
+ #
30
+ # @return [NRSER::Props::Props]
31
+ # Instance of a propertied class.
32
+ #
33
+ def self.UNSAFE_load_instance_from_data data, class_key: DEFAULT_CLASS_KEY
34
+ t.hash_.check data
35
+
36
+ unless data.key?( class_key )
37
+ raise ArgumentError.new binding.erb <<-ERB
38
+ Data is missing <%= class_key %> key - no idea what class to
39
+ instantiate.
40
+
41
+ Data:
42
+
43
+ <%= data.pretty_inspect %>
44
+
45
+ ERB
46
+ end
47
+
48
+ # Get the class name from the data hash using the key, checking that it's
49
+ # a non-empty string.
50
+ class_name = t.non_empty_str.check! data[class_key]
51
+
52
+ # Resolve the constant at that name.
53
+ klass = class_name.to_const!
54
+
55
+ # Make sure it's one of ours
56
+ unless klass.included_modules.include?( NRSER::Props )
57
+ raise ArgumentError.new binding.erb <<-ERB
58
+ Can not load instance from data - bad class name.
59
+
60
+ Extracted class name
61
+
62
+ <%= class_name.inspect %>
63
+
64
+ from class key
65
+
66
+ <%= class_key.inspect %>
67
+
68
+ which resolved to constant
69
+
70
+ <%= klass.inspect %>
71
+
72
+ but that class does not include the NRSER::Props::Props mixin, which we
73
+ check for to help protect against executing an unrelated `.from_data`
74
+ class method when attempting to load.
75
+
76
+ Data:
77
+
78
+ <%= data.pretty_inspect %>
79
+
80
+ ERB
81
+ end
82
+
83
+ # Kick off the restore and return the result
84
+ klass.from_data data
85
+
86
+ end # .UNSAFE_load_instance_from_data
9
87
  end
data/lib/nrser/version.rb CHANGED
@@ -18,7 +18,7 @@ module NRSER
18
18
  #
19
19
  # @return [String]
20
20
  #
21
- VERSION = '0.3.2'
21
+ VERSION = '0.3.3'
22
22
 
23
23
 
24
24
  module Version
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nrser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - nrser