smart_adapters 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +15 -6
- data/lib/smart_adapters/version.rb +1 -1
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eb19d73886d1004c3f7a6013730e74d8850c2a62f9ebdbadc5bd73a20c4174b7
|
4
|
+
data.tar.gz: 5d84224b5c54ee3dee1b57f83d5cdc0d0e5d4009889da2ec0abd99eb60581e78
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 27280726284035ae961f4c0e76da0a72d9d6cc2522300b09597b2632f7b10740fc239dd79b35472e9d12f3b8c656f3eaed8d901344e1808ebaf8384907c3b6b5
|
7
|
+
data.tar.gz: a692b2b8ae0c5848fc51f88c26cd0cc27fa94ef8fa3a437148c4a0e7fbf506225644fd968d350bbf20b962f522561532ade1496dfcce69c853b62b95a5ff6d90
|
data/README.md
CHANGED
@@ -2,8 +2,8 @@
|
|
2
2
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/smart_adapters.svg)](https://badge.fury.io/rb/smart_adapters) [![Build Status](https://travis-ci.org/andrearampin/smart_adapters.svg?branch=master)](https://travis-ci.org/andrearampin/smart_adapters) [![Maintainability](https://api.codeclimate.com/v1/badges/9d55d1d054401ab93a6e/maintainability)](https://codeclimate.com/github/andrearampin/smart_adapters/maintainability)
|
4
4
|
|
5
|
-
Smart Adapters
|
6
|
-
In the [Ruby on Rails documentation](https://apidock.com/rails/ActionController/MimeResponds/InstanceMethods/respond_to), the controller
|
5
|
+
Smart Adapters neatly decouple the controllers from the views independently by the request format.
|
6
|
+
In the [Ruby on Rails documentation](https://apidock.com/rails/ActionController/MimeResponds/InstanceMethods/respond_to), the controller decides the format of the answer based on the (Content-Type) request:
|
7
7
|
|
8
8
|
```ruby
|
9
9
|
# app/controllers/people_controller.rb
|
@@ -16,28 +16,29 @@ def index
|
|
16
16
|
end
|
17
17
|
```
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
The Smart Adapters solve this problem by delegating the task of properly render the response based on the request format to well-defined classes, one per format. The following implementation makes use of the Smart Adapters:
|
19
|
+
The problem here is that over time the controllers inherit unnecessary complexity due to the evolution of the project (new logics and/or formats). The Smart Adapters solve the problem responding to a request with the appropriate format by using purposely designed classes.
|
22
20
|
|
23
21
|
```ruby
|
22
|
+
# Controller
|
24
23
|
# app/controllers/people_controller.rb
|
25
24
|
def index
|
26
25
|
current_adapter.success Person.all
|
27
26
|
end
|
28
27
|
|
28
|
+
# HTML Adapter
|
29
29
|
# app/models/smart_adapters/people/index/html_adapter.rb
|
30
30
|
def success(people)
|
31
31
|
render 'people/show', locals: { people: people }
|
32
32
|
end
|
33
33
|
|
34
|
+
# XML Adapter
|
34
35
|
# app/models/smart_adapters/people/index/xml_adapter.rb
|
35
36
|
def success(people)
|
36
37
|
render xml: people, status: :ok
|
37
38
|
end
|
38
39
|
```
|
39
40
|
|
40
|
-
|
41
|
+
The application can now be kept dry while introducing new formats and functionalities. For instance, in case of an XML request, the adapter could add more details to the `people` collection or track some metrics without bloating the controller.
|
41
42
|
|
42
43
|
```ruby
|
43
44
|
# app/models/smart_adapters/people/index/xml_adapter.rb
|
@@ -69,6 +70,14 @@ end
|
|
69
70
|
|
70
71
|
Add the adapters for your **controller/action/format**.
|
71
72
|
|
73
|
+
## Formats supported
|
74
|
+
- HTML
|
75
|
+
- JSON
|
76
|
+
- JS
|
77
|
+
- XML
|
78
|
+
- CSV
|
79
|
+
- TXT
|
80
|
+
|
72
81
|
### Example
|
73
82
|
|
74
83
|
`app/controlles/users_controller.rb`
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smart_adapters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrea Rampin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-11-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -52,8 +52,7 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
description: Smart Adapters
|
56
|
-
respond to any request format.
|
55
|
+
description: Smart Adapters keep Rails controllers dry.
|
57
56
|
email:
|
58
57
|
- andrea.rampin@gmail.com
|
59
58
|
executables: []
|