nested 0.0.23 → 0.0.24

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 471ad424d8ae4b8514a2ed8e1916d8cc0994d4eb
4
- data.tar.gz: c2084f08d95070dd85116579c8bb3b87d4c560cc
3
+ metadata.gz: 38c9a0d53765eb2f648f9d4f5ef2a9fb5fc4b528
4
+ data.tar.gz: 3d8212a2d22886567404470d0b288514a961f2b5
5
5
  SHA512:
6
- metadata.gz: 9de43f76316d0f4406df50cb9535c9fa9fd936fc5e96a4d5962c47464bff532467d709d82aaecb2f753f5630627fbd44fac8cfd7a8e7584d7834bb255064e7d6
7
- data.tar.gz: 1b0861468765d7928b9ba85e172e91a31fc3cdf51b9ca27774dc24a3f3b00048485056fdb9acb0a211246da840a238b02120df83c010fa3680da05ee67e0be7a
6
+ metadata.gz: 9e9ebefde4d1f116da2ceeaa625334d2eba6b1b54dcd45cf25e2c71f32722467b1bfbd9181f634c440704d19251cfe7fdcffd8ba4542011f48e70bde04b05698
7
+ data.tar.gz: 2d7284067772373590898e15fc13cfe8df87581c39c4f446360b82199a851e32dd7a788c6f97647067d85059a732f24b328ca3a437cd8c0df49980a541158b60
data/lib/nested.rb CHANGED
@@ -60,12 +60,7 @@ module Nested
60
60
  end
61
61
 
62
62
  if member?
63
- __serialize_args = @parent.instance_variable_get("@__serialize_args")
64
- __serialize_block = @parent.instance_variable_get("@__serialize_block")
65
-
66
- serialize *__serialize_args, &__serialize_block
67
- else
68
- serialize &->(obj) { raise "implement serializer for #{@__resource.type} #{@__resource.name}" }
63
+ serialize *@parent.instance_variable_get("@__serialize_args")
69
64
  end
70
65
  end
71
66
 
@@ -93,17 +88,20 @@ module Nested
93
88
  end
94
89
  end
95
90
 
96
- def serialize(*args, &block)
97
- raise "pass either *args or &block" if args.empty? && !block && !member?
98
-
91
+ def serialize(*args)
99
92
  @__serialize_args = args
100
- @__serialize_block = block
101
93
 
102
94
  @__serialize = ->(obj) do
103
- obj = self.instance_exec(obj, &block) if block
104
- obj = obj.attributes if obj.is_a?(ActiveRecord::Base)
105
- obj = obj.symbolize_keys.slice(*args) unless args.empty?
106
- obj
95
+ args.inject({}) do |memo, field|
96
+ case field
97
+ when Symbol
98
+ memo[field] = obj.is_a?(Hash) ? obj[field] : obj.send(field)
99
+ when Hash
100
+ field, proc = field.to_a.first
101
+ memo[field] = self.instance_exec(obj, &proc)
102
+ end
103
+ memo
104
+ end
107
105
  end
108
106
  end
109
107
 
data/nested.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "nested"
3
- s.version = "0.0.23"
3
+ s.version = "0.0.24"
4
4
 
5
5
  s.authors = ["Jan Zimmek"]
6
6
  s.email = %q{jan.zimmek@web.de}
data/test/nested_test.rb CHANGED
@@ -313,12 +313,15 @@ class NestedTest < Test::Unit::TestCase
313
313
 
314
314
  def test_serializer
315
315
  singleton!
316
- # assert_equal(@r.serializer, Nested::Resource::SERIALIZE)
317
316
 
318
- ser = ->(obj) { obj }
319
- @r.serialize &ser
317
+ @r.serialize :name
318
+
319
+ assert_equal({name: "joe"}, @r.instance_variable_get("@__serialize").call({name: "joe"}))
320
+ assert_equal({name: "joe"}, @r.instance_variable_get("@__serialize").call({name: "joe", boss: true}))
321
+ assert_equal({name: "joe"}, @r.instance_variable_get("@__serialize").call(OpenStruct.new({name: "joe"})))
320
322
 
321
- assert_equal 1, @r.instance_variable_get("@__serialize").call(1)
323
+ @r.serialize :name, virtual: ->(o){ o[:name] + "!!" }
324
+ assert_equal({name: "joe", virtual: "joe!!"}, @r.instance_variable_get("@__serialize").call({name: "joe"}))
322
325
  end
323
326
 
324
327
  # ----
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nested
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.23
4
+ version: 0.0.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Zimmek
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-05 00:00:00.000000000 Z
11
+ date: 2014-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport