Module: OutPut
- Defined in:
- lib/out_put.rb,
lib/out_put/view.rb,
lib/out_put/config.rb,
lib/out_put/version.rb
Defined Under Namespace
Constant Summary collapse
- VERSION =
'2.3.0'
Instance Method Summary collapse
- #_output_cache(time, data: { }, only: nil, &block) ⇒ Object
- #_output_data(data) ⇒ Object
-
#_output_result(code, msg) ⇒ Object
***.
- #build_with(code = 0, msg = 'success', **data) ⇒ Object
- #output(code = 0, msg = '', only: nil, http: 200, cache: nil, **data, &block) ⇒ Object (also: #ok, #ok_with, #error, #error_with)
Instance Method Details
#_output_cache(time, data: { }, only: nil, &block) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/out_put.rb', line 48 def _output_cache(time, data: { }, only: nil, &block) cached = Rails.cache.fetch("output/#{action_name}", expires_in: time) do instance_eval(&block) end [ data.merge(cached), cached&.[](:only)&.merge(only || { }) ] end |
#_output_data(data) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/out_put.rb', line 39 def _output_data(data) if data&.key?(Config.pagination_for) # TODO now is only for AR data.merge!(total: data[Config.pagination_for].try(:unscoped).count) end data || { } end |
#_output_result(code, msg) ⇒ Object
***
32 33 34 35 36 37 |
# File 'lib/out_put.rb', line 32 def _output_result(code, msg) # code = code.zero? ? 0 : Config.project_code + code msg = 'success' if msg.blank? && code.zero? msg = "[#{instance_exec(&Config.request_id)}] #{msg}" if Config.request_id && !code.zero? { code: code, message: msg } end |
#build_with(code = 0, msg = 'success', **data) ⇒ Object
25 26 27 28 |
# File 'lib/out_put.rb', line 25 def build_with(code = 0, msg = 'success', **data) (@view ||= View.new(code, msg)).merge!(data) # Then jump to your view end |
#output(code = 0, msg = '', only: nil, http: 200, cache: nil, **data, &block) ⇒ Object Also known as: ok, ok_with, error, error_with
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/out_put.rb', line 8 def output(code = 0, msg = '', only: nil, http: 200, cache: nil, **data, &block) if !code.is_a?(Integer) && code.respond_to?(:info) only = code.info[:only] code, msg, http, data = code.info.values_at(:code, :msg, :http, :data) elsif cache && block_given? data, only = _output_cache(cache, data: data, only: only, &block) end return rendrer json: only.except(:http), status: only[:http] || http if only.present? render json: { result: _output_result(code, msg), data: _output_data(data) }, status: http end |