attribute_serializer 0.4.1 → 0.5.0
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/VERSION +1 -1
- data/examples.rb +4 -0
- data/lib/attribute_serializer.rb +13 -6
- metadata +12 -5
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.5.0
|
data/examples.rb
CHANGED
@@ -146,4 +146,8 @@ end
|
|
146
146
|
|
147
147
|
eg "Hashes are passed straight through" do
|
148
148
|
Assert(AttributeSerializer({ :foo => 'bar' }) == { :foo => 'bar' })
|
149
|
+
end
|
150
|
+
|
151
|
+
eg "Arrays of strings are passed straight through" do
|
152
|
+
Assert(AttributeSerializer(['a','b']) == ['a','b'])
|
149
153
|
end
|
data/lib/attribute_serializer.rb
CHANGED
@@ -12,7 +12,7 @@ module AttributeSerializer
|
|
12
12
|
@attribs = attribs
|
13
13
|
@delegate_class = DelegateClass(klass)
|
14
14
|
@delegate_class.class_eval do
|
15
|
-
def delegatee; __getobj__ end
|
15
|
+
def delegatee; __getobj__ end
|
16
16
|
def id; formatee.id end # DelegateClass was written before object_id became the new world order
|
17
17
|
alias_method :formatee, :delegatee # this was a stupid name
|
18
18
|
end
|
@@ -49,6 +49,7 @@ module AttributeSerializer
|
|
49
49
|
end
|
50
50
|
|
51
51
|
def generate_single(klass, context_name, object)
|
52
|
+
return object if klass == String
|
52
53
|
context = context_for(klass, context_name)
|
53
54
|
raise ArgumentError, "no contextual attributes setup for #{klass}:#{context_name}" unless context
|
54
55
|
context.generate(object)
|
@@ -64,9 +65,11 @@ module AttributeSerializer
|
|
64
65
|
|
65
66
|
end
|
66
67
|
|
67
|
-
#
|
68
|
+
# Public: Define a serializer or serialize an object or collection
|
68
69
|
#
|
69
|
-
#
|
70
|
+
# Examples
|
71
|
+
#
|
72
|
+
# Define a default serializer on your class:
|
70
73
|
#
|
71
74
|
# AttributeSerializer BlogPost, %w(id created_at title body) do
|
72
75
|
# def body
|
@@ -74,18 +77,22 @@ end
|
|
74
77
|
# end
|
75
78
|
# end
|
76
79
|
#
|
77
|
-
# Then
|
80
|
+
# Then serialize and instance like:
|
78
81
|
#
|
79
82
|
# AttributeSerializer @post
|
80
83
|
#
|
81
|
-
# You can also define other
|
84
|
+
# You can also define other serializers:
|
82
85
|
#
|
83
86
|
# AttributeSerializer BlogPost, :summary, %w(id created_at title)
|
84
87
|
#
|
85
|
-
# And
|
88
|
+
# And serialize collections:
|
86
89
|
#
|
87
90
|
# AttributeSerializer @posts, :summary
|
88
91
|
#
|
92
|
+
# AttributeSerializer returns an OrderedHash as the serialization, it's up to
|
93
|
+
# you to call #to_json or #to_yaml on that object
|
94
|
+
#
|
95
|
+
# Returns an OrderedHash serialization when given an object or collection
|
89
96
|
def AttributeSerializer(*args, &blk)
|
90
97
|
if args.first.is_a?(Class)
|
91
98
|
klass, context_name, attribs = (args.size == 3) ? args : [args[0], :default, args[1]]
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attribute_serializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 11
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
8
|
+
- 5
|
9
|
+
- 0
|
10
|
+
version: 0.5.0
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Myles Byrne
|
@@ -14,16 +15,18 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2010-05-
|
18
|
+
date: 2010-05-26 00:00:00 -05:00
|
18
19
|
default_executable:
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|
21
22
|
name: exemplor
|
22
23
|
prerelease: false
|
23
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
24
26
|
requirements:
|
25
27
|
- - ">="
|
26
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 24027
|
27
30
|
segments:
|
28
31
|
- 3000
|
29
32
|
- 1
|
@@ -55,23 +58,27 @@ rdoc_options:
|
|
55
58
|
require_paths:
|
56
59
|
- lib
|
57
60
|
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
58
62
|
requirements:
|
59
63
|
- - ">="
|
60
64
|
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
61
66
|
segments:
|
62
67
|
- 0
|
63
68
|
version: "0"
|
64
69
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
65
71
|
requirements:
|
66
72
|
- - ">="
|
67
73
|
- !ruby/object:Gem::Version
|
74
|
+
hash: 3
|
68
75
|
segments:
|
69
76
|
- 0
|
70
77
|
version: "0"
|
71
78
|
requirements: []
|
72
79
|
|
73
80
|
rubyforge_project:
|
74
|
-
rubygems_version: 1.3.
|
81
|
+
rubygems_version: 1.3.7
|
75
82
|
signing_key:
|
76
83
|
specification_version: 3
|
77
84
|
summary: Takes an object, serializes the attributes to an ordered hash based on a pre-defined schema
|