thirtythirty 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.rdoc +25 -0
- data/Rakefile +3 -0
- data/lib/thirtythirty.rb +31 -4
- data/spec/spec_helper.rb +3 -4
- data/spec/thirtythirty_spec.rb +144 -0
- data/thirtythirty.gemspec +2 -1
- metadata +28 -11
- data/spec/thirtythirty/thirtythirty_spec.rb +0 -46
data/.gitignore
CHANGED
data/README.rdoc
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
= thirty-thirty
|
2
|
+
|
3
|
+
the right hand of Marshall BraveStarr
|
4
|
+
|
5
|
+
Selectively marshal objects without the fancy ruby 1.9 marshalling:
|
6
|
+
|
7
|
+
class Marshalled
|
8
|
+
extend Thirtythirty
|
9
|
+
|
10
|
+
marshalled_accessor :attr1, :attr2
|
11
|
+
attr_accessor :volatile_attr
|
12
|
+
end
|
13
|
+
|
14
|
+
original = Marshalled.new
|
15
|
+
original.attr1 = "value1"
|
16
|
+
original.volatile_attr = "non-marshalled"
|
17
|
+
|
18
|
+
marshalled = Marshal.dump(original)
|
19
|
+
|
20
|
+
reloaded = Marshal.load(marshalled)
|
21
|
+
|
22
|
+
reloaded.attr1 # => "value1"
|
23
|
+
reloaded.volatile_attr # => nil
|
24
|
+
|
25
|
+
Note: Custom marshalling will only be activated by calling one of the marshalling methods (marshal, marshalled_reader, marshalled_writer, marshalled_accessor), not by extending Thirtythirty alone.
|
data/Rakefile
CHANGED
data/lib/thirtythirty.rb
CHANGED
@@ -2,10 +2,37 @@ require "json"
|
|
2
2
|
|
3
3
|
module Thirtythirty
|
4
4
|
|
5
|
+
# Activates marshalling for the given attributes - you have to implement getters/setters yourself!
|
5
6
|
def marshal(*attributes)
|
6
|
-
|
7
|
-
|
8
|
-
|
7
|
+
configure_marshalling(attributes)
|
8
|
+
end
|
9
|
+
|
10
|
+
# Activates marshalling for the given attributes and generates getters - you have to implement setters yourself!
|
11
|
+
def marshalled_reader(*attributes)
|
12
|
+
attr_reader *configure_marshalling(attributes)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Activates marshalling for the given attributes and generates setters - you have to implement getters yourself!
|
16
|
+
def marshalled_writer(*attributes)
|
17
|
+
attr_writer *configure_marshalling(attributes)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Activates marshalling for the given attributes and generates getters/setters.
|
21
|
+
def marshalled_accessor(*attributes)
|
22
|
+
attr_accessor *configure_marshalling(attributes)
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def configure_marshalling(attributes)
|
28
|
+
unless defined?(@marshalled_attributes)
|
29
|
+
extend ClassMethods
|
30
|
+
send :include, InstanceMethods
|
31
|
+
@marshalled_attributes = []
|
32
|
+
end
|
33
|
+
attributes = attributes.flatten.map(&:to_s).uniq
|
34
|
+
@marshalled_attributes = (@marshalled_attributes | attributes).sort.freeze
|
35
|
+
attributes.map(&:to_sym)
|
9
36
|
end
|
10
37
|
|
11
38
|
module ClassMethods
|
@@ -18,7 +45,7 @@ module Thirtythirty
|
|
18
45
|
data = JSON.parse(dumped)
|
19
46
|
obj = new
|
20
47
|
marshalled_attributes.each do |attr|
|
21
|
-
obj.send(:"#{attr}=", Marshal.load(data[attr
|
48
|
+
obj.send(:"#{attr}=", Marshal.load(data[attr]))
|
22
49
|
end
|
23
50
|
obj
|
24
51
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -5,13 +5,12 @@ Bundler.require(:default, :development)
|
|
5
5
|
Dir[File.expand_path '../support/*.rb', __FILE__].each {|f| require f}
|
6
6
|
|
7
7
|
|
8
|
-
class
|
8
|
+
class ThirtythirtyBase
|
9
9
|
extend Thirtythirty
|
10
10
|
end
|
11
11
|
|
12
|
-
class BlogPost <
|
12
|
+
class BlogPost < ThirtythirtyBase
|
13
13
|
marshal :title, :description, :comments
|
14
|
-
|
15
14
|
attr_accessor :title, :description, :comments, :secret_password
|
16
15
|
def initialize(attributes={})
|
17
16
|
self.title = attributes[:title] || 'My first blog post'
|
@@ -21,7 +20,7 @@ class BlogPost < Base
|
|
21
20
|
end
|
22
21
|
end
|
23
22
|
|
24
|
-
class Comment <
|
23
|
+
class Comment < ThirtythirtyBase
|
25
24
|
marshal :author, :body, :date
|
26
25
|
attr_accessor :author, :body, :date
|
27
26
|
def initialize(attributes={})
|
@@ -0,0 +1,144 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Thirtythirty do
|
4
|
+
|
5
|
+
describe ".marshal" do
|
6
|
+
|
7
|
+
subject do
|
8
|
+
cls = Class.new(ThirtythirtyBase) do
|
9
|
+
marshal :attr1, :attr3
|
10
|
+
marshal [:attr1, :attr2]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should add given attributes to marshalled attributes (stringified, unique, sorted, frozen)" do
|
15
|
+
subject.marshalled_attributes.should == %w(attr1 attr2 attr3)
|
16
|
+
subject.marshalled_attributes.should be_frozen
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
describe ".marshalled_reader" do
|
22
|
+
|
23
|
+
subject do
|
24
|
+
Class.new(ThirtythirtyBase) do
|
25
|
+
marshalled_reader :attr1, :attr3
|
26
|
+
marshalled_reader [:attr1, :attr2]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should add given attributes to marshalled attributes (stringified, unique, sorted, frozen)" do
|
31
|
+
subject.marshalled_attributes.should == %w(attr1 attr2 attr3)
|
32
|
+
subject.marshalled_attributes.should be_frozen
|
33
|
+
end
|
34
|
+
|
35
|
+
%w(attr1 attr2 attr3).each do |attr|
|
36
|
+
|
37
|
+
it "should generate a reader for attribute #{attr} (but no writer)" do
|
38
|
+
obj = subject.new
|
39
|
+
obj.should respond_to(attr.to_sym)
|
40
|
+
obj.should_not respond_to(:"#{attr}=")
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
describe ".marshalled_writer" do
|
48
|
+
|
49
|
+
subject do
|
50
|
+
Class.new(ThirtythirtyBase) do
|
51
|
+
marshalled_writer :attr1, :attr3
|
52
|
+
marshalled_writer [:attr1, :attr2]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should add given attributes to marshalled attributes (stringified, unique, sorted, frozen)" do
|
57
|
+
subject.marshalled_attributes.should == %w(attr1 attr2 attr3)
|
58
|
+
subject.marshalled_attributes.should be_frozen
|
59
|
+
end
|
60
|
+
|
61
|
+
%w(attr1 attr2 attr3).each do |attr|
|
62
|
+
|
63
|
+
it "should generate a writer for attribute #{attr} (but no reader)" do
|
64
|
+
obj = subject.new
|
65
|
+
obj.should_not respond_to(attr.to_sym)
|
66
|
+
obj.should respond_to(:"#{attr}=")
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
describe ".marshalled_accessor" do
|
74
|
+
|
75
|
+
subject do
|
76
|
+
Class.new(ThirtythirtyBase) do
|
77
|
+
marshalled_accessor :attr1, :attr3
|
78
|
+
marshalled_accessor :attr1, :attr2
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should add given attributes to marshalled attributes (stringified, unique, sorted, frozen)" do
|
83
|
+
subject.marshalled_attributes.should == %w(attr1 attr2 attr3)
|
84
|
+
subject.marshalled_attributes.should be_frozen
|
85
|
+
end
|
86
|
+
|
87
|
+
%w(attr1 attr2 attr3).each do |attr|
|
88
|
+
|
89
|
+
it "should generate a reader and a writer for attribute #{attr}" do
|
90
|
+
obj = subject.new
|
91
|
+
obj.should respond_to(attr.to_sym)
|
92
|
+
obj.should respond_to(:"#{attr}=")
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
describe "marshalling" do
|
100
|
+
|
101
|
+
describe "#_dump" do
|
102
|
+
|
103
|
+
it "should only dump attributes that should be exposed" do
|
104
|
+
password = "VERY SECRET!"
|
105
|
+
blog_post = BlogPost.new(:secret_password => password)
|
106
|
+
marshalled_blog_post = Marshal.dump(blog_post)
|
107
|
+
marshalled_blog_post.should_not match(Regexp.new(password))
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should also serialize nested objects so that these objects can also be retrieved by loading the main object" do
|
111
|
+
blog_post = BlogPost.new(:comments => [Comment.new(:author => 'tomj'), Comment.new(:author => 'bennyb')])
|
112
|
+
marshalled_blog_post = Marshal.dump(blog_post)
|
113
|
+
marshalled_blog_post.should match(/bennyb/)
|
114
|
+
marshalled_blog_post.should match(/tomj/)
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
describe "._load" do
|
120
|
+
|
121
|
+
it "should return a fully deserialized object" do
|
122
|
+
blog_post = BlogPost.new(:comments => [], :title => 'blau is happy')
|
123
|
+
marshalled_blog_post = Marshal.dump(blog_post)
|
124
|
+
deserialized_blog_post = Marshal.load(marshalled_blog_post)
|
125
|
+
deserialized_blog_post.should be_kind_of(BlogPost)
|
126
|
+
deserialized_blog_post.title.should == 'blau is happy'
|
127
|
+
end
|
128
|
+
|
129
|
+
it "should return a fully deserialized object and nested objects" do
|
130
|
+
nice_comment = Comment.new(:author => 'tomj', :body => 'nice article, dude!')
|
131
|
+
blog_post = BlogPost.new(:comments => [nice_comment])
|
132
|
+
marshalled_blog_post = Marshal.dump(blog_post)
|
133
|
+
deserialized_blog_post = Marshal.load(marshalled_blog_post)
|
134
|
+
retrieved_comment = deserialized_blog_post.comments.first
|
135
|
+
retrieved_comment.should be_kind_of(Comment)
|
136
|
+
retrieved_comment.author.should == 'tomj'
|
137
|
+
retrieved_comment.body.should == 'nice article, dude!'
|
138
|
+
end
|
139
|
+
|
140
|
+
end
|
141
|
+
|
142
|
+
end
|
143
|
+
|
144
|
+
end
|
data/thirtythirty.gemspec
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
Gem::Specification.new do |s|
|
3
3
|
s.name = "thirtythirty"
|
4
|
-
s.version = "0.0.
|
4
|
+
s.version = "0.0.2"
|
5
5
|
s.date = Time.now
|
6
6
|
s.platform = Gem::Platform::RUBY
|
7
7
|
s.authors = ["Benjamin Behr", "Thomas Jachmann"]
|
@@ -12,6 +12,7 @@ Gem::Specification.new do |s|
|
|
12
12
|
|
13
13
|
s.rubyforge_project = "thirtythirty"
|
14
14
|
|
15
|
+
s.add_development_dependency "ci_reporter", "~> 1.6.3"
|
15
16
|
s.add_development_dependency "rspec", "~> 2.4.0"
|
16
17
|
s.add_runtime_dependency "json", "~> 1.5.1"
|
17
18
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: thirtythirty
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Benjamin Behr
|
@@ -16,13 +16,29 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2011-02-
|
19
|
+
date: 2011-02-04 00:00:00 +01:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
23
|
-
name:
|
23
|
+
name: ci_reporter
|
24
24
|
prerelease: false
|
25
25
|
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ~>
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
hash: 9
|
31
|
+
segments:
|
32
|
+
- 1
|
33
|
+
- 6
|
34
|
+
- 3
|
35
|
+
version: 1.6.3
|
36
|
+
type: :development
|
37
|
+
version_requirements: *id001
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: rspec
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
26
42
|
none: false
|
27
43
|
requirements:
|
28
44
|
- - ~>
|
@@ -34,11 +50,11 @@ dependencies:
|
|
34
50
|
- 0
|
35
51
|
version: 2.4.0
|
36
52
|
type: :development
|
37
|
-
version_requirements: *
|
53
|
+
version_requirements: *id002
|
38
54
|
- !ruby/object:Gem::Dependency
|
39
55
|
name: json
|
40
56
|
prerelease: false
|
41
|
-
requirement: &
|
57
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
42
58
|
none: false
|
43
59
|
requirements:
|
44
60
|
- - ~>
|
@@ -50,7 +66,7 @@ dependencies:
|
|
50
66
|
- 1
|
51
67
|
version: 1.5.1
|
52
68
|
type: :runtime
|
53
|
-
version_requirements: *
|
69
|
+
version_requirements: *id003
|
54
70
|
description: This gem allows for customization of which data to marshal, especially useful for selective session data serialization.
|
55
71
|
email:
|
56
72
|
- benny@digitalbehr.de
|
@@ -65,10 +81,11 @@ files:
|
|
65
81
|
- .gitignore
|
66
82
|
- .rspec
|
67
83
|
- Gemfile
|
84
|
+
- README.rdoc
|
68
85
|
- Rakefile
|
69
86
|
- lib/thirtythirty.rb
|
70
87
|
- spec/spec_helper.rb
|
71
|
-
- spec/
|
88
|
+
- spec/thirtythirty_spec.rb
|
72
89
|
- thirtythirty.gemspec
|
73
90
|
has_rdoc: true
|
74
91
|
homepage: http://github.com/blaulabs/thirtythirty
|
@@ -100,10 +117,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
117
|
requirements: []
|
101
118
|
|
102
119
|
rubyforge_project: thirtythirty
|
103
|
-
rubygems_version: 1.
|
120
|
+
rubygems_version: 1.5.0
|
104
121
|
signing_key:
|
105
122
|
specification_version: 3
|
106
123
|
summary: Marshalling customization
|
107
124
|
test_files:
|
108
125
|
- spec/spec_helper.rb
|
109
|
-
- spec/
|
126
|
+
- spec/thirtythirty_spec.rb
|
@@ -1,46 +0,0 @@
|
|
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
|