attribute_serializer 0.3.0 → 0.4.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 +6 -5
  3. data/test.rb +7 -0
  4. metadata +20 -9
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
@@ -40,6 +40,7 @@ module AttributeSerializer
40
40
  end
41
41
 
42
42
  def generate(context_name, object)
43
+ return object if object.is_a?(Hash) && context_name == :default
43
44
  if object.respond_to? :collect
44
45
  object.collect { |o| generate_single(o.class, context_name, o) }
45
46
  else
@@ -48,15 +49,15 @@ module AttributeSerializer
48
49
  end
49
50
 
50
51
  def generate_single(klass, context_name, object)
51
- context = context_for([klass, context_name])
52
+ context = context_for(klass, context_name)
52
53
  raise ArgumentError, "no contextual attributes setup for #{klass}:#{context_name}" unless context
53
54
  context.generate(object)
54
55
  end
55
56
 
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) }
57
+ def context_for(klass, context_name)
58
+ closest_context_match = contexts.keys.select do |c,n|
59
+ klass.ancestors.include?(c) && context_name == n
60
+ end.min_by { |c, _| klass.ancestors.index(c) }
60
61
 
61
62
  contexts[closest_context_match]
62
63
  end
data/test.rb CHANGED
@@ -45,6 +45,7 @@ end
45
45
 
46
46
  context "Formatable subclass object, default formator" do
47
47
  setup do
48
+ # define AttributeSerializer for *ancestor class*
48
49
  AttributeSerializer BlogPost, %w(id title body)
49
50
 
50
51
  @post = PhotoBlogPost.create(
@@ -139,4 +140,10 @@ context "A non-default formatter" do
139
140
  equals(
140
141
  OHash { |h| h['id'] = 1; h['title'] = "Contextual Attributes" }
141
142
  )
143
+ end
144
+
145
+ context "Hashes are passed straight through" do
146
+ setup { AttributeSerializer({ :foo => 'bar' }) }
147
+
148
+ asserts_topic.equals({:foo => 'bar'})
142
149
  end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: attribute_serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 4
8
+ - 0
9
+ version: 0.4.0
5
10
  platform: ruby
6
11
  authors:
7
12
  - Myles Byrne
@@ -9,19 +14,23 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-03-17 00:00:00 -05:00
17
+ date: 2010-03-24 00:00:00 -05:00
13
18
  default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: riot
17
- type: :development
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 10
30
+ - 13
23
31
  version: 0.10.13
24
- version:
32
+ type: :development
33
+ version_requirements: *id001
25
34
  description:
26
35
  email: myles@myles.id.au
27
36
  executables: []
@@ -49,18 +58,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
49
58
  requirements:
50
59
  - - ">="
51
60
  - !ruby/object:Gem::Version
61
+ segments:
62
+ - 0
52
63
  version: "0"
53
- version:
54
64
  required_rubygems_version: !ruby/object:Gem::Requirement
55
65
  requirements:
56
66
  - - ">="
57
67
  - !ruby/object:Gem::Version
68
+ segments:
69
+ - 0
58
70
  version: "0"
59
- version:
60
71
  requirements: []
61
72
 
62
73
  rubyforge_project:
63
- rubygems_version: 1.3.5
74
+ rubygems_version: 1.3.6
64
75
  signing_key:
65
76
  specification_version: 3
66
77
  summary: Takes an object, serializes the attributes to an ordered hash based on a pre-defined schema