thirtythirty 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/Rakefile +5 -0
- data/lib/thirtythirty.rb +39 -0
- data/spec/spec_helper.rb +32 -0
- data/spec/thirtythirty/thirtythirty_spec.rb +46 -0
- data/thirtythirty.gemspec +22 -0
- metadata +109 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
data/lib/thirtythirty.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require "json"
|
2
|
+
|
3
|
+
module Thirtythirty
|
4
|
+
|
5
|
+
def marshal(*attributes)
|
6
|
+
extend ClassMethods
|
7
|
+
send :include, InstanceMethods
|
8
|
+
@marshalled_attributes = attributes
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
|
13
|
+
attr_reader :marshalled_attributes
|
14
|
+
|
15
|
+
protected
|
16
|
+
|
17
|
+
def _load(dumped)
|
18
|
+
data = JSON.parse(dumped)
|
19
|
+
obj = new
|
20
|
+
marshalled_attributes.each do |attr|
|
21
|
+
obj.send(:"#{attr}=", Marshal.load(data[attr.to_s]))
|
22
|
+
end
|
23
|
+
obj
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
module InstanceMethods
|
29
|
+
|
30
|
+
def _dump(*args)
|
31
|
+
self.class.marshalled_attributes.inject({}) do |hash, attr|
|
32
|
+
hash[attr] = Marshal.dump(self.send(attr.to_sym))
|
33
|
+
hash
|
34
|
+
end.to_json
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
Bundler.require(:default, :development)
|
4
|
+
|
5
|
+
Dir[File.expand_path '../support/*.rb', __FILE__].each {|f| require f}
|
6
|
+
|
7
|
+
|
8
|
+
class Base
|
9
|
+
extend Thirtythirty
|
10
|
+
end
|
11
|
+
|
12
|
+
class BlogPost < Base
|
13
|
+
marshal :title, :description, :comments
|
14
|
+
|
15
|
+
attr_accessor :title, :description, :comments, :secret_password
|
16
|
+
def initialize(attributes={})
|
17
|
+
self.title = attributes[:title] || 'My first blog post'
|
18
|
+
self.description = attributes[:description] || "This is a very short description of the things i've done yesterday"
|
19
|
+
self.comments = attributes[:comments] || [Comment.new, Comment.new(:author => 'bennyb', :comment => 'Ponies and Unicorns!')]
|
20
|
+
self.secret_password = attributes[:secret_password] || "SECRET!"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Comment < Base
|
25
|
+
marshal :author, :body, :date
|
26
|
+
attr_accessor :author, :body, :date
|
27
|
+
def initialize(attributes={})
|
28
|
+
self.author = attributes[:author] || 'tomj'
|
29
|
+
self.body = attributes[:body] || "Very nice article!"
|
30
|
+
self.date = attributes[:date] || Time.now
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Thirtythirty do
|
4
|
+
|
5
|
+
describe "trying to dump an object" do
|
6
|
+
|
7
|
+
it "should only dump attributes that should be exposed" do
|
8
|
+
password = "VERY SECRET!"
|
9
|
+
blog_post = BlogPost.new(:secret_password => password)
|
10
|
+
marshalled_blog_post = Marshal.dump(blog_post)
|
11
|
+
marshalled_blog_post.should_not match(Regexp.new(password))
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should also serialize nested objects so that these objects can also be retrieved by loading the main object" do
|
15
|
+
blog_post = BlogPost.new(:comments => [Comment.new(:author => 'tomj'), Comment.new(:author => 'bennyb')])
|
16
|
+
marshalled_blog_post = Marshal.dump(blog_post)
|
17
|
+
marshalled_blog_post.should match(/bennyb/)
|
18
|
+
marshalled_blog_post.should match(/tomj/)
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "trying to load an object" do
|
24
|
+
|
25
|
+
it "should return a fully deserialized object" do
|
26
|
+
blog_post = BlogPost.new(:comments => [], :title => 'blau is happy')
|
27
|
+
marshalled_blog_post = Marshal.dump(blog_post)
|
28
|
+
deserialized_blog_post = Marshal.load(marshalled_blog_post)
|
29
|
+
deserialized_blog_post.should be_kind_of(BlogPost)
|
30
|
+
deserialized_blog_post.title.should == 'blau is happy'
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should return a fully deserialized object and nested objects" do
|
34
|
+
nice_comment = Comment.new(:author => 'tomj', :body => 'nice article, dude!')
|
35
|
+
blog_post = BlogPost.new(:comments => [nice_comment])
|
36
|
+
marshalled_blog_post = Marshal.dump(blog_post)
|
37
|
+
deserialized_blog_post = Marshal.load(marshalled_blog_post)
|
38
|
+
retrieved_comment = deserialized_blog_post.comments.first
|
39
|
+
retrieved_comment.should be_kind_of(Comment)
|
40
|
+
retrieved_comment.author.should == 'tomj'
|
41
|
+
retrieved_comment.body.should == 'nice article, dude!'
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
Gem::Specification.new do |s|
|
3
|
+
s.name = "thirtythirty"
|
4
|
+
s.version = "0.0.1"
|
5
|
+
s.date = Time.now
|
6
|
+
s.platform = Gem::Platform::RUBY
|
7
|
+
s.authors = ["Benjamin Behr", "Thomas Jachmann"]
|
8
|
+
s.email = ["benny@digitalbehr.de", "self@thomasjachmann.com"]
|
9
|
+
s.homepage = "http://github.com/blaulabs/thirtythirty"
|
10
|
+
s.summary = %q{Marshalling customization}
|
11
|
+
s.description = %q{This gem allows for customization of which data to marshal, especially useful for selective session data serialization.}
|
12
|
+
|
13
|
+
s.rubyforge_project = "thirtythirty"
|
14
|
+
|
15
|
+
s.add_development_dependency "rspec", "~> 2.4.0"
|
16
|
+
s.add_runtime_dependency "json", "~> 1.5.1"
|
17
|
+
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
20
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
21
|
+
s.require_paths = ["lib"]
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: thirtythirty
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Benjamin Behr
|
14
|
+
- Thomas Jachmann
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2011-02-03 00:00:00 +01:00
|
20
|
+
default_executable:
|
21
|
+
dependencies:
|
22
|
+
- !ruby/object:Gem::Dependency
|
23
|
+
name: rspec
|
24
|
+
prerelease: false
|
25
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ~>
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 31
|
31
|
+
segments:
|
32
|
+
- 2
|
33
|
+
- 4
|
34
|
+
- 0
|
35
|
+
version: 2.4.0
|
36
|
+
type: :development
|
37
|
+
version_requirements: *id001
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: json
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
hash: 1
|
47
|
+
segments:
|
48
|
+
- 1
|
49
|
+
- 5
|
50
|
+
- 1
|
51
|
+
version: 1.5.1
|
52
|
+
type: :runtime
|
53
|
+
version_requirements: *id002
|
54
|
+
description: This gem allows for customization of which data to marshal, especially useful for selective session data serialization.
|
55
|
+
email:
|
56
|
+
- benny@digitalbehr.de
|
57
|
+
- self@thomasjachmann.com
|
58
|
+
executables: []
|
59
|
+
|
60
|
+
extensions: []
|
61
|
+
|
62
|
+
extra_rdoc_files: []
|
63
|
+
|
64
|
+
files:
|
65
|
+
- .gitignore
|
66
|
+
- .rspec
|
67
|
+
- Gemfile
|
68
|
+
- Rakefile
|
69
|
+
- lib/thirtythirty.rb
|
70
|
+
- spec/spec_helper.rb
|
71
|
+
- spec/thirtythirty/thirtythirty_spec.rb
|
72
|
+
- thirtythirty.gemspec
|
73
|
+
has_rdoc: true
|
74
|
+
homepage: http://github.com/blaulabs/thirtythirty
|
75
|
+
licenses: []
|
76
|
+
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
hash: 3
|
88
|
+
segments:
|
89
|
+
- 0
|
90
|
+
version: "0"
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
none: false
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
hash: 3
|
97
|
+
segments:
|
98
|
+
- 0
|
99
|
+
version: "0"
|
100
|
+
requirements: []
|
101
|
+
|
102
|
+
rubyforge_project: thirtythirty
|
103
|
+
rubygems_version: 1.4.2
|
104
|
+
signing_key:
|
105
|
+
specification_version: 3
|
106
|
+
summary: Marshalling customization
|
107
|
+
test_files:
|
108
|
+
- spec/spec_helper.rb
|
109
|
+
- spec/thirtythirty/thirtythirty_spec.rb
|