typed_serialize 0.1.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ doc
2
+ pkg
3
+ spec/test.sqlite3
4
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
@@ -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 'spec/rake/spectask'
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
- task :test => :spec
6
+ RSpec::Core::RakeTask.new(:spec) do |spec|
7
+ spec.rspec_opts = %w[--color --backtrace]
8
+ end
@@ -1,14 +1,37 @@
1
- class ActiveRecord::Base
2
- def self.typed_serialize(attr_name, class_name)
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
- expected_class = self.class.serialized_attributes[attr_name.to_s]
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}=", expected_class.new)
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
@@ -0,0 +1,3 @@
1
+ module TypedSerialize
2
+ VERSION = '1.0.0'
3
+ end
@@ -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
@@ -0,0 +1,6 @@
1
+ require 'active_record'
2
+ require 'typed_serialize'
3
+
4
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
5
+ ActiveRecord::Migration.verbose = false
6
+ load "schema.rb"
@@ -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
@@ -1,30 +1,21 @@
1
- # -*- encoding: utf-8 -*-
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "typed_serialize/version"
2
3
 
3
4
  Gem::Specification.new do |s|
4
- s.name = %q{typed_serialize}
5
- s.version = "0.1.1"
6
-
7
- s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
- s.authors = ["Elijah Miller"]
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
- if s.respond_to? :specification_version then
22
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
- s.specification_version = 3
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
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
26
- else
27
- end
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
- version: 0.1.1
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: 2010-10-21 00:00:00 -04:00
18
+ date: 2011-12-12 00:00:00 -05:00
18
19
  default_executable:
19
- dependencies: []
20
-
21
- description: Typed serialize makes sure your serialized attribute is always the specified type.
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
- - lib/typed_serialize.rb
84
+ - Gemfile
30
85
  - LICENSE
86
+ - Manifest
31
87
  - README.rdoc
32
- files:
33
- - CHANGELOG
88
+ - Rakefile
34
89
  - init.rb
35
90
  - install.rb
36
91
  - lib/typed_serialize.rb
37
- - LICENSE
38
- - Manifest
39
- - Rakefile
40
- - README.rdoc
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
- - --line-numbers
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
- - 1
72
- - 2
73
- version: "1.2"
120
+ - 0
121
+ segments_generated: true
122
+ version: "0"
74
123
  requirements: []
75
124
 
76
- rubyforge_project: typed_serialize
77
- rubygems_version: 1.3.7
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