hstore_attributes 1.0.1 → 1.0.2

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: 9cde79926055a503605a9ad31cbad8853ec47766
4
- data.tar.gz: 3cecb728f3de2f143665d98b37b4b391f1f35bbb
3
+ metadata.gz: 7670a1540b41cf655db5638737cb86086193661d
4
+ data.tar.gz: 6cb7f874632c3d5f92b5bb47d4edfa3460a7e9b4
5
5
  SHA512:
6
- metadata.gz: eb3396da7e19369d6ee3e17ffb8df48b068fb02f975f84c03f49bb5b561bef6747ead9645d0171a26f6fee4e2da053b507dbf10fc576b59f2261c052873a246a
7
- data.tar.gz: 022ef8515b44f9eb34e4f68bfe745c61d9e371f91a4fa4e05a78340379086dca0051c0030a6b3ea5b05b716dbcb5969d1d1cfa880010f57a34f59d0c7295b1e3
6
+ metadata.gz: 3a7dd48f1c828733a7b42947d1231c5fb3f2546976923d7b179b0120f858232b28bfe970f4d1e87ba04bb9c4edd26fbfdcc3a0a774b1183ba7fb8059b1709425
7
+ data.tar.gz: 6d0c78db0d3d51ef6b8cd59495f099593d63ab2ca56e0b79bc23bd096d5d9be137cc6172dda79740765ddbb5d5badb617e30b11817d24d5a60d0d882eed3e78f
@@ -1,3 +1,3 @@
1
1
  module HstoreAttributes
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.2"
3
3
  end
@@ -1,13 +1,22 @@
1
1
  require "hstore_attributes/version"
2
2
 
3
3
  module HstoreAttributes
4
+ def self.convert(value, options = {})
5
+ if options[:allow_nil] == true and value.nil?
6
+ nil
7
+ elsif options[:type_cast]
8
+ options[:type_cast].to_proc.call(value)
9
+ else
10
+ value
11
+ end
12
+ end
13
+
4
14
  def hstore_reader(hstore, *attributes)
5
15
  options = attributes.extract_options!
6
16
  attributes.map(&:to_s).each do |attribute|
7
17
  define_method(attribute) do
8
18
  value = (send(hstore) || {})[attribute]
9
- value = options[:type_cast].to_proc.call(value) if options[:type_cast]
10
- value
19
+ HstoreAttributes.convert(value, options)
11
20
  end
12
21
  end
13
22
  end
@@ -16,7 +25,7 @@ module HstoreAttributes
16
25
  options = attributes.extract_options!
17
26
  attributes.map(&:to_s).each do |attribute|
18
27
  define_method("#{attribute}=") do |value|
19
- value = options[:type_cast].to_proc.call(value) if options[:type_cast]
28
+ value = HstoreAttributes.convert(value, options)
20
29
  send("#{hstore}=", (send(hstore) || {}).merge(attribute => value))
21
30
  end
22
31
  end
@@ -7,14 +7,7 @@ class Product
7
7
  hstore_reader :data, :price, :price_2, type_cast: :to_i
8
8
  hstore_reader :data, :name, type_cast: ->(value) { value.to_s.upcase }
9
9
  hstore_reader :data, :description
10
-
11
- def data
12
- @data
13
- end
14
-
15
- def data=(data)
16
- @data = data
17
- end
10
+ hstore_reader :data, :money, type_cast: :to_d, allow_nil: true
18
11
  end
19
12
 
20
13
  describe "hstore_reader" do
@@ -68,4 +61,14 @@ describe "hstore_reader" do
68
61
  @product.data = {"name" => "zomg"}
69
62
  assert_equal "ZOMG", @product.name
70
63
  end
64
+
65
+ it "returns nil for nil money" do
66
+ @product.data = {"money" => nil}
67
+ assert_equal nil, @product.money
68
+ end
69
+
70
+ it "returns typecasted money" do
71
+ @product.data = {"money" => "1.23"}
72
+ assert_equal BigDecimal.new("1.23"), @product.money
73
+ end
71
74
  end
@@ -7,6 +7,7 @@ class Product
7
7
  hstore_writer :data, :name, :name_2
8
8
  hstore_writer :data, :price, type_cast: :to_i
9
9
  hstore_writer :data, :cents, type_cast: ->(value) { (value.to_f * 100).to_i }
10
+ hstore_writer :data, :money, type_cast: :to_d, allow_nil: true
10
11
  end
11
12
 
12
13
  describe "hstore_writer" do
@@ -70,4 +71,14 @@ describe "hstore_writer" do
70
71
  @product.cents = "1"
71
72
  assert_equal({"cents" => 100}, @product.data)
72
73
  end
74
+
75
+ it "sets money using type_cast function" do
76
+ @product.money = "1.53"
77
+ assert_equal({"money" => BigDecimal.new("1.53")}, @product.data)
78
+ end
79
+
80
+ it "allows money to be nil function" do
81
+ @product.money = nil
82
+ assert_equal({"money" => nil}, @product.data)
83
+ end
73
84
  end
data/test/test_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
1
  require "minitest/autorun"
2
2
  require "active_support"
3
+ require "bigdecimal"
4
+ require "bigdecimal/util"
3
5
  require "hstore_attributes"
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.1
4
+ version: 1.0.2
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-07 00:00:00.000000000 Z
11
+ date: 2013-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -92,7 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
92
  version: '0'
93
93
  requirements: []
94
94
  rubyforge_project:
95
- rubygems_version: 2.0.0
95
+ rubygems_version: 2.0.2
96
96
  signing_key:
97
97
  specification_version: 4
98
98
  summary: Easy accessors for Hstore data.