typed_serialize 0.1.1 → 1.0.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.
- data/.gitignore +4 -0
- data/Gemfile +3 -0
- data/README.rdoc +40 -4
- data/Rakefile +5 -19
- data/lib/typed_serialize.rb +28 -5
- data/lib/typed_serialize/version.rb +3 -0
- data/spec/schema.rb +19 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/typed_serialize/typed_serialize_spec.rb +35 -0
- data/typed_serialize.gemspec +15 -24
- metadata +83 -32
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.rdoc
CHANGED
@@ -10,17 +10,48 @@ type. This is especially nice for serialized hashes and new models.
|
|
10
10
|
end
|
11
11
|
|
12
12
|
User.new.settings # => {}
|
13
|
-
|
13
|
+
|
14
14
|
u = User.new
|
15
15
|
u.settings[:theme] = 1
|
16
16
|
u.settings # => { :theme => 1 }
|
17
|
-
|
17
|
+
|
18
|
+
== Define serialized attributes
|
19
|
+
|
20
|
+
=== Avalaible only for Hash columns!!!
|
21
|
+
|
22
|
+
class User < ActiveRecord::Base
|
23
|
+
typed_serialize :settings, Hash
|
24
|
+
|
25
|
+
serialized_accessor :settings, :photo, :city, :nickname
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
u = User.new
|
30
|
+
u.photo = '/home/photos/me.png'
|
31
|
+
u.photo
|
32
|
+
=> '/home/photos/me.png'
|
33
|
+
u.settings
|
34
|
+
=>{:photo=>'/home/photos/me.png'}
|
35
|
+
|
36
|
+
or even better
|
37
|
+
|
38
|
+
class User < ActiveRecord::Base
|
39
|
+
typed_serialize :settings, Hash, :photo, :city, :nickname
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
u = User.new(:city=>'NY')
|
44
|
+
u.city
|
45
|
+
=> 'NY'
|
46
|
+
|
47
|
+
Also avalaible <b>serialized_reader</b> and <b>serialized_writer</b>.
|
48
|
+
|
18
49
|
== Install
|
19
50
|
|
20
51
|
As a Rails plugin:
|
21
52
|
|
22
|
-
./script/plugin install git://github.com/jqr/typed_serialize.git
|
23
|
-
|
53
|
+
./script/plugin install git://github.com/jqr/typed_serialize.git
|
54
|
+
|
24
55
|
Prefer gems? Add this to your environment.rb and run the following command.
|
25
56
|
|
26
57
|
config.gem 'typed_serialize'
|
@@ -32,5 +63,10 @@ Prefer gems? Add this to your environment.rb and run the following command.
|
|
32
63
|
|
33
64
|
http://rdoc.info/projects/jqr/typed_serialize
|
34
65
|
|
66
|
+
== Contributors
|
67
|
+
|
68
|
+
* Toby Matejovsky (toby.matejovsky@gmail.com)
|
69
|
+
* Sergey Pchelincev (jalkoby91@gmail.com)
|
70
|
+
|
35
71
|
Homepage:: http://github.com/jqr/typed_serialize
|
36
72
|
License:: Copyright (c) 2008 Elijah Miller <mailto:elijah.miller@gmail.com>, released under the MIT license
|
data/Rakefile
CHANGED
@@ -1,22 +1,8 @@
|
|
1
|
-
require '
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rspec/core/rake_task'
|
2
3
|
|
3
|
-
require 'echoe'
|
4
|
-
Echoe.new 'typed_serialize' do |p|
|
5
|
-
p.description = "Typed serialize makes sure your serialized attribute is always the specified type."
|
6
|
-
# p.url = "http://typed_serialize.rubyforge.org"
|
7
|
-
p.author = "Elijah Miller"
|
8
|
-
p.email = "elijah.miller@gmail.com"
|
9
|
-
p.retain_gemspec = true
|
10
|
-
p.need_tar_gz = false
|
11
|
-
p.extra_deps = [
|
12
|
-
]
|
13
|
-
p.ignore_pattern = ['spec/test.sqlite3']
|
14
|
-
end
|
15
|
-
|
16
|
-
desc 'Default: run specs'
|
17
4
|
task :default => :spec
|
18
|
-
Spec::Rake::SpecTask.new do |t|
|
19
|
-
t.spec_files = FileList["spec/**/*_spec.rb"]
|
20
|
-
end
|
21
5
|
|
22
|
-
|
6
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
7
|
+
spec.rspec_opts = %w[--color --backtrace]
|
8
|
+
end
|
data/lib/typed_serialize.rb
CHANGED
@@ -1,14 +1,37 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
ActiveRecord::Base.instance_eval do
|
2
|
+
|
3
|
+
def typed_serialize(attr_name, class_name, *attributes)
|
3
4
|
serialize(attr_name, class_name)
|
4
5
|
|
5
6
|
define_method(attr_name) do
|
6
|
-
|
7
|
-
if (value = super()).is_a?(expected_class)
|
7
|
+
if (value = super()).is_a?(class_name)
|
8
8
|
value
|
9
9
|
else
|
10
|
-
send("#{attr_name}=",
|
10
|
+
send("#{attr_name}=", class_name.new)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
serialized_accessor(attr_name, attributes)
|
14
|
+
end
|
15
|
+
|
16
|
+
def serialized_accessor(holder, *attributes)
|
17
|
+
serialized_reader(holder, attributes)
|
18
|
+
serialized_writer(holder, attributes)
|
19
|
+
end
|
20
|
+
|
21
|
+
def serialized_reader(holder, *attributes)
|
22
|
+
attributes.flatten.each do |attr_name|
|
23
|
+
define_method(attr_name) do
|
24
|
+
send(holder)[attr_name]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def serialized_writer(holder, *attributes)
|
30
|
+
attributes.flatten.each do |attr_name|
|
31
|
+
define_method("#{attr_name}=") do |value|
|
32
|
+
send(holder)[attr_name] = value
|
11
33
|
end
|
12
34
|
end
|
13
35
|
end
|
36
|
+
|
14
37
|
end
|
data/spec/schema.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 0) do
|
2
|
+
|
3
|
+
create_table :mice, :force => true do |t|
|
4
|
+
t.text :info
|
5
|
+
end
|
6
|
+
|
7
|
+
end
|
8
|
+
|
9
|
+
class Mouse < ActiveRecord::Base
|
10
|
+
|
11
|
+
typed_serialize :info, Hash, :price, :color
|
12
|
+
|
13
|
+
serialized_accessor :info, :manufacture, :material
|
14
|
+
|
15
|
+
serialized_reader :info, :used, :port_type
|
16
|
+
|
17
|
+
serialized_writer :info, :size, :weight
|
18
|
+
|
19
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ActiveRecord::Base do
|
4
|
+
|
5
|
+
it 'should generate default value' do
|
6
|
+
mouse = Mouse.new
|
7
|
+
mouse.info.should == {}
|
8
|
+
mouse = Mouse.new
|
9
|
+
mouse.info[:foo] = "master"
|
10
|
+
mouse.info.should == {:foo=>'master'}
|
11
|
+
test_hash = {:price=>23, :color=>'Gray', :material=>'Plastic'}
|
12
|
+
mouse = Mouse.new(test_hash)
|
13
|
+
mouse.info.should == test_hash
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should allow serialized accessors' do
|
17
|
+
mouse = Mouse.new
|
18
|
+
mouse.manufacture = "A4Tech"
|
19
|
+
mouse.info.should == {:manufacture=>'A4Tech'}
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'should allow serialized reader' do
|
23
|
+
mouse = Mouse.new
|
24
|
+
mouse.used.should be_nil
|
25
|
+
mouse.info[:used] = true
|
26
|
+
mouse.used.should be_true
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should allow serialized writer' do
|
30
|
+
test_hash = {:size=>'Large', :weight=>'122 gr'}
|
31
|
+
mouse = Mouse.new(test_hash)
|
32
|
+
mouse.info.should == test_hash
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
data/typed_serialize.gemspec
CHANGED
@@ -1,30 +1,21 @@
|
|
1
|
-
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
require "typed_serialize/version"
|
2
3
|
|
3
4
|
Gem::Specification.new do |s|
|
4
|
-
s.name
|
5
|
-
s.version
|
6
|
-
|
7
|
-
s.
|
8
|
-
s.
|
9
|
-
s.date = %q{2010-10-21}
|
10
|
-
s.description = %q{Typed serialize makes sure your serialized attribute is always the specified type.}
|
11
|
-
s.email = %q{elijah.miller@gmail.com}
|
12
|
-
s.extra_rdoc_files = ["CHANGELOG", "lib/typed_serialize.rb", "LICENSE", "README.rdoc"]
|
13
|
-
s.files = ["CHANGELOG", "init.rb", "install.rb", "lib/typed_serialize.rb", "LICENSE", "Manifest", "Rakefile", "README.rdoc", "typed_serialize.gemspec", "uninstall.rb"]
|
14
|
-
s.homepage = %q{}
|
15
|
-
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Typed_serialize", "--main", "README.rdoc"]
|
5
|
+
s.name = "typed_serialize"
|
6
|
+
s.version = TypedSerialize::VERSION
|
7
|
+
s.authors = ["Elijah Miller"]
|
8
|
+
s.email = %q{elijah.miller@gmail.com}
|
9
|
+
s.summary = %q{Typed serialize makes sure your serialized attribute is always the specified type.}
|
16
10
|
s.require_paths = ["lib"]
|
17
|
-
s.rubyforge_project = %q{typed_serialize}
|
18
|
-
s.rubygems_version = %q{1.3.7}
|
19
|
-
s.summary = %q{Typed serialize makes sure your serialized attribute is always the specified type.}
|
20
11
|
|
21
|
-
|
22
|
-
|
23
|
-
|
12
|
+
s.files = `git ls-files`.split("\n")
|
13
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
15
|
+
|
16
|
+
s.add_dependency 'activerecord'
|
24
17
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
else
|
29
|
-
end
|
18
|
+
s.add_development_dependency 'rake'
|
19
|
+
s.add_development_dependency 'sqlite3-ruby'
|
20
|
+
s.add_development_dependency 'rspec'
|
30
21
|
end
|
metadata
CHANGED
@@ -3,10 +3,11 @@ name: typed_serialize
|
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
|
-
- 0
|
7
|
-
- 1
|
8
6
|
- 1
|
9
|
-
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
segments_generated: true
|
10
|
+
version: 1.0.0
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Elijah Miller
|
@@ -14,69 +15,119 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date:
|
18
|
+
date: 2011-12-12 00:00:00 -05:00
|
18
19
|
default_executable:
|
19
|
-
dependencies:
|
20
|
-
|
21
|
-
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
segments:
|
27
|
+
- 0
|
28
|
+
segments_generated: true
|
29
|
+
version: "0"
|
30
|
+
requirement: *id001
|
31
|
+
name: activerecord
|
32
|
+
prerelease: false
|
33
|
+
type: :runtime
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
segments:
|
40
|
+
- 0
|
41
|
+
segments_generated: true
|
42
|
+
version: "0"
|
43
|
+
requirement: *id002
|
44
|
+
name: rake
|
45
|
+
prerelease: false
|
46
|
+
type: :development
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
segments:
|
53
|
+
- 0
|
54
|
+
segments_generated: true
|
55
|
+
version: "0"
|
56
|
+
requirement: *id003
|
57
|
+
name: sqlite3-ruby
|
58
|
+
prerelease: false
|
59
|
+
type: :development
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
segments:
|
66
|
+
- 0
|
67
|
+
segments_generated: true
|
68
|
+
version: "0"
|
69
|
+
requirement: *id004
|
70
|
+
name: rspec
|
71
|
+
prerelease: false
|
72
|
+
type: :development
|
73
|
+
description:
|
22
74
|
email: elijah.miller@gmail.com
|
23
75
|
executables: []
|
24
76
|
|
25
77
|
extensions: []
|
26
78
|
|
27
|
-
extra_rdoc_files:
|
79
|
+
extra_rdoc_files: []
|
80
|
+
|
81
|
+
files:
|
82
|
+
- .gitignore
|
28
83
|
- CHANGELOG
|
29
|
-
-
|
84
|
+
- Gemfile
|
30
85
|
- LICENSE
|
86
|
+
- Manifest
|
31
87
|
- README.rdoc
|
32
|
-
|
33
|
-
- CHANGELOG
|
88
|
+
- Rakefile
|
34
89
|
- init.rb
|
35
90
|
- install.rb
|
36
91
|
- lib/typed_serialize.rb
|
37
|
-
-
|
38
|
-
-
|
39
|
-
-
|
40
|
-
-
|
92
|
+
- lib/typed_serialize/version.rb
|
93
|
+
- spec/schema.rb
|
94
|
+
- spec/spec_helper.rb
|
95
|
+
- spec/typed_serialize/typed_serialize_spec.rb
|
41
96
|
- typed_serialize.gemspec
|
42
97
|
- uninstall.rb
|
43
98
|
has_rdoc: true
|
44
|
-
homepage:
|
99
|
+
homepage:
|
45
100
|
licenses: []
|
46
101
|
|
47
102
|
post_install_message:
|
48
|
-
rdoc_options:
|
49
|
-
|
50
|
-
- --inline-source
|
51
|
-
- --title
|
52
|
-
- Typed_serialize
|
53
|
-
- --main
|
54
|
-
- README.rdoc
|
103
|
+
rdoc_options: []
|
104
|
+
|
55
105
|
require_paths:
|
56
106
|
- lib
|
57
107
|
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
-
none: false
|
59
108
|
requirements:
|
60
109
|
- - ">="
|
61
110
|
- !ruby/object:Gem::Version
|
62
111
|
segments:
|
63
112
|
- 0
|
113
|
+
segments_generated: true
|
64
114
|
version: "0"
|
65
115
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
-
none: false
|
67
116
|
requirements:
|
68
117
|
- - ">="
|
69
118
|
- !ruby/object:Gem::Version
|
70
119
|
segments:
|
71
|
-
-
|
72
|
-
|
73
|
-
version: "
|
120
|
+
- 0
|
121
|
+
segments_generated: true
|
122
|
+
version: "0"
|
74
123
|
requirements: []
|
75
124
|
|
76
|
-
rubyforge_project:
|
77
|
-
rubygems_version: 1.3.
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 1.3.6
|
78
127
|
signing_key:
|
79
128
|
specification_version: 3
|
80
129
|
summary: Typed serialize makes sure your serialized attribute is always the specified type.
|
81
|
-
test_files:
|
82
|
-
|
130
|
+
test_files:
|
131
|
+
- spec/schema.rb
|
132
|
+
- spec/spec_helper.rb
|
133
|
+
- spec/typed_serialize/typed_serialize_spec.rb
|