ar_serialize 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source :rubygems
2
+
3
+ gem 'activerecord'
4
+
5
+ group :dev do # not development <-> would add unneeded development dependencies in gemspec
6
+ gem 'sqlite3'
7
+ gem 'rake'
8
+ gem 'rspec', '~>2'
9
+ gem 'jeweler'
10
+ end
@@ -0,0 +1,43 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activemodel (3.0.9)
5
+ activesupport (= 3.0.9)
6
+ builder (~> 2.1.2)
7
+ i18n (~> 0.5.0)
8
+ activerecord (3.0.9)
9
+ activemodel (= 3.0.9)
10
+ activesupport (= 3.0.9)
11
+ arel (~> 2.0.10)
12
+ tzinfo (~> 0.3.23)
13
+ activesupport (3.0.9)
14
+ arel (2.0.10)
15
+ builder (2.1.2)
16
+ diff-lcs (1.1.2)
17
+ git (1.2.5)
18
+ i18n (0.5.0)
19
+ jeweler (1.6.2)
20
+ bundler (~> 1.0)
21
+ git (>= 1.2.5)
22
+ rake
23
+ rake (0.9.2)
24
+ rspec (2.6.0)
25
+ rspec-core (~> 2.6.0)
26
+ rspec-expectations (~> 2.6.0)
27
+ rspec-mocks (~> 2.6.0)
28
+ rspec-core (2.6.4)
29
+ rspec-expectations (2.6.0)
30
+ diff-lcs (~> 1.1.2)
31
+ rspec-mocks (2.6.0)
32
+ sqlite3 (1.3.3)
33
+ tzinfo (0.3.28)
34
+
35
+ PLATFORMS
36
+ ruby
37
+
38
+ DEPENDENCIES
39
+ activerecord
40
+ jeweler
41
+ rake
42
+ rspec (~> 2)
43
+ sqlite3
@@ -0,0 +1,18 @@
1
+ task :default do
2
+ sh "rspec spec/"
3
+ end
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gem|
8
+ gem.name = 'ar_serialize'
9
+ gem.summary = "Efficient serialize/deserialize of nested Hashes/Arrays including ActiveRecords "
10
+ gem.email = "michael@grosser.it"
11
+ gem.homepage = "http://github.com/grosser/#{gem.name}"
12
+ gem.authors = ["Michael Grosser"]
13
+ end
14
+
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: gem install jeweler"
18
+ end
@@ -0,0 +1,23 @@
1
+ Efficient serialize/deserialize of nested Hashes/Arrays with ActiveRecords.
2
+
3
+ Strips unnecessary info like attributes/associations from infinetly deep Hashes/Arrays.
4
+
5
+ Install
6
+ =======
7
+ sudo gem install ar_serialize
8
+ Or
9
+ rails plugin install git://github.com/grosser/ar_serialize.git
10
+
11
+ Usage
12
+ =====
13
+
14
+ {:foo => <User>}.to_yaml --> tons of stuff including asociations and attriutes
15
+ ArSerialize.seriazlize(:foo => <User>) --> {:foo => "ActiveRecord:User:15"}
16
+ ArSerialize.deseriazlize(:foo => 'ActiveRecord:User:15') --> {:foo => <User>}
17
+
18
+
19
+ Author
20
+ ======
21
+ [Michael Grosser](http://grosser.it)<br/>
22
+ michael@grosser.it<br/>
23
+ Hereby placed under public domain, do what you want, just do not hold me accountable...
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,43 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{ar_serialize}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Michael Grosser"]
12
+ s.date = %q{2011-06-19}
13
+ s.email = %q{michael@grosser.it}
14
+ s.files = [
15
+ "Gemfile",
16
+ "Gemfile.lock",
17
+ "Rakefile",
18
+ "Readme.md",
19
+ "VERSION",
20
+ "ar_serialize.gemspec",
21
+ "lib/ar_serialize.rb",
22
+ "spec/ar_serialize_spec.rb",
23
+ "spec/setup_test_model.rb",
24
+ "spec/spec_helper.rb"
25
+ ]
26
+ s.homepage = %q{http://github.com/grosser/ar_serialize}
27
+ s.require_paths = ["lib"]
28
+ s.rubygems_version = %q{1.6.2}
29
+ s.summary = %q{Efficient serialize/deserialize of nested Hashes/Arrays including ActiveRecords}
30
+
31
+ if s.respond_to? :specification_version then
32
+ s.specification_version = 3
33
+
34
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
35
+ s.add_runtime_dependency(%q<activerecord>, [">= 0"])
36
+ else
37
+ s.add_dependency(%q<activerecord>, [">= 0"])
38
+ end
39
+ else
40
+ s.add_dependency(%q<activerecord>, [">= 0"])
41
+ end
42
+ end
43
+
@@ -0,0 +1,39 @@
1
+ require 'active_record'
2
+
3
+ module ARSerialize
4
+ VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
5
+
6
+ def self.serialize(v)
7
+ if v.is_a?(ActiveRecord::Base)
8
+ "ActiveRecord:#{v.class}:#{v.id}"
9
+ elsif v.is_a?(Hash)
10
+ Hash[v.map do |k,value|
11
+ [k, serialize(value)]
12
+ end]
13
+ elsif v.is_a?(Array)
14
+ v.map do |value|
15
+ serialize(value)
16
+ end
17
+ else
18
+ v
19
+ end
20
+ end
21
+
22
+ def self.deserialize(v)
23
+ if v.is_a?(String) and v =~ /^ActiveRecord:(\w+):(\d+)$/
24
+ $1.constantize.find($2)
25
+ elsif v.is_a?(Hash)
26
+ result = Hash[v.map do |k,value|
27
+ [k, deserialize(value)]
28
+ end]
29
+
30
+ v.is_a?(HashWithIndifferentAccess) ? result.with_indifferent_access : result
31
+ elsif v.is_a?(Array)
32
+ v.map do |value|
33
+ deserialize(value)
34
+ end
35
+ else
36
+ v
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe ARSerialize do
4
+ describe :serialize do
5
+ it "does not change normal values" do
6
+ ARSerialize.serialize('x').should == 'x'
7
+ ARSerialize.serialize([1,2,[3,4]]).should == [1,2,[3,4]]
8
+ ARSerialize.serialize({:x => {:y => 1}}).should == {:x => {:y => 1}}
9
+ end
10
+
11
+ it "serializes ar" do
12
+ user = User.create!(:title => 'foo')
13
+ serialized = "ActiveRecord:User:#{user.id}"
14
+ ARSerialize.serialize(user).should == serialized
15
+ ARSerialize.serialize([1,2,[3,user]]).should == [1,2,[3,serialized]]
16
+ ARSerialize.serialize({:x => {:y => user}}).should == {:x => {:y => serialized}}
17
+ end
18
+ end
19
+
20
+ describe :deserialize do
21
+ it "does not change normal values" do
22
+ ARSerialize.deserialize('x').should == 'x'
23
+ ARSerialize.deserialize([1,2,[3,4]]).should == [1,2,[3,4]]
24
+ ARSerialize.deserialize({:x => {:y => 1}}).should == {:x => {:y => 1}}
25
+ end
26
+
27
+ it "deserializes ar" do
28
+ user = User.create!(:title => 'foo')
29
+ serialized = "ActiveRecord:User:#{user.id}"
30
+ ARSerialize.deserialize(serialized).should == user
31
+ ARSerialize.deserialize([1,2,[3,serialized]]).should == [1,2,[3,user]]
32
+ ARSerialize.deserialize([1,2,[serialized,3]]).should == [1,2,[user,3]]
33
+ ARSerialize.deserialize({:x => {:y => serialized}}).should == {:x => {:y => user}}
34
+ end
35
+
36
+ it "deserializes with indifferent access" do
37
+ result = ARSerialize.deserialize({:x => {:y => 1}}.with_indifferent_access)
38
+ result['x']['y'].should == 1
39
+ result = ARSerialize.deserialize({:x => {:y => 1}})
40
+ result['x'].should == nil
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,16 @@
1
+ # connect
2
+ ActiveRecord::Base.establish_connection(
3
+ :adapter => "sqlite3",
4
+ :database => ":memory:"
5
+ )
6
+
7
+ # create tables
8
+ ActiveRecord::Schema.define(:version => 1) do
9
+ create_table :users do |t|
10
+ t.string :title
11
+ end
12
+ end
13
+
14
+ # create models
15
+ class User < ActiveRecord::Base
16
+ end
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift 'lib'
2
+ require 'ar_serialize'
3
+ require 'active_support/all'
4
+ require 'spec/setup_test_model'
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ar_serialize
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Michael Grosser
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-06-19 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ name: activerecord
33
+ version_requirements: *id001
34
+ prerelease: false
35
+ description:
36
+ email: michael@grosser.it
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - Gemfile
45
+ - Gemfile.lock
46
+ - Rakefile
47
+ - Readme.md
48
+ - VERSION
49
+ - ar_serialize.gemspec
50
+ - lib/ar_serialize.rb
51
+ - spec/ar_serialize_spec.rb
52
+ - spec/setup_test_model.rb
53
+ - spec/spec_helper.rb
54
+ has_rdoc: true
55
+ homepage: http://github.com/grosser/ar_serialize
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options: []
60
+
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project:
84
+ rubygems_version: 1.6.2
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Efficient serialize/deserialize of nested Hashes/Arrays including ActiveRecords
88
+ test_files: []
89
+