hstore_accessor 0.5.1 → 0.5.2

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: 2753b17f6ee9e44bfba27f7b30e7cdaf4fdd4d2b
4
- data.tar.gz: f33add0bc32112703c01523a559107bb69fdd8ad
3
+ metadata.gz: c81705ab4bfeff9568f1f9e580411a1a8fb52387
4
+ data.tar.gz: bf60a484be88e5f63a5d08165bc76db186b05b33
5
5
  SHA512:
6
- metadata.gz: 109e4f9e17c588beedd863053c33ebd28d9cfb65b754fc56572a08a421b74be127d056249111f33264b1db9d2849f9aa18b5cdfe3abfc55b46ce2f3c517ea22c
7
- data.tar.gz: 670573a1d9d928f125bf7bd1a6abee48e070e7aa207b874a855c562cc4125ee9e57017b8a59a70859471653f794137599c0d2986773866a9b310e8a15f9e59e9
6
+ metadata.gz: 1a8cb7b16656ef6cf9e12677d1206a9b5663e3d6b97bac6267fba3bacef27e24ec675c06d8173eac0434ad14a128b9f73da0e9b2d8fa55c7fae3b84ed6aecb82
7
+ data.tar.gz: 819667a3a55d48918d2e6b79dd52a71bbf43c3ff91752f389d66c8a020e29f1feb58d114c31df69b46592dc0aeb9092c9996161171816bcc07ab1b399e6a4834
@@ -1,4 +1,5 @@
1
1
  require "hstore_accessor/version"
2
+ require "hstore_accessor/time_helper"
2
3
  require "active_support"
3
4
  require "active_record"
4
5
 
@@ -15,21 +16,21 @@ module HstoreAccessor
15
16
  DEFAULT_DESERIALIZER = DEFAULT_SERIALIZER
16
17
 
17
18
  SERIALIZERS = {
18
- :array => -> value { (value && value.join(SEPARATOR)) || nil },
19
- :hash => -> value { (value && value.to_json) || nil },
20
- :time => -> value { value.to_i },
21
- :boolean => -> value { (value.to_s == "true").to_s },
22
- :date => -> value { (value && value.to_s) || nil }
19
+ array: -> value { (value && value.join(SEPARATOR)) || nil },
20
+ hash: -> value { (value && value.to_json) || nil },
21
+ time: -> value { value.to_i },
22
+ boolean: -> value { (value.to_s == "true").to_s },
23
+ date: -> value { (value && value.to_s) || nil }
23
24
  }
24
25
 
25
26
  DESERIALIZERS = {
26
- :array => -> value { (value && value.split(SEPARATOR)) || nil },
27
- :hash => -> value { (value && JSON.parse(value)) || nil },
28
- :integer => -> value { value.to_i },
29
- :float => -> value { value.to_f },
30
- :time => -> value { Time.at(value.to_i) },
31
- :boolean => -> value { value == "true" },
32
- :date => -> value { (value && Date.parse(value)) || nil }
27
+ array: -> value { (value && value.split(SEPARATOR)) || nil },
28
+ hash: -> value { (value && JSON.parse(value)) || nil },
29
+ integer: -> value { value.to_i },
30
+ float: -> value { value.to_f },
31
+ time: -> value { Time.at(value.to_i) },
32
+ boolean: -> value { value == "true" },
33
+ date: -> value { (value && Date.parse(value)) || nil }
33
34
  }
34
35
 
35
36
  def serialize(type, value, serializer=nil)
@@ -44,6 +45,20 @@ module HstoreAccessor
44
45
  deserializer.call(value)
45
46
  end
46
47
 
48
+ def type_cast(type, value)
49
+ return nil if value.nil?
50
+ column_class = ActiveRecord::ConnectionAdapters::Column
51
+ case type
52
+ when :string,:hash,:array then value
53
+ when :integer then column_class.value_to_integer(value)
54
+ when :float then value.to_f
55
+ when :time then TimeHelper.string_to_time(value)
56
+ when :date then column_class.value_to_date(value)
57
+ when :boolean then column_class.value_to_boolean(value)
58
+ else value
59
+ end
60
+ end
61
+
47
62
  module ClassMethods
48
63
 
49
64
  def hstore_accessor(hstore_attribute, fields)
@@ -66,7 +81,7 @@ module HstoreAccessor
66
81
  end
67
82
 
68
83
  define_method("#{key}=") do |value|
69
- send("#{hstore_attribute}=", (send(hstore_attribute) || {}).merge(store_key.to_s => serialize(data_type, value)))
84
+ send("#{hstore_attribute}=", (send(hstore_attribute) || {}).merge(store_key.to_s => serialize(data_type, type_cast(type, value))))
70
85
  send("#{hstore_attribute}_will_change!")
71
86
  end
72
87
 
@@ -0,0 +1,31 @@
1
+ module HstoreAccessor
2
+ module TimeHelper
3
+
4
+ # There is a bug in ActiveRecord::ConnectionAdapters::Column#string_to_time
5
+ # which drops the timezone. This has been fixed, but not released.
6
+ # This method includes the fix. See: https://github.com/rails/rails/pull/12290
7
+
8
+ def self.string_to_time(string)
9
+ return string unless string.is_a?(String)
10
+ return nil if string.empty?
11
+
12
+ time_hash = Date._parse(string)
13
+ time_hash[:sec_fraction] = ActiveRecord::ConnectionAdapters::Column.send(:microseconds, time_hash)
14
+ (year, mon, mday, hour, min, sec, microsec, offset) = *time_hash.values_at(:year, :mon, :mday, :hour, :min, :sec, :sec_fraction, :offset)
15
+
16
+ # Treat 0000-00-00 00:00:00 as nil.
17
+ return nil if year.nil? || (year == 0 && mon == 0 && mday == 0)
18
+
19
+ if offset
20
+ time = Time.utc(year, mon, mday, hour, min, sec, microsec) rescue nil
21
+ return nil unless time
22
+
23
+ time -= offset
24
+ ActiveRecord::Base.default_timezone == :utc ? time : time.getlocal
25
+ else
26
+ Time.public_send(ActiveRecord::Base.default_timezone, year, mon, mday, hour, min, sec, microsec) rescue nil
27
+ end
28
+ end
29
+
30
+ end
31
+ end
@@ -1,3 +1,3 @@
1
1
  module HstoreAccessor
2
- VERSION = "0.5.1"
2
+ VERSION = "0.5.2"
3
3
  end
@@ -325,6 +325,40 @@ describe HstoreAccessor do
325
325
  product.color = "green"
326
326
  end
327
327
 
328
+ describe "type casting" do
329
+ it "type casts integer values" do
330
+ product.price = '468'
331
+ expect(product.price).to eq 468
332
+ end
333
+ it "type casts float values" do
334
+ product.weight = '93.45'
335
+ expect(product.weight).to eq 93.45
336
+ end
337
+ it "type casts time values" do
338
+ timestamp = Time.now - 10.days
339
+ product.build_timestamp = timestamp.to_s
340
+ expect(product.build_timestamp.to_i).to eq timestamp.to_i
341
+ end
342
+ it "type casts date values" do
343
+ datestamp = Date.today - 9.days
344
+ product.released_at = datestamp.to_s
345
+ expect(product.released_at).to eq datestamp
346
+ end
347
+
348
+ it "type casts boolean values" do
349
+ ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.each do |value|
350
+ product.popular = value
351
+ expect(product.popular).to be_true
352
+ end
353
+ ActiveRecord::ConnectionAdapters::Column::FALSE_VALUES.each do |value|
354
+ product.popular = value
355
+ expect(product.popular).to be_false
356
+ end
357
+ end
358
+
359
+
360
+ end
361
+
328
362
  end
329
363
 
330
364
  end
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.1
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joe Hirn
@@ -10,118 +10,118 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-12-16 00:00:00.000000000 Z
13
+ date: 2014-01-29 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: pg
17
17
  requirement: !ruby/object:Gem::Requirement
18
18
  requirements:
19
- - - '>='
19
+ - - ">="
20
20
  - !ruby/object:Gem::Version
21
21
  version: 0.14.1
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
- - - '>='
26
+ - - ">="
27
27
  - !ruby/object:Gem::Version
28
28
  version: 0.14.1
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: activesupport
31
31
  requirement: !ruby/object:Gem::Requirement
32
32
  requirements:
33
- - - '>='
33
+ - - ">="
34
34
  - !ruby/object:Gem::Version
35
35
  version: 3.2.0
36
36
  type: :runtime
37
37
  prerelease: false
38
38
  version_requirements: !ruby/object:Gem::Requirement
39
39
  requirements:
40
- - - '>='
40
+ - - ">="
41
41
  - !ruby/object:Gem::Version
42
42
  version: 3.2.0
43
43
  - !ruby/object:Gem::Dependency
44
44
  name: activerecord
45
45
  requirement: !ruby/object:Gem::Requirement
46
46
  requirements:
47
- - - '>='
47
+ - - ">="
48
48
  - !ruby/object:Gem::Version
49
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
56
  version: 4.0.0
57
57
  - !ruby/object:Gem::Dependency
58
58
  name: bundler
59
59
  requirement: !ruby/object:Gem::Requirement
60
60
  requirements:
61
- - - ~>
61
+ - - "~>"
62
62
  - !ruby/object:Gem::Version
63
63
  version: '1.3'
64
64
  type: :development
65
65
  prerelease: false
66
66
  version_requirements: !ruby/object:Gem::Requirement
67
67
  requirements:
68
- - - ~>
68
+ - - "~>"
69
69
  - !ruby/object:Gem::Version
70
70
  version: '1.3'
71
71
  - !ruby/object:Gem::Dependency
72
72
  name: rake
73
73
  requirement: !ruby/object:Gem::Requirement
74
74
  requirements:
75
- - - '>='
75
+ - - ">="
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
78
  type: :development
79
79
  prerelease: false
80
80
  version_requirements: !ruby/object:Gem::Requirement
81
81
  requirements:
82
- - - '>='
82
+ - - ">="
83
83
  - !ruby/object:Gem::Version
84
84
  version: '0'
85
85
  - !ruby/object:Gem::Dependency
86
86
  name: rspec
87
87
  requirement: !ruby/object:Gem::Requirement
88
88
  requirements:
89
- - - '>='
89
+ - - ">="
90
90
  - !ruby/object:Gem::Version
91
91
  version: '0'
92
92
  type: :development
93
93
  prerelease: false
94
94
  version_requirements: !ruby/object:Gem::Requirement
95
95
  requirements:
96
- - - '>='
96
+ - - ">="
97
97
  - !ruby/object:Gem::Version
98
98
  version: '0'
99
99
  - !ruby/object:Gem::Dependency
100
100
  name: pry
101
101
  requirement: !ruby/object:Gem::Requirement
102
102
  requirements:
103
- - - '>='
103
+ - - ">="
104
104
  - !ruby/object:Gem::Version
105
105
  version: '0'
106
106
  type: :development
107
107
  prerelease: false
108
108
  version_requirements: !ruby/object:Gem::Requirement
109
109
  requirements:
110
- - - '>='
110
+ - - ">="
111
111
  - !ruby/object:Gem::Version
112
112
  version: '0'
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: database_cleaner
115
115
  requirement: !ruby/object:Gem::Requirement
116
116
  requirements:
117
- - - '>='
117
+ - - ">="
118
118
  - !ruby/object:Gem::Version
119
119
  version: '0'
120
120
  type: :development
121
121
  prerelease: false
122
122
  version_requirements: !ruby/object:Gem::Requirement
123
123
  requirements:
124
- - - '>='
124
+ - - ">="
125
125
  - !ruby/object:Gem::Version
126
126
  version: '0'
127
127
  description: Adds typed hstore backed fields to an ActiveRecord model.
@@ -133,13 +133,14 @@ executables: []
133
133
  extensions: []
134
134
  extra_rdoc_files: []
135
135
  files:
136
- - .gitignore
136
+ - ".gitignore"
137
137
  - Gemfile
138
138
  - LICENSE.txt
139
139
  - README.md
140
140
  - Rakefile
141
141
  - hstore_accessor.gemspec
142
142
  - lib/hstore_accessor.rb
143
+ - lib/hstore_accessor/time_helper.rb
143
144
  - lib/hstore_accessor/version.rb
144
145
  - spec/hstore_accessor_spec.rb
145
146
  - spec/spec_helper.rb
@@ -153,17 +154,17 @@ require_paths:
153
154
  - lib
154
155
  required_ruby_version: !ruby/object:Gem::Requirement
155
156
  requirements:
156
- - - '>='
157
+ - - ">="
157
158
  - !ruby/object:Gem::Version
158
159
  version: '0'
159
160
  required_rubygems_version: !ruby/object:Gem::Requirement
160
161
  requirements:
161
- - - '>='
162
+ - - ">="
162
163
  - !ruby/object:Gem::Version
163
164
  version: '0'
164
165
  requirements: []
165
166
  rubyforge_project:
166
- rubygems_version: 2.0.3
167
+ rubygems_version: 2.2.0
167
168
  signing_key:
168
169
  specification_version: 4
169
170
  summary: Adds typed hstore backed fields to an ActiveRecord model.