hstore_accessor 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -0
- data/hstore_accessor.gemspec +1 -1
- data/lib/hstore_accessor.rb +9 -3
- data/lib/hstore_accessor/version.rb +1 -1
- data/spec/hstore_accessor_spec.rb +33 -5
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 762278330d698c66a6d2926e1595c539da4fbaf7
|
4
|
+
data.tar.gz: e4f38f9cb7e9b43384f2880a5b46860b63c3e0d9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 29530074370a555d44d91a2b60a5b7621935f4f062585e9c329a6ba508e762564f7548d2d6220a247b5c83779a6684545cc39668b48a0d44d905c5575ccce786
|
7
|
+
data.tar.gz: 7c9227822c95e61b23b900aebd2f93f76ba3302e095e202fd0e50376a2f229278b9bc603d5b0136211a5058b0e9a9ea9a82ed09d6d4384b5bf45713ad04404d2
|
data/README.md
CHANGED
@@ -63,6 +63,19 @@ p.color #=> "green"
|
|
63
63
|
p.tags #=> ["housewares", "kitchen"]
|
64
64
|
```
|
65
65
|
|
66
|
+
In order to reduce the storage overhead of hstore keys (especially when
|
67
|
+
indexed) you can specify an alternate key.
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
hstore_accessor :options,
|
71
|
+
color: { data_type: :string, store_key: "c" },
|
72
|
+
weight: { data_type: :integer, store_key: "w" }
|
73
|
+
```
|
74
|
+
|
75
|
+
In the above example you can continue to interact with the fields using
|
76
|
+
their full name but when saved to the database the field will be set
|
77
|
+
using the `store_key`.
|
78
|
+
|
66
79
|
### Scopes
|
67
80
|
|
68
81
|
The `hstore_accessor` macro also creates scopes for `string`, `integer`,
|
data/hstore_accessor.gemspec
CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.add_dependency "pg", ">= 0.14.1"
|
22
22
|
spec.add_dependency "activesupport", ">= 3.2.0"
|
23
23
|
|
24
|
-
spec.add_development_dependency "activerecord", ">= 4.0.0
|
24
|
+
spec.add_development_dependency "activerecord", ">= 4.0.0"
|
25
25
|
spec.add_development_dependency "bundler", "~> 1.3"
|
26
26
|
spec.add_development_dependency "rake"
|
27
27
|
spec.add_development_dependency "rspec"
|
data/lib/hstore_accessor.rb
CHANGED
@@ -7,7 +7,7 @@ module HstoreAccessor
|
|
7
7
|
|
8
8
|
InvalidDataTypeError = Class.new(StandardError)
|
9
9
|
|
10
|
-
VALID_TYPES = [:string, :integer, :float, :time, :boolean, :array, :hash]
|
10
|
+
VALID_TYPES = [:string, :integer, :float, :time, :boolean, :array, :hash, :date]
|
11
11
|
|
12
12
|
SEPARATOR = "||;||"
|
13
13
|
|
@@ -18,7 +18,8 @@ module HstoreAccessor
|
|
18
18
|
:array => -> value { (value && value.join(SEPARATOR)) || nil },
|
19
19
|
:hash => -> value { (value && value.to_json) || nil },
|
20
20
|
:time => -> value { value.to_i },
|
21
|
-
:boolean => -> value { (value == true).to_s }
|
21
|
+
:boolean => -> value { (value == true).to_s },
|
22
|
+
:date => -> value { (value && value.to_s) || nil }
|
22
23
|
}
|
23
24
|
|
24
25
|
DESERIALIZERS = {
|
@@ -27,7 +28,8 @@ module HstoreAccessor
|
|
27
28
|
:integer => -> value { value.to_i },
|
28
29
|
:float => -> value { value.to_f },
|
29
30
|
:time => -> value { Time.at(value.to_i) },
|
30
|
-
:boolean => -> value { value == "true" }
|
31
|
+
:boolean => -> value { value == "true" },
|
32
|
+
:date => -> value { (value && Date.parse(value)) || nil }
|
31
33
|
}
|
32
34
|
|
33
35
|
def serialize(type, value, serializer=nil)
|
@@ -82,6 +84,10 @@ module HstoreAccessor
|
|
82
84
|
send(:scope, "#{key}_before", -> value { where("(#{query_field})::integer < ?", value.to_i) })
|
83
85
|
send(:scope, "#{key}_eq", -> value { where("(#{query_field})::integer = ?", value.to_i) })
|
84
86
|
send(:scope, "#{key}_after", -> value { where("(#{query_field})::integer > ?", value.to_i) })
|
87
|
+
when :date
|
88
|
+
send(:scope, "#{key}_before", -> value { where("#{query_field} < ?", value.to_s) })
|
89
|
+
send(:scope, "#{key}_eq", -> value { where("#{query_field} = ?", value.to_s) })
|
90
|
+
send(:scope, "#{key}_after", -> value { where("#{query_field} > ?", value.to_s) })
|
85
91
|
when :boolean
|
86
92
|
send(:scope, "is_#{key}", -> { where("#{query_field} = 'true'") })
|
87
93
|
send(:scope, "not_#{key}", -> { where("#{query_field} = 'false'") })
|
@@ -8,7 +8,8 @@ FIELDS = {
|
|
8
8
|
popular: :boolean,
|
9
9
|
build_timestamp: :time,
|
10
10
|
tags: :array,
|
11
|
-
reviews: :hash
|
11
|
+
reviews: :hash,
|
12
|
+
released_at: :date
|
12
13
|
}
|
13
14
|
|
14
15
|
class Product < ActiveRecord::Base
|
@@ -54,8 +55,9 @@ describe HstoreAccessor do
|
|
54
55
|
|
55
56
|
context "nil values" do
|
56
57
|
let!(:timestamp) { Time.now }
|
58
|
+
let!(:datestamp) { Date.today }
|
57
59
|
let!(:product) { Product.new }
|
58
|
-
let!(:product_a) { Product.create(color: "green", price: 10, weight: 10.1, tags: ["tag1", "tag2", "tag3"], popular: true, build_timestamp: (timestamp - 10.days)) }
|
60
|
+
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)) }
|
59
61
|
|
60
62
|
FIELDS.keys.each do |field|
|
61
63
|
it "reponds with nil when #{field} is not set" do
|
@@ -74,9 +76,10 @@ describe HstoreAccessor do
|
|
74
76
|
describe "scopes" do
|
75
77
|
|
76
78
|
let!(:timestamp) { Time.now }
|
77
|
-
let!(:
|
78
|
-
let!(:
|
79
|
-
let!(:
|
79
|
+
let!(:datestamp) { Date.today }
|
80
|
+
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)) }
|
81
|
+
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)) }
|
82
|
+
let!(:product_c) { Product.create(color: "blue", price: 30, weight: 30.3, tags: ["tag3", "tag4", "tag5"], popular: true, build_timestamp: timestamp, released_at: datestamp) }
|
80
83
|
|
81
84
|
context "for string fields support" do
|
82
85
|
|
@@ -163,6 +166,22 @@ describe HstoreAccessor do
|
|
163
166
|
|
164
167
|
end
|
165
168
|
|
169
|
+
context "for date fields support" do
|
170
|
+
|
171
|
+
it "before" do
|
172
|
+
expect(Product.released_at_before(datestamp)).to eq [product_a, product_b]
|
173
|
+
end
|
174
|
+
|
175
|
+
it "equality" do
|
176
|
+
expect(Product.released_at_eq(datestamp)).to eq [product_c]
|
177
|
+
end
|
178
|
+
|
179
|
+
it "after" do
|
180
|
+
expect(Product.released_at_after(datestamp - 6.days)).to eq [product_b, product_c]
|
181
|
+
end
|
182
|
+
|
183
|
+
end
|
184
|
+
|
166
185
|
context "for boolean field support" do
|
167
186
|
|
168
187
|
it "true" do
|
@@ -242,6 +261,15 @@ describe HstoreAccessor do
|
|
242
261
|
expect(product.build_timestamp.to_i).to eq timestamp.to_i
|
243
262
|
end
|
244
263
|
|
264
|
+
it "correctly stores date values" do
|
265
|
+
datestamp = Date.today - 9.days
|
266
|
+
product.released_at = datestamp
|
267
|
+
product.save
|
268
|
+
product.reload
|
269
|
+
expect(product.released_at.to_s).to eq datestamp.to_s
|
270
|
+
expect(product.released_at).to eq datestamp
|
271
|
+
end
|
272
|
+
|
245
273
|
it "setters call the _will_change! method of the store attribute" do
|
246
274
|
product.should_receive(:options_will_change!)
|
247
275
|
product.color = "green"
|
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.4.
|
4
|
+
version: 0.4.1
|
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: 2013-
|
13
|
+
date: 2013-09-30 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: pg
|
@@ -46,14 +46,14 @@ dependencies:
|
|
46
46
|
requirements:
|
47
47
|
- - '>='
|
48
48
|
- !ruby/object:Gem::Version
|
49
|
-
version: 4.0.0
|
49
|
+
version: 4.0.0
|
50
50
|
type: :development
|
51
51
|
prerelease: false
|
52
52
|
version_requirements: !ruby/object:Gem::Requirement
|
53
53
|
requirements:
|
54
54
|
- - '>='
|
55
55
|
- !ruby/object:Gem::Version
|
56
|
-
version: 4.0.0
|
56
|
+
version: 4.0.0
|
57
57
|
- !ruby/object:Gem::Dependency
|
58
58
|
name: bundler
|
59
59
|
requirement: !ruby/object:Gem::Requirement
|
@@ -149,7 +149,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
149
149
|
version: '0'
|
150
150
|
requirements: []
|
151
151
|
rubyforge_project:
|
152
|
-
rubygems_version: 2.0.
|
152
|
+
rubygems_version: 2.0.3
|
153
153
|
signing_key:
|
154
154
|
specification_version: 4
|
155
155
|
summary: Adds typed hstore backed fields to an ActiveRecord model.
|