freezer 0.0.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ ### v0.5.0
2
+
3
+ * Initial release
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2012 Godfrey Chan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,2 @@
1
+ freezer
2
+ =======
@@ -0,0 +1,6 @@
1
+ require 'freezer/active_record_extensions'
2
+ require 'active_record/base'
3
+
4
+ class ActiveRecord::Base
5
+ include Freezer::ActiveRecordExtensions
6
+ end
@@ -0,0 +1,62 @@
1
+ require 'freezer/serialization'
2
+ require 'active_support/concern'
3
+ require 'active_support/core_ext/hash/reverse_merge'
4
+ require 'active_support/core_ext/string/inflections'
5
+
6
+ module Freezer
7
+ module ActiveRecordExtensions
8
+ extend ActiveSupport::Concern
9
+
10
+ module ClassMethods
11
+ def has_one_frozen(association_name, options = {})
12
+ options.reverse_merge!({
13
+ class_name: association_name.to_s,
14
+ column_name: "frozen_#{association_name.to_s}",
15
+ slient: false
16
+ })
17
+
18
+ klass = options[:class_name].camelize.constantize
19
+ accessor_name = association_name.to_s.underscore
20
+
21
+ # Reader
22
+ define_method(accessor_name) do
23
+ @freezer_cache ||= {}
24
+
25
+ if @freezer_cache.key? accessor_name
26
+ return @freezer_cache[accessor_name]
27
+ end
28
+
29
+ # On cache miss, try to read from the raw accessor
30
+ if hstore = read_attribute(options[:column_name])
31
+ @freezer_cache[accessor_name] = Serialization.deserialize(options[:class_name], hstore, options[:slient])
32
+ else
33
+ @freezer_cache[accessor_name] = nil
34
+ end
35
+
36
+ @freezer_cache[accessor_name]
37
+ end
38
+
39
+ # Writer
40
+ define_method("#{accessor_name}=") do |record|
41
+ unless record.nil? || record.is_a?(klass)
42
+ message = "#{klass}(##{klass.object_id}) expected, got #{record.class}(##{record.class.object_id})"
43
+ raise ArgumentError, message
44
+ end
45
+
46
+ @freezer_cache ||= {}
47
+
48
+ if record
49
+ write_attribute(options[:column_name], Serialization.serialize(record))
50
+ else
51
+ write_attribute(options[:column_name], nil)
52
+ end
53
+
54
+ @freezer_cache.delete(accessor_name)
55
+
56
+ # Return the frozen copy of record by calling the reader
57
+ self.__send__(accessor_name.to_sym)
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,40 @@
1
+ require 'active_support/core_ext/string/inflections'
2
+ require 'active_record/errors'
3
+
4
+ module Freezer
5
+ module FrozenRecordFactory
6
+ class << self
7
+ def get_class(klass)
8
+ klass.is_a?(Class) ? klass : klass.to_s.camelize.constantize
9
+ end
10
+
11
+ def build(klass, attributes, slient = false)
12
+ record = get_class(klass).new attributes, without_protection: true
13
+ FrozenRecordProxy.new(record, slient)
14
+ end
15
+ end
16
+ end
17
+
18
+ class FrozenRecordProxy < BasicObject
19
+ [:==, :equal?, :!, :!=, :instance_eval, :instance_exec].each { |meth| undef_method(meth) }
20
+
21
+ def initialize(record, silent = false)
22
+ @silent = silent
23
+ @record = record
24
+ @record.readonly!
25
+ @record.freeze
26
+ end
27
+
28
+ private
29
+
30
+ def method_missing(method, *args, &block)
31
+ begin
32
+ @record.__send__(method, *args, &block)
33
+ rescue ::RuntimeError => e
34
+ @record.__send__(:raise, e) unless @silent && e.message == "can't modify frozen Hash"
35
+ rescue ::ActiveRecord::ReadOnlyRecord => e
36
+ @record.__send__(:raise, e) unless @silent
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,64 @@
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
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.0.0
4
+ version: 0.5.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,14 +9,70 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-20 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2012-07-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.4
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.4
30
+ - !ruby/object:Gem::Dependency
31
+ name: activerecord
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 3.2.4
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
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
14
62
  description: ''
15
- email: godfreykfc@gmail.com
63
+ email:
64
+ - godfreykfc@gmail.com
16
65
  executables: []
17
66
  extensions: []
18
67
  extra_rdoc_files: []
19
- files: []
68
+ files:
69
+ - lib/freezer/active_record_extensions.rb
70
+ - lib/freezer/frozen_record_proxy.rb
71
+ - lib/freezer/serialization.rb
72
+ - lib/freezer.rb
73
+ - README.md
74
+ - CHANGELOG.md
75
+ - LICENSE
20
76
  homepage: https://github.com/chancancode/freezer
21
77
  licenses: []
22
78
  post_install_message:
@@ -34,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
34
90
  requirements:
35
91
  - - ! '>='
36
92
  - !ruby/object:Gem::Version
37
- version: '0'
93
+ version: 1.3.6
38
94
  requirements: []
39
95
  rubyforge_project:
40
96
  rubygems_version: 1.8.24