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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 562c366519112f79fb774a0a0d95ee3a67eb102c
4
- data.tar.gz: b4a6a2aeb1095f3fa29bd92e349dc26442f9ca34
3
+ metadata.gz: 220ae624d098a1da52d11b5a2f80968e1b8f4aeb
4
+ data.tar.gz: 686682a267560e4aa0e7b3c27e307449d5cf4446
5
5
  SHA512:
6
- metadata.gz: 6576cf6f73c0f602b8f825d326d09e3dd1d03eab7e6e1f4ed298484a33f96d0754cbeca846cffb13c45562bd3fac09bfbd8596c781cecfcb77fb3d70ed699909
7
- data.tar.gz: 1bec8ccdd58531c62333ff9b9ab6b8276741b65b5406312052be78e178d02a7c250d803383b81a9aa14aa4fdab21bf8e9aa78637d2493b6b343689fef113275d
6
+ metadata.gz: 58a6b38bb4d388e7d775cd2c0ed0e6cc268ff46981d12504db72e7a68a2f718d4f9abf2ef3179d73f523e88d1a421cf81a7db195982fbbe0f80e9dc88ea8ad20
7
+ data.tar.gz: a396a35279696b767cc5b463244ac1fd38a087dc2d9af5f07fdd8a31c106c26f134d049fa1a459cd638614afe6931485fed3601fe6bccb7b85895dae0564102e
@@ -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
@@ -47,29 +47,37 @@ module HstoreAccessor
47
47
  def hstore_accessor(hstore_attribute, fields)
48
48
  fields.each do |key, type|
49
49
 
50
- raise InvalidDataTypeError unless VALID_TYPES.include?(type)
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(key.to_s => serialize(type, value)))
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[key.to_s]
59
- deserialize(type, value)
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} -> '#{key}'"
70
+ query_field = "#{hstore_attribute} -> '#{store_key}'"
63
71
 
64
- case type
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})::#{type} < ?", value.to_s) })
69
- send(:scope, "#{key}_lte", -> value { where("(#{query_field})::#{type} <= ?", value.to_s) })
70
- send(:scope, "#{key}_eq", -> value { where("(#{query_field})::#{type} = ?", value.to_s) })
71
- send(:scope, "#{key}_gte", -> value { where("(#{query_field})::#{type} >= ?", value.to_s) })
72
- send(:scope, "#{key}_gt", -> value { where("(#{query_field})::#{type} > ?", value.to_s) })
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) })
@@ -1,3 +1,3 @@
1
1
  module HstoreAccessor
2
- VERSION = "0.3.2"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -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.3.2
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-07-23 00:00:00.000000000 Z
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