freezer 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ ### v0.6.0
2
+
3
+ * Added support for different serialization strategies based on the column type
4
+
1
5
  ### v0.5.0
2
6
 
3
7
  * Initial release
@@ -1,4 +1,3 @@
1
- require 'freezer/serialization'
2
1
  require 'active_support/concern'
3
2
  require 'active_support/core_ext/hash/reverse_merge'
4
3
  require 'active_support/core_ext/string/inflections'
@@ -17,6 +16,14 @@ module Freezer
17
16
 
18
17
  klass = options[:class_name].camelize.constantize
19
18
  accessor_name = association_name.to_s.underscore
19
+ serializer = if self.columns_hash[options[:column_name].to_s].type == :hstore
20
+ require 'freezer/serialization/hstore'
21
+ ::Freezer::Serialization::HStore
22
+ else
23
+ require 'freezer/serialization/serialize'
24
+ self.serialize options[:column_name].to_sym, Hash
25
+ ::Freezer::Serialization::Serialize
26
+ end
20
27
 
21
28
  # Reader
22
29
  define_method(accessor_name) do
@@ -28,7 +35,7 @@ module Freezer
28
35
 
29
36
  # On cache miss, try to read from the raw accessor
30
37
  if hstore = read_attribute(options[:column_name])
31
- @freezer_cache[accessor_name] = Serialization.deserialize(options[:class_name], hstore, options[:slient])
38
+ @freezer_cache[accessor_name] = serializer.deserialize(options[:class_name], hstore, options[:slient])
32
39
  else
33
40
  @freezer_cache[accessor_name] = nil
34
41
  end
@@ -46,7 +53,7 @@ module Freezer
46
53
  @freezer_cache ||= {}
47
54
 
48
55
  if record
49
- write_attribute(options[:column_name], Serialization.serialize(record))
56
+ write_attribute(options[:column_name], serializer.serialize(record))
50
57
  else
51
58
  write_attribute(options[:column_name], nil)
52
59
  end
@@ -0,0 +1,66 @@
1
+ require 'freezer/frozen_record_proxy'
2
+ require 'active_record/connection_adapters/column'
3
+
4
+ module Freezer
5
+ module Serialization
6
+ module HStore
7
+ SERIALIZABLE_TYPES = [
8
+ :primary_key, :string, :text, :integer, :float, :decimal,
9
+ :datetime,:timestamp, :time, :date, :boolean
10
+ ].freeze
11
+
12
+ class << self
13
+ # TODO give each version its own module or something
14
+ def serialize(record)
15
+ # Start with the serialization format version string
16
+ hstore = {':sv'=>'1'}
17
+
18
+ columns = record.class.columns
19
+ attributes = record.attributes
20
+
21
+ columns.each do |column|
22
+ key = serialize_key(column.name, column.type)
23
+ value = serialize_value(column.type, attributes[column.name])
24
+ hstore[key] = value
25
+ end
26
+
27
+ hstore
28
+ end
29
+
30
+ def deserialize(klass, attributes, silent = false)
31
+ attributes = attributes.dup
32
+ sv = attributes.delete(':sv')
33
+ raise ArgumentError, "Unknown serialization format #{sv.to_s}" unless sv == '1'
34
+ attributes = attributes.inject({}) do |hash,(key,value)|
35
+ real_key, type = deserialize_key(key)
36
+ hash[real_key] = deserialize_value(type, value)
37
+ hash
38
+ end
39
+ FrozenRecordFactory.build(klass, attributes, silent)
40
+ end
41
+
42
+ def serialize_key(key, type)
43
+ "#{key.gsub(':','::')}:#{type}"
44
+ end
45
+
46
+ def deserialize_key(serialized)
47
+ m = serialized.match(/(.*):([^:]+)\z/)
48
+ [m[1].gsub('::',':'), m[2].to_sym]
49
+ end
50
+
51
+ def serialize_value(type, value)
52
+ raise ArgumentError, "Do not know how to serialize #{type.to_s}" unless SERIALIZABLE_TYPES.include? type
53
+ return nil if value.nil?
54
+ value.to_s
55
+ end
56
+
57
+ def deserialize_value(type, value)
58
+ raise ArgumentError, "Do not know how to deserialize #{type.to_s}" unless SERIALIZABLE_TYPES.include? type
59
+ return nil if value.nil?
60
+ # For now, we will let AR handle the casting for us
61
+ value.to_s
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,66 @@
1
+ require 'freezer/frozen_record_proxy'
2
+ require 'active_record/connection_adapters/column'
3
+
4
+ module Freezer
5
+ module Serialization
6
+ module HStore
7
+ SERIALIZABLE_TYPES = [
8
+ :primary_key, :string, :text, :integer, :float, :decimal,
9
+ :datetime,:timestamp, :time, :date, :boolean
10
+ ].freeze
11
+
12
+ class << self
13
+ # TODO give each version its own module or something
14
+ def serialize(record)
15
+ # Start with the serialization format version string
16
+ hstore = {':sv'=>'1'}
17
+
18
+ columns = record.class.columns
19
+ attributes = record.attributes
20
+
21
+ columns.each do |column|
22
+ key = serialize_key(column.name, column.type)
23
+ value = serialize_value(column.type, attributes[column.name])
24
+ hstore[key] = value
25
+ end
26
+
27
+ hstore
28
+ end
29
+
30
+ def deserialize(klass, attributes, silent = false)
31
+ attributes = attributes.dup
32
+ sv = attributes.delete(':sv')
33
+ raise ArgumentError, "Unknown serialization format #{sv.to_s}" unless sv == '1'
34
+ attributes = attributes.inject({}) do |hash,(key,value)|
35
+ real_key, type = deserialize_key(key)
36
+ hash[real_key] = deserialize_value(type, value)
37
+ hash
38
+ end
39
+ FrozenRecordFactory.build(klass, attributes, silent)
40
+ end
41
+
42
+ def serialize_key(key, type)
43
+ "#{key.gsub(':','::')}:#{type}"
44
+ end
45
+
46
+ def deserialize_key(serialized)
47
+ m = serialized.match(/(.*):([^:]+)\z/)
48
+ [m[1].gsub('::',':'), m[2].to_sym]
49
+ end
50
+
51
+ def serialize_value(type, value)
52
+ raise ArgumentError, "Do not know how to serialize #{type.to_s}" unless SERIALIZABLE_TYPES.include? type
53
+ return nil if value.nil?
54
+ value.to_s
55
+ end
56
+
57
+ def deserialize_value(type, value)
58
+ raise ArgumentError, "Do not know how to deserialize #{type.to_s}" unless SERIALIZABLE_TYPES.include? type
59
+ return nil if value.nil?
60
+ # For now, we will let AR handle the casting for us
61
+ value.to_s
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,17 @@
1
+ require 'freezer/frozen_record_proxy'
2
+
3
+ module Freezer
4
+ module Serialization
5
+ module Serialize
6
+ class << self
7
+ def serialize(record)
8
+ record.attributes.dup
9
+ end
10
+
11
+ def deserialize(klass, attributes, silent = false)
12
+ FrozenRecordFactory.build(klass, attributes.dup, silent)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: freezer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-07-25 00:00:00.000000000 Z
12
+ date: 2012-08-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -43,22 +43,6 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: 3.2.4
46
- - !ruby/object:Gem::Dependency
47
- name: activerecord-postgres-hstore
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ! '>='
52
- - !ruby/object:Gem::Version
53
- version: 0.4.0
54
- type: :runtime
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ! '>='
60
- - !ruby/object:Gem::Version
61
- version: 0.4.0
62
46
  description: ''
