out_put 2.2.1 → 2.3.0

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: 5eb4abc2cead8a3b3f71d44018f0d4effc25229b
4
- data.tar.gz: eebd9c033b8bab41a2c8f293b6b38cf281b84cfa
3
+ metadata.gz: 8b5565a2cfe9fabb5a97d40b9bc7c2f512fcad54
4
+ data.tar.gz: f57098b58b8d4ba72d8c7536bfd89e9712b670bb
5
5
  SHA512:
6
- metadata.gz: b933aeeaf70c301eac67f472eb3380916d78d4799cd7868773f4632aebb8c2e4752ed32b5b9ec087bc356648283da316a16df007c4dfb78c5f8f74e034e5c8a8
7
- data.tar.gz: ec751a39a1c4d5e8b6c85d763bf5e1dedd8e3053da4c41da7450ff6778bdd850e3584ab50126c3f35aedced4a9258eed44c56be2b5dae4f4195c1419d07a25dd
6
+ metadata.gz: 79c6083a2d017dc7831c4d07e5bf2dec795e7143e4041f49684d5652eab88f8024d8d5ca90ddd7e31e0bf9938b1393da7d6d80eaa991facb6f63494eb2bcae24
7
+ data.tar.gz: d56be4ee9103076c9c7850c227611b3f64862576e40fc287d20d42bad8134f6e26cd6d80d816f837580f8433ae9d7cb1cd79d73767d68d44d798c06539aefcb5
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module OutPut
2
4
  class Config
3
- cattr_accessor :project_code do
4
- 0
5
- end
5
+ cattr_accessor :project_code, default: 0
6
6
 
7
7
  cattr_accessor :pagination_for
8
+
9
+ cattr_accessor :request_id, default: nil
8
10
  end
9
11
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module OutPut
2
- VERSION = '2.2.1'
4
+ VERSION = '2.3.0'
3
5
  end
data/lib/out_put/view.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module OutPut
2
4
  class View < Hash
3
5
  def initialize(code, msg)
data/lib/out_put.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'out_put/version'
2
4
  require 'out_put/config'
3
5
  require 'out_put/view'
@@ -6,19 +8,13 @@ module OutPut
6
8
  def output(code = 0, msg = '', only: nil, http: 200, cache: nil, **data, &block)
7
9
  if !code.is_a?(Integer) && code.respond_to?(:info)
8
10
  only = code.info[:only]
9
- code, msg, http = code.info.slice(:code, :msg, :http).values
11
+ code, msg, http, data = code.info.values_at(:code, :msg, :http, :data)
10
12
  elsif cache && block_given?
11
13
  data, only = _output_cache(cache, data: data, only: only, &block)
12
14
  end
13
15
 
14
- return render json: only.except(:http), status: only[:http] || http if only.present?
15
-
16
- code = code.zero? ? 0 : Config.project_code + code
17
- msg = 'success' if msg.blank? && code.zero?
18
- render json: {
19
- result: { code: code, message: msg },
20
- data: _output_data(data)
21
- }, status: http
16
+ return rendrer json: only.except(:http), status: only[:http] || http if only.present?
17
+ render json: { result: _output_result(code, msg), data: _output_data(data) }, status: http
22
18
  end
23
19
 
24
20
  alias ok output
@@ -31,13 +27,22 @@ module OutPut
31
27
  # Then jump to your view
32
28
  end
33
29
 
30
+ # ***
31
+
32
+ def _output_result(code, msg)
33
+ # code = code.zero? ? 0 : Config.project_code + code
34
+ msg = 'success' if msg.blank? && code.zero?
35
+ msg = "[#{instance_exec(&Config.request_id)}] #{msg}" if Config.request_id && !code.zero?
36
+ { code: code, message: msg }
37
+ end
38
+
34
39
  def _output_data(data)
35
- if data.key?(Config.pagination_for)
40
+ if data&.key?(Config.pagination_for)
36
41
  # TODO now is only for AR
37
42
  data.merge!(total: data[Config.pagination_for].try(:unscoped).count)
38
43
  end
39
44
 
40
- data
45
+ data || { }
41
46
  end
42
47
 
43
48
  def _output_cache(time, data: { }, only: nil, &block)
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.2.1
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - zhandao
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-16 00:00:00.000000000 Z
11
+ date: 2019-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler