out_put 2.1.3 → 2.2.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: 2f0bbf9be68f7e714bb08b8a421ec1466399e769
4
- data.tar.gz: d246c2858b54288aff98ae6ac12fc63aa46c239f
3
+ metadata.gz: c39b950419b24b87b7be771a242866ca754b66d4
4
+ data.tar.gz: e91476c98ce8e57c1f99f65ae5984adced45a407
5
5
  SHA512:
6
- metadata.gz: 82b6913f4908c1c3ac71a8b2bfe07716cce4e2405a78a714a5bb9224b0f1788ba2acc2c8fbb16d83bdaf5626ff978a68d7f3ee48d1526c6c31a0a595bb712393
7
- data.tar.gz: 189abd12eb85aa49b3ba77f92660c010fa4d8dec07aa87a3216aa83e75986e30268149186e6df250b6b35170baae5ca946ed21f9a8ef635c854a23ed2f7dbb43
6
+ metadata.gz: b71f6421cbcf135c415fe436336d5bd9bcd4c904d2c22246714008c17b5471b78219fa93fa89efcc56c8ec9d236480e8568182abe1e0ee98b1d49819ecadab58
7
+ data.tar.gz: 233b32f3cd0bad137225e799d3a68073975aa8458fcee54e7f6fda33eadeba538611850a7a28985496f86c8b6869593f5ba72418808e473e1fb667f4a9659bcc
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- out_put (2.1.3)
4
+ out_put (2.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -105,9 +105,27 @@ output only: { foo: 'bar' }
105
105
  output only: [ 1, 2, 3 ]
106
106
  ```
107
107
 
108
- ### 8. Other
108
+ ### 8. cache
109
109
 
110
- #### 8.a automatically set `total`:
110
+ ```ruby
111
+ output cache: 24.hours do
112
+ { list: User.all }
113
+ end # will render: { list: [...] }
114
+
115
+ output foo: 'bar', cache: 24.hours do
116
+ { list: User.all }
117
+ end # will render: { foo: 'bar', list: [...] }
118
+
119
+ output only: { foo: 'bar' }, cache: 24.hours do
120
+ { only: { list: User.all } }
121
+ end # will ONLY render: { foo: 'bar', list: [...] }
122
+ ```
123
+
124
+ Note: the cache key will be `"output/#{action_name}"`
125
+
126
+ ### X. Other
127
+
128
+ #### X.a automatically set `total`:
111
129
 
112
130
  if `config.pagination_for = :list`:
113
131
 
@@ -120,7 +138,7 @@ output 0, list: [ 1, 2, 3 ]
120
138
  # }
121
139
  ```
122
140
 
123
- #### 8.b use `build_with` to pass an variable `@view` to your view
141
+ #### X.b use `build_with` to pass an variable `@view` to your view
124
142
 
125
143
  ```ruby
126
144
  build_with key: 'val'
data/lib/out_put.rb CHANGED
@@ -3,9 +3,11 @@ require 'out_put/config'
3
3
  require 'out_put/view'
4
4
 
5
5
  module OutPut
6
- def output(code = 0, msg = '', only: nil, http: 200, **data)
6
+ def output(code = 0, msg = '', only: nil, http: 200, cache: nil, **data, &block)
7
7
  if !code.is_a?(Integer) && code.respond_to?(:info)
8
8
  code, msg, http, only = code.info.slice(:code, :msg, :http, :only).values
9
+ elsif cache && block_given?
10
+ data, only = _output_cache(cache, data: data, only: only, &block)
9
11
  end
10
12
 
11
13
  return render json: only, status: http if only.present?
@@ -23,6 +25,11 @@ module OutPut
23
25
  alias error output
24
26
  alias error_with output
25
27
 
28
+ def build_with(code = 0, msg = 'success', **data)
29
+ @view = View.new(code, msg, **data)
30
+ # Then jump to your view
31
+ end
32
+
26
33
  def _output_data(data)
27
34
  if data.key?(Config.pagination_for)
28
35
  # TODO now is only for AR
@@ -32,8 +39,11 @@ module OutPut
32
39
  data
33
40
  end
34
41
 
35
- def build_with(code = 0, msg = 'success', **data)
36
- @view = View.new(code, msg, **data)
37
- # Then jump to your view
42
+ def _output_cache(time, data: { }, only: nil, &block)
43
+ cached = Rails.cache.fetch("output/#{action_name}", expires_in: time) do
44
+ instance_eval(&block)
45
+ end
46
+
47
+ [ data.merge(cached), only&.merge(cached[:only]) ]
38
48
  end
39
49
  end
@@ -1,3 +1,3 @@
1
1
  module OutPut
2
- VERSION = '2.1.3'
2
+ VERSION = '2.2.0'
3
3
  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.3
4
+ version: 2.2.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-10-17 00:00:00.000000000 Z
11
+ date: 2018-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler