Class: Readymade::Response
- Inherits:
-
Object
- Object
- Readymade::Response
- Defined in:
- lib/readymade/response.rb
Defined Under Namespace
Classes: NonKeywordArgumentsError
Constant Summary collapse
- DEFAULT_STATUSES =
%i[success fail].freeze
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
- #consider_success? ⇒ Boolean
- #errors ⇒ Object
- #humanize_errors(errors) ⇒ Object
- #humanized_errors ⇒ Object
-
#initialize(status, *args) ⇒ Response
constructor
A new instance of Response.
- #method_missing(method_name, *args, &block) ⇒ Object
- #respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Constructor Details
#initialize(status, *args) ⇒ Response
Returns a new instance of Response.
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/readymade/response.rb', line 17 def initialize(status, *args) @status = status.to_s raise NonKeywordArgumentsError if args.present? && !args[0].is_a?(Hash) @args = @data = args[0] @consider_success = @args.delete(:consider_success) if @args.present? define_singleton_method("#{status}?") do true end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
52 53 54 55 56 |
# File 'lib/readymade/response.rb', line 52 def method_missing(method_name, *args, &block) return false if method_name.to_s.end_with?('?') super end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
15 16 17 |
# File 'lib/readymade/response.rb', line 15 def args @args end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
15 16 17 |
# File 'lib/readymade/response.rb', line 15 def data @data end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
15 16 17 |
# File 'lib/readymade/response.rb', line 15 def status @status end |
Instance Method Details
#consider_success? ⇒ Boolean
48 49 50 |
# File 'lib/readymade/response.rb', line 48 def consider_success? @consider_success end |
#errors ⇒ Object
30 31 32 33 34 |
# File 'lib/readymade/response.rb', line 30 def errors return {} unless args.is_a?(Hash) args[:errors].presence || {} end |
#humanize_errors(errors) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/readymade/response.rb', line 40 def humanize_errors(errors) if errors.is_a?(ActiveModel::Errors) errors..join('. ') else errors.map { |k, values| "#{k.to_s.humanize}: #{values.join(', ')}" }.join('. ') end end |
#humanized_errors ⇒ Object
36 37 38 |
# File 'lib/readymade/response.rb', line 36 def humanized_errors humanize_errors(errors) end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
58 59 60 |
# File 'lib/readymade/response.rb', line 58 def respond_to_missing?(method_name, include_private = false) method_name.to_s.end_with?('?') || super end |