63
47
  email:
64
48
  - godfreykfc@gmail.com
@@ -68,7 +52,9 @@ extra_rdoc_files: []
68
52
  files:
69
53
  - lib/freezer/active_record_extensions.rb
70
54
  - lib/freezer/frozen_record_proxy.rb
71
- - lib/freezer/serialization.rb
55
+ - lib/freezer/hstore_serialization.rb
56
+ - lib/freezer/serialization/hstore.rb
57
+ - lib/freezer/serialization/serialize.rb
72
58
  - lib/freezer.rb
73
59
  - README.md
74
60
  - CHANGELOG.md
@@ -1,64 +0,0 @@
1
- require 'freezer/frozen_record_proxy'
2
- require 'active_record/connection_adapters/column'
3
-
4
- module Freezer
5
- module Serialization
6
- SERIALIZABLE_TYPES = [
7
- :primary_key, :string, :text, :integer, :float, :decimal,
8
- :datetime,:timestamp, :time, :date, :boolean
9
- ].freeze
10
-
11
- class << self
12
- # TODO give each version its own module or something
13
- def serialize(record)
14
- # Start with the serialization format version string
15
- hstore = {':sv'=>'1'}
16
-
17
- columns = record.class.columns
18
- attributes = record.attributes
19
-
20
- columns.each do |column|
21
- key = serialize_key(column.name, column.type)
22
- value = serialize_value(column.type, attributes[column.name])
23
- hstore[key] = value
24
- end
25
-
26
- hstore
27
- end
28
-
29
- def deserialize(klass, attributes, silent = false)
30
- attributes = attributes.dup
31
- sv = attributes.delete(':sv')
32
- raise ArgumentError, "Unknown serialization format #{sv.to_s}" unless sv == '1'
33
- attributes = attributes.inject({}) do |hash,(key,value)|
34
- real_key, type = deserialize_key(key)
35
- hash[real_key] = deserialize_value(type, value)
36
- hash
37
- end
38
- FrozenRecordFactory.build(klass, attributes, silent)
39
- end
40
-
41
- def serialize_key(key, type)
42
- "#{key.gsub(':','::')}:#{type}"
43
- end
44
-
45
- def deserialize_key(serialized)
46
- m = serialized.match(/(.*):([^:]+)\z/)
47
- [m[1].gsub('::',':'), m[2].to_sym]
48
- end
49
-
50
- def serialize_value(type, value)
51
- raise ArgumentError, "Do not know how to serialize #{type.to_s}" unless SERIALIZABLE_TYPES.include? type
52
- return nil if value.nil?
53
- value.to_s
54
- end
55
-
56
- def deserialize_value(type, value)
57
- raise ArgumentError, "Do not know how to deserialize #{type.to_s}" unless SERIALIZABLE_TYPES.include? type
58
- return nil if value.nil?
59
- # For now, we will let AR handle the casting for us
60
- value.to_s
61
- end
62
- end
63
- end
64
- end