hstore_accessor 0.5.7 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/hstore_accessor/macro.rb +20 -6
- data/lib/hstore_accessor/version.rb +1 -1
- data/spec/hstore_accessor_spec.rb +28 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8d23ddd527acbb0674ecfd96295e7d3beb700227
|
4
|
+
data.tar.gz: 6b28bf320949ccd5dd7c4fe4db8b635b62eee289
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 66ffe18f15a1460a4acdfc105548351853b67a2c346578c415ff066e5d4c69e5f9902b935dae09d6781405162a2b4797f36edd7d570d38337215bd45126f47a7
|
7
|
+
data.tar.gz: 7e033e6d488067acd6ba7d3d4517427e2bc69920264d7b8e736ccb660d02054371a704130f6b82019054525390c8ab947023343da0c09ea6e0197ed526f4ca49
|
@@ -4,6 +4,10 @@ module HstoreAccessor
|
|
4
4
|
module ClassMethods
|
5
5
|
|
6
6
|
def hstore_accessor(hstore_attribute, fields)
|
7
|
+
define_method("hstore_metadata_for_#{hstore_attribute}") do
|
8
|
+
fields
|
9
|
+
end
|
10
|
+
|
7
11
|
field_methods = Module.new
|
8
12
|
fields.each do |key, type|
|
9
13
|
|
@@ -19,13 +23,11 @@ module HstoreAccessor
|
|
19
23
|
|
20
24
|
raise Serialization::InvalidDataTypeError unless Serialization::VALID_TYPES.include?(data_type)
|
21
25
|
|
22
|
-
define_method("hstore_metadata_for_#{hstore_attribute}") do
|
23
|
-
fields
|
24
|
-
end
|
25
|
-
|
26
26
|
field_methods.send(:define_method, "#{key}=") do |value|
|
27
|
-
|
27
|
+
serialized_value = serialize(data_type, TypeHelpers.cast(type, value))
|
28
|
+
send(:attribute_will_change!, key)
|
28
29
|
send("#{hstore_attribute}_will_change!")
|
30
|
+
send("#{hstore_attribute}=", (send(hstore_attribute) || {}).merge(store_key.to_s => serialized_value))
|
29
31
|
end
|
30
32
|
|
31
33
|
field_methods.send(:define_method, key) do
|
@@ -33,10 +35,22 @@ module HstoreAccessor
|
|
33
35
|
deserialize(data_type, value)
|
34
36
|
end
|
35
37
|
|
36
|
-
define_method
|
38
|
+
field_methods.send(:define_method, "#{key}?") do
|
37
39
|
send("#{key}").present?
|
38
40
|
end
|
39
41
|
|
42
|
+
field_methods.send(:define_method, "#{key}_changed?") do
|
43
|
+
send(:attribute_changed?, key)
|
44
|
+
end
|
45
|
+
|
46
|
+
field_methods.send(:define_method, "#{key}_was") do
|
47
|
+
send(:attribute_was, key)
|
48
|
+
end
|
49
|
+
|
50
|
+
field_methods.send(:define_method, "#{key}_change") do
|
51
|
+
send(:attribute_change, key)
|
52
|
+
end
|
53
|
+
|
40
54
|
query_field = "#{hstore_attribute} -> '#{store_key}'"
|
41
55
|
|
42
56
|
case data_type
|
@@ -470,4 +470,32 @@ describe HstoreAccessor do
|
|
470
470
|
end
|
471
471
|
end
|
472
472
|
|
473
|
+
describe "dirty tracking" do
|
474
|
+
|
475
|
+
let(:product) { Product.new }
|
476
|
+
|
477
|
+
it "<attr>_changed? should return the expected value" do
|
478
|
+
expect(product.color_changed?).to be_false
|
479
|
+
product.color = "ORANGE"
|
480
|
+
expect(product.color_changed?).to be_true
|
481
|
+
product.save
|
482
|
+
expect(product.color_changed?).to be_false
|
483
|
+
end
|
484
|
+
|
485
|
+
it "<attr>_was should return the expected value" do
|
486
|
+
product.color = "ORANGE"
|
487
|
+
product.save
|
488
|
+
product.color = "GREEN"
|
489
|
+
expect(product.color_was).to eq "ORANGE"
|
490
|
+
end
|
491
|
+
|
492
|
+
it "<attr>_change should return the expected value" do
|
493
|
+
product.color = "ORANGE"
|
494
|
+
product.save
|
495
|
+
product.color = "GREEN"
|
496
|
+
expect(product.color_change).to eq ["ORANGE", "GREEN"]
|
497
|
+
end
|
498
|
+
|
499
|
+
end
|
500
|
+
|
473
501
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hstore_accessor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe Hirn
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2014-
|
13
|
+
date: 2014-05-08 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activesupport
|