attribute_serializer 0.2.0 → 0.3.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.
Files changed (4) hide show
  1. data/VERSION +1 -1
  2. data/lib/attribute_serializer.rb +14 -5
  3. data/test.rb +43 -1
  4. metadata +1 -1
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
@@ -12,8 +12,9 @@ module AttributeSerializer
12
12
  @attribs = attribs
13
13
  @delegate_class = DelegateClass(klass)
14
14
  @delegate_class.class_eval do
15
- def formatee; __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
+ alias_method :formatee, :delegatee # this was a stupid name
17
18
  end
18
19
  @delegate_class.class_eval(&delegate_methods) if delegate_methods
19
20
  end
@@ -47,9 +48,17 @@ module AttributeSerializer
47
48
  end
48
49
 
49
50
  def generate_single(klass, context_name, object)
50
- key = [klass, context_name]
51
- raise ArgumentError, "no contextual attributes setup for #{klass}:#{context_name}" unless contexts.keys.include?(key)
52
- contexts[key].generate(object)
51
+ context = context_for([klass, context_name])
52
+ raise ArgumentError, "no contextual attributes setup for #{klass}:#{context_name}" unless context
53
+ context.generate(object)
54
+ end
55
+
56
+ def context_for(key)
57
+ closest_context_match = contexts.keys.select do |klass,context_name|
58
+ key.first.ancestors.include?(klass) && key.last == context_name
59
+ end.min_by { |(klass, _)| key.first.ancestors.index(klass) }
60
+
61
+ contexts[closest_context_match]
53
62
  end
54
63
 
55
64
  end
@@ -69,7 +78,7 @@ end
69
78
  # AttributeSerializer @post
70
79
  #
71
80
  # You can also define other formatters
72
- #
81
+ #
73
82
  # AttributeSerializer BlogPost, :summary, %w(id created_at title)
74
83
  #
75
84
  # And you AttributeSerializer can produce formatted collections:
data/test.rb CHANGED
@@ -19,6 +19,10 @@ class BlogPost
19
19
  attr_accessor :id, :title, :body, :author
20
20
  end
21
21
 
22
+ class PhotoBlogPost < BlogPost
23
+ attr_accessor :photo_url
24
+ end
25
+
22
26
  context "Formatable object, default formator" do
23
27
  setup do
24
28
  AttributeSerializer BlogPost, %w(id title body)
@@ -31,7 +35,7 @@ context "Formatable object, default formator" do
31
35
  end
32
36
 
33
37
  asserts('produces the correct hash') do
34
- AttributeSerializer topic # equivanlent to AttributeSerializer(topic,:default)
38
+ AttributeSerializer topic # equivalent to AttributeSerializer(topic,:default)
35
39
  end.equals(OHash { |h|
36
40
  h['id'] = 1
37
41
  h['title'] = "Contextual Attributes"
@@ -39,6 +43,44 @@ context "Formatable object, default formator" do
39
43
  })
40
44
  end
41
45
 
46
+ context "Formatable subclass object, default formator" do
47
+ setup do
48
+ AttributeSerializer BlogPost, %w(id title body)
49
+
50
+ @post = PhotoBlogPost.create(
51
+ :id => 1,
52
+ :title => "Contextual Attributes",
53
+ :body => "The layer you've always wanted for generating your json",
54
+ :photo_url => "http://foobar.com/lemonparty.jpg"
55
+ )
56
+ end
57
+
58
+ asserts('produces the correct hash') do
59
+ AttributeSerializer topic # equivalent to AttributeSerializer(topic,:default)
60
+ end.equals(OHash { |h|
61
+ h['id'] = 1
62
+ h['title'] = "Contextual Attributes"
63
+ h['body'] = "The layer you've always wanted for generating your json"
64
+ })
65
+
66
+ context "with subclass serializer" do
67
+ setup do
68
+ AttributeSerializer PhotoBlogPost, %w(id title body photo_url)
69
+ @post
70
+ end
71
+
72
+ asserts('produces the correct hash') do
73
+ AttributeSerializer topic # equivalent to AttributeSerializer(topic,:default)
74
+ end.equals(OHash { |h|
75
+ h['id'] = 1
76
+ h['title'] = "Contextual Attributes"
77
+ h['body'] = "The layer you've always wanted for generating your json"
78
+ h['photo_url'] = "http://foobar.com/lemonparty.jpg"
79
+ })
80
+ end
81
+
82
+ end
83
+
42
84
  context "Nested formatable attrib" do
43
85
  setup do
44
86
  AttributeSerializer Author, %w(name email)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attribute_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Myles Byrne