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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 396066b7fdc41c0c1039d85ebad2b42e5d04355b
4
- data.tar.gz: eb91ce7217c92ef41ff6f953304da15d9143d31e
3
+ metadata.gz: 9cde79926055a503605a9ad31cbad8853ec47766
4
+ data.tar.gz: 3cecb728f3de2f143665d98b37b4b391f1f35bbb
5
5
  SHA512:
6
- metadata.gz: 159f4b7ce845ba685475e02bf293422bc985cbcfc8a9da3655f6a5c74180d64114879cebaedb82c1381d0b92d0190feb3aaa2709623a141411de7df05dcae4df
7
- data.tar.gz: b2bea8c6b9b38134e8d416ec5e1ce096738ee19989fe30aeb9385c170657951cc2fd24019c53893e793870dba450db9ff6f35dffd8934c233d845202ac5af862
6
+ metadata.gz: eb3396da7e19369d6ee3e17ffb8df48b068fb02f975f84c03f49bb5b561bef6747ead9645d0171a26f6fee4e2da053b507dbf10fc576b59f2261c052873a246a
7
+ data.tar.gz: 022ef8515b44f9eb34e4f68bfe745c61d9e371f91a4fa4e05a78340379086dca0051c0030a6b3ea5b05b716dbcb5969d1d1cfa880010f57a34f59d0c7295b1e3
@@ -1,29 +1,29 @@
1
1
  require "hstore_attributes/version"
2
2
 
3
3
  module HstoreAttributes
4
- def hstore_reader(hstore, *symbols)
5
- options = symbols.extract_options!
6
- symbols.each do |symbol|
7
- define_method(symbol) do
8
- value = (send(hstore) || {})[symbol.to_s]
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, *symbols)
16
- options = symbols.extract_options!
17
- symbols.each do |symbol|
18
- define_method("#{symbol}=") do |value|
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(symbol.to_s => value.to_s))
20
+ send("#{hstore}=", (send(hstore) || {}).merge(attribute => value))
21
21
  end
22
22
  end
23
23
  end
24
24
 
25
- def hstore_accessor(hstore, *symbols)
26
- hstore_reader(hstore, *symbols)
27
- hstore_writer(hstore, *symbols)
25
+ def hstore_accessor(hstore, *attributes)
26
+ hstore_reader(hstore, *attributes)
27
+ hstore_writer(hstore, *attributes)
28
28
  end
29
29
  end
@@ -1,3 +1,3 @@
1
1
  module HstoreAttributes
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -1,6 +1,7 @@
1
1
  require "test_helper"
2
2
 
3
- class Product < DummyModel
3
+ class Product
4
+ attr_accessor :data
4
5
  extend HstoreAttributes
5
6
 
6
7
  hstore_accessor :data, :name, :description, type_cast: ->(value) { value.to_s.upcase }
@@ -1,6 +1,7 @@
1
1
  require "test_helper"
2
2
 
3
- class Product < DummyModel
3
+ class Product
4
+ attr_accessor :data
4
5
  extend HstoreAttributes
5
6
 
6
7
  hstore_reader :data, :price, :price_2, type_cast: :to_i
@@ -1,6 +1,7 @@
1
1
  require "test_helper"
2
2
 
3
- class Product < DummyModel
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" => "0"}, @product.data)
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" => "100"}, @product.data)
71
+ assert_equal({"cents" => 100}, @product.data)
50
72
  end
51
73
  end
data/test/test_helper.rb CHANGED
@@ -1,15 +1,3 @@
1
1
  require "minitest/autorun"
2
2
  require "active_support"
3
3
  require "hstore_attributes"
4
-
5
- # DummyModel emulates an ActiveRecord model with hstore "data"
6
-
7
- class DummyModel
8
- def data
9
- @data
10
- end
11
-
12
- def data=(data)
13
- @data = data
14
- end
15
- end
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.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-06 00:00:00.000000000 Z
11
+ date: 2013-05-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport