hstore_attributes 1.0.0 → 1.0.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 +4 -4
- data/lib/hstore_attributes.rb +13 -13
- data/lib/hstore_attributes/version.rb +1 -1
- data/test/hstore_accessor_test.rb +2 -1
- data/test/hstore_reader_test.rb +2 -1
- data/test/hstore_writer_test.rb +25 -3
- data/test/test_helper.rb +0 -12
- 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: 9cde79926055a503605a9ad31cbad8853ec47766
|
4
|
+
data.tar.gz: 3cecb728f3de2f143665d98b37b4b391f1f35bbb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb3396da7e19369d6ee3e17ffb8df48b068fb02f975f84c03f49bb5b561bef6747ead9645d0171a26f6fee4e2da053b507dbf10fc576b59f2261c052873a246a
|
7
|
+
data.tar.gz: 022ef8515b44f9eb34e4f68bfe745c61d9e371f91a4fa4e05a78340379086dca0051c0030a6b3ea5b05b716dbcb5969d1d1cfa880010f57a34f59d0c7295b1e3
|
data/lib/hstore_attributes.rb
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
require "hstore_attributes/version"
|
2
2
|
|
3
3
|
module HstoreAttributes
|
4
|
-
def hstore_reader(hstore, *
|
5
|
-
options =
|
6
|
-
|
7
|
-
define_method(
|
8
|
-
value = (send(hstore) || {})[
|
4
|
+
def hstore_reader(hstore, *attributes)
|
5
|
+
options = attributes.extract_options!
|
6
|
+
attributes.map(&:to_s).each do |attribute|
|
7
|
+
define_method(attribute) do
|
8
|
+
value = (send(hstore) || {})[attribute]
|
9
9
|
value = options[:type_cast].to_proc.call(value) if options[:type_cast]
|
10
10
|
value
|
11
11
|
end
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
-
def hstore_writer(hstore, *
|
16
|
-
options =
|
17
|
-
|
18
|
-
define_method("#{
|
15
|
+
def hstore_writer(hstore, *attributes)
|
16
|
+
options = attributes.extract_options!
|
17
|
+
attributes.map(&:to_s).each do |attribute|
|
18
|
+
define_method("#{attribute}=") do |value|
|
19
19
|
value = options[:type_cast].to_proc.call(value) if options[:type_cast]
|
20
|
-
send("#{hstore}=", (send(hstore) || {}).merge(
|
20
|
+
send("#{hstore}=", (send(hstore) || {}).merge(attribute => value))
|
21
21
|
end
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
-
def hstore_accessor(hstore, *
|
26
|
-
hstore_reader(hstore, *
|
27
|
-
hstore_writer(hstore, *
|
25
|
+
def hstore_accessor(hstore, *attributes)
|
26
|
+
hstore_reader(hstore, *attributes)
|
27
|
+
hstore_writer(hstore, *attributes)
|
28
28
|
end
|
29
29
|
end
|
data/test/hstore_reader_test.rb
CHANGED
data/test/hstore_writer_test.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require "test_helper"
|
2
2
|
|
3
|
-
class Product
|
3
|
+
class Product
|
4
|
+
attr_accessor :data
|
4
5
|
extend HstoreAttributes
|
5
6
|
|
6
7
|
hstore_writer :data, :name, :name_2
|
@@ -18,6 +19,11 @@ describe "hstore_writer" do
|
|
18
19
|
assert_equal({"name" => "zomg"}, @product.data)
|
19
20
|
end
|
20
21
|
|
22
|
+
it "sets name to nil if no hstore" do
|
23
|
+
@product.name = nil
|
24
|
+
assert_equal({"name" => nil}, @product.data)
|
25
|
+
end
|
26
|
+
|
21
27
|
it "sets name if empty hstore" do
|
22
28
|
hstore = {}
|
23
29
|
@product.data = hstore
|
@@ -26,6 +32,14 @@ describe "hstore_writer" do
|
|
26
32
|
refute_same hstore, @product.data
|
27
33
|
end
|
28
34
|
|
35
|
+
it "sets name to nil if empty hstore" do
|
36
|
+
hstore = {}
|
37
|
+
@product.data = hstore
|
38
|
+
@product.name = nil
|
39
|
+
assert_equal({"name" => nil}, @product.data)
|
40
|
+
refute_same hstore, @product.data
|
41
|
+
end
|
42
|
+
|
29
43
|
it "sets name if already in hstore" do
|
30
44
|
hstore = {"name" => "trolololo"}
|
31
45
|
@product.data = hstore
|
@@ -34,6 +48,14 @@ describe "hstore_writer" do
|
|
34
48
|
refute_same hstore, @product.data
|
35
49
|
end
|
36
50
|
|
51
|
+
it "sets name to nil if already in hstore" do
|
52
|
+
hstore = {"name" => "trolololo"}
|
53
|
+
@product.data = hstore
|
54
|
+
@product.name = nil
|
55
|
+
assert_equal({"name" => nil}, @product.data)
|
56
|
+
refute_same hstore, @product.data
|
57
|
+
end
|
58
|
+
|
37
59
|
it "sets name_2 if no hstore" do
|
38
60
|
@product.name_2 = "lolo"
|
39
61
|
assert_equal({"name_2" => "lolo"}, @product.data)
|
@@ -41,11 +63,11 @@ describe "hstore_writer" do
|
|
41
63
|
|
42
64
|
it "sets price if no hstore" do
|
43
65
|
@product.price = 0
|
44
|
-
assert_equal({"price" =>
|
66
|
+
assert_equal({"price" => 0}, @product.data)
|
45
67
|
end
|
46
68
|
|
47
69
|
it "sets cents using type_cast function" do
|
48
70
|
@product.cents = "1"
|
49
|
-
assert_equal({"cents" =>
|
71
|
+
assert_equal({"cents" => 100}, @product.data)
|
50
72
|
end
|
51
73
|
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hstore_attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kuba Kuźma
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-05-
|
11
|
+
date: 2013-05-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|