cerealize 0.8.0 → 0.8.1
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.
- data/CHANGES +21 -2
- data/{README → README.rdoc} +19 -17
- data/Rakefile +4 -2
- data/TODO +13 -3
- data/bench/simple.png +0 -0
- data/bench/simple.rb +99 -0
- data/cerealize.gemspec +53 -0
- data/lib/cerealize.rb +94 -54
- data/lib/cerealize/version.rb +1 -1
- data/slide/2010-04-24-cerealize.key/Contents/PkgInfo +1 -0
- data/slide/2010-04-24-cerealize.key/QuickLook/Thumbnail.jpg +0 -0
- data/slide/2010-04-24-cerealize.key/background.png +0 -0
- data/slide/2010-04-24-cerealize.key/cardinal-blue-black_200.png +0 -0
- data/slide/2010-04-24-cerealize.key/cereal-box.png +0 -0
- data/slide/2010-04-24-cerealize.key/color-profile +0 -0
- data/slide/2010-04-24-cerealize.key/index.apxl.gz +0 -0
- data/slide/2010-04-24-cerealize.key/simple.png +0 -0
- data/slide/2010-04-24-cerealize.key/thumbs/st0-11.tiff +0 -0
- data/slide/2010-04-24-cerealize.key/thumbs/st0-16.tiff +0 -0
- data/slide/2010-04-24-cerealize.key/thumbs/st0-19.tiff +0 -0
- data/slide/2010-04-24-cerealize.key/thumbs/st0-2.tiff +0 -0
- data/slide/2010-04-24-cerealize.key/thumbs/st0-22.tiff +0 -0
- data/slide/2010-04-24-cerealize.key/thumbs/st0-27.tiff +0 -0
- data/slide/2010-04-24-cerealize.key/thumbs/st0-29.tiff +0 -0
- data/slide/2010-04-24-cerealize.key/thumbs/st0-30.tiff +0 -0
- data/slide/2010-04-24-cerealize.key/thumbs/st0-32.tiff +0 -0
- data/slide/2010-04-24-cerealize.key/thumbs/st0-34.tiff +0 -0
- data/slide/2010-04-24-cerealize.key/thumbs/st0-38.tiff +0 -0
- data/slide/2010-04-24-cerealize.key/thumbs/st0-40.tiff +0 -0
- data/slide/2010-04-24-cerealize.key/thumbs/st0-46.tiff +0 -0
- data/slide/2010-04-24-cerealize.key/thumbs/st0-50.tiff +0 -0
- data/slide/2010-04-24-cerealize.key/thumbs/st0-51.tiff +0 -0
- data/slide/2010-04-24-cerealize.key/thumbs/st0-52.tiff +0 -0
- data/slide/2010-04-24-cerealize.key/thumbs/st0-54.tiff +0 -0
- data/slide/2010-04-24-cerealize.key/thumbs/st0-7.tiff +0 -0
- data/slide/2010-04-24-cerealize.key/thumbs/st0.tiff +0 -0
- data/slide/2010-04-24-cerealize.pdf +0 -0
- data/slide/cereal-box.ai +1619 -8
- data/slide/cereal-box.jpg +0 -0
- data/slide/cereal-box.png +0 -0
- data/test/helper.rb +3 -1
- data/test/helper_active_record.rb +16 -1
- data/test/test_basic.rb +35 -0
- metadata +104 -12
data/CHANGES
CHANGED
@@ -1,4 +1,23 @@
|
|
1
1
|
= cerealize changes history
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
== cerealize 0.8.1 -- 2010-07-16
|
4
|
+
* module_eval is a lot faster than define_method with a block,
|
5
|
+
I think it's all about closure.
|
6
|
+
|
7
|
+
* Now we're creating cerealize methods inside a saperate,
|
8
|
+
this makes it easier to override things, thus doing extension.
|
9
|
+
An example:
|
10
|
+
|
11
|
+
class Dog < ActiveRecord::Base
|
12
|
+
include Cerealize
|
13
|
+
|
14
|
+
cerealize :mood, String
|
15
|
+
def mood
|
16
|
+
"mood: #{super}"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
Previously, this would break things. (Please avoid alias_method_chain.)
|
21
|
+
|
22
|
+
== cerealize 0.8.0 -- 2010-04-18
|
23
|
+
* release early, release often
|
data/{README → README.rdoc}
RENAMED
@@ -1,32 +1,34 @@
|
|
1
|
-
= cerealize 0.8.
|
1
|
+
= cerealize 0.8.1
|
2
2
|
by Cardinal Blue ( http://cardinalblue.com )
|
3
3
|
|
4
4
|
== LINKS:
|
5
5
|
|
6
|
-
* github[http://github.com/cardinalblue/cerealize]
|
7
|
-
* rubygems[http://rubygems.org/gems/cerealize]
|
6
|
+
* {github}[http://github.com/cardinalblue/cerealize]
|
7
|
+
* {rubygems}[http://rubygems.org/gems/cerealize]
|
8
|
+
* {rdoc}[http://rdoc.info/projects/cardinalblue/cerealize]
|
8
9
|
|
9
10
|
== DESCRIPTION:
|
10
11
|
|
11
|
-
|
12
|
-
|
12
|
+
Serialize out of the Cerealize Box
|
13
|
+
- a drop-in replacement for ActiveRecord's serialize
|
13
14
|
|
14
|
-
|
15
|
-
|
15
|
+
It can auto transcode old encoding (yaml if you're using AR's serialize),
|
16
|
+
to new encoding (marshal, json, you name it) without any migration.
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
Current supported encoding:
|
19
|
+
1. YAML
|
20
|
+
2. Marshal
|
21
|
+
3. JSON (planned)
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
23
|
+
Current supported ORM:
|
24
|
+
1. ActiveRecord (tested with 2.3.5)
|
25
|
+
2. DataMapper (planned)
|
25
26
|
|
26
27
|
== SYNOPSIS:
|
27
28
|
|
28
29
|
require 'cerealize'
|
29
30
|
class User < ActiveRecord::Base
|
31
|
+
include Cerealize
|
30
32
|
# when force_encoding set to false (this is default), cerealize is
|
31
33
|
# smart enough to transcode old encoding (e.g. yaml) to new encoding
|
32
34
|
# (i.e. marshal)
|
@@ -40,9 +42,9 @@ by Cardinal Blue ( http://cardinalblue.com )
|
|
40
42
|
|
41
43
|
== INSTALL:
|
42
44
|
|
43
|
-
> gem install cerealize
|
44
|
-
or
|
45
|
-
> script/plugin install git://github.com/cardinalblue/cerealize.git
|
45
|
+
> gem install cerealize
|
46
|
+
# or if you want rails plugin and bleeding edge
|
47
|
+
> script/plugin install git://github.com/cardinalblue/cerealize.git
|
46
48
|
|
47
49
|
== LICENSE:
|
48
50
|
|
data/Rakefile
CHANGED
@@ -17,16 +17,18 @@ Bones{
|
|
17
17
|
|
18
18
|
depend_on 'activerecord', :version => '>=2.3.5'
|
19
19
|
# depend_on 'activerecord', :development => true, :version => '>=2.3.5'
|
20
|
+
depend_on 'sqlite3-ruby', :development => true
|
20
21
|
|
21
22
|
name proj
|
22
23
|
url "http://github.com/cardinalblue/#{proj}"
|
23
|
-
authors 'Cardinal Blue'
|
24
|
+
authors ['Cardinal Blue', 'Lin Jen-Shin (aka godfat 真常)', 'Jaime Cham']
|
24
25
|
email 'dev (XD) cardinalblue.com'
|
25
26
|
|
26
27
|
history_file 'CHANGES'
|
27
|
-
readme_file 'README'
|
28
|
+
readme_file 'README.rdoc'
|
28
29
|
ignore_file '.gitignore'
|
29
30
|
rdoc.include ['\w+']
|
31
|
+
rdoc.exclude ['test', 'doc', 'Rakefile']
|
30
32
|
}
|
31
33
|
|
32
34
|
CLEAN.include Dir['**/*.rbc']
|
data/TODO
CHANGED
@@ -1,7 +1,17 @@
|
|
1
1
|
= cerealize todo list
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
= ?
|
4
|
+
|
5
|
+
* cleanup test cases...
|
6
|
+
|
7
|
+
= 1.1+
|
8
|
+
|
9
|
+
* json support
|
10
|
+
|
11
|
+
= 1.2+
|
12
|
+
|
5
13
|
* extract base64 decorator, then we can use pure marshal
|
14
|
+
|
15
|
+
= 1.3+
|
16
|
+
|
6
17
|
* datamapper support
|
7
|
-
* json support
|
data/bench/simple.png
ADDED
Binary file
|
data/bench/simple.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
|
2
|
+
require 'benchmark'
|
3
|
+
|
4
|
+
require 'active_record'
|
5
|
+
require 'cerealize'
|
6
|
+
|
7
|
+
ActiveRecord::Base.establish_connection(
|
8
|
+
:adapter => 'sqlite3',
|
9
|
+
:database => ':memory:'
|
10
|
+
)
|
11
|
+
|
12
|
+
[:hats, :cats].each{ |table|
|
13
|
+
ActiveRecord::Base.connection.create_table table, :force => true do |t|
|
14
|
+
t.text :box
|
15
|
+
end
|
16
|
+
}
|
17
|
+
|
18
|
+
class Hat < ActiveRecord::Base
|
19
|
+
serialize :box, Hash
|
20
|
+
end
|
21
|
+
|
22
|
+
class Cat < ActiveRecord::Base
|
23
|
+
include Cerealize
|
24
|
+
cerealize :box, Hash
|
25
|
+
end
|
26
|
+
|
27
|
+
def simple klass
|
28
|
+
box = Hash[(0..300).zip(301..600)]
|
29
|
+
mox = box.invert
|
30
|
+
klass.find(klass.create(:box => box).id).
|
31
|
+
update_attributes(:box => mox)
|
32
|
+
end
|
33
|
+
|
34
|
+
Times = 100
|
35
|
+
|
36
|
+
Benchmark.bmbm{ |bm|
|
37
|
+
bm.report('serialize (build in ActiveRecord)') do
|
38
|
+
Times.times{ simple(Hat) }
|
39
|
+
end
|
40
|
+
|
41
|
+
bm.report('cerealize') do
|
42
|
+
Times.times{ simple(Cat) }
|
43
|
+
end
|
44
|
+
}
|
45
|
+
|
46
|
+
__END__
|
47
|
+
> system_profiler -detailLevel mini | grep -A 15 'Hardware Overview:'
|
48
|
+
Hardware Overview:
|
49
|
+
|
50
|
+
Model Name: MacBook
|
51
|
+
Model Identifier: MacBook2,1
|
52
|
+
Processor Name: Intel Core 2 Duo
|
53
|
+
Processor Speed: 2.16 GHz
|
54
|
+
Number Of Processors: 1
|
55
|
+
Total Number Of Cores: 2
|
56
|
+
L2 Cache: 4 MB
|
57
|
+
Memory: 3 GB
|
58
|
+
Bus Speed: 667 MHz
|
59
|
+
Boot ROM Version: MB21.00A5.B07
|
60
|
+
SMC Version (system): 1.17f0
|
61
|
+
Sudden Motion Sensor:
|
62
|
+
State: Enabled
|
63
|
+
|
64
|
+
> uname -a
|
65
|
+
Darwin godfat.local 10.3.0 Darwin Kernel Version 10.3.0: Fri Feb 26 11:58:09 PST 2010; root:xnu-1504.3.12~1/RELEASE_I386 i386 i386
|
66
|
+
|
67
|
+
> ruby -v
|
68
|
+
ruby 1.9.1p378 (2010-01-10 revision 26273) [i386-darwin10]
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
> ruby -I lib bench/simple.rb
|
75
|
+
Rehearsal ---------------------------------------------------------------------
|
76
|
+
serialize (build in ActiveRecord) 4.050000 0.080000 4.130000 ( 4.459223)
|
77
|
+
cerealize 0.810000 0.010000 0.820000 ( 1.119697)
|
78
|
+
------------------------------------------------------------ total: 4.950000sec
|
79
|
+
|
80
|
+
user system total real
|
81
|
+
serialize (build in ActiveRecord) 3.870000 0.070000 3.940000 ( 4.299835)
|
82
|
+
cerealize 0.690000 0.010000 0.700000 ( 0.733006)
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
|
88
|
+
http://chart.apis.google.com/chart?cht=bvg&chs=350x650&chd=t:85.9967,14.66012&chxl=0:|serialize|cerealize&chxt=x,y&chbh=150&chxr=1,0,5&chma=25
|
89
|
+
|
90
|
+
http://code.google.com/apis/chart/docs/chart_playground.html
|
91
|
+
|
92
|
+
cht=bvg
|
93
|
+
chs=350x650
|
94
|
+
chd=t:85.9967,14.66012
|
95
|
+
chxl=0:|serialize|cerealize
|
96
|
+
chxt=x,y
|
97
|
+
chbh=150
|
98
|
+
chxr=1,0,5
|
99
|
+
chma=25
|
data/cerealize.gemspec
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{cerealize}
|
5
|
+
s.version = "0.8.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Cardinal Blue", "Lin Jen-Shin (aka godfat 真常)", "Jaime Cham"]
|
9
|
+
s.date = %q{2010-07-16}
|
10
|
+
s.description = %q{ Serialize out of the Cerealize Box
|
11
|
+
- a drop-in replacement for ActiveRecord's serialize
|
12
|
+
|
13
|
+
It can auto transcode old encoding (yaml if you're using AR's serialize),
|
14
|
+
to new encoding (marshal, json, you name it) without any migration.
|
15
|
+
|
16
|
+
Current supported encoding:
|
17
|
+
1. YAML
|
18
|
+
2. Marshal
|
19
|
+
3. JSON (planned)
|
20
|
+
|
21
|
+
Current supported ORM:
|
22
|
+
1. ActiveRecord (tested with 2.3.5)
|
23
|
+
2. DataMapper (planned)}
|
24
|
+
s.email = %q{dev (XD) cardinalblue.com}
|
25
|
+
s.extra_rdoc_files = ["CHANGES", "LICENSE", "TODO", "bench/simple.png", "cerealize.gemspec", "slide/2010-04-24-cerealize.key/Contents/PkgInfo", "slide/2010-04-24-cerealize.key/QuickLook/Thumbnail.jpg", "slide/2010-04-24-cerealize.key/background.png", "slide/2010-04-24-cerealize.key/cardinal-blue-black_200.png", "slide/2010-04-24-cerealize.key/cereal-box.png", "slide/2010-04-24-cerealize.key/color-profile", "slide/2010-04-24-cerealize.key/index.apxl.gz", "slide/2010-04-24-cerealize.key/simple.png", "slide/2010-04-24-cerealize.key/thumbs/st0-11.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-16.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-19.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-2.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-22.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-27.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-29.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-30.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-32.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-34.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-38.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-40.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-46.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-50.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-51.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-52.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-54.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-7.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0.tiff", "slide/2010-04-24-cerealize.pdf", "slide/cereal-box.ai", "slide/cereal-box.jpg", "slide/cereal-box.png"]
|
26
|
+
s.files = ["CHANGES", "LICENSE", "README.rdoc", "Rakefile", "TODO", "bench/simple.png", "bench/simple.rb", "cerealize.gemspec", "init.rb", "lib/cerealize.rb", "lib/cerealize/codec/marshal.rb", "lib/cerealize/codec/yaml.rb", "lib/cerealize/version.rb", "slide/2010-04-24-cerealize.key/Contents/PkgInfo", "slide/2010-04-24-cerealize.key/QuickLook/Thumbnail.jpg", "slide/2010-04-24-cerealize.key/background.png", "slide/2010-04-24-cerealize.key/cardinal-blue-black_200.png", "slide/2010-04-24-cerealize.key/cereal-box.png", "slide/2010-04-24-cerealize.key/color-profile", "slide/2010-04-24-cerealize.key/index.apxl.gz", "slide/2010-04-24-cerealize.key/simple.png", "slide/2010-04-24-cerealize.key/thumbs/st0-11.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-16.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-19.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-2.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-22.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-27.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-29.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-30.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-32.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-34.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-38.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-40.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-46.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-50.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-51.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-52.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-54.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0-7.tiff", "slide/2010-04-24-cerealize.key/thumbs/st0.tiff", "slide/2010-04-24-cerealize.pdf", "slide/cereal-box.ai", "slide/cereal-box.jpg", "slide/cereal-box.png", "test/helper.rb", "test/helper_active_record.rb", "test/stub.rb", "test/test_all_codec.rb", "test/test_basic.rb", "test/test_transcode.rb"]
|
27
|
+
s.homepage = %q{http://github.com/cardinalblue/cerealize}
|
28
|
+
s.rdoc_options = ["--main", "README.rdoc"]
|
29
|
+
s.require_paths = ["lib"]
|
30
|
+
s.rubyforge_project = %q{cerealize}
|
31
|
+
s.rubygems_version = %q{1.3.7}
|
32
|
+
s.summary = %q{Serialize out of the Cerealize Box - a drop-in replacement for ActiveRecord's serialize It can auto transcode old encoding (yaml if you're using AR's serialize), to new encoding (marshal, json, you name it) without any migration}
|
33
|
+
s.test_files = ["test/test_all_codec.rb", "test/test_basic.rb", "test/test_transcode.rb"]
|
34
|
+
|
35
|
+
if s.respond_to? :specification_version then
|
36
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
37
|
+
s.specification_version = 3
|
38
|
+
|
39
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
40
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 2.3.5"])
|
41
|
+
s.add_development_dependency(%q<sqlite3-ruby>, [">= 1.3.1"])
|
42
|
+
s.add_development_dependency(%q<bones>, [">= 3.4.7"])
|
43
|
+
else
|
44
|
+
s.add_dependency(%q<activerecord>, [">= 2.3.5"])
|
45
|
+
s.add_dependency(%q<sqlite3-ruby>, [">= 1.3.1"])
|
46
|
+
s.add_dependency(%q<bones>, [">= 3.4.7"])
|
47
|
+
end
|
48
|
+
else
|
49
|
+
s.add_dependency(%q<activerecord>, [">= 2.3.5"])
|
50
|
+
s.add_dependency(%q<sqlite3-ruby>, [">= 1.3.1"])
|
51
|
+
s.add_dependency(%q<bones>, [">= 3.4.7"])
|
52
|
+
end
|
53
|
+
end
|
data/lib/cerealize.rb
CHANGED
@@ -17,20 +17,19 @@ module Cerealize
|
|
17
17
|
#
|
18
18
|
|
19
19
|
def self.included(base)
|
20
|
-
base.send :extend,
|
20
|
+
base.send( :extend, ClassMethods)
|
21
|
+
base.send(:include, InstanceMethods)
|
21
22
|
end
|
22
23
|
|
23
24
|
module_function
|
24
25
|
def codecs
|
25
|
-
@codecs ||=
|
26
|
-
|
26
|
+
@codecs ||= codec_names.map{ |codec_name|
|
27
|
+
codec_get(codec_name)
|
27
28
|
}
|
28
29
|
end
|
29
30
|
|
30
31
|
def codec_names
|
31
|
-
@codec_names ||=
|
32
|
-
codec.to_s.sub(/(\w+::)+/, '').downcase.to_sym
|
33
|
-
}
|
32
|
+
@codec_names ||= [:yaml, :marshal]
|
34
33
|
end
|
35
34
|
|
36
35
|
def codec_detect(str)
|
@@ -59,13 +58,56 @@ module Cerealize
|
|
59
58
|
end
|
60
59
|
end
|
61
60
|
|
61
|
+
module InstanceMethods
|
62
|
+
def cerealize_decode property, value
|
63
|
+
opt = self.class.cerealize_option[property]
|
64
|
+
Cerealize.decode( value, opt[:force_encoding] && opt[:codec] )
|
65
|
+
end
|
66
|
+
|
67
|
+
def cerealize_encode property, value
|
68
|
+
opt = self.class.cerealize_option[property]
|
69
|
+
Cerealize.encode( value, opt[:codec] )
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
62
73
|
module ClassMethods
|
74
|
+
def cerealize_option
|
75
|
+
@cerealize_option ||= {}
|
76
|
+
end
|
63
77
|
|
64
|
-
def
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
78
|
+
def cerealize_update_codec_cache
|
79
|
+
cerealize_option.each{ |(property, opt)|
|
80
|
+
opt.merge!(:codec => Cerealize.codec_get(opt[:encoding]))
|
81
|
+
}
|
82
|
+
end
|
83
|
+
|
84
|
+
def cerealize property, klass=nil, opt={}
|
85
|
+
opt[:encoding] ||= :marshal
|
86
|
+
cerealize_option[property] =
|
87
|
+
opt.merge(:class => klass,
|
88
|
+
:codec => Cerealize.codec_get(opt[:encoding]))
|
89
|
+
|
90
|
+
field_orig = "#{property}_orig"
|
91
|
+
field_cache = "#{property}_cache"
|
92
|
+
|
93
|
+
attr_accessor field_orig
|
94
|
+
private field_orig, "#{field_orig}="
|
95
|
+
|
96
|
+
mod = if const_defined?('CerealizeMethods')
|
97
|
+
const_get('CerealizeMethods')
|
98
|
+
else
|
99
|
+
const_set('CerealizeMethods', Module.new)
|
100
|
+
end
|
101
|
+
|
102
|
+
mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
103
|
+
def #{field_cache}
|
104
|
+
@#{property}
|
105
|
+
end
|
106
|
+
|
107
|
+
def #{field_cache}=(new_value)
|
108
|
+
@#{property} = new_value
|
109
|
+
end
|
110
|
+
RUBY
|
69
111
|
|
70
112
|
# Invariants:
|
71
113
|
# - instance_variable_defined?(field_cache) IFF the READER or WRITER has been called
|
@@ -74,61 +116,59 @@ module Cerealize
|
|
74
116
|
|
75
117
|
# READER method
|
76
118
|
#
|
77
|
-
|
78
|
-
|
119
|
+
mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
120
|
+
def #{property}
|
121
|
+
# Return cached
|
122
|
+
return #{field_cache} if #{field_cache}
|
79
123
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
# Save property if not already saved
|
84
|
-
if !instance_variable_defined?(field_pre)
|
85
|
-
instance_variable_set(field_pre, read_attribute(property))
|
86
|
-
end
|
124
|
+
# No assignment yet, save property if not already saved
|
125
|
+
self.#{field_orig}= self[:#{property}] if !#{field_orig}
|
87
126
|
|
88
127
|
# Set cached from pre
|
89
|
-
|
90
|
-
force_encoding && codec )
|
91
|
-
raise ActiveRecord::SerializationTypeMismatch, "expected #{klass}, got #{v.class}" \
|
92
|
-
if klass && !v.nil? && !v.kind_of?(klass)
|
93
|
-
instance_variable_set(field_cache, v)
|
94
|
-
end
|
128
|
+
value = cerealize_decode(:#{property}, #{field_orig})
|
95
129
|
|
96
|
-
|
97
|
-
|
98
|
-
|
130
|
+
raise ActiveRecord::SerializationTypeMismatch, "expected #{klass}, got \#{value.class}" \\
|
131
|
+
if #{klass.inspect} && !value.nil? && !value.kind_of?(#{klass})
|
132
|
+
|
133
|
+
self.#{field_cache} = value
|
134
|
+
end
|
135
|
+
RUBY
|
99
136
|
|
100
137
|
# WRITER method
|
101
138
|
#
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
139
|
+
mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
140
|
+
def #{property}=(value)
|
141
|
+
#{property}_will_change! if #{field_cache} != value
|
142
|
+
self.#{field_cache} = value
|
143
|
+
end
|
144
|
+
RUBY
|
107
145
|
|
108
146
|
# Callback for before_save
|
109
147
|
#
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
write_attribute(property, v_enc)
|
148
|
+
mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
149
|
+
def #{property}_update_if_dirty
|
150
|
+
# See if we have a new cur value
|
151
|
+
if #{field_cache}
|
152
|
+
value = #{field_cache}
|
153
|
+
value_enc = cerealize_encode(:#{property}, value)
|
154
|
+
|
155
|
+
# See if no orig at all (i.e. it was written to before
|
156
|
+
# being read), or if different. When comparing, compare
|
157
|
+
# both marshalized string, and Object ==.
|
158
|
+
#
|
159
|
+
if !#{field_orig} ||
|
160
|
+
(value_enc != #{field_orig} &&
|
161
|
+
value != cerealize_decode(:#{property}, #{field_orig}))
|
162
|
+
self[:#{property}] = value_enc
|
163
|
+
end
|
127
164
|
end
|
165
|
+
|
166
|
+
self.#{field_orig} = nil
|
167
|
+
self.#{field_cache} = nil
|
128
168
|
end
|
129
|
-
|
130
|
-
|
131
|
-
|
169
|
+
RUBY
|
170
|
+
|
171
|
+
include mod unless self < mod
|
132
172
|
before_save("#{property}_update_if_dirty")
|
133
173
|
end
|
134
174
|
end
|