attribute_serializer 0.4.0 → 0.4.1
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/Rakefile +4 -4
- data/VERSION +1 -1
- data/examples.rb +149 -0
- metadata +8 -8
- data/test.rb +0 -149
data/Rakefile
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
task :default => [:test]
|
2
|
-
|
2
|
+
|
3
3
|
begin
|
4
4
|
require 'jeweler'
|
5
5
|
Jeweler::Tasks.new do |gs|
|
@@ -8,14 +8,14 @@ begin
|
|
8
8
|
gs.summary = "Takes an object, serializes the attributes to an ordered hash based on a pre-defined schema"
|
9
9
|
gs.email = "myles@myles.id.au"
|
10
10
|
gs.authors = ["Myles Byrne"]
|
11
|
-
gs.add_development_dependency('
|
11
|
+
gs.add_development_dependency('exemplor', '>= 3000.1.0')
|
12
12
|
gs.files.include %(lib/attribute_serializer.rb) # jewler used to include files in lib automatically, what happened?
|
13
13
|
end
|
14
14
|
Jeweler::GemcutterTasks.new
|
15
15
|
rescue LoadError
|
16
16
|
puts "Install jeweler to build gem"
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
task :test do
|
20
|
-
ruby '-rubygems', "
|
20
|
+
ruby '-rubygems', "examples.rb"
|
21
21
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.1
|
data/examples.rb
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
require 'exemplor'
|
2
|
+
require 'lib/attribute_serializer'
|
3
|
+
|
4
|
+
def OHash &blk
|
5
|
+
ActiveSupport::OrderedHash.new.tap(&blk)
|
6
|
+
end
|
7
|
+
|
8
|
+
Class.class_eval do
|
9
|
+
def create(hash)
|
10
|
+
self.new.tap do |o| hash.each { |k,v| o.send("#{k}=", v) } end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
eg.helpers do
|
15
|
+
|
16
|
+
def self.mem(name, &blk)
|
17
|
+
define_method name do
|
18
|
+
instance_variable_get("@#{name}") or
|
19
|
+
instance_variable_set("@#{name}", blk.call)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
mem :article_class do
|
24
|
+
Class.new { attr_accessor :id, :title, :body, :author }
|
25
|
+
end
|
26
|
+
|
27
|
+
mem :author_class do
|
28
|
+
Class.new { attr_accessor :name, :email }
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
eg "Serializable object" do
|
34
|
+
AttributeSerializer article_class, %w(id title body)
|
35
|
+
|
36
|
+
article = article_class.create(
|
37
|
+
:id => 1,
|
38
|
+
:title => "Contextual Attributes",
|
39
|
+
:body => "The layer you've always wanted for generating your json"
|
40
|
+
)
|
41
|
+
|
42
|
+
expected_hash = OHash do |h|
|
43
|
+
h['id'] = 1
|
44
|
+
h['title'] = "Contextual Attributes"
|
45
|
+
h['body'] = "The layer you've always wanted for generating your json"
|
46
|
+
end
|
47
|
+
|
48
|
+
# AttributeSerializer(article) is equivalent to:
|
49
|
+
# AttributeSerializer(article,:default)
|
50
|
+
Assert(AttributeSerializer(article) == expected_hash)
|
51
|
+
end
|
52
|
+
|
53
|
+
eg "Serializable subclass object" do
|
54
|
+
subclass = Class.new(article_class) { attr_accessor :photo_url }
|
55
|
+
|
56
|
+
# AttributeSerializer only defined for *ancestor class*
|
57
|
+
AttributeSerializer article_class, %w(id title body)
|
58
|
+
|
59
|
+
photo_post = subclass.create(
|
60
|
+
:id => 1,
|
61
|
+
:title => "Contextual Attributes",
|
62
|
+
:body => "The layer you've always wanted for generating your json",
|
63
|
+
:photo_url => "http://foobar.com/lemonparty.jpg"
|
64
|
+
)
|
65
|
+
|
66
|
+
expected_hash = OHash do |h|
|
67
|
+
h['id'] = 1
|
68
|
+
h['title'] = "Contextual Attributes"
|
69
|
+
h['body'] = "The layer you've always wanted for generating your json"
|
70
|
+
end
|
71
|
+
|
72
|
+
# Serialization on instance of *subclass*
|
73
|
+
Assert(AttributeSerializer(photo_post) == expected_hash)
|
74
|
+
|
75
|
+
# define AttributeSerializer for the actual class
|
76
|
+
AttributeSerializer subclass, %w(id title body photo_url)
|
77
|
+
|
78
|
+
expected_hash['photo_url'] = "http://foobar.com/lemonparty.jpg"
|
79
|
+
|
80
|
+
# Serialization on instance of subclass now that there's a serializer for it
|
81
|
+
Assert(AttributeSerializer(photo_post) == expected_hash)
|
82
|
+
end
|
83
|
+
|
84
|
+
eg "Object with nested serializable attribute" do
|
85
|
+
article_class = Class.new { attr_accessor :id, :author }
|
86
|
+
|
87
|
+
AttributeSerializer author_class, %w(name email)
|
88
|
+
AttributeSerializer article_class, %w(id author) do
|
89
|
+
# no implicit support for nesting, intentionally
|
90
|
+
def author
|
91
|
+
AttributeSerializer delegatee.author
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
article = article_class.create(
|
96
|
+
:id => 1,
|
97
|
+
:author => author_class.create(
|
98
|
+
:name => "Myles",
|
99
|
+
:email => "myles@"
|
100
|
+
)
|
101
|
+
)
|
102
|
+
|
103
|
+
expected_hash = OHash do |h|
|
104
|
+
h['id'] = 1
|
105
|
+
h['author'] = OHash do |h|
|
106
|
+
h['name'] = "Myles"
|
107
|
+
h['email'] = "myles@"
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
Assert(AttributeSerializer(article) == expected_hash)
|
112
|
+
end
|
113
|
+
|
114
|
+
eg "Array of serializable objects" do
|
115
|
+
AttributeSerializer author_class, %w(name email)
|
116
|
+
|
117
|
+
array = [
|
118
|
+
author_class.create(:name => "Myles", :email => 'myles@'),
|
119
|
+
author_class.create(:name => "Gabriel", :email => 'gabriel@')
|
120
|
+
]
|
121
|
+
|
122
|
+
expected_array = [
|
123
|
+
OHash { |h| h['name'] = "Myles"; h['email'] = 'myles@' },
|
124
|
+
OHash { |h| h['name'] = "Gabriel"; h['email'] = 'gabriel@' }
|
125
|
+
]
|
126
|
+
|
127
|
+
Assert(AttributeSerializer(array) == expected_array)
|
128
|
+
end
|
129
|
+
|
130
|
+
eg "Non-default serializer" do
|
131
|
+
AttributeSerializer article_class, :summary, %w(id title)
|
132
|
+
|
133
|
+
article = article_class.create(
|
134
|
+
:id => 1,
|
135
|
+
:title => "Contextual Attributes",
|
136
|
+
:body => "The layer you've always wanted for generating your json"
|
137
|
+
)
|
138
|
+
|
139
|
+
expected_hash = OHash do |h|
|
140
|
+
h['id'] = 1
|
141
|
+
h['title'] = "Contextual Attributes"
|
142
|
+
end
|
143
|
+
|
144
|
+
Assert(AttributeSerializer(article, :summary) == expected_hash)
|
145
|
+
end
|
146
|
+
|
147
|
+
eg "Hashes are passed straight through" do
|
148
|
+
Assert(AttributeSerializer({ :foo => 'bar' }) == { :foo => 'bar' })
|
149
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 4
|
8
|
-
-
|
9
|
-
version: 0.4.
|
8
|
+
- 1
|
9
|
+
version: 0.4.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Myles Byrne
|
@@ -14,21 +14,21 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-05-15 00:00:00 -05:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
|
-
name:
|
21
|
+
name: exemplor
|
22
22
|
prerelease: false
|
23
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - ">="
|
26
26
|
- !ruby/object:Gem::Version
|
27
27
|
segments:
|
28
|
+
- 3000
|
29
|
+
- 1
|
28
30
|
- 0
|
29
|
-
|
30
|
-
- 13
|
31
|
-
version: 0.10.13
|
31
|
+
version: 3000.1.0
|
32
32
|
type: :development
|
33
33
|
version_requirements: *id001
|
34
34
|
description:
|
@@ -43,8 +43,8 @@ files:
|
|
43
43
|
- .gitignore
|
44
44
|
- Rakefile
|
45
45
|
- VERSION
|
46
|
+
- examples.rb
|
46
47
|
- lib/attribute_serializer.rb
|
47
|
-
- test.rb
|
48
48
|
has_rdoc: true
|
49
49
|
homepage: http://github.com/quackingduck/attribute_serializer
|
50
50
|
licenses: []
|
data/test.rb
DELETED
@@ -1,149 +0,0 @@
|
|
1
|
-
require 'riot'
|
2
|
-
require 'lib/attribute_serializer'
|
3
|
-
|
4
|
-
def OHash &blk
|
5
|
-
ActiveSupport::OrderedHash.new.tap(&blk)
|
6
|
-
end
|
7
|
-
|
8
|
-
Class.class_eval do
|
9
|
-
def create(hash)
|
10
|
-
self.new.tap do |o| hash.each { |k,v| o.send("#{k}=", v) } end
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
class Author
|
15
|
-
attr_accessor :name, :email
|
16
|
-
end
|
17
|
-
|
18
|
-
class BlogPost
|
19
|
-
attr_accessor :id, :title, :body, :author
|
20
|
-
end
|
21
|
-
|
22
|
-
class PhotoBlogPost < BlogPost
|
23
|
-
attr_accessor :photo_url
|
24
|
-
end
|
25
|
-
|
26
|
-
context "Formatable object, default formator" do
|
27
|
-
setup do
|
28
|
-
AttributeSerializer BlogPost, %w(id title body)
|
29
|
-
|
30
|
-
BlogPost.create(
|
31
|
-
:id => 1,
|
32
|
-
:title => "Contextual Attributes",
|
33
|
-
:body => "The layer you've always wanted for generating your json"
|
34
|
-
)
|
35
|
-
end
|
36
|
-
|
37
|
-
asserts('produces the correct hash') do
|
38
|
-
AttributeSerializer topic # equivalent to AttributeSerializer(topic,:default)
|
39
|
-
end.equals(OHash { |h|
|
40
|
-
h['id'] = 1
|
41
|
-
h['title'] = "Contextual Attributes"
|
42
|
-
h['body'] = "The layer you've always wanted for generating your json"
|
43
|
-
})
|
44
|
-
end
|
45
|
-
|
46
|
-
context "Formatable subclass object, default formator" do
|
47
|
-
setup do
|
48
|
-
# define AttributeSerializer for *ancestor class*
|
49
|
-
AttributeSerializer BlogPost, %w(id title body)
|
50
|
-
|
51
|
-
@post = PhotoBlogPost.create(
|
52
|
-
:id => 1,
|
53
|
-
:title => "Contextual Attributes",
|
54
|
-
:body => "The layer you've always wanted for generating your json",
|
55
|
-
:photo_url => "http://foobar.com/lemonparty.jpg"
|
56
|
-
)
|
57
|
-
end
|
58
|
-
|
59
|
-
asserts('produces the correct hash') do
|
60
|
-
AttributeSerializer topic # equivalent to AttributeSerializer(topic,:default)
|
61
|
-
end.equals(OHash { |h|
|
62
|
-
h['id'] = 1
|
63
|
-
h['title'] = "Contextual Attributes"
|
64
|
-
h['body'] = "The layer you've always wanted for generating your json"
|
65
|
-
})
|
66
|
-
|
67
|
-
context "with subclass serializer" do
|
68
|
-
setup do
|
69
|
-
AttributeSerializer PhotoBlogPost, %w(id title body photo_url)
|
70
|
-
@post
|
71
|
-
end
|
72
|
-
|
73
|
-
asserts('produces the correct hash') do
|
74
|
-
AttributeSerializer topic # equivalent to AttributeSerializer(topic,:default)
|
75
|
-
end.equals(OHash { |h|
|
76
|
-
h['id'] = 1
|
77
|
-
h['title'] = "Contextual Attributes"
|
78
|
-
h['body'] = "The layer you've always wanted for generating your json"
|
79
|
-
h['photo_url'] = "http://foobar.com/lemonparty.jpg"
|
80
|
-
})
|
81
|
-
end
|
82
|
-
|
83
|
-
end
|
84
|
-
|
85
|
-
context "Nested formatable attrib" do
|
86
|
-
setup do
|
87
|
-
AttributeSerializer Author, %w(name email)
|
88
|
-
|
89
|
-
AttributeSerializer BlogPost, %w(id author) do
|
90
|
-
# no implicit support for nesting, intentionally
|
91
|
-
def author
|
92
|
-
AttributeSerializer formatee.author
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
BlogPost.create(
|
97
|
-
:id => 1,
|
98
|
-
:author => Author.create(
|
99
|
-
:name => "Myles",
|
100
|
-
:email => "myles@"
|
101
|
-
)
|
102
|
-
)
|
103
|
-
end
|
104
|
-
|
105
|
-
asserts('produces the correct hash') { AttributeSerializer(topic, :default) }.
|
106
|
-
equals(OHash { |h|
|
107
|
-
h['id'] = 1
|
108
|
-
h['author'] = OHash do |h|
|
109
|
-
h['name'] = "Myles"
|
110
|
-
h['email'] = "myles@"
|
111
|
-
end
|
112
|
-
})
|
113
|
-
end
|
114
|
-
|
115
|
-
context "Array of formatable objects" do
|
116
|
-
setup do
|
117
|
-
AttributeSerializer Author, %w(name email)
|
118
|
-
[ Author.create(:name => "Myles", :email => 'myles@'),
|
119
|
-
Author.create(:name => "Gabriel", :email => 'gabriel@') ]
|
120
|
-
end
|
121
|
-
|
122
|
-
asserts('produces the correct hash') { AttributeSerializer(topic, :default) }.
|
123
|
-
equals([
|
124
|
-
OHash { |h| h['name'] = "Myles"; h['email'] = 'myles@' },
|
125
|
-
OHash { |h| h['name'] = "Gabriel"; h['email'] = 'gabriel@' }
|
126
|
-
])
|
127
|
-
end
|
128
|
-
|
129
|
-
context "A non-default formatter" do
|
130
|
-
setup do
|
131
|
-
AttributeSerializer BlogPost, :summary, %w(id title)
|
132
|
-
BlogPost.create(
|
133
|
-
:id => 1,
|
134
|
-
:title => "Contextual Attributes",
|
135
|
-
:body => "The layer you've always wanted for generating your json"
|
136
|
-
)
|
137
|
-
end
|
138
|
-
|
139
|
-
asserts('produces the correct hash') { AttributeSerializer(topic, :summary) }.
|
140
|
-
equals(
|
141
|
-
OHash { |h| h['id'] = 1; h['title'] = "Contextual Attributes" }
|
142
|
-
)
|
143
|
-
end
|
144
|
-
|
145
|
-
context "Hashes are passed straight through" do
|
146
|
-
setup { AttributeSerializer({ :foo => 'bar' }) }
|
147
|
-
|
148
|
-
asserts_topic.equals({:foo => 'bar'})
|
149
|
-
end
|