structured_store 1.1.0 → 1.1.2
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: aa0ea30c53e6654f9332650da69b066c2af4d69c6a3007e69d735c554cec8b8a
|
|
4
|
+
data.tar.gz: c612007e1a63d0805b71ef2f2ef106e13480a2866e6a7170fdeeab277a590997
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d904be6d35ad841db181ca8c27b299a2140efadc5b7b19ef82f49a0c6b3c96d35680debf016ca990bda9e8fca2af05b28820630226136e2a063ac2b9b80510f0
|
|
7
|
+
data.tar.gz: 909365fad19af689d4e5694a55bd0e0977d14b0124c0d49d0c89aee1a39e2f82b8cd153a97cc1867ab563bd63ed663c76209ee8b22f7c4249f395f89387918c4
|
data/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# StructuredStore
|
|
2
2
|
|
|
3
|
+
[](https://badge.fury.io/rb/structured_store)
|
|
4
|
+
|
|
3
5
|
StructuredStore is a Ruby gem designed for Rails applications that provides a robust system for managing JSON data using versioned JSON schemas. The library enables developers to store structured data in a JSON database column while maintaining strict schema validation through the json_schemer gem.
|
|
4
6
|
|
|
5
7
|
It features a VersionedSchema model that tracks different versions of JSON schemas using semantic versioning, and a Storable concern that can be included in ActiveRecord models to automatically define accessor methods for schema properties. The gem supports schema evolution by allowing multiple versions of the same schema to coexist, making it ideal for applications that need to maintain backward compatibility while evolving their data structures.
|
|
@@ -29,6 +29,32 @@ module StructuredStore
|
|
|
29
29
|
class_attribute :_structured_store_configurations, default: []
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
+
# Override initialize to handle store attributes after accessors are defined
|
|
33
|
+
def initialize(attributes = nil)
|
|
34
|
+
unless attributes.is_a?(Hash)
|
|
35
|
+
super
|
|
36
|
+
return
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Separate known from unknown attributes and call super, then re-assign
|
|
40
|
+
known_attrs, unknown_attrs = separate_known_and_unknown_attributes(attributes)
|
|
41
|
+
|
|
42
|
+
super(known_attrs)
|
|
43
|
+
|
|
44
|
+
assign_attributes(unknown_attrs) if unknown_attrs.present?
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
private
|
|
48
|
+
|
|
49
|
+
# Separates known attributes (columns and schema associations) from potential store attributes
|
|
50
|
+
def separate_known_and_unknown_attributes(attributes)
|
|
51
|
+
attributes.each_with_object([{}, {}]) do |(key, value), (known, unknown)|
|
|
52
|
+
(respond_to?(key.to_s) ? known : unknown)[key] = value
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
public
|
|
57
|
+
|
|
32
58
|
class_methods do
|
|
33
59
|
# Configures a structured store column and creates the necessary associations.
|
|
34
60
|
#
|
|
@@ -133,7 +159,8 @@ module StructuredStore
|
|
|
133
159
|
@property_resolvers ||= {}
|
|
134
160
|
@property_resolvers[column_name] ||= json_schema_properties(column_name).keys.index_with do |property_name|
|
|
135
161
|
StructuredStore::RefResolvers::Registry.matching_resolver(schema_inspector(column_name),
|
|
136
|
-
property_name
|
|
162
|
+
property_name,
|
|
163
|
+
{ column_name: column_name })
|
|
137
164
|
end
|
|
138
165
|
end
|
|
139
166
|
|
|
@@ -16,9 +16,12 @@ module StructuredStore
|
|
|
16
16
|
# @return [Proc] a lambda that defines the attribute on the singleton class
|
|
17
17
|
# @raise [RuntimeError] if the property type is unsupported
|
|
18
18
|
def define_attribute
|
|
19
|
-
# Capture the property name in
|
|
19
|
+
# Capture the property name and store column name in local variables for closure.
|
|
20
|
+
# context[:column_name] is set by Storable#property_resolvers; fall back to 'store'
|
|
21
|
+
# for any resolver instantiated without that context (e.g. in isolation in tests).
|
|
20
22
|
prop_name = property_name
|
|
21
|
-
|
|
23
|
+
col_name = (context[:column_name] || 'store').to_s
|
|
24
|
+
resolver = self
|
|
22
25
|
|
|
23
26
|
# Define the attribute on the singleton class of the object
|
|
24
27
|
lambda do |object|
|
|
@@ -26,11 +29,11 @@ module StructuredStore
|
|
|
26
29
|
|
|
27
30
|
# Define custom getter and setter methods
|
|
28
31
|
object.singleton_class.define_method(prop_name) do
|
|
29
|
-
resolver.send(:cast_stored_value,
|
|
32
|
+
resolver.send(:cast_stored_value, send(col_name), prop_name, converter)
|
|
30
33
|
end
|
|
31
34
|
|
|
32
35
|
object.singleton_class.define_method("#{prop_name}=") do |value|
|
|
33
|
-
resolver.send(:serialize_value_to_store, self, prop_name, value, converter)
|
|
36
|
+
resolver.send(:serialize_value_to_store, self, col_name, prop_name, value, converter)
|
|
34
37
|
end
|
|
35
38
|
end
|
|
36
39
|
end
|
|
@@ -67,13 +70,13 @@ module StructuredStore
|
|
|
67
70
|
end
|
|
68
71
|
|
|
69
72
|
# Serializes an input value to the store as a hash
|
|
70
|
-
def serialize_value_to_store(object, prop_name, value, converter)
|
|
73
|
+
def serialize_value_to_store(object, col_name, prop_name, value, converter)
|
|
71
74
|
# Initialize store as empty hash if nil
|
|
72
|
-
object.
|
|
73
|
-
return object.
|
|
75
|
+
object.send("#{col_name}=", {}) unless object.send(col_name)
|
|
76
|
+
return object.send(col_name)[prop_name] = nil if value.blank?
|
|
74
77
|
|
|
75
78
|
date1, date2 = converter.convert_to_dates(value)
|
|
76
|
-
object.
|
|
79
|
+
object.send(col_name)[prop_name] = {
|
|
77
80
|
'date1' => date1&.to_fs(:db),
|
|
78
81
|
'date2' => date2&.to_fs(:db)
|
|
79
82
|
}
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: structured_store
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.1.
|
|
4
|
+
version: 1.1.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Tim Gentry
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2026-02-24 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: json_schemer
|