ar_doc_store 0.0.9 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ar_doc_store/attribute_types/embeds_many.rb +0 -3
- data/lib/ar_doc_store/embeddable_model.rb +8 -3
- data/lib/ar_doc_store/model.rb +14 -1
- data/lib/ar_doc_store/storage.rb +26 -3
- data/lib/ar_doc_store/version.rb +1 -1
- data/test/dirty_attributes_test.rb +24 -0
- data/test/test_helper.rb +7 -14
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7e522200ea6aeccb2f865c7642e7ce75ac64224a
|
4
|
+
data.tar.gz: 76c9d6f63fa544eb7bd38fc1e857db5c99f590aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f247dfe4caaac8e32e33bb2c77fde2e7667e46c3a08e54f1fd6b4fd28a9685d445fb935c20b0964044658330b91f14bec70a3ed1823e651e2768252d6bcf4359
|
7
|
+
data.tar.gz: 5218202cab8de9cec3b81990a95087e9b3d3498837cf283528d8a792458448e837b366a14e2ec3a4ce3b84aa03c2a9160d8ae9e5d43575f906df8a3090fa20d4
|
@@ -115,12 +115,9 @@ module ArDocStore
|
|
115
115
|
@parent, @class_name, @assn_name, @models, @values = parent, class_name, assn_name, models, values
|
116
116
|
values.each { |value|
|
117
117
|
value = value.symbolize_keys
|
118
|
-
Rails.logger.info value.inspect
|
119
118
|
if value.key?(:id)
|
120
|
-
Rails.logger.info 'process_existing_model'
|
121
119
|
process_existing_model(value)
|
122
120
|
else
|
123
|
-
Rails.logger.info 'adding new model'
|
124
121
|
add(value)
|
125
122
|
end
|
126
123
|
}
|
@@ -33,10 +33,11 @@ module ArDocStore
|
|
33
33
|
|
34
34
|
def initialize(attrs=HashWithIndifferentAccess.new)
|
35
35
|
@attributes = HashWithIndifferentAccess.new
|
36
|
-
self.parent = attrs.delete(:parent)
|
36
|
+
self.parent = attrs.delete(:parent) if attrs
|
37
37
|
apply_attributes attrs
|
38
|
+
@_initialized = true
|
38
39
|
end
|
39
|
-
|
40
|
+
|
40
41
|
def apply_attributes(attrs=HashWithIndifferentAccess.new)
|
41
42
|
virtual_attributes.keys.each do |attr|
|
42
43
|
@attributes[attr] ||= nil
|
@@ -67,7 +68,11 @@ module ArDocStore
|
|
67
68
|
end
|
68
69
|
|
69
70
|
def write_store_attribute(store, key, value)
|
70
|
-
changed_attributes[key] = read_store_attribute(:data, key)
|
71
|
+
changed_attributes[key] = read_store_attribute(:data, key) if @_initialized
|
72
|
+
@attributes[key] = value
|
73
|
+
end
|
74
|
+
|
75
|
+
def write_default_store_attribute(key, value)
|
71
76
|
@attributes[key] = value
|
72
77
|
end
|
73
78
|
|
data/lib/ar_doc_store/model.rb
CHANGED
@@ -5,7 +5,20 @@ module ArDocStore
|
|
5
5
|
def self.included(mod)
|
6
6
|
mod.send :include, ArDocStore::Storage
|
7
7
|
mod.send :include, ArDocStore::Embedding
|
8
|
+
mod.send :include, InstanceMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
module InstanceMethods
|
12
|
+
|
13
|
+
def write_store_attribute(store_attribute, key, value)
|
14
|
+
public_send "#{key}_will_change!"
|
15
|
+
super(store_attribute, key, value)
|
16
|
+
end
|
17
|
+
|
18
|
+
def write_default_store_attribute(key, default_value)
|
19
|
+
data[key] = default_value
|
20
|
+
end
|
21
|
+
|
8
22
|
end
|
9
|
-
|
10
23
|
end
|
11
24
|
end
|
data/lib/ar_doc_store/storage.rb
CHANGED
@@ -27,7 +27,7 @@ module ArDocStore
|
|
27
27
|
super
|
28
28
|
end
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
private
|
32
32
|
|
33
33
|
def is_stored_attribute?(name)
|
@@ -44,6 +44,8 @@ module ArDocStore
|
|
44
44
|
is_store_accessor_method?(name) ? name : name.match(/\'(\w+)\'/)[0].gsub("'", '')
|
45
45
|
end
|
46
46
|
|
47
|
+
|
48
|
+
|
47
49
|
end
|
48
50
|
|
49
51
|
module ClassMethods
|
@@ -56,6 +58,7 @@ module ArDocStore
|
|
56
58
|
raise "Invalid attribute type: #{name}" unless const_defined?(class_name)
|
57
59
|
class_name = class_name.constantize
|
58
60
|
class_name.build self, name, options
|
61
|
+
define_virtual_attribute_method name
|
59
62
|
end
|
60
63
|
|
61
64
|
def add_ransacker(key, predicate = nil)
|
@@ -88,7 +91,8 @@ module ArDocStore
|
|
88
91
|
if value
|
89
92
|
value.public_send(typecast_method)
|
90
93
|
elsif default_value
|
91
|
-
|
94
|
+
write_default_store_attribute(key, default_value)
|
95
|
+
# write_store_attribute(:data, key, default_value)
|
92
96
|
default_value
|
93
97
|
end
|
94
98
|
}
|
@@ -101,7 +105,7 @@ module ArDocStore
|
|
101
105
|
end
|
102
106
|
}
|
103
107
|
end
|
104
|
-
|
108
|
+
|
105
109
|
def store_attribute_from_class(class_name, key)
|
106
110
|
define_method key.to_sym, -> {
|
107
111
|
ivar = "@#{key}"
|
@@ -125,6 +129,25 @@ module ArDocStore
|
|
125
129
|
write_store_attribute :data, key, value
|
126
130
|
}
|
127
131
|
end
|
132
|
+
|
133
|
+
# Pretty much the same as define_attribute_method but skipping the matches that create read and write methods
|
134
|
+
def define_virtual_attribute_method(attr_name)
|
135
|
+
attr_name = attr_name.to_s
|
136
|
+
attribute_method_matchers.each do |matcher|
|
137
|
+
method_name = matcher.method_name(attr_name)
|
138
|
+
next if instance_method_already_implemented?(method_name)
|
139
|
+
next if %w{attribute attribute= attribute_before_type_cast}.include? matcher.method_missing_target
|
140
|
+
generate_method = "define_method_#{matcher.method_missing_target}"
|
141
|
+
if respond_to?(generate_method, true)
|
142
|
+
send(generate_method, attr_name)
|
143
|
+
else
|
144
|
+
define_proxy_call true, generated_attribute_methods, method_name, matcher.method_missing_target, attr_name.to_s
|
145
|
+
end
|
146
|
+
end
|
147
|
+
attribute_method_matchers_cache.clear
|
148
|
+
end
|
149
|
+
|
150
|
+
|
128
151
|
|
129
152
|
def string_attributes(*args)
|
130
153
|
args.each do |arg|
|
data/lib/ar_doc_store/version.rb
CHANGED
@@ -0,0 +1,24 @@
|
|
1
|
+
require_relative './test_helper'
|
2
|
+
|
3
|
+
class DirtylAttributeTest < MiniTest::Test
|
4
|
+
|
5
|
+
def test_dirty_attributes_on_model
|
6
|
+
b = Building.new name: 'Foo!'
|
7
|
+
assert_equal b.name_changed?, false
|
8
|
+
b.name = 'Bar.'
|
9
|
+
assert b.name_changed?
|
10
|
+
assert_equal 'Foo!', b.name_was
|
11
|
+
assert_equal 'Bar.', b.name
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_dirty_attributes_on_embedded_model
|
15
|
+
b = Building.new
|
16
|
+
r = b.build_restroom is_signage_clear: true
|
17
|
+
assert_equal r.is_signage_clear_changed?, false
|
18
|
+
r.is_signage_clear = false
|
19
|
+
assert r.is_signage_clear_changed?
|
20
|
+
assert_equal true, r.is_signage_clear_was
|
21
|
+
assert_equal false, r.is_signage_clear
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -24,11 +24,12 @@ class ARDuck
|
|
24
24
|
|
25
25
|
def initialize(attrs=nil)
|
26
26
|
@attributes = HashWithIndifferentAccess.new
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
27
|
+
unless attrs.nil?
|
28
|
+
attrs.each { |key, value|
|
29
|
+
@attributes[key] = value
|
30
|
+
}
|
31
|
+
end
|
32
|
+
@_initialized = true
|
32
33
|
end
|
33
34
|
|
34
35
|
def persisted?
|
@@ -52,18 +53,10 @@ class ARDuck
|
|
52
53
|
end
|
53
54
|
|
54
55
|
def write_store_attribute(store, key, value)
|
55
|
-
changed_attributes[key] = read_store_attribute(:data, key)
|
56
|
+
#changed_attributes[key] = read_store_attribute(:data, key) if @_initialized
|
56
57
|
@attributes[key] = value
|
57
58
|
end
|
58
59
|
|
59
|
-
def data_will_change!
|
60
|
-
true
|
61
|
-
end
|
62
|
-
|
63
|
-
def self.columns_hash
|
64
|
-
@@columns_hash ||= HashWithIndifferentAccess.new
|
65
|
-
end
|
66
|
-
|
67
60
|
end
|
68
61
|
|
69
62
|
class EmptyModel
|
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: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Furber
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -84,6 +84,7 @@ files:
|
|
84
84
|
- lib/ar_doc_store/model.rb
|
85
85
|
- lib/ar_doc_store/storage.rb
|
86
86
|
- lib/ar_doc_store/version.rb
|
87
|
+
- test/dirty_attributes_test.rb
|
87
88
|
- test/embedded_model_attribute_test.rb
|
88
89
|
- test/embedding_test.rb
|
89
90
|
- test/model_attribute_access_test.rb
|
@@ -113,6 +114,7 @@ signing_key:
|
|
113
114
|
specification_version: 4
|
114
115
|
summary: A document storage gem meant for ActiveRecord PostgresQL JSON storage.
|
115
116
|
test_files:
|
117
|
+
- test/dirty_attributes_test.rb
|
116
118
|
- test/embedded_model_attribute_test.rb
|
117
119
|
- test/embedding_test.rb
|
118
120
|
- test/model_attribute_access_test.rb
|