ar_doc_store 2.1 → 2.1.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: '0118efe822fe133ce4561bbb5feb3dc3640666b6f9cab8b116f041241e160594'
4
- data.tar.gz: db55c5000a7215f5d1a3d312965f562c79c209b3446b1a630cd84ffe25a79f49
3
+ metadata.gz: ab415cae0a0fff588b20796660e16da2d406a9800d689ffc1d4e3c02e725636c
4
+ data.tar.gz: ec609a135f59c8e04c05fdbc8b907a86e49b7818e0d3f406f57d2f62223ce4ae
5
5
  SHA512:
6
- metadata.gz: 00b523748532b1e6109e12e09941cead7a4df9b927286e82ced80d6d47bc47859e2df9ede2fcb7b77b483cd2c19470714c386cd866dfb09fb874fab9274e0753
7
- data.tar.gz: b3020b345638f79c1ebb035d40d0ac764343a7a6caf58a94c3bbeaeceeb46570b56953adcd3fc8081eea7a5185809cc471d47973d538571fe16f9c23fd88dd15
6
+ metadata.gz: 1d321c097e506f2005a2be86dbbe6245011fdda1859bd819c80af147b9d1357106a70c4fcc983e00e50ccb3b4800984f734302c8da8700098a5a3dd33e34da46
7
+ data.tar.gz: 931e6d3655e830d7df0913508aca632f1bdcb7f32b0d356efb9b8aac9d058210c2d70f2fd23093d6ef012b14d43e7c46ff257c0f5a1247af79c1537bd64bfb7a
@@ -34,12 +34,13 @@ module ArDocStore
34
34
  options.merge!(default: default) if default.present?
35
35
  model.class_eval do
36
36
  attribute attribute_name, attribute_type.new, **options
37
- define_method "#{attribute_name}=".to_sym, -> (value) {
38
- value = nil if value == '' || value == ['']
39
- write_attribute attribute_name, value
40
- new_value = read_attribute attribute_name
41
- write_store_attribute(json_column, attribute_name, new_value)
42
- }
37
+ # define_method "#{attribute_name}=".to_sym, -> (value) {
38
+ # value = nil if value == '' || value == ['']
39
+ # write_attribute attribute_name, value
40
+ # # new_value = read_attribute attribute_name
41
+ # # write_store_attribute(json_column, attribute_name, new_value)
42
+ # attributes[attribute_name]
43
+ # }
43
44
  end
44
45
  end
45
46
 
@@ -9,7 +9,7 @@ module ArDocStore
9
9
  model.class_eval <<-CODE, __FILE__, __LINE__ + 1
10
10
  attribute :#{attribute}, ArDocStore::Types::EmbedsMany.new("#{@class_name}")
11
11
  def #{attribute}
12
- value = send :attribute, :#{attribute}
12
+ value = read_attribute :#{attribute}
13
13
  if value && !value.is_a?(ArDocStore::EmbeddedCollection)
14
14
  value = ArDocStore::EmbeddedCollection.new value
15
15
  else
@@ -24,8 +24,8 @@ module ArDocStore
24
24
  end
25
25
  def #{attribute}=(value)
26
26
  value = nil if value == '' || value == ['']
27
- send :attribute=, :#{attribute}, value
28
- new_value = send :attribute, :#{attribute}
27
+ write_attribute :#{attribute}, value
28
+ new_value = read_attribute :#{attribute}
29
29
  new_value.parent = self
30
30
  new_value.embedded_as = :#{attribute}
31
31
  new_value.each do |item|
@@ -9,7 +9,7 @@ module ArDocStore
9
9
  model.class_eval <<-CODE, __FILE__, __LINE__ + 1
10
10
  attribute :#{attribute}, ArDocStore::Types::EmbedsOne.new("#{@class_name}")
11
11
  def #{attribute}
12
- value = send :attribute, :#{attribute}
12
+ value = read_attribute :#{attribute}
13
13
  value && value.parent = self
14
14
  value && value.embedded_as = :#{attribute}
15
15
  value
@@ -19,11 +19,11 @@ module ArDocStore
19
19
  write_attribute :#{attribute}, value
20
20
  value && #{attribute}.parent = self
21
21
  value && #{attribute}.embedded_as = :#{attribute}
22
- write_store_attribute json_column, :#{attribute}, #{attribute}
23
22
  #{attribute}
24
23
  end
25
24
  CODE
26
25
  end
26
+ # TODO: figure out how to set parent on read and write
27
27
 
28
28
  def create_build_method
29
29
  model.class_eval <<-CODE, __FILE__, __LINE__ + 1
@@ -87,6 +87,14 @@ module ArDocStore
87
87
  value
88
88
  end
89
89
 
90
+ def read_attribute(attribute)
91
+ attributes[attribute.to_s]
92
+ end
93
+
94
+ def write_attribute(attribute, value)
95
+ attributes[attribute.to_s] = value
96
+ end
97
+
90
98
  def to_param
91
99
  id
92
100
  end
@@ -7,6 +7,7 @@ module ArDocStore
7
7
  mod.send :include, ArDocStore::Embedding
8
8
  mod.send :include, InstanceMethods
9
9
  mod.after_initialize :assign_json_data
10
+ mod.before_validation :save_json_data
10
11
  mod.before_save :mark_embeds_as_persisted
11
12
  end
12
13
 
@@ -23,6 +24,15 @@ module ArDocStore
23
24
  mutations_from_database.forget_change(key) unless new_record?
24
25
  end
25
26
  end
27
+
28
+ def save_json_data
29
+ json_attributes.each do |key, value|
30
+ next unless changes.key?(key)
31
+
32
+ write_store_attribute json_column, key, read_attribute(key)
33
+ end
34
+ end
35
+
26
36
  def mark_embeds_as_persisted
27
37
  json_attributes.values.each do |value|
28
38
  if value.respond_to?(:embedded?) && value.embedded? && respond_to?(value.attribute)
@@ -11,21 +11,19 @@ module ArDocStore
11
11
 
12
12
  def cast(value)
13
13
  @class_name = @class_name.constantize if class_name.respond_to?(:constantize)
14
- if value.nil?
15
- value
16
- elsif value.kind_of?(class_name)
14
+ return if value.nil?
15
+
16
+ if value.kind_of?(class_name)
17
17
  value
18
18
  elsif value.respond_to?(:to_hash)
19
19
  class_name.new value
20
- else
21
- nil
22
20
  end
23
21
  end
24
22
 
25
23
  def serialize(value)
26
- if value.nil?
27
- nil
28
- elsif value.kind_of?(class_name)
24
+ return if value.nil?
25
+
26
+ if value.kind_of?(class_name)
29
27
  value.serializable_hash
30
28
  else
31
29
  cast(value).serializable_hash
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ArDocStore
4
- VERSION = "2.1"
4
+ VERSION = "2.1.1"
5
5
  end
@@ -45,7 +45,8 @@ class EmbedsOneAttributeTest < MiniTest::Test
45
45
  b.entrance.name = 'Baz'
46
46
  assert b.entrance_changed?
47
47
  assert b.entrance.name_changed?
48
- assert_equal b.data['entrance']['name'], b.entrance.name
48
+ # b.validate
49
+ # assert_equal b.data['entrance']['name'], b.entrance.name
49
50
  b.save
50
51
  c = Building.find(a.id)
51
52
  assert_equal b.data, c.data
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar_doc_store
3
3
  version: !ruby/object:Gem::Version
4
- version: '2.1'
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Furber
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-01-15 00:00:00.000000000 Z
11
+ date: 2021-01-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -150,7 +150,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
150
150
  - !ruby/object:Gem::Version
151
151
  version: '0'
152
152
  requirements: []
153
- rubygems_version: 3.1.4
153
+ rubygems_version: 3.2.3
154
154
  signing_key:
155
155
  specification_version: 4
156
156
  summary: A document storage gem meant for ActiveRecord PostgresQL JSON storage.