hstore_accessor 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/hstore_accessor.gemspec +2 -1
- data/lib/hstore_accessor.rb +19 -11
- data/lib/hstore_accessor/version.rb +1 -1
- data/spec/hstore_accessor_spec.rb +9 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 220ae624d098a1da52d11b5a2f80968e1b8f4aeb
|
4
|
+
data.tar.gz: 686682a267560e4aa0e7b3c27e307449d5cf4446
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 58a6b38bb4d388e7d775cd2c0ed0e6cc268ff46981d12504db72e7a68a2f718d4f9abf2ef3179d73f523e88d1a421cf81a7db195982fbbe0f80e9dc88ea8ad20
|
7
|
+
data.tar.gz: a396a35279696b767cc5b463244ac1fd38a087dc2d9af5f07fdd8a31c106c26f134d049fa1a459cd638614afe6931485fed3601fe6bccb7b85895dae0564102e
|
data/hstore_accessor.gemspec
CHANGED
@@ -20,9 +20,10 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
spec.add_dependency "pg", ">= 0.14.1"
|
22
22
|
spec.add_dependency "activesupport", ">= 3.2.0"
|
23
|
-
|
23
|
+
|
24
24
|
spec.add_development_dependency "activerecord", ">= 4.0.0.rc1"
|
25
25
|
spec.add_development_dependency "bundler", "~> 1.3"
|
26
26
|
spec.add_development_dependency "rake"
|
27
27
|
spec.add_development_dependency "rspec"
|
28
|
+
spec.add_development_dependency "pry"
|
28
29
|
end
|
data/lib/hstore_accessor.rb
CHANGED
@@ -47,29 +47,37 @@ module HstoreAccessor
|
|
47
47
|
def hstore_accessor(hstore_attribute, fields)
|
48
48
|
fields.each do |key, type|
|
49
49
|
|
50
|
-
|
50
|
+
data_type = type
|
51
|
+
store_key = key
|
52
|
+
if type.is_a?(Hash)
|
53
|
+
type = type.with_indifferent_access
|
54
|
+
data_type = type[:data_type]
|
55
|
+
store_key = type[:store_key]
|
56
|
+
end
|
57
|
+
|
58
|
+
raise InvalidDataTypeError unless VALID_TYPES.include?(data_type)
|
51
59
|
|
52
60
|
define_method("#{key}=") do |value|
|
53
|
-
send("#{hstore_attribute}=", (send(hstore_attribute) || {}).merge(
|
61
|
+
send("#{hstore_attribute}=", (send(hstore_attribute) || {}).merge(store_key.to_s => serialize(data_type, value)))
|
54
62
|
send("#{hstore_attribute}_will_change!")
|
55
63
|
end
|
56
64
|
|
57
65
|
define_method(key) do
|
58
|
-
value = send(hstore_attribute) && send(hstore_attribute).with_indifferent_access[
|
59
|
-
deserialize(
|
66
|
+
value = send(hstore_attribute) && send(hstore_attribute).with_indifferent_access[store_key.to_s]
|
67
|
+
deserialize(data_type, value)
|
60
68
|
end
|
61
69
|
|
62
|
-
query_field = "#{hstore_attribute} -> '#{
|
70
|
+
query_field = "#{hstore_attribute} -> '#{store_key}'"
|
63
71
|
|
64
|
-
case
|
72
|
+
case data_type
|
65
73
|
when :string
|
66
74
|
send(:scope, "with_#{key}", -> value { where("#{query_field} = ?", value.to_s) })
|
67
75
|
when :integer, :float
|
68
|
-
send(:scope, "#{key}_lt", -> value { where("(#{query_field})::#{
|
69
|
-
send(:scope, "#{key}_lte", -> value { where("(#{query_field})::#{
|
70
|
-
send(:scope, "#{key}_eq", -> value { where("(#{query_field})::#{
|
71
|
-
send(:scope, "#{key}_gte", -> value { where("(#{query_field})::#{
|
72
|
-
send(:scope, "#{key}_gt", -> value { where("(#{query_field})::#{
|
76
|
+
send(:scope, "#{key}_lt", -> value { where("(#{query_field})::#{data_type} < ?", value.to_s) })
|
77
|
+
send(:scope, "#{key}_lte", -> value { where("(#{query_field})::#{data_type} <= ?", value.to_s) })
|
78
|
+
send(:scope, "#{key}_eq", -> value { where("(#{query_field})::#{data_type} = ?", value.to_s) })
|
79
|
+
send(:scope, "#{key}_gte", -> value { where("(#{query_field})::#{data_type} >= ?", value.to_s) })
|
80
|
+
send(:scope, "#{key}_gt", -> value { where("(#{query_field})::#{data_type} > ?", value.to_s) })
|
73
81
|
when :time
|
74
82
|
send(:scope, "#{key}_before", -> value { where("(#{query_field})::integer < ?", value.to_i) })
|
75
83
|
send(:scope, "#{key}_eq", -> value { where("(#{query_field})::integer = ?", value.to_i) })
|
@@ -4,7 +4,7 @@ require "active_support/all"
|
|
4
4
|
FIELDS = {
|
5
5
|
color: :string,
|
6
6
|
price: :integer,
|
7
|
-
weight: :float,
|
7
|
+
weight: { data_type: :float, store_key: "w" },
|
8
8
|
popular: :boolean,
|
9
9
|
build_timestamp: :time,
|
10
10
|
tags: :array,
|
@@ -42,6 +42,14 @@ describe HstoreAccessor do
|
|
42
42
|
end.to raise_error(HstoreAccessor::InvalidDataTypeError)
|
43
43
|
end
|
44
44
|
|
45
|
+
it "stores using the store_key if one is provided" do
|
46
|
+
product.weight = 38.5
|
47
|
+
product.save
|
48
|
+
product.reload
|
49
|
+
expect(product.options["w"]).to eq "38.5"
|
50
|
+
expect(product.weight).to eq 38.5
|
51
|
+
end
|
52
|
+
|
45
53
|
end
|
46
54
|
|
47
55
|
context "nil values" do
|
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
|
+
version: 0.4.0
|
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-08-03 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: pg
|
@@ -96,6 +96,20 @@ dependencies:
|
|
96
96
|
- - '>='
|
97
97
|
- !ruby/object:Gem::Version
|
98
98
|
version: '0'
|
99
|
+
- !ruby/object:Gem::Dependency
|
100
|
+
name: pry
|
101
|
+
requirement: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
type: :development
|
107
|
+
prerelease: false
|
108
|
+
version_requirements: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - '>='
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
99
113
|
description: Adds typed hstore backed fields to an ActiveRecord model.
|
100
114
|
email:
|
101
115
|
- joe@devmynd.com
|