decoradar 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +13 -7
- data/lib/decoradar.rb +8 -1
- data/lib/decoradar/errors.rb +15 -0
- data/lib/decoradar/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NWRjNjA5NGQzOGE3MzI4Y2I2MDljYmZhMmVjOWI1MjkyMDE4NDU0OA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
N2YxZWVmNzg0MDVkOGVlYjZmZmZkNTY2ZTNmYzkyNzY5NjRhNWZjNg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
YmMyZjM3YTA0ZjlmNjA5MDMyOTNmYTExYjYxM2NmYmY0ZjUwMjcxODFlODU1
|
10
|
+
ZjgzZTRmZGY4ZjYyYmVkM2YzNGFiNTZkZTA5ZDFlNzA0NTM0NzcyOGQyMTM0
|
11
|
+
NDk5Mjk3YzEzNDExODczMGZmNmY2NDMyYjc4ODZkN2M1MjA0MDg=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NDhhNGNjYjIxOGE3OGQ0OTY4NTIzYzQ1YzU0ZDQzMzJjODE3Y2IwZmZiMzUx
|
14
|
+
M2RiZmI2OTk0MjUyMjNjYzFmZmM3ZWY3NGJkNGFmZTgzYjE2ZWFhMWY1NWU1
|
15
|
+
NDdmMDRmNGVkNGZhZmYzMGM0MzI3ZDZiMTIwZWQxNjAzOWYzNGU=
|
data/README.md
CHANGED
@@ -1,25 +1,31 @@
|
|
1
1
|
# Decoradar
|
2
2
|
|
3
|
-
|
3
|
+
[![Travis](https://img.shields.io/travis/qcam/decoradar.svg)](https://travis-ci.org/qcam/decoradar)
|
4
|
+
[![RubyGems](https://img.shields.io/gem/v/decoradar.svg)](https://rubygems.org/gems/decoradar)
|
5
|
+
|
6
|
+
As the name might have implied, Decoradar is a simple JSON serializer + decorator in Ruby.
|
4
7
|
|
5
8
|
## Installation
|
6
9
|
|
7
10
|
Add this line to your application's Gemfile:
|
8
11
|
|
9
|
-
gem 'decoradar'
|
12
|
+
gem 'decoradar', '~> 0.1.1'
|
10
13
|
|
11
14
|
And then execute:
|
12
15
|
|
13
16
|
$ bundle
|
14
17
|
|
15
|
-
|
18
|
+
## Why Decoradar?
|
16
19
|
|
17
|
-
|
20
|
+
Decoradar is really **simple** and **magic-free** (zero monkey patch, zero auto coercion)
|
21
|
+
and aims to bring ActiveModel::Serializer to Plain Ruby Object.
|
18
22
|
|
19
|
-
|
23
|
+
What will Decoradar brings you?
|
20
24
|
|
21
|
-
|
22
|
-
and
|
25
|
+
- `ActiveModel::Serializer` DSL style.
|
26
|
+
- Magic free and explicit: there is no monkey patching or auto coercion.
|
27
|
+
- Isolated testing: it's just Ruby objects without Rails dependence so feel free to unit
|
28
|
+
test it. (when I said unit-test I meant it)
|
23
29
|
|
24
30
|
## Usage
|
25
31
|
|
data/lib/decoradar.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'set'
|
2
2
|
require 'decoradar/attribute'
|
3
3
|
require 'decoradar/collection'
|
4
|
+
require 'decoradar/errors'
|
4
5
|
|
5
6
|
# Decorates and serializes model into a hash
|
6
7
|
#
|
@@ -77,6 +78,8 @@ module Decoradar
|
|
77
78
|
end
|
78
79
|
end
|
79
80
|
|
81
|
+
private
|
82
|
+
|
80
83
|
def enrich_json(json, attribute)
|
81
84
|
if attribute.including?(model)
|
82
85
|
attribute.serialize(json, value_of(attribute))
|
@@ -86,7 +89,11 @@ module Decoradar
|
|
86
89
|
end
|
87
90
|
|
88
91
|
def value_of(attribute)
|
89
|
-
|
92
|
+
attribute_name = attribute.name.to_sym
|
93
|
+
|
94
|
+
self.public_send(attribute_name)
|
95
|
+
rescue NoMethodError
|
96
|
+
raise Decoradar::AttributeNotFound.new(self, attribute_name)
|
90
97
|
end
|
91
98
|
end
|
92
99
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module Decoradar
|
2
|
+
class AttributeNotFound < ::RuntimeError
|
3
|
+
attr_reader :decorated, :attribute_name
|
4
|
+
|
5
|
+
def initialize(decorated, attribute_name)
|
6
|
+
@decorated = decorated
|
7
|
+
@attribute_name = attribute_name
|
8
|
+
super()
|
9
|
+
end
|
10
|
+
|
11
|
+
def message
|
12
|
+
"Attribute ##{attribute_name} not implemented on model #{decorated.class}"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/decoradar/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: decoradar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
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-06-
|
11
|
+
date: 2017-06-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -73,6 +73,7 @@ files:
|
|
73
73
|
- lib/decoradar.rb
|
74
74
|
- lib/decoradar/attribute.rb
|
75
75
|
- lib/decoradar/collection.rb
|
76
|
+
- lib/decoradar/errors.rb
|
76
77
|
- lib/decoradar/version.rb
|
77
78
|
homepage: https://github.com/huynhquancam/decoradar
|
78
79
|
licenses:
|