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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2b47e3c21fb0e55a817beb1ee28a8054673d493f
4
- data.tar.gz: cb8ed732ecb17fcfbf59019a979ec31bf66a2b5d
3
+ metadata.gz: 8d23ddd527acbb0674ecfd96295e7d3beb700227
4
+ data.tar.gz: 6b28bf320949ccd5dd7c4fe4db8b635b62eee289
5
5
  SHA512:
6
- metadata.gz: bba53b175d4e96b1ddf78df7b1fb72cf2dc41e7c2f723a0e222e1927e0564cdf0678b14ff7ef9a68f6a7d4abe71f50be205b477f31fb85a694e6fac06756024f
7
- data.tar.gz: 2cf9f0099ade2feb58e73d3bca95ac8731d22f8253127ca3fa5412c8019c9bcea6e67e908514d20e7d72f494f8f5efd279b65675673772a1d51201c01b377b16
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
- send("#{hstore_attribute}=", (send(hstore_attribute) || {}).merge(store_key.to_s => serialize(data_type, TypeHelpers.cast(type, value))))
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("#{key}?") do
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
@@ -1,3 +1,3 @@
1
1
  module HstoreAccessor
2
- VERSION = "0.5.7"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -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.5.7
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-04-13 00:00:00.000000000 Z
13
+ date: 2014-05-08 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport