decoradar 0.0.1 → 0.1.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.
- checksums.yaml +13 -5
- data/.travis.yml +11 -2
- data/README.md +5 -5
- data/lib/decoradar/collection.rb +22 -0
- data/lib/decoradar/version.rb +1 -1
- data/lib/decoradar.rb +7 -0
- metadata +15 -14
checksums.yaml
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZjdhYTE0NDhjN2YyZjQ0NjljYjNhYzQzMGE2MDRhNGIyMDQxN2MzZQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MzBhOWI0YmM1ZjZkYzgwMzI5YTA1MWE0YjE2ZjBjMjc2MTc2YmZhMw==
|
5
7
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZWZhNjBiM2E3ZDE0NGUzN2VhZTM4ZDJlY2FiNTRmZTVjYjg5ODBjYmE5NTZm
|
10
|
+
MmNhMzc5ZjM3MmVjNGMyNGEzN2ZlNWUwYWNjODEwZTY4YjkwYjVjNjM4YzU5
|
11
|
+
YmI5ZDA5N2VkZjQ1NmZiNWQ3ZTNmZWE4ZGNlMWQyZWJmMDMyMTY=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
YWI3MTViZjg2Njk4NWMxNzcxYTM5OGU1Yjk0NTgwNTllYjhhZmIxYjRiZDEw
|
14
|
+
Y2RmZTJhNmNkOTQ5MmJjM2UyNTcwZjU5NmZmZDExMDEyYmI1NDI4Zjc1MThh
|
15
|
+
Mzk3NTAyOGNkNTRkYTNkYmFkYzkxZGNhYjQ0NTk0NzMyMjdmYjQ=
|
data/.travis.yml
CHANGED
@@ -1,6 +1,15 @@
|
|
1
1
|
sudo: false
|
2
2
|
language: ruby
|
3
|
+
bundler_args: --without tools
|
4
|
+
cache: bundler
|
3
5
|
rvm:
|
4
|
-
-
|
5
|
-
- 2.
|
6
|
+
- 1.9.3
|
7
|
+
- 2.0
|
8
|
+
- 2.1
|
9
|
+
- 2.2
|
10
|
+
- jruby
|
11
|
+
- ruby-head
|
6
12
|
before_install: gem install bundler -v 1.12.5
|
13
|
+
matrix:
|
14
|
+
allow_failures:
|
15
|
+
- rvm: ruby-head
|
data/README.md
CHANGED
@@ -6,9 +6,7 @@ Decoradar is a simple decorator + serializer in Ruby
|
|
6
6
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
|
-
|
10
|
-
gem 'decoradar'
|
11
|
-
```
|
9
|
+
gem 'decoradar'
|
12
10
|
|
13
11
|
And then execute:
|
14
12
|
|
@@ -31,10 +29,12 @@ class UserSerializer
|
|
31
29
|
|
32
30
|
attributes :id, :username
|
33
31
|
attribute :full_name, as: :name
|
32
|
+
|
33
|
+
collection :posts, serializer: PostSerializer
|
34
34
|
end
|
35
35
|
|
36
36
|
UserSerializer.new(@user).as_json
|
37
|
-
# => { id: 1, username: "huynhquancam", name: "Cam Huynh" }
|
37
|
+
# => { id: 1, username: "huynhquancam", name: "Cam Huynh", posts: [{...}] }
|
38
38
|
```
|
39
39
|
|
40
40
|
Let's say you want to tweak `full_name` a bit.
|
@@ -86,7 +86,7 @@ UserSerializer.decorate_collection(@users).map(&:as_json)
|
|
86
86
|
|
87
87
|
## Contributing
|
88
88
|
|
89
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
89
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/qcam/decoradar.
|
90
90
|
This project is intended to be a safe, welcoming space for collaboration, and contributors are
|
91
91
|
expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
92
92
|
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Decoradar
|
2
|
+
class Collection < Attribute
|
3
|
+
attr_reader :serializer
|
4
|
+
|
5
|
+
def initialize(options = {})
|
6
|
+
super(options)
|
7
|
+
@serializer = options.fetch(:serializer)
|
8
|
+
end
|
9
|
+
|
10
|
+
def serialize(hash, collection)
|
11
|
+
hash.merge(as => _serialize(collection))
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def _serialize(collection)
|
17
|
+
collection.map do |obj|
|
18
|
+
serializer.new(obj).as_json
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/decoradar/version.rb
CHANGED
data/lib/decoradar.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require 'set'
|
2
2
|
require 'decoradar/attribute'
|
3
|
+
require 'decoradar/collection'
|
3
4
|
|
4
5
|
# Decorates and serializes model into a hash
|
5
6
|
#
|
@@ -44,6 +45,12 @@ module Decoradar
|
|
44
45
|
class_eval { def_delegators(:model, attr.name) }
|
45
46
|
end
|
46
47
|
|
48
|
+
def collection(name, options = {})
|
49
|
+
col = Collection.new(options.merge(name: name))
|
50
|
+
self.attribute_set << col
|
51
|
+
class_eval { def_delegators(:model, col.name) }
|
52
|
+
end
|
53
|
+
|
47
54
|
def decorate_collection(collection)
|
48
55
|
raise TypeError if !collection.respond_to?(:map)
|
49
56
|
|
metadata
CHANGED
@@ -1,55 +1,55 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: decoradar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cẩm Huỳnh
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-06-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.12'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.12'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ~>
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '10.0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '10.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ~>
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '3.5'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ~>
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.5'
|
55
55
|
description: Simple decorator + serializer in Ruby
|
@@ -59,9 +59,9 @@ executables: []
|
|
59
59
|
extensions: []
|
60
60
|
extra_rdoc_files: []
|
61
61
|
files:
|
62
|
-
-
|
63
|
-
-
|
64
|
-
-
|
62
|
+
- .gitignore
|
63
|
+
- .rspec
|
64
|
+
- .travis.yml
|
65
65
|
- CODE_OF_CONDUCT.md
|
66
66
|
- Gemfile
|
67
67
|
- LICENSE.txt
|
@@ -72,6 +72,7 @@ files:
|
|
72
72
|
- decoradar.gemspec
|
73
73
|
- lib/decoradar.rb
|
74
74
|
- lib/decoradar/attribute.rb
|
75
|
+
- lib/decoradar/collection.rb
|
75
76
|
- lib/decoradar/version.rb
|
76
77
|
homepage: https://github.com/huynhquancam/decoradar
|
77
78
|
licenses:
|
@@ -84,17 +85,17 @@ require_paths:
|
|
84
85
|
- lib
|
85
86
|
required_ruby_version: !ruby/object:Gem::Requirement
|
86
87
|
requirements:
|
87
|
-
- -
|
88
|
+
- - ! '>='
|
88
89
|
- !ruby/object:Gem::Version
|
89
90
|
version: '0'
|
90
91
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
92
|
requirements:
|
92
|
-
- -
|
93
|
+
- - ! '>='
|
93
94
|
- !ruby/object:Gem::Version
|
94
95
|
version: '0'
|
95
96
|
requirements: []
|
96
97
|
rubyforge_project:
|
97
|
-
rubygems_version: 2.
|
98
|
+
rubygems_version: 2.6.10
|
98
99
|
signing_key:
|
99
100
|
specification_version: 4
|
100
101
|
summary: Simple decorator + serializer
|