out_put 2.1.1 → 2.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -2
- data/README.md +18 -9
- data/lib/out_put/version.rb +1 -1
- data/lib/out_put.rb +9 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0e453a328154fd0f958739f27e03523228013d42
|
4
|
+
data.tar.gz: b262f49030b0fc9b0a930e5d7471b37b8bbc918a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d79b90d8f999543c6cc9c0b1e294b415e8d858f799ade8cba7f4f4263b807e81fac840e4061de8f46be3d4a39424d1206c354b98a4c8fd736f3da0de4d731243
|
7
|
+
data.tar.gz: 513191dd1f9e1a750300e3f3714a12bb71ce046ab88299759c6710c7ac07b875641fb03972241385d2aa83542bad49f39666452dcf5080b769e7938d4cdac27d
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -18,7 +18,7 @@ And then execute:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
-
### Config (it's optional)
|
21
|
+
### 1. Config (it's optional)
|
22
22
|
|
23
23
|
initializers: `out_put.rb`
|
24
24
|
|
@@ -31,7 +31,7 @@ end
|
|
31
31
|
|
32
32
|
`project_code` (defaults to 0) + `code` will be the final code
|
33
33
|
|
34
|
-
### Basic
|
34
|
+
### 2. Basic
|
35
35
|
|
36
36
|
Add this line in your (base) controller:
|
37
37
|
|
@@ -53,7 +53,7 @@ output code: 0, msg: 'success'
|
|
53
53
|
ok # => code: 0, message: 'success'
|
54
54
|
```
|
55
55
|
|
56
|
-
### Response `data` filed
|
56
|
+
### 3. Response `data` filed
|
57
57
|
|
58
58
|
```ruby
|
59
59
|
output 0, foo: 'bar', list: [ 1, 2, 3 ]
|
@@ -67,13 +67,13 @@ output 0, foo: 'bar', list: [ 1, 2, 3 ]
|
|
67
67
|
ok_with foo: 'bar'
|
68
68
|
```
|
69
69
|
|
70
|
-
### Set HTTP status
|
70
|
+
### 4. Set HTTP status
|
71
71
|
|
72
72
|
```ruby
|
73
73
|
output 0, 'success', http: 200
|
74
74
|
```
|
75
75
|
|
76
|
-
### Error Response
|
76
|
+
### 5. Error Response
|
77
77
|
|
78
78
|
You don't need to pass your project code like '101', after **config**:
|
79
79
|
|
@@ -85,7 +85,7 @@ error_with 700, 'msg', foo: 'bar'
|
|
85
85
|
|
86
86
|
`error` is an alias of `output`
|
87
87
|
|
88
|
-
### `output` any objects which have implemented serialization method `info`
|
88
|
+
### 6. `output` any objects which have implemented serialization method `info`
|
89
89
|
|
90
90
|
```ruby
|
91
91
|
BusinessError.record_not_found.info # => { code: ..., msg: ... }
|
@@ -95,7 +95,7 @@ output BusinessError.record_not_found
|
|
95
95
|
|
96
96
|
[About `business_error`](https://github.com/zhandao/business_error/)
|
97
97
|
|
98
|
-
### Just render the given data without default format
|
98
|
+
### 7. Just render the given data without default format
|
99
99
|
|
100
100
|
```ruby
|
101
101
|
output only: { foo: 'bar' }
|
@@ -105,9 +105,9 @@ output only: { foo: 'bar' }
|
|
105
105
|
output only: [ 1, 2, 3 ]
|
106
106
|
```
|
107
107
|
|
108
|
-
### Other
|
108
|
+
### 8. Other
|
109
109
|
|
110
|
-
####
|
110
|
+
#### 8.a automatically set `total`:
|
111
111
|
|
112
112
|
if `config.pagination_for = :list`:
|
113
113
|
|
@@ -119,3 +119,12 @@ output 0, list: [ 1, 2, 3 ]
|
|
119
119
|
# data: { total: 3, list: [1,2,3] }
|
120
120
|
# }
|
121
121
|
```
|
122
|
+
|
123
|
+
#### 8.b use `build_with` to pass an variable `@view` to your view
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
build_with data: 'hello'
|
127
|
+
|
128
|
+
# the in your .jbuilder
|
129
|
+
json.data @view[:data]
|
130
|
+
```
|
data/lib/out_put/version.rb
CHANGED
data/lib/out_put.rb
CHANGED
@@ -13,7 +13,7 @@ module OutPut
|
|
13
13
|
msg = 'success' if msg.blank? && code.zero?
|
14
14
|
render json: {
|
15
15
|
result: { code: code, message: msg },
|
16
|
-
data:
|
16
|
+
data: _output_data(data)
|
17
17
|
}, status: http
|
18
18
|
end
|
19
19
|
|
@@ -22,11 +22,17 @@ module OutPut
|
|
22
22
|
alias error output
|
23
23
|
alias error_with output
|
24
24
|
|
25
|
-
def
|
25
|
+
def _output_data(data)
|
26
26
|
if data.key?(Config.pagination_for)
|
27
|
-
|
27
|
+
# TODO now is noly for AR
|
28
|
+
data.merge!(total: data[Config.pagination_for].try(:unscoped).count)
|
28
29
|
end
|
29
30
|
|
30
31
|
data
|
31
32
|
end
|
33
|
+
|
34
|
+
def build_with(**data)
|
35
|
+
@view = data
|
36
|
+
# Then jump to your view
|
37
|
+
end
|
32
38
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: out_put
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- zhandao
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-10-
|
11
|
+
date: 2018-10-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|