hstore_accessor 0.5.3 → 0.5.4
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_accessor.rb +7 -4
- data/lib/hstore_accessor/version.rb +1 -1
- data/spec/hstore_accessor_spec.rb +45 -7
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7288a6f9802b78a2c213dfd478de0d5963bd0947
|
4
|
+
data.tar.gz: 1b73b39baa507fbd1bdfc9380ef825832466ab51
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c608a5b3e069c9e325257183ea5056c15060ca8e590a6f85626606feaa506a1f2f46b42243d8c2bf556d851c81cec202abd864f587cd9d855dd75405e1cfb00
|
7
|
+
data.tar.gz: cb83f6c2a8b239fe43fdf267c41e57ca6893ea80cae4d3a2fd8f4843005dc394cdb70f23ac67587dac2c91d30dd754e5411d35bc01275b93fa9ad28105bfa380
|
data/lib/hstore_accessor.rb
CHANGED
@@ -2,13 +2,14 @@ require "hstore_accessor/version"
|
|
2
2
|
require "hstore_accessor/time_helper"
|
3
3
|
require "active_support"
|
4
4
|
require "active_record"
|
5
|
+
require "bigdecimal"
|
5
6
|
|
6
7
|
module HstoreAccessor
|
7
8
|
extend ActiveSupport::Concern
|
8
9
|
|
9
10
|
InvalidDataTypeError = Class.new(StandardError)
|
10
11
|
|
11
|
-
VALID_TYPES = [:string, :integer, :float, :time, :boolean, :array, :hash, :date]
|
12
|
+
VALID_TYPES = [:string, :integer, :float, :time, :boolean, :array, :hash, :date, :decimal]
|
12
13
|
|
13
14
|
SEPARATOR = "||;||"
|
14
15
|
|
@@ -30,7 +31,8 @@ module HstoreAccessor
|
|
30
31
|
float: -> value { value.to_f },
|
31
32
|
time: -> value { Time.at(value.to_i) },
|
32
33
|
boolean: -> value { value == "true" },
|
33
|
-
date: -> value { (value && Date.parse(value)) || nil }
|
34
|
+
date: -> value { (value && Date.parse(value)) || nil },
|
35
|
+
decimal: -> value { BigDecimal.new(value) }
|
34
36
|
}
|
35
37
|
|
36
38
|
def serialize(type, value, serializer=nil)
|
@@ -49,7 +51,8 @@ module HstoreAccessor
|
|
49
51
|
return nil if value.nil?
|
50
52
|
column_class = ActiveRecord::ConnectionAdapters::Column
|
51
53
|
case type
|
52
|
-
when :string,:hash,:array
|
54
|
+
when :string,:hash,:array,
|
55
|
+
:decimal then value
|
53
56
|
when :integer then column_class.value_to_integer(value)
|
54
57
|
when :float then value.to_f
|
55
58
|
when :time then TimeHelper.string_to_time(value)
|
@@ -101,7 +104,7 @@ module HstoreAccessor
|
|
101
104
|
case data_type
|
102
105
|
when :string
|
103
106
|
send(:scope, "with_#{key}", -> value { where("#{query_field} = ?", value.to_s) })
|
104
|
-
when :integer, :float
|
107
|
+
when :integer, :float, :decimal
|
105
108
|
send(:scope, "#{key}_lt", -> value { where("(#{query_field})::#{data_type} < ?", value.to_s) })
|
106
109
|
send(:scope, "#{key}_lte", -> value { where("(#{query_field})::#{data_type} <= ?", value.to_s) })
|
107
110
|
send(:scope, "#{key}_eq", -> value { where("(#{query_field})::#{data_type} = ?", value.to_s) })
|
@@ -9,7 +9,8 @@ FIELDS = {
|
|
9
9
|
build_timestamp: :time,
|
10
10
|
tags: :array,
|
11
11
|
reviews: :hash,
|
12
|
-
released_at: :date
|
12
|
+
released_at: :date,
|
13
|
+
miles: :decimal
|
13
14
|
}
|
14
15
|
|
15
16
|
class Product < ActiveRecord::Base
|
@@ -68,16 +69,16 @@ describe HstoreAccessor do
|
|
68
69
|
let!(:timestamp) { Time.now }
|
69
70
|
let!(:datestamp) { Date.today }
|
70
71
|
let!(:product) { Product.new }
|
71
|
-
let!(:product_a) { Product.create(color: "green", price: 10, weight: 10.1, tags: ["tag1", "tag2", "tag3"], popular: true, build_timestamp: (timestamp - 10.days), released_at: (datestamp - 8.days)) }
|
72
|
+
let!(:product_a) { Product.create(color: "green", price: 10, weight: 10.1, tags: ["tag1", "tag2", "tag3"], popular: true, build_timestamp: (timestamp - 10.days), released_at: (datestamp - 8.days), miles: BigDecimal.new('9.133790001')) }
|
72
73
|
|
73
74
|
FIELDS.keys.each do |field|
|
74
|
-
it "
|
75
|
+
it "responds with nil when #{field} is not set" do
|
75
76
|
expect(product.send(field)).to be_nil
|
76
77
|
end
|
77
78
|
end
|
78
79
|
|
79
80
|
FIELDS.keys.each do |field|
|
80
|
-
it "
|
81
|
+
it "responds with nil when #{field} is set back to nil after being set initially" do
|
81
82
|
product_a.send("#{field}=", nil)
|
82
83
|
expect(product_a.send(field)).to be_nil
|
83
84
|
end
|
@@ -109,9 +110,9 @@ describe HstoreAccessor do
|
|
109
110
|
|
110
111
|
let!(:timestamp) { Time.now }
|
111
112
|
let!(:datestamp) { Date.today }
|
112
|
-
let!(:product_a) { Product.create(color: "green", price: 10, weight: 10.1, tags: ["tag1", "tag2", "tag3"], popular: true, build_timestamp: (timestamp - 10.days), released_at: (datestamp - 8.days)) }
|
113
|
-
let!(:product_b) { Product.create(color: "orange", price: 20, weight: 20.2, tags: ["tag2", "tag3", "tag4"], popular: false, build_timestamp: (timestamp - 5.days), released_at: (datestamp - 4.days)) }
|
114
|
-
let!(:product_c) { Product.create(color: "blue", price: 30, weight: 30.3, tags: ["tag3", "tag4", "tag5"], popular: true, build_timestamp: timestamp, released_at: datestamp) }
|
113
|
+
let!(:product_a) { Product.create(color: "green", price: 10, weight: 10.1, tags: ["tag1", "tag2", "tag3"], popular: true, build_timestamp: (timestamp - 10.days), released_at: (datestamp - 8.days), miles: BigDecimal.new('10.113379001')) }
|
114
|
+
let!(:product_b) { Product.create(color: "orange", price: 20, weight: 20.2, tags: ["tag2", "tag3", "tag4"], popular: false, build_timestamp: (timestamp - 5.days), released_at: (datestamp - 4.days), miles: BigDecimal.new('20.213379001')) }
|
115
|
+
let!(:product_c) { Product.create(color: "blue", price: 30, weight: 30.3, tags: ["tag3", "tag4", "tag5"], popular: true, build_timestamp: timestamp, released_at: datestamp, miles: BigDecimal.new('30.313379001')) }
|
115
116
|
|
116
117
|
context "for string fields support" do
|
117
118
|
|
@@ -169,6 +170,30 @@ describe HstoreAccessor do
|
|
169
170
|
|
170
171
|
end
|
171
172
|
|
173
|
+
context "for decimal fields support" do
|
174
|
+
|
175
|
+
it "less than" do
|
176
|
+
expect(Product.miles_lt(BigDecimal.new('10.55555')).to_a).to eq [product_a]
|
177
|
+
end
|
178
|
+
|
179
|
+
it "less than or equal" do
|
180
|
+
expect(Product.miles_lte(BigDecimal.new('20.213379001')).to_a).to eq [product_a, product_b]
|
181
|
+
end
|
182
|
+
|
183
|
+
it "equality" do
|
184
|
+
expect(Product.miles_eq(BigDecimal.new('10.113379001')).to_a).to eq [product_a]
|
185
|
+
end
|
186
|
+
|
187
|
+
it "greater than or equal" do
|
188
|
+
expect(Product.miles_gte(BigDecimal.new('20.213379001')).to_a).to eq [product_b, product_c]
|
189
|
+
end
|
190
|
+
|
191
|
+
it "greater than" do
|
192
|
+
expect(Product.miles_gt(BigDecimal.new('20.555555')).to_a).to eq [product_c]
|
193
|
+
end
|
194
|
+
|
195
|
+
end
|
196
|
+
|
172
197
|
context "for array fields support" do
|
173
198
|
|
174
199
|
it "equality" do
|
@@ -302,6 +327,15 @@ describe HstoreAccessor do
|
|
302
327
|
expect(product.released_at).to eq datestamp
|
303
328
|
end
|
304
329
|
|
330
|
+
it "correctly stores decimal values" do
|
331
|
+
decimal = BigDecimal.new('9.13370009001')
|
332
|
+
product.miles = decimal
|
333
|
+
product.save
|
334
|
+
product.reload
|
335
|
+
expect(product.miles.to_s).to eq decimal.to_s
|
336
|
+
expect(product.miles).to eq decimal
|
337
|
+
end
|
338
|
+
|
305
339
|
context "correctly stores boolean values" do
|
306
340
|
|
307
341
|
it "when string 'true' is passed" do
|
@@ -344,6 +378,10 @@ describe HstoreAccessor do
|
|
344
378
|
product.released_at = datestamp.to_s
|
345
379
|
expect(product.released_at).to eq datestamp
|
346
380
|
end
|
381
|
+
it "type casts decimal values" do
|
382
|
+
product.miles = '1.337900129339202'
|
383
|
+
expect(product.miles).to eq BigDecimal.new('1.337900129339202')
|
384
|
+
end
|
347
385
|
|
348
386
|
it "type casts boolean values" do
|
349
387
|
ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.each do |value|
|
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.
|
4
|
+
version: 0.5.4
|
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-02
|
13
|
+
date: 2014-03-02 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activesupport
|
@@ -150,7 +150,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
150
150
|
version: '0'
|
151
151
|
requirements: []
|
152
152
|
rubyforge_project:
|
153
|
-
rubygems_version: 2.2.
|
153
|
+
rubygems_version: 2.2.2
|
154
154
|
signing_key:
|
155
155
|
specification_version: 4
|
156
156
|
summary: Adds typed hstore backed fields to an ActiveRecord model.
